Presentation
Documentation
FastGQL
FastGQL is a python GraphQL library that uses Pydantic models to build GraphQL types. Think FastAPI for GraphQL.
from fastapi import FastAPI
from fastgql import GQL, build_router
class User(GQL):
name: str
age: int
class Query(GQL):
def user_by_name(self, name: str) -> User:
return User(name=name, age=27)
router = build_router(query_models=[Query])
app = FastAPI()
app.include_router(router, prefix="/graphql")
I built FastGQL because I wanted a GraphQL framework that
- let me use
pydantic.BaseModelobjects to define my schema - let me build dynamic database queries based on incoming requests
We are now using FastGQL in 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
$ pip install fastgql
---> 100%
Successfully installed fastgql
Example
All you need to do is create objects that inherit from fastgql.GQL, which is a simple subclass of pydantic.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 it
- Create a file
main.pywith:
from uuid import UUID, uuid4
from fastapi import FastAPI
from fastgql import GQL, build_router
class Account(GQL): # (4)!
id: UUID
username: str
def watchlist(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!"
class Person(GQL):
id: UUID
name: str
def filmography(self) -> list["Movie"]:
return [
Movie(id=uuid4(), title="Barbie", release_year=2023),
Movie(id=uuid4(), title="Wolf of Wallstreet", release_year=2013),
]
class Movie(GQL):
id: UUID
title: str
release_year: int
def actors(self) -> list["Person"]:
return [
Person(id=uuid4(), name="Margot Robbie"),
Person(id=uuid4(), name="Ryan Gosling"),
]
class Query(GQL):
def account_by_username(self, username: str) -> Account: # (5)!
# Usually you'd use a database to get this account. For this example, it is hardcoded.
return Account(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 of
pydantic.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 it
Run the server with:
$ uvicorn main:app --reload
INFO: 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 command uvicorn main:app --reload...
The command uvicorn main:app refers 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 it
Open your browser at http://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:
Metadata
Release files for fastgql 0.24.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastgql-0.24.4.tar.gz | 33.7 kB | Without provenance | Details for fastgql-0.24.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastgql-0.24.4-py3-none-any.whl | Python 3 | none | any | Details for fastgql-0.24.4-py3-none-any.whl |
Total release size: 75.2 kB
Release files / fastgql-0.24.4.tar.gz
| Download URL | fastgql-0.24.4.tar.gz |
|---|---|
| Size | 33.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ceae1c1d5b86172242c0549463c95c19309bf056446b8ef5c829a746331b14dc
|
|
BLAKE2b-256 checksum How to use checksums |
79932cb3f7de1dcdfd3531dd20ee962e7d5e8301beadc391e22c6abc473c4138
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.10.10 Darwin/23.6.0
|
Release files / fastgql-0.24.4-py3-none-any.whl
| Download URL | fastgql-0.24.4-py3-none-any.whl |
|---|---|
| Size | 41.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4bdea38fd6f3efb93038322a1d325e19cb5d7d7347030f348911152b34e3c781
|
|
BLAKE2b-256 checksum How to use checksums |
6e3c9dae5f993297d62d6b6513111ccc68851ebe11e9f60620bb072f7e958ad2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.10.10 Darwin/23.6.0
|
Security for fastgql 0.24.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.