ariadne-codegen icon indicating copy to clipboard operation
ariadne-codegen copied to clipboard

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

Open wesleyjin opened this issue 2 years ago • 1 comments

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

wesleyjin avatar Aug 14 '24 18:08 wesleyjin

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.

DamianCzajkowski avatar Aug 23 '24 07:08 DamianCzajkowski