| 1 |
import asyncio |
| 2 |
from typing import Awaitable |
| 3 |
|
| 4 |
from pytest import mark |
| 5 |
|
| 6 |
from graphql.execution import execute |
| 7 |
from graphql.language import parse |
| 8 |
from graphql.type import ( |
| 9 |
GraphQLSchema, |
| 10 |
GraphQLObjectType, |
| 11 |
GraphQLField, |
| 12 |
GraphQLList, |
| 13 |
GraphQLInterfaceType, |
| 14 |
GraphQLBoolean, |
| 15 |
GraphQLInt, |
| 16 |
GraphQLString, |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
class Barrier: |
| 21 |
"""Barrier that makes progress only after a certain number of waits.""" |
| 22 |
|
| 23 |
def __init__(self, number: int): |
| 24 |
self.event = asyncio.Event() |
| 25 |
self.number = number |
| 26 |
|
| 27 |
async def wait(self) -> bool: |
| 28 |
self.number -= 1 |
| 29 |
if not self.number: |
| 30 |
self.event.set() |
| 31 |
return await self.event.wait() |
| 32 |
|
| 33 |
|
| 34 |
def describe_parallel_execution(): |
| 35 |
@mark.asyncio |
| 36 |
async def resolve_fields_in_parallel(): |
| 37 |
barrier = Barrier(2) |
| 38 |
|
| 39 |
async def resolve(*_args): |
| 40 |
return await barrier.wait() |
| 41 |
|
| 42 |
schema = GraphQLSchema( |
| 43 |
GraphQLObjectType( |
| 44 |
"Query", |
| 45 |
{ |
| 46 |
"foo": GraphQLField(GraphQLBoolean, resolve=resolve), |
| 47 |
"bar": GraphQLField(GraphQLBoolean, resolve=resolve), |
| 48 |
}, |
| 49 |
) |
| 50 |
) |
| 51 |
|
| 52 |
ast = parse("{foo, bar}") |
| 53 |
|
| 54 |
# raises TimeoutError if not parallel |
| 55 |
awaitable_result = execute(schema, ast) |
| 56 |
assert isinstance(awaitable_result, Awaitable) |
| 57 |
result = await asyncio.wait_for(awaitable_result, 1.0) |
| 58 |
|
| 59 |
assert result == ({"foo": True, "bar": True}, None) |
| 60 |
|
| 61 |
@mark.asyncio |
| 62 |
async def resolve_list_in_parallel(): |
| 63 |
barrier = Barrier(2) |
| 64 |
|
| 65 |
async def resolve(*_args): |
| 66 |
return await barrier.wait() |
| 67 |
|
| 68 |
async def resolve_list(*args): |
| 69 |
return [resolve(*args), resolve(*args)] |
| 70 |
|
| 71 |
schema = GraphQLSchema( |
| 72 |
GraphQLObjectType( |
| 73 |
"Query", |
| 74 |
{ |
| 75 |
"foo": GraphQLField( |
| 76 |
GraphQLList(GraphQLBoolean), resolve=resolve_list |
| 77 |
) |
| 78 |
}, |
| 79 |
) |
| 80 |
) |
| 81 |
|
| 82 |
ast = parse("{foo}") |
| 83 |
|
| 84 |
# raises TimeoutError if not parallel |
| 85 |
awaitable_result = execute(schema, ast) |
| 86 |
assert isinstance(awaitable_result, Awaitable) |
| 87 |
result = await asyncio.wait_for(awaitable_result, 1.0) |
| 88 |
|
| 89 |
assert result == ({"foo": [True, True]}, None) |
| 90 |
|
| 91 |
@mark.asyncio |
| 92 |
async def resolve_is_type_of_in_parallel(): |
| 93 |
FooType = GraphQLInterfaceType("Foo", {"foo": GraphQLField(GraphQLString)}) |
| 94 |
|
| 95 |
barrier = Barrier(4) |
| 96 |
|
| 97 |
async def is_type_of_bar(obj, *_args): |
| 98 |
await barrier.wait() |
| 99 |
return obj["foo"] == "bar" |
| 100 |
|
| 101 |
BarType = GraphQLObjectType( |
| 102 |
"Bar", |
| 103 |
{"foo": GraphQLField(GraphQLString), "foobar": GraphQLField(GraphQLInt)}, |
| 104 |
interfaces=[FooType], |
| 105 |
is_type_of=is_type_of_bar, |
| 106 |
) |
| 107 |
|
| 108 |
async def is_type_of_baz(obj, *_args): |
| 109 |
await barrier.wait() |
| 110 |
return obj["foo"] == "baz" |
| 111 |
|
| 112 |
BazType = GraphQLObjectType( |
| 113 |
"Baz", |
| 114 |
{"foo": GraphQLField(GraphQLString), "foobaz": GraphQLField(GraphQLInt)}, |
| 115 |
interfaces=[FooType], |
| 116 |
is_type_of=is_type_of_baz, |
| 117 |
) |
| 118 |
|
| 119 |
schema = GraphQLSchema( |
| 120 |
GraphQLObjectType( |
| 121 |
"Query", |
| 122 |
{ |
| 123 |
"foo": GraphQLField( |
| 124 |
GraphQLList(FooType), |
| 125 |
resolve=lambda *_args: [ |
| 126 |
{"foo": "bar", "foobar": 1}, |
| 127 |
{"foo": "baz", "foobaz": 2}, |
| 128 |
], |
| 129 |
) |
| 130 |
}, |
| 131 |
), |
| 132 |
types=[BarType, BazType], |
| 133 |
) |
| 134 |
|
| 135 |
ast = parse(""" |
| 136 |
{ |
| 137 |
foo { |
| 138 |
foo |
| 139 |
... on Bar { foobar } |
| 140 |
... on Baz { foobaz } |
| 141 |
} |
| 142 |
} |
| 143 |
""") |
| 144 |
|
| 145 |
# raises TimeoutError if not parallel |
| 146 |
awaitable_result = execute(schema, ast) |
| 147 |
assert isinstance(awaitable_result, Awaitable) |
| 148 |
result = await asyncio.wait_for(awaitable_result, 1.0) |
| 149 |
|
| 150 |
assert result == ( |
| 151 |
{"foo": [{"foo": "bar", "foobar": 1}, {"foo": "baz", "foobaz": 2}]}, |
| 152 |
None, |
| 153 |
) |