jhnnsrs / turms

Turms is a pure python implementation of the awesome graphql-codegen library, following a simliar extensible design.
https://jhnnsrs.github.io/turms/
MIT License
60 stars 16 forks source link

Camel case arguments are not working with Func plugin #63

Closed DGolubets closed 1 week ago

DGolubets commented 1 year ago

Hi,

It seems that there is some inconsistency in arguments casing when using the Func plugin.

I use the following config:

projects:
  images:
    schema: ---
    documents: queries/*.graphql
    extensions:
      turms:
        out_dir: api
        plugins:
          - type: turms.plugins.enums.EnumsPlugin
          - type: turms.plugins.inputs.InputsPlugin
          - type: turms.plugins.fragments.FragmentsPlugin
          - type: turms.plugins.operations.OperationsPlugin
          - type: turms.plugins.funcs.FuncsPlugin
            global_args:
              - type: gql.client.AsyncClientSession
                key: client
            definitions:
              - type: query
                use: api.proxies.execute
                is_async: True
              - type: mutation
                use: api.proxies.execute
                is_async: True
        stylers:
          - type: turms.stylers.capitalize.CapitalizeStyler
          - type: turms.stylers.snake_case.SnakeCaseStyler
        scalar_definitions:
          ID: str
          Long: int
          Instant: datetime.datetime

I got this Arguments class generated:

class Arguments(BaseModel):
        user_id: str

But my query uses $userId.

jhnnsrs commented 1 year ago

Hi there!

Thanks for the issue. I have just created a pull request that should fix this : #64 .

Arguments now automatically aliases arguments when the stylers version doesn't match the original field_name

Here:

class Arguments(BaseModel):
    user_id: str = Field(alias="userId")

As this may introduce some inconveniences when parsing arguments, this PR also introduce a new operations plugin option:

- type: turms.plugins.operations.OperationsPlugin
  arguments_allow_population_by_field_name: True #default is false
class Arguments(BaseModel):
    user_id: str = Field(alias="userId")

    class Config:
       allow_population_by_field_name= True

Which allows population like this: "QueryName.Arguments(user_id=3)" on to op "QueryName.Arguments(userId=3)"

Feel this is not the smoothest solution though, any ideas on that?

DGolubets commented 1 year ago

Hi!

Thank you for tackling this issue so quick. This looks good.

What's the downside of having arguments_allow_population_by_field_name true by default?

jhnnsrs commented 1 week ago

Hi @DGolubets, i am closing this issue now, which i should have done a while ago :D hope everything still works for you, this should now be fixed in 0.6.0 that relies on pydantic v2 as well :)