8000 Fix enable_async for aiohttp and sanic by r-owen · Pull Request #67 · graphql-python/graphql-server · GitHub
[go: up one dir, main page]

Skip to content

Fix enable_async for aiohttp and sanic #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement< 8000 /a>. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
7 changes: 5 additions & 2 deletions graphql_server/aiohttp/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List

from aiohttp import web
from graphql import GraphQLError
from graphql import ExecutionResult, GraphQLError
from graphql.type.schema import GraphQLSchema

from graphql_server import (
Expand Down Expand Up @@ -152,7 +152,10 @@ async def __call__(self, request):
)

exec_res = (
[await ex for ex in execution_results]
[
ex if ex is None or isinstance(ex, ExecutionResult) else await ex
for ex in execution_results
]
if self.enable_async
else execution_results
)
Expand Down
9 changes: 7 additions & 2 deletions graphql_server/sanic/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import partial
from typing import List

from graphql import GraphQLError
from graphql import ExecutionResult, GraphQLError
from graphql.type.schema import GraphQLSchema
from sanic.response import HTTPResponse, html
from sanic.views import HTTPMethodView
Expand Down Expand Up @@ -105,7 +105,12 @@ async def dispatch_request(self, request, *args, **kwargs):
middleware=self.get_middleware(),
)
exec_res = (
[await ex for ex in execution_results]
[
ex
if ex is None or isinstance(ex, ExecutionResult)
else await ex
for ex in execution_results
]
if self.enable_async
else execution_results
)
Expand Down
18 changes: 18 additions & 0 deletions tests/aiohttp/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,22 @@ def resolver_field_sync(_obj, info):
)


def resolver_field_sync_1(_obj, info):
return "synced_one"


def resolver_field_sync_2(_obj, info):
return "synced_two"


SyncQueryType = GraphQLObjectType(
"SyncQueryType",
{
"a": GraphQLField(GraphQLString, resolve=resolver_field_sync_1),
"b": GraphQLField(GraphQLString, resolve=resolver_field_sync_2),
},
)


AsyncSchema = GraphQLSchema(AsyncQueryType)
SyncSchema = GraphQLSchema(SyncQueryType)
48 changes: 44 additions & 4 deletions tests/aiohttp/test_graphiqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from jinja2 import Environment

from tests.aiohttp.app import create_app, url_string
from tests.aiohttp.schema import AsyncSchema, Schema
from tests.aiohttp.schema import AsyncSchema, Schema, SyncSchema


@pytest.fixture
Expand Down Expand Up @@ -102,11 +102,51 @@ async def test_graphiql_get_subscriptions(app, client):


@pytest.mark.asyncio
@pytest.mark.parametrize("app", [create_app(schema=AsyncSchema, enable_async=True)])
async def test_graphiql_async_schema(app, client):
@pytest.mark.parametrize(
"app", [create_app(schema=AsyncSchema, 8000 enable_async=True, graphiql=True)]
)
async def test_graphiql_enabled_async_schema(app, client):
response = await client.get(
url_string(query="{a,b,c}"), headers={"Accept": "text/html"},
)

expected_response = (
(
"{\n"
' "data": {\n'
' "a": "hey",\n'
' "b": "hey2",\n'
' "c": "hey3"\n'
" }\n"
"}"
)
.replace('"', '\\"')
.replace("\n", "\\n")
)
assert response.status == 200
assert expected_response in await response.text()


@pytest.mark.asyncio
@pytest.mark.parametrize(
"app", [create_app(schema=SyncSchema, enable_async=True, graphiql=True)]
)
async def test_graphiql_enabled_sync_schema(app, client):
response = await client.get(
url_string(query="{a,b}"), headers={"Accept": "text/html"},
)

expected_response = (
(
"{\n"
' "data": {\n'
' "a": "synced_one",\n'
' "b": "synced_two"\n'
" }\n"
"}"
)
.replace('"', '\\"')
.replace("\n", "\\n")
)
assert response.status == 200
assert await response.json() == {"data": {"a": "hey", "b": "hey2", "c": "hey3"}}
assert expected_response in await response.text()
18 changes: 18 additions & 0 deletions tests/sanic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,22 @@ def resolver_field_sync(_obj, info):
},
)


def resolver_field_sync_1(_obj, info):
return "synced_one"


def resolver_field_sync_2(_obj, info):
return "synced_two"


SyncQueryType = GraphQLObjectType(
"SyncQueryType",
{
"a": GraphQLField(GraphQLString, resolve=resolver_field_sync_1),
"b": GraphQLField(GraphQLString, resolve=resolver_field_sync_2),
},
)

AsyncSchema = GraphQLSchema(AsyncQueryType)
SyncSchema = GraphQLSchema(SyncQueryType)
31 changes: 28 additions & 3 deletions tests/sanic/test_graphiqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from jinja2 import Environment

from .app import create_app, url_string
from .schema import AsyncSchema
from .schema import AsyncSchema, SyncSchema


@pytest.fixture
Expand Down Expand Up @@ -62,9 +62,9 @@ def test_graphiql_html_is_not_accepted(app):


@pytest.mark.parametrize(
"app", [create_app(graphiql=True, schema=AsyncSchema, enable_async=True)]
"app", [create_app(schema=AsyncSchema, enable_async=True, graphiql=True)]
)
def test_graphiql_asyncio_schema(app):
def test_graphiql_enabled_async_schema(app):
query = "{a,b,c}"
_, response = app.client.get(
uri=url_string(query=query), headers={"Accept": "text/html"}
Expand All @@ -86,3 +86,28 @@ def test_graphiql_asyncio_schema(app):

assert response.status == 200
assert expected_response in response.body.decode("utf-8")


@pytest.mark.parametrize(
"app", [create_app(schema=SyncSchema, enable_async=True, graphiql=True)]
)
def test_graphiql_enabled_sync_schema(app):
query = "{a,b}"
_, response = app.client.get(
uri=url_string(query=query), headers={"Accept": "text/html"}
)

expected_response = (
(
"{\n"
' "data": {\n'
' "a": "synced_one",\n'
' "b": "synced_two"\n'
" }\n"
"}"
)
.replace('"', '\\"')
.replace("\n", "\\n")
)
assert response.status == 200
assert expected_response in response.body.decode("utf-8")
0