Last active
July 19, 2021 21:35
-
-
Save James-Hudson3010/d9327389513fb5e939c36a474504b3bf 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
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/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
when I try to run, I get the error:
TypeError: Query fields cannot be resolved. Unexpected type '<class 'main.Person'>'