GraphQL Server for Python
graphql-server is a Python library that provides integrations for setting up GraphQL servers in various web frameworks such as Flask, Starlette, Django, and Sanic. It currently includes version 3.0.0, which acts as a meta-package bundling core server logic and framework-specific adapters. Releases are made as needed, often in conjunction with updates to its underlying `graphql-core` and framework integration packages.
Common errors
-
ModuleNotFoundError: No module named 'graphql_server.GraphQLView'
cause Attempting to import `GraphQLView` (or `GraphQLRouter`) directly from the top-level `graphql_server` package, which is a meta-package and does not expose these symbols directly.fixChange the import to `from graphql_server.flask import GraphQLView` (for Flask) or `from graphql_server.starlette import GraphQLRouter` (for Starlette), etc., depending on your chosen framework. -
TypeError: 'NoneType' object is not callable
cause Often occurs when the GraphQL schema is not properly defined, instantiated, or passed as an argument to `GraphQLView` or `GraphQLRouter`.fixEnsure you have a valid `graphene.Schema(...)` object (or equivalent) and that it's correctly passed as the `schema` argument when initializing your GraphQL endpoint handler. -
AssertionError: You must provide a 'schema' or 'schema_path' to GraphQLView.
cause The `GraphQLView` or `GraphQLRouter` was instantiated without a valid GraphQL schema object or a path to a schema file.fixProvide a `graphene.Schema` instance (e.g., `GraphQLRouter(schema=my_graphene_schema)`) or a schema path if using an alternative schema loading mechanism.
Warnings
- breaking Version 3.x of `graphql-server` is a meta-package; direct imports like `from graphql_server import GraphQLView` are no longer valid. Instead, you must import from framework-specific submodules (e.g., `from graphql_server.flask import GraphQLView`).
- gotcha Installing `graphql-server` installs all framework integrations by default. If you only need one (e.g., for Flask), it's more efficient to install the specific package (e.g., `pip install graphql-server-flask`).
- gotcha GraphQL Server for Python relies on `graphql-core` for the underlying GraphQL implementation and `graphene` (or similar) for schema definition. Ensure these dependencies are correctly installed and your schema is properly constructed.
Install
-
pip install graphql-server -
pip install graphql-server-starlette # Or -flask, -django, etc.
Imports
- GraphQLRouter
from graphql_server import GraphQLRouter
from graphql_server.starlette import GraphQLRouter
- GraphQLView
from graphql_server import GraphQLView
from graphql_server.flask import GraphQLView
- run_http_query
from graphql_server_core.http import run_http_query
Quickstart
import graphene
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
from graphql_server.starlette import GraphQLRouter
# 1. Define your GraphQL schema using Graphene
class Query(graphene.ObjectType):
hello = graphene.String(name=graphene.String(default_value="World"))
def resolve_hello(self, info, name):
return f"Hello, {name}!"
schema = graphene.Schema(query=Query)
# 2. Integrate with Starlette using GraphQLRouter
async def homepage(request):
return JSONResponse({"message": "Visit /graphql for the API or /graphql-playground for the UI."})
routes = [
Route("/", homepage),
Route("/graphql", GraphQLRouter(schema)),
Route("/graphql-playground", GraphQLRouter(schema, playground=True))
]
app = Starlette(routes=routes)
# To run this app, save it as `app.py` and execute:
# uvicorn app:app --reload
# Then visit http://127.0.0.1:8000/graphql-playground in your browser.