Created
July 19, 2021 20:24
-
-
Save James-Hudson3010/626469fbcd9ca2bc68c8907967d0687c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I try to run the query:
I get back:
Not sure what is going wrong