Adds GraphQL support to any ASGI framework.
Install the ASGI integration with:
pip install graphql-server[asgi]
Use the GraphQL class from graphql_server.asgi.
from starlette.applications import Starlette
from graphql_server.asgi import GraphQL
from schema import schema
app = Starlette()
graphql_app = GraphQL(schema=schema, graphiql=True)
app.mount("/graphql", graphql_app)CORS
Add Starlette's
CORSMiddlewareif you need cross-origin requests:from starlette.middleware.cors import CORSMiddleware app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], )
This mounts /graphql in your ASGI app and enables the GraphiQL IDE.
schemagraphiqlgraphql_ideallow_queries_via_getkeep_alivekeep_alive_intervaldebugsubscription_protocolsconnection_init_wait_timeoutmultipart_uploads_enabled
You can also subclass GraphQL and overwrite get_root_value(self, request) to have a dynamic root value
per request.
class UserRootValue(GraphQL):
def get_root_value(self, request):
return request.userSee CONTRIBUTING.md