app-sre / qenerate

Code Generator for GraphQL Query and Fragment Data Classes
Apache License 2.0
9 stars 7 forks source link

Working with fields that start with an underscore #88

Closed PORTB closed 7 months ago

PORTB commented 8 months ago

Hi, I am interacting with a graphql endpoint (not under my control) that has a field that begins with an underscore, my query looks similar to this:

# qenerate: plugin=pydantic_v1

query MyQuery {
    foobars {
      _id  # <-------------
      name
      other_stuff
    }
}

And pydantic throws an error with this: NameError: Fields must not use names with leading underscores; e.g., use 'id' instead of '_id'.

Is there a workaround for this?

fishi0x01 commented 8 months ago

Hi :wave:

I dont think there is a workaround in place for that. This is a special case that needs to be addressed in this function https://github.com/app-sre/qenerate/blob/main/qenerate/plugins/pydantic_v1/mapper.py#L54

Like adding a simple check to remove any trailing special char from the string

fishi0x01 commented 7 months ago

Latest version 0.6.4 should work now. In your specific case it would result in field name q_id (note, that we consider id a reserved keyword, so we prepend it with q_. See https://github.com/app-sre/qenerate/blob/main/qenerate/plugins/pydantic_v1/mapper.py#L7)

Please re-open this issue if you still witness any problems.