Skip to content

Instantly share code, notes, and snippets.

@James-Hudson3010
Created July 19, 2021 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save James-Hudson3010/626469fbcd9ca2bc68c8907967d0687c to your computer and use it in GitHub Desktop.
Save James-Hudson3010/626469fbcd9ca2bc68c8907967d0687c to your computer and use it in GitHub Desktop.
import typing
import strawberry
from strawberry.asgi import GraphQL
from fastapi import FastAPI
import uvicorn
# Pydantic types
@strawberry.type
class ImbeddedPerson:
id: int
first_name: str
last_name: str
@strawberry.type
class Person(ImbeddedPerson):
person: ImbeddedPerson
# Data set
def get_person():
return [
Person(
person=ImbeddedPerson(
id=1,
first_name='John',
last_name='Doe'
)
)
]
# Define Query
@strawberry.type
class Query:
people: typing.List[Person] = strawberry.field(resolver=get_person)
# Instantiate schema
schema = strawberry.Schema(query=Query)
# Create REST api
fastapi_app = FastAPI()
# Default route
@fastapi_app.get("/")
def root_route():
return {'GraphQLRoute': 'localhost:8000/graphql/'}
# create graphql (strawberry flavor -- see what I did there?) app
gql_qpp = GraphQL(schema)
# pass GraphQL app to it's dedicated route
fastapi_app.add_route('/graphql/', gql_qpp)
if __name__ == '__main__':
uvicorn.run(fastapi_app, host="localhost", port=8000)
@James-Hudson3010
Copy link
Author

If I try to run the query:

{
  people {
    id
  }
}

I get back:

{
  "data": null,
  "errors": [
    {
      "message": "__init__() missing 3 required positional arguments: 'id', 'first_name', and 'last_name'",
      "locations": [
        {
          "line": 33,
          "column": 3
        }
      ],
      "path": [
        "people"
      ]
    }
  ]
}

Not sure what is going wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment