Hi folks :) I've been thinking that maybe at some point we should add first class support for Pydantic.
Currently we support Pydantic via an, albeit experimental, integration, but the API is a cumbersome, as we need to create two classes, one for Pydantic and one for the Strawberry type.
Example:
class User(BaseModel):
id: int
name: str
signup_ts: Optional[datetime] = None
friends: List[int] = []
@strawberry.experimental.pydantic.type(model=User)
class UserType:
id: strawberry.auto
name: strawberry.auto
friends: strawberry.auto
This great if you already have Pydantic models and want to add an API on top of them, and I still think this API makes a lot of sense for converting database models to a GraphQL API.
With first class support I mean having a way to define both the Strawberry type and the Pydantic model at once, like this:
@strawberry.type
class User(BaseModel):
id: int
name: str
signup_ts: Optional[datetime] = None
friends: List[int] = []
An API like this should make it much easier to define types that need to be validated (input types might need this often)
I don't know when we'll have time to work on this, I'd definitely work on improving the code for the integrations (making sure we reduce the code duplication we have now) first and we should also finalise the internal API too.
There's also question on how we can make sure we type this correctly, as we currently use a dataclass
transform decorator to tell pyright what happens to the class. Maybe we introduce a new decorator just for pydantic?
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too