Add working complete example to subscriptions doc
@@ -19,7 +19,9 @@ Defining subscriptions
In schema definition subscriptions look similar to queries::
type_def = """
- type Query {}
+ type Query {
+ _unused: Boolean
+ }
type Subscription {
counter: Int!
@@ -28,7 +30,7 @@ In schema definition subscriptions look similar to queries::
This example contains:
-The ``Query`` type with no fields. Ariadne requires you to always have a ``Query`` type.
+The ``Query`` type with single unused field. GraphQL considers empty type an syntax error and requires API to always define ``Query`` type.
The ``Subscription`` type with a single field: ``counter`` that returns a number.
@@ -49,16 +51,12 @@ A ``resolver`` that tells the server how to send data to the client. This is sim
The signatures are as follows::
- async def counter_generator(
- obj: Any, info: GraphQLResolveInfo
- ) -> AsyncGenerator[int, None]:
+ async def counter_generator(obj, info):
for i in range(5):
await asyncio.sleep(1)
yield i
- def counter_resolver(
- count: int, info: GraphQLResolveInfo
- ) -> int:
+ def counter_resolver(count, info):
return count + 1
Note that the resolver consumes the same type (in this case ``int``) that the generator yields.
@@ -85,3 +83,40 @@ You can also use the ``source`` decorator::
obj: Any, info: GraphQLResolveInfo
) -> AsyncGenerator[int, None]:
...
+
+
+Complete example
+----------------
+
+For reference here is a complete example of the GraphQL API that supports subscription::
+
+ import asyncio
+ from ariadne import SubscriptionType, make_executable_schema
+ from ariadne.asgi import GraphQL
+
+ type_def = """
+ type Query {
+ _unused: Boolean
+ }
+
+ type Subscription {
+ counter: Int!
+ }
+ """
+
+ subscription = SubscriptionType()
+
+ @subscription.source("counter")
+ async def counter_generator(obj, info):
+ for i in range(5):
+ await asyncio.sleep(1)
+ yield i
+
+
+ @subscription.field("counter")
+ def counter_resolver(count, info):
+ return count + 1
+
+
+ schema = make_executable_schema(type_def, subscription)
+ app = GraphQL(schema, debug=True)
