Package: fastgql
PresentationDocumentationFastGQLFastGQLis a python GraphQL library that uses Pydantic models to build GraphQL types. Think FastAPI for GraphQL.fromfastapiimportFastAPIfromfastgqlimportGQL,build_routerclassUser(GQL):name:strage:intclassQuery(GQL):defuser_by_name(self,name:str)->User:returnUser(name=name,age=27)router=build_router(query_models=[Query])app=FastAPI()app.include_router(router,prefix="/graphql")I builtFastGQLbecause I wanted a GraphQL framework thatlet me usepydantic.BaseModelobjects to define my schemalet me build dynamic database queries based on incoming requestsWe are now usingFastGQLin production and have experienced a massive (10x) speedup in average response times because of 2).You can find out more about how we build dynamic database queries in the Advanced Tutorial section of the docs.Installation$pipinstallfastgql---> 100%Successfully installed fastgqlExampleAll you need to do is create objects that inherit fromfastgql.GQL, which is a simple subclass ofpydantic.BaseModel. For this example, I am creating a mock schema based on movies. For the functions, you'd usually use a database but I hardcoded the data for this example.This code generates a GraphQL schema, reading the object fields and functions. Functions can be sync or async.Code itCreate a filemain.pywith:fromuuidimportUUID,uuid4fromfastapiimportFastAPIfromfastgqlimportGQL,build_routerclassAccount(GQL):# (4)!id:UUIDusername:strdefwatchlist(self)->list["Movie"]:# (1)!# Usually you'd use a database to get the user's watchlist. For this example, it is hardcoded.return[Movie(id=uuid4(),title="Barbie",release_year=2023),Movie(id=uuid4(),title="Oppenheimer",release_year=2023),]def_secret_function(self)->str:# (2)!return"this is not exposed!"classPerson(GQL):id:UUIDname:strdeffilmography(self)->list["Movie"]:return[Movie(id=uuid4(),title="Barbie",release_year=2023),Movie(id=uuid4(),title="Wolf of Wallstreet",release_year=2013),]classMovie(GQL):id:UUIDtitle:strrelease_year:intdefactors(self)->list["Person"]:return[Person(id=uuid4(),name="Margot Robbie"),Person(id=uuid4(),name="Ryan Gosling"),]classQuery(GQL):defaccount_by_username(self,username:str)->Account:# (5)!# Usually you'd use a database to get this account. For this example, it is hardcoded.returnAccount(id=uuid4(),username=username)router=build_router(query_models=[Query])app=FastAPI()# (3)!app.include_router(router,prefix="/graphql")Usually this would be a database call. There is an advanced tutorial showing this.Functions that start with_are not included in the GraphQL schema.This is just a normal FastAPI app.fastgql.build_routerreturns a router that can be included on any FastAPI app.These objects are subclasses ofpydantic.BaseModel, so anything you'd want to do with aBaseModelyou can do with these. You'll see how this comes in handy in future tutorials.All of these functions can be sync or async. In a future tutorial I'll use an async database call to get data.Run itRun the server with:$uvicornmain:app--reloadINFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)INFO:     Started reloader process [28720]INFO:     Started server process [28722]INFO:     Waiting for application startup.INFO:     Application startup complete.(Taken from FastAPI docs) About the commanduvicorn main:app --reload...The commanduvicorn main:apprefers to:main: the filemain.py(the Python "module").app: the object created inside ofmain.pywith the lineapp = FastAPI().--reload: make the server restart after code changes. Only do this for development.Check itOpen your browser athttp://127.0.0.1:8000/graphql.You will see a GraphiQL UI. This is your homebase for creating GraphQL queries and checking the schema.You can see the schema, build queries, and access query history from the icons in the top left respectivly. Here is an example query: