Classes
Python classes should be defined as Pydantic models to be supported.
Classes with only primitive types
Starting with a simple example,
main.py |
---|
| from fastapi import FastAPI
from fastgraphql import FastGraphQL
from fastgraphql.fastapi import make_ariadne_fastapi_router
from pydantic import BaseModel
app = FastAPI()
fast_graphql = FastGraphQL()
@fast_graphql.type()
class Person(BaseModel):
first_name: str
last_name: str
age: int
height: float
@fast_graphql.query()
def get_person() -> Person:
return Person(first_name="Luke", last_name="Skywalker", height=1.7, age=23)
app.include_router(make_ariadne_fastapi_router(fast_graphql=fast_graphql))
|
then execute,
$ uvicorn main:app --reload
open http://127.0.0.1:8000/graphql
and Vuolà !
Star Wars
Please don't take the Star Wars references so seriously. The height and age of
Luke Skywalker may vary depending on the episode.
Dissecting the Code
Again, taking a closer look into the code.
Step 1: Importing BaseModel
main.py |
---|
| from fastapi import FastAPI
from fastgraphql import FastGraphQL
from fastgraphql.fastapi import make_ariadne_fastapi_router
from pydantic import BaseModel
app = FastAPI()
fast_graphql = FastGraphQL()
@fast_graphql.type()
class Person(BaseModel):
first_name: str
last_name: str
age: int
height: float
@fast_graphql.query()
def get_person() -> Person:
return Person(first_name="Luke", last_name="Skywalker", height=1.7, age=23)
app.include_router(make_ariadne_fastapi_router(fast_graphql=fast_graphql))
|
Here we import Pydantic's base class for all models,
pydantic.BaseModel
.
As mentioned already, this is the base class for all classes handled by FastGraphQL.
Step 2: Annotating your class
main.py |
---|
| from fastapi import FastAPI
from fastgraphql import FastGraphQL
from fastgraphql.fastapi import make_ariadne_fastapi_router
from pydantic import BaseModel
app = FastAPI()
fast_graphql = FastGraphQL()
@fast_graphql.type()
class Person(BaseModel):
first_name: str
last_name: str
age: int
height: float
@fast_graphql.query()
def get_person() -> Person:
return Person(first_name="Luke", last_name="Skywalker", height=1.7, age=23)
app.include_router(make_ariadne_fastapi_router(fast_graphql=fast_graphql))
|
fastgraphql.FastGraphQL.type()
is used to annotate classes that will
be part of the GraphQL schema.
Best Practices
Even though fastgraphql.FastGraphQL.type()
can be omitted, if the class is being
used by a query or mutation and if there is no naming conflict, annotating your
classes should be considered a best-practice
Customizations
fastgraphql.FastGraphQL.type()
and internal class attributes can be
customized. This topic will be covered in Classes Customizations
Step 3: Defining your class
main.py |
---|
| from fastapi import FastAPI
from fastgraphql import FastGraphQL
from fastgraphql.fastapi import make_ariadne_fastapi_router
from pydantic import BaseModel
app = FastAPI()
fast_graphql = FastGraphQL()
@fast_graphql.type()
class Person(BaseModel):
first_name: str
last_name: str
age: int
height: float
@fast_graphql.query()
def get_person() -> Person:
return Person(first_name="Luke", last_name="Skywalker", height=1.7, age=23)
app.include_router(make_ariadne_fastapi_router(fast_graphql=fast_graphql))
|
Models can be defined exactly as described in Pydantic's
documentation. Take a pick under
Basic model usage
Step 4: Use your class
main.py |
---|
| from fastapi import FastAPI
from fastgraphql import FastGraphQL
from fastgraphql.fastapi import make_ariadne_fastapi_router
from pydantic import BaseModel
app = FastAPI()
fast_graphql = FastGraphQL()
@fast_graphql.type()
class Person(BaseModel):
first_name: str
last_name: str
age: int
height: float
@fast_graphql.query()
def get_person() -> Person:
return Person(first_name="Luke", last_name="Skywalker", height=1.7, age=23)
app.include_router(make_ariadne_fastapi_router(fast_graphql=fast_graphql))
|
As in the previous chapter, we annotate a function with @fast_graphql.query()
,
define the function signature and return type, and we are done!