|
| 1 | +import inspect |
1 | 2 | from typing import Awaitable, cast
|
2 | 3 |
|
3 | 4 | import pytest
|
4 |
| -from graphql.execution import Middleware, MiddlewareManager, execute |
| 5 | +from graphql.execution import Middleware, MiddlewareManager, execute, subscribe |
5 | 6 | from graphql.language.parser import parse
|
6 | 7 | from graphql.type import GraphQLField, GraphQLObjectType, GraphQLSchema, GraphQLString
|
7 | 8 |
|
@@ -236,6 +237,45 @@ async def resolve(self, next_, *args, **kwargs):
|
236 | 237 | result = await awaitable_result
|
237 | 238 | assert result.data == {"field": "devloseR"}
|
238 | 239 |
|
| 240 | + @pytest.mark.asyncio() |
| 241 | + async def subscription_simple(): |
| 242 | + async def bar_resolve(_obj, _info): |
| 243 | + yield "bar" |
| 244 | + yield "oof" |
| 245 | + |
| 246 | + test_type = GraphQLObjectType( |
| 247 | + "Subscription", |
| 248 | + { |
| 249 | + "bar": GraphQLField( |
| 250 | + GraphQLString, |
| 251 | + resolve=lambda message, _info: message, |
| 252 | + subscribe=bar_resolve, |
| 253 | + ), |
| 254 | + }, |
| 255 | + ) |
| 256 | + doc = parse("subscription { bar }") |
| 257 | + |
| 258 | + async def reverse_middleware(next_, value, info, **kwargs): |
| 259 | + awaitable_maybe = next_(value, info, **kwargs) |
| 260 | + return awaitable_maybe[::-1] |
| 261 | + |
| 262 | + noop_type = GraphQLObjectType( |
| 263 | + "Noop", |
| 264 | + {"noop": GraphQLField(GraphQLString)}, |
| 265 | + ) |
| 266 | + schema = GraphQLSchema(query=noop_type, subscription=test_type) |
| 267 | + |
| 268 | + agen = subscribe( |
| 269 | + schema, |
| 270 | + doc, |
| 271 | + middleware=MiddlewareManager(reverse_middleware), |
| 272 | + ) |
| 273 | + assert inspect.isasyncgen(agen) |
| 274 | + data = (await agen.__anext__()).data |
| 275 | + assert data == {"bar": "rab"} |
| 276 | + data = (await agen.__anext__()).data |
| 277 | + assert data == {"bar": "foo"} |
| 278 | + |
239 | 279 | def describe_without_manager():
|
240 | 280 | def no_middleware():
|
241 | 281 | doc = parse("{ field }")
|
|
0 commit comments