| 1 |
import asyncio |
| 2 |
from graphql import ( |
| 3 |
GraphQLSchema, |
| 4 |
GraphQLObjectType, |
| 5 |
GraphQLField, |
| 6 |
GraphQLString, |
| 7 |
graphql, |
| 8 |
) |
| 9 |
|
| 10 |
user = GraphQLObjectType( |
| 11 |
name="User", |
| 12 |
fields={ |
| 13 |
"id": GraphQLField(GraphQLString), |
| 14 |
"name": GraphQLField(GraphQLString), |
| 15 |
}, |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
async def resolve_user(obj, info): |
| 20 |
return { |
| 21 |
"id": "1", |
| 22 |
"name": "Sarah", |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
schema = GraphQLSchema( |
| 27 |
query=GraphQLObjectType( |
| 28 |
name="Query", |
| 29 |
fields={ |
| 30 |
"user": GraphQLField( |
| 31 |
user, |
| 32 |
resolve=resolve_user, |
| 33 |
) |
| 34 |
}, |
| 35 |
) |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
def test_execute_basic_async(benchmark): |
| 40 |
# Note: we are creating the async loop outside of the benchmark code so that |
| 41 |
# the setup is not included in the benchmark timings |
| 42 |
loop = asyncio.events.new_event_loop() |
| 43 |
asyncio.events.set_event_loop(loop) |
| 44 |
result = benchmark( |
| 45 |
lambda: loop.run_until_complete(graphql(schema, "query { user { id, name }}")) |
| 46 |
) |
| 47 |
asyncio.events.set_event_loop(None) |
| 48 |
loop.close() |
| 49 |
assert not result.errors |
| 50 |
assert result.data == { |
| 51 |
"user": { |
| 52 |
"id": "1", |
| 53 |
"name": "Sarah", |
| 54 |
}, |
| 55 |
} |