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

Set default values for optional field in input types #75

Merged
merged 1 commit into from
May 14, 2024

Conversation

rnovacek
Copy link

@rnovacek rnovacek commented May 7, 2024

For given input

input X {
  field: str
  otherField: str
}

It changes:

  • field: Optional[str] to field: Optional[str] = None if there is no pydantic field
  • other_field: Optional[str] = Field(alias="otherField") to other_field: Optional[str] = Field(alias="otherField", default=None) otherwise

Resolves #74

For given input

```
input X {
  field: str
  otherField: str
}
```

It changes:
* `field: Optional[str]` to `field: Optional[str] = None` if there is no pydantic field
* `other_field: Optional[str] = Field(alias="otherField")` to `other_field: Optional[str] = Field(alias="otherField", default=None)` otherwise
@jhnnsrs
Copy link
Owner

jhnnsrs commented May 7, 2024

Hey thanks a lot for that PR!
In general it looks good to me.

I just wonder if there is a potential issue, as unset values and nullable
values are not by default the same according to the spec:

https://spec.graphql.org/October2021/#sel-EAFdRDBBACDAl1T

Also there is a related issue on best practices in pydantic:

pydantic/pydantic#990 (comment)

However I also feel that this is maybe an edge case and this approach looks
like a good default for me. Would just maybe appreciate a bit more input on this:
@rnovacek @nocnokneo

#74 (comment)

Happy to merge this after a short deliberation though :)
Thanks a lot!

@rnovacek
Copy link
Author

rnovacek commented May 8, 2024

I have to admit I'm no expert in GraphQL nor Pydantic. I cannot tell if this is the correct solution. It just scratches my own itch - no need to specify all fields in an input when they are not mandatory.

@jhnnsrs
Copy link
Owner

jhnnsrs commented May 14, 2024

The more i get my head around I feel like its a good compromise given the differences between pydantic and graphql and the "standard" dev experience and what people would try to achieve.
So for me it looks good. Will merge :) Thanks a lot!

@jhnnsrs jhnnsrs merged commit 1df9492 into jhnnsrs:master May 14, 2024
7 checks passed
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

Successfully merging this pull request may close these issues.

Default values for input type fields
2 participants