mirumee / ariadne-codegen

Generate fully typed Python client for any GraphQL API from schema, queries and mutations
BSD 3-Clause "New" or "Revised" License
268 stars 34 forks source link

[feature request] default `= None` for nullable fields that evaluate to `Optional[T]` in Python model #309

Open wesleyjin opened 2 months ago

wesleyjin commented 2 months ago

Ariadne has been very useful for our team which uses GraphQL backed by Python services.

When we add new fields to a type, existing records in our underlying database (DynamoDB) do not have the new attribute - so we run into a ValidationError (missing) when creating a Python object with newly generated Pydantic model. The fix for this would be to add the default = None for attributes that are Optional, which allows creation of the model class instance to continue even if the new attribute is not in the old record.

Is there already a way to do this that we're not leveraging properly?

# In schema.graphql which is our config `schema_path`
type Location {
  locationId: ID!
  parentId: ID
  name: String
}
# In queries.graphql which is our config `queries_path`
fragment Location on Location {
  locationId
  parentId
  name
}

After running codegen we get:

# generated/fragments.py
class Location(BaseModel):
    locationId: str
    parentId: Optional[str]
    name: Optional[str]

We'd like:

# generated/fragments.py
class Location(BaseModel):
    locationId: str
    parentId: Optional[str] = None
    name: Optional[str] = None
DamianCzajkowski commented 2 months ago

Thank you for the idea! It looks like it will be easy to implement. I'm adding this to my list of things to work on right now.