diff --git a/docs/execution/execute.rst b/docs/execution/execute.rst index cd29d72d..4bb5613e 100644 --- a/docs/execution/execute.rst +++ b/docs/execution/execute.rst @@ -3,7 +3,6 @@ Executing a query ================= - For executing a query against a schema, you can directly call the ``execute`` method on it. @@ -17,43 +16,6 @@ For executing a query against a schema, you can directly call the ``execute`` me ``result`` represents the result of execution. ``result.data`` is the result of executing the query, ``result.errors`` is ``None`` if no errors occurred, and is a non-empty list if an error occurred. -For executing a subscription, you can directly call the ``subscribe`` method on it. -This method is async and must be awaited. - -.. code:: python - - import asyncio - from datetime import datetime - from graphene import ObjectType, String, Schema, Field - - # All schema require a query. - class Query(ObjectType): - hello = String() - - def resolve_hello(root, info): - return 'Hello, world!' - - class Subscription(ObjectType): - time_of_day = Field(String) - - async def subscribe_time_of_day(root, info): - while True: - yield { 'time_of_day': datetime.now().isoformat()} - await asyncio.sleep(1) - - SCHEMA = Schema(query=Query, subscription=Subscription) - - async def main(schema): - - subscription = 'subscription { timeOfDay }' - result = await schema.subscribe(subscription) - async for item in result: - print(item.data['timeOfDay']) - - asyncio.run(main(SCHEMA)) - -The ``result`` is an async iterator which yields items in the same manner as a query. - .. _SchemaExecuteContext: Context diff --git a/docs/execution/index.rst b/docs/execution/index.rst index 93a02845..dbfbfa72 100644 --- a/docs/execution/index.rst +++ b/docs/execution/index.rst @@ -9,3 +9,4 @@ Execution middleware dataloader fileuploading + subscriptions diff --git a/docs/execution/subscriptions.rst b/docs/execution/subscriptions.rst new file mode 100644 index 00000000..86ed78a1 --- /dev/null +++ b/docs/execution/subscriptions.rst @@ -0,0 +1,40 @@ +.. _SchemaSubscription: + +Subscriptions +============= + +To create a subscription, you can directly call the ``subscribe`` method on the +schema. This method is async and must be awaited. + +.. code:: python + + import asyncio + from datetime import datetime + from graphene import ObjectType, String, Schema, Field + + # Every schema requires a query. + class Query(ObjectType): + hello = String() + + def resolve_hello(root, info): + return "Hello, world!" + + class Subscription(ObjectType): + time_of_day = String() + + async def subscribe_time_of_day(root, info): + while True: + yield datetime.now().isoformat() + await asyncio.sleep(1) + + schema = Schema(query=Query, subscription=Subscription) + + async def main(schema): + subscription = 'subscription { timeOfDay }' + result = await schema.subscribe(subscription) + async for item in result: + print(item.data['timeOfDay']) + + asyncio.run(main(schema)) + +The ``result`` is an async iterator which yields items in the same manner as a query.