|
16 | 16 |
|
17 | 17 | """Test the top-level (root) GraphQL queries.""" |
18 | 18 |
|
| 19 | +from contextlib import suppress |
19 | 20 | import pytest |
20 | 21 | from typing import TYPE_CHECKING |
21 | 22 |
|
| 23 | +from graphql import parse, MiddlewareManager |
| 24 | + |
22 | 25 | from cylc.flow.id import Tokens |
23 | 26 | from cylc.flow.network.client import WorkflowRuntimeClient |
| 27 | +from cylc.flow.network.schema import schema, SUB_RESOLVER_MAPPING |
| 28 | +from cylc.flow.network.graphql import ( |
| 29 | + CylcExecutionContext, |
| 30 | + IgnoreFieldMiddleware, |
| 31 | + instantiate_middleware, |
| 32 | +) |
| 33 | +from cylc.flow.network.graphql_subscribe import subscribe |
24 | 34 |
|
25 | 35 | if TYPE_CHECKING: |
26 | 36 | from cylc.flow.scheduler import Scheduler |
@@ -362,3 +372,40 @@ async def test_jobs(harness): |
362 | 372 | assert ret == { |
363 | 373 | 'job': {'id': f'{j_id}'} |
364 | 374 | } |
| 375 | + |
| 376 | + |
| 377 | +@pytest.mark.asyncio(loop_scope="module") |
| 378 | +async def test_subscription(harness): |
| 379 | + """Test the GraphQL subscription infrastructure. |
| 380 | +
|
| 381 | + (currently only used at UIS) |
| 382 | + """ |
| 383 | + schd, client, w_tokens = harness |
| 384 | + |
| 385 | + request_string = 'subscription { workflows (stripNull: true) { id } }' |
| 386 | + kwargs = { |
| 387 | + "variable_values": {}, |
| 388 | + "operation_name": None, |
| 389 | + "context_value": { |
| 390 | + 'op_id': 1, |
| 391 | + 'resolvers': schd.server.resolvers, |
| 392 | + 'meta': {}, |
| 393 | + }, |
| 394 | + "subscribe_resolver_map": SUB_RESOLVER_MAPPING, |
| 395 | + "middleware": MiddlewareManager( |
| 396 | + instantiate_middleware( |
| 397 | + [IgnoreFieldMiddleware] |
| 398 | + ) |
| 399 | + ), |
| 400 | + "execution_context_class": CylcExecutionContext, |
| 401 | + } |
| 402 | + document = parse(request_string) |
| 403 | + result = await subscribe( |
| 404 | + schema.graphql_schema, |
| 405 | + document, |
| 406 | + **kwargs |
| 407 | + ) |
| 408 | + with suppress(GeneratorExit): |
| 409 | + async for item in result: |
| 410 | + assert item.data['workflows'][0]['id'] == w_tokens.id |
| 411 | + await result.aclose() |
0 commit comments