Skip to content

Instantly share code, notes, and snippets.

@James-Hudson3010
Last active July 19, 2021 21:35
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/d9327389513fb5e939c36a474504b3bf to your computer and use it in GitHub Desktop.
Save James-Hudson3010/d9327389513fb5e939c36a474504b3bf to your computer and use it in GitHub Desktop.
from typing import List
import strawberry
from strawberry.asgi import GraphQL
from pydantic import BaseModel
from fastapi import FastAPI
import uvicorn
from pprint import pprint
# Pydantic types
class Person(BaseModel):
id: int
first_name: str
last_name: str
# Strawberry schema mapping
@strawberry.experimental.pydantic.type(model=Person, fields=[
'id',
'first_name',
'last_name'
])
class PersonType:
pass
# Data set
def get_person():
return [
Person(
id=42,
first_name = 'Bill',
last_name = 'Smith'
)
]
# Define Query
@strawberry.type
class Query:
people: 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)
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint.
if __name__ == '__main__':
uvicorn.run(fastapi_app, host="localhost", port=8000)
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
@James-Hudson3010
Copy link
Author

James-Hudson3010 commented Jul 19, 2021

when I try to run, I get the error:

TypeError: Query fields cannot be resolved. Unexpected type '<class 'main.Person'>'

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