Skip to content

Commit 86b904d

Browse files
authored
Split out the subscriptions documentation a separate file and fix it (#1245)
1 parent 29dd3f8 commit 86b904d

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

docs/execution/execute.rst

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Executing a query
44
=================
55

6-
76
For executing a query against a schema, you can directly call the ``execute`` method on it.
87

98

@@ -17,43 +16,6 @@ For executing a query against a schema, you can directly call the ``execute`` me
1716
``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.
1817

1918

20-
For executing a subscription, you can directly call the ``subscribe`` method on it.
21-
This method is async and must be awaited.
22-
23-
.. code:: python
24-
25-
import asyncio
26-
from datetime import datetime
27-
from graphene import ObjectType, String, Schema, Field
28-
29-
# All schema require a query.
30-
class Query(ObjectType):
31-
hello = String()
32-
33-
def resolve_hello(root, info):
34-
return 'Hello, world!'
35-
36-
class Subscription(ObjectType):
37-
time_of_day = Field(String)
38-
39-
async def subscribe_time_of_day(root, info):
40-
while True:
41-
yield { 'time_of_day': datetime.now().isoformat()}
42-
await asyncio.sleep(1)
43-
44-
SCHEMA = Schema(query=Query, subscription=Subscription)
45-
46-
async def main(schema):
47-
48-
subscription = 'subscription { timeOfDay }'
49-
result = await schema.subscribe(subscription)
50-
async for item in result:
51-
print(item.data['timeOfDay'])
52-
53-
asyncio.run(main(SCHEMA))
54-
55-
The ``result`` is an async iterator which yields items in the same manner as a query.
56-
5719
.. _SchemaExecuteContext:
5820

5921
Context

docs/execution/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Execution
99
middleware
1010
dataloader
1111
fileuploading
12+
subscriptions

docs/execution/subscriptions.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
.. _SchemaSubscription:
2+
3+
Subscriptions
4+
=============
5+
6+
To create a subscription, you can directly call the ``subscribe`` method on the
7+
schema. This method is async and must be awaited.
8+
9+
.. code:: python
10+
11+
import asyncio
12+
from datetime import datetime
13+
from graphene import ObjectType, String, Schema, Field
14+
15+
# Every schema requires a query.
16+
class Query(ObjectType):
17+
hello = String()
18+
19+
def resolve_hello(root, info):
20+
return "Hello, world!"
21+
22+
class Subscription(ObjectType):
23+
time_of_day = String()
24+
25+
async def subscribe_time_of_day(root, info):
26+
while True:
27+
yield datetime.now().isoformat()
28+
await asyncio.sleep(1)
29+
30+
schema = Schema(query=Query, subscription=Subscription)
31+
32+
async def main(schema):
33+
subscription = 'subscription { timeOfDay }'
34+
result = await schema.subscribe(subscription)
35+
async for item in result:
36+
print(item.data['timeOfDay'])
37+
38+
asyncio.run(main(schema))
39+
40+
The ``result`` is an async iterator which yields items in the same manner as a query.

0 commit comments

Comments
 (0)