Use Graphene v3 on Starlette
Project description
starlette-graphene3
A simple ASGI app for using Graphene v3 with Starlette.
It supports:
- Queries and Mutations (over HTTP or WebSocket)
- Subscriptions (over WebSocket)
- File uploading (https://github.com/jaydenseric/graphql-multipart-request-spec)
- GraphiQL / GraphQL Playground
Installation
pip3 install -U starlette-graphene3
Example
import asyncio
import graphene
from graphene_file_upload.scalars import Upload
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):
return {"id": "john", "name": "John"}
class FileUploadMutation(graphene.Mutation):
class Arguments:
file = Upload(required=True)
ok = graphene.Boolean()
def mutate(self, info, file, **kwargs):
return FileUploadMutation(ok=True)
class Mutation(graphene.ObjectType):
upload_file = FileUploadMutation.Field()
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, mutation=Mutation, subscription=Subscription)
app.mount("/", GraphQLApp(schema, on_get=make_graphiql_handler())) # Graphiql IDE
# app.mount("/", GraphQLApp(schema, on_get=make_playground_handler())) # Playground IDE
# app.mount("/", GraphQLApp(schema)) # no IDE
GraphQLApp
GraphQLApp(schema, [options...])
class GraphQLApp:
def __init__(
self,
schema: graphene.Schema, # Requied
*,
# Optional keyword parameters
on_get: Optional[
Callable[[Request], Union[Response, Awaitable[Response]]]
] = None, # optional HTTP handler for GET requests
context_value: ContextValue = None,
root_value: RootValue = None,
middleware: Optional[Middleware] = None,
error_formatter: Callable[[GraphQLError], Dict[str, Any]] = format_error,
logger_name: Optional[str] = None,
playground: bool = False, # deprecating
execution_context_class: Optional[Type[ExecutionContext]] = None,
):
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for starlette-graphene3-0.5.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50601d7da41013cf65a17f0d7f51ce53b9e19b2013f8b522d7f23b301f483498 |
|
MD5 | 3a3c543ae9512ff38e4ce92981762f6a |
|
BLAKE2b-256 | 8444c331b6ac8e19c9b4d7a942eecea9f0dd8d171138327618c0a4c2c8d64672 |
Close
Hashes for starlette_graphene3-0.5.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ddf4321fb423060797dd99a54f9a73991ffc6a7ac4c41720b2b561cf60979cd |
|
MD5 | 1d81da0458723f6f5138bcddae979cee |
|
BLAKE2b-256 | f9cf956ca5de496437fcea9e488a2cbab0cf6fde9bc149aca58d36c7a4abef35 |