Replies: 3 comments 7 replies
-
I think we absolutely should integrate with SQLModel, which looks like a very cool project. I actually decided to try and see if it would work with our current pydantic integration and it does! The following works in my local tests: class UserModel(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
age: int
password: Optional[str]
@strawberry.experimental.pydantic.type(UserModel, fields=["age", "password"])
class User:
pass
@strawberry.type
class Query:
@strawberry.field
def user(self) -> User:
return User(age=1, password="ABC")
schema = strawberry.Schema(query=Query)
query = "{ user { age } }"
result = schema.execute_sync(query)
assert not result.errors
assert result.data == { "user": { "age": 1 } } Now we should create a specific integration for SQLModel but it shouldn't be that different to the pydantic integration which is great. |
Beta Was this translation helpful? Give feedback.
-
One of the limitations I've run into with sqlmodel is bidirectional relationships. Here are two repl.its.
They use similar models to the code presented by @giulianomarinella. The first one works because the @strawberry.experimental.pydantic.type(
HeroModel, fields=["id", "name", "secret_name", "age", "team_id", "team"])
class Hero:
pass
@strawberry.experimental.pydantic.type(
TeamModel, fields=["id", "name", "headquarters", "heroes"])
class Team:
heroes: List["Hero"] The second one does not work because the @strawberry.experimental.pydantic.type(
HeroModel, fields=["id", "name", "secret_name", "age", "team_id", "team"])
class Hero:
# now we make the relationship bidirectional and we fail
team: Optional["Team"]
@strawberry.experimental.pydantic.type(
TeamModel, fields=["id", "name", "headquarters", "heroes"])
class Team:
heroes: List["Hero"] Not sure if there is a way to get around this limitation but it is the only limitation I've run into so far. |
Beta Was this translation helpful? Give feedback.
-
If anyone interested, #1637 attempts to add full support for SQLModel (and ormar) |
Beta Was this translation helpful? Give feedback.
-
SQLModel was released 4 days ago
I'm not even sure if such integration would be helpul or if it is a good idea. (or maybe it isn't needed and things will just work?)
But I can imagine how it would make things simpler:
You define database model in SQLModel (which is also a Pydantic model) and the same model is working as GraphQL endpoint thanks to strawberry.
This is assuming that you want to expose all columns from a database table, which is often not the case. I suspect it may require more work if you would want to expose only some columns.
Related: #444
Beta Was this translation helpful? Give feedback.
All reactions