Starlette-Graphene3

0.6.0 · active · verified Thu Apr 16

Starlette-Graphene3 is an ASGI application for seamlessly integrating Graphene v3 with Starlette and FastAPI. It provides support for GraphQL queries, mutations, subscriptions (over WebSockets), file uploading, and includes built-in GraphiQL/GraphQL Playground interfaces. The current version is 0.6.0, and releases appear to be infrequent, primarily driven by updates to underlying dependencies like Graphene and Starlette.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up a basic Starlette application with GraphQL using `starlette-graphene3`. It defines a simple query and a subscription, then mounts the `GraphQLApp` at the root path with the GraphiQL handler enabled for an interactive interface.

import asyncio
import graphene
from starlette.applications import Starlette
from starlette_graphene3 import GraphQLApp, make_graphiql_handler

class User(graphene.ObjectType):
    id = graphene.ID()
    name = graphene.String()

class Query(graphene.ObjectType):
    me = graphene.Field(User)

    def resolve_me(root, info):
        # In a real app, this would fetch data from a database or service
        return User(id="1", name="Test User")

class Subscription(graphene.ObjectType):
    count = graphene.Int(upto=graphene.Int())

    async def subscribe_count(root, info, upto=3):
        for i in range(upto):
            yield i
            await asyncio.sleep(1)

app = Starlette()
schema = graphene.Schema(query=Query, subscription=Subscription)

app.mount("/", GraphQLApp(schema, on_get=make_graphiql_handler()))

# To run this example, save it as `main.py` and execute:
# uvicorn main:app --reload
# Then open your browser to http://127.0.0.1:8000/

view raw JSON →