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

Check native dataclasses work #1

Open
coretl opened this issue Aug 2, 2022 · 4 comments
Open

Check native dataclasses work #1

coretl opened this issue Aug 2, 2022 · 4 comments

Comments

@coretl
Copy link

coretl commented Aug 2, 2022

Take the example off the strawberry page:

import strawberry

@strawberry.type
class User:
    name: str
    age: int

@strawberry.type
class Query:
    @strawberry.field
    def user(self) -> User:
        return User(name="Patrick", age=100)

schema = strawberry.Schema(query=Query)

Would this also work with plain dataclasses?

import strawberry
from dataclasses import dataclass

@dataclass
class User:
    name: str
    age: int

@strawberry.type
class Query:
    @strawberry.field
    def user(self) -> User:
        return User(name="Patrick", age=100)

schema = strawberry.Schema(query=Query)
@rjwills28
Copy link
Owner

I have tried testing this simple example but unfortunately it does not work with the plain dataclass. When starting up the Strawberry server I get an error 'Query fields cannot be resolved. Unexpected type <class app.User>'.
I will do a little bit more investigating to see if there is any other way round this.

@coretl
Copy link
Author

coretl commented Aug 9, 2022

You might be able to do something like this for dataclasses

@rjwills28
Copy link
Owner

Thanks for the idea!

This implementation of native dataclasses with strawberry.experimental.pydantic.types does work so hopefully we should be able to do something similar for our application.

import strawberry
from dataclasses import dataclass
from pydantic import BaseModel

@dataclass
class User(BaseModel):
    name: str
    age: int

@strawberry.experimental.pydantic.type(model=User, all_fields=True)
class UserType:
    pass

@strawberry.type
class Query:
    @strawberry.field
    def user(self) -> UserType:
        return UserType(name="Patrick", age=100)


schema = strawberry.Schema(query=Query)

@rjwills28
Copy link
Owner

A more complicated check is if we can directly integrate a schema generated by 'apischema' (https://wyfo.github.io/apischema/0.17/graphql/overview/) in Strawberry. Potentially we could use the engine from FastAPI.

I tried to get Strawberry to use a schema generated by apischema but I don't think this is possible. I got the apischema working and creating a schema from the code but I cannot see how to inject this into a Strawberry + FastAPI implementation. Strawberry will only work with a strawberry.Schema object. See GraphQLRouter on https://strawberry.rocks/docs/integrations/fastapi.

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