Skip to content

Commit 914791e

Browse files
committed
chore: add async schema test for quart
1 parent c671d83 commit 914791e

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

tests/quart/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
from tests.quart.schema import Schema
55

66

7-
def create_app(path="/graphql", **kwargs):
7+
def create_app(path="/graphql", schema=Schema, **kwargs):
88
server = Quart(__name__)
99
server.debug = True
1010
server.add_url_rule(
11-
path, view_func=GraphQLView.as_view("graphql", schema=Schema, **kwargs)
11+
path, view_func=GraphQLView.as_view("graphql", schema=schema, **kwargs)
1212
)
1313
return server
1414

tests/quart/schema.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import asyncio
2+
13
from graphql.type.definition import (
24
GraphQLArgument,
35
GraphQLField,
@@ -12,6 +14,7 @@ def resolve_raises(*_):
1214
raise Exception("Throws!")
1315

1416

17+
# Sync schema
1518
QueryRootType = GraphQLObjectType(
1619
name="QueryRoot",
1720
fields={
@@ -36,7 +39,7 @@ def resolve_raises(*_):
3639
"test": GraphQLField(
3740
type_=GraphQLString,
3841
args={"who": GraphQLArgument(GraphQLString)},
39-
resolve=lambda obj, info, who="World": "Hello %s" % who,
42+
resolve=lambda obj, info, who="World": f"Hello {who}",
4043
),
4144
},
4245
)
@@ -49,3 +52,48 @@ def resolve_raises(*_):
4952
)
5053

5154
Schema = GraphQLSchema(QueryRootType, MutationRootType)
55+
56+
57+
# Schema with async methods
58+
async def resolver_field_async_1(_obj, info):
59+
await asyncio.sleep(0.001)
60+
return "hey"
61+
62+
63+
async def resolver_field_async_2(_obj, info):
64+
await asyncio.sleep(0.003)
65+
return "hey2"
66+
67+
68+
def resolver_field_sync(_obj, info):
69+
return "hey3"
70+
71+
72+
AsyncQueryType = GraphQLObjectType(
73+
name="AsyncQueryType",
74+
fields={
75+
"a": GraphQLField(GraphQLString, resolve=resolver_field_async_1),
76+
"b": GraphQLField(GraphQLString, resolve=resolver_field_async_2),
77+
"c": GraphQLField(GraphQLString, resolve=resolver_field_sync),
78+
},
79+
)
80+
81+
82+
def resolver_field_sync_1(_obj, info):
83+
return "synced_one"
84+
85+
86+
def resolver_field_sync_2(_obj, info):
87+
return "synced_two"
88+
89+
90+
SyncQueryType = GraphQLObjectType(
91+
"SyncQueryType",
92+
{
93+
"a": GraphQLField(GraphQLString, resolve=resolver_field_sync_1),
94+
"b": GraphQLField(GraphQLString, resolve=resolver_field_sync_2),
95+
},
96+
)
97+
98+
AsyncSchema = GraphQLSchema(AsyncQueryType)
99+
SyncSchema = GraphQLSchema(SyncQueryType)

tests/quart/test_graphqlview.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from werkzeug.datastructures import Headers
99

1010
from .app import create_app
11+
from .schema import AsyncSchema
1112

1213

1314
@pytest.fixture
@@ -733,3 +734,17 @@ async def test_batch_allows_post_with_operation_name(
733734
assert response_json(result) == [
734735
{"data": {"test": "Hello World", "shared": "Hello Everyone"}}
735736
]
737+
738+
739+
@pytest.mark.asyncio
740+
@pytest.mark.parametrize("app", [create_app(schema=AsyncSchema, enable_async=True)])
741+
async def test_async_schema(app, client):
742+
response = await execute_client(
743+
app,
744+
client,
745+
query="{a,b,c}",
746+
)
747+
748+
assert response.status_code == 200
749+
result = await response.get_data(as_text=True)
750+
assert response_json(result) == {"data": {"a": "hey", "b": "hey2", "c": "hey3"}}

0 commit comments

Comments
 (0)