Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wesleyjin opened this issue Aug 14, 2024 · 1 comment

Comments

@wesleyjin
Copy link

wesleyjin commented Aug 14, 2024

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants