Skip to content

Commit

Permalink
Fix enable_async for aiohttp and sanic if graphiql is enabled (#67)
Browse files Browse the repository at this point in the history
* Fix enable_async=True in aiohttp

Apply the fix suggested by ketanbshah in
#64

* Apply the same fix to sanic

* tests: add tests for graphiql enabled plus async

Co-authored-by: Manuel Bojato <30560560+KingDarBoja@users.noreply.github.com>
Co-authored-by: KingDarBoja <stevenbojato04@gmail.com>
  • Loading branch information
3 people authored Oct 17, 2020
1 parent 49f73c3 commit e39398a
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 11 deletions.
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, 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 comments on commit e39398a

Please sign in to comment.