Skip to content

Commit c40a233

Browse files
committed
Fix deprecation warning, improve coverage (#13)
1 parent 918bc97 commit c40a233

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

aiohttp_graphql/graphqlview.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,6 @@ async def __call__(self, request):
163163
)
164164

165165
except HttpQueryError as err:
166-
if err.headers and "Allow" in err.headers:
167-
# bug in graphql_server.execute_graphql_request
168-
# https://github.com/graphql-python/graphql-server-core/pull/4
169-
if isinstance(err.headers["Allow"], list):
170-
err.headers["Allow"] = ", ".join(err.headers["Allow"])
171-
172166
return web.Response(
173167
text=self.encoder({"errors": [self.error_formatter(err)]}),
174168
status=err.status_code,
@@ -198,4 +192,18 @@ def process_preflight(self, request):
198192
@classmethod
199193
def attach(cls, app, *, route_path="/graphql", route_name="graphql", **kwargs):
200194
view = cls(**kwargs)
201-
app.router.add_route("*", route_path, view, name=route_name)
195+
app.router.add_route("*", route_path, _asyncify(view), name=route_name)
196+
197+
198+
def _asyncify(handler):
199+
"""Return an async version of the given handler.
200+
201+
This is mainly here because ``aiohttp`` can't infer the async definition of
202+
:py:meth:`.GraphQLView.__call__` and raises a :py:class:`DeprecationWarning`
203+
in tests. Wrapping it into an async function avoids the noisy warning.
204+
"""
205+
206+
async def _dispatch(request):
207+
return await handler(request)
208+
209+
return _dispatch

tests/test_graphqlview.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,18 @@ async def test_passes_request_into_request_context(client, url_builder):
486486

487487
class TestCustomContext:
488488
@pytest.fixture
489-
def view_kwargs(self, view_kwargs):
489+
def view_kwargs(self, request, view_kwargs):
490490
# pylint: disable=no-self-use
491491
# pylint: disable=redefined-outer-name
492-
view_kwargs.update(context="CUSTOM CONTEXT")
492+
view_kwargs.update(context=request.param)
493493
return view_kwargs
494494

495+
@pytest.mark.parametrize(
496+
"view_kwargs",
497+
["CUSTOM CONTEXT", {"CUSTOM_CONTEXT": "test"}],
498+
indirect=True,
499+
ids=repr,
500+
)
495501
@pytest.mark.asyncio
496502
async def test_context_remapped(self, client, url_builder):
497503
response = await client.get(url_builder(query="{context}"))
@@ -501,6 +507,18 @@ async def test_context_remapped(self, client, url_builder):
501507
assert "request" in _json["data"]["context"]
502508
assert "CUSTOM CONTEXT" not in _json["data"]["context"]
503509

510+
@pytest.mark.parametrize(
511+
"view_kwargs", [{"request": "test"}], indirect=True, ids=repr
512+
)
513+
@pytest.mark.asyncio
514+
async def test_request_not_replaced(self, client, url_builder):
515+
response = await client.get(url_builder(query="{context}"))
516+
517+
_json = await response.json()
518+
assert response.status == 200
519+
assert "request" in _json["data"]["context"]
520+
assert _json["data"]["context"] == str({"request": "test"})
521+
504522

505523
@pytest.mark.asyncio
506524
async def test_post_multipart_data(client, base_url):

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = black, flake8, pylint, manifest, py{36,37,38}
2+
envlist = black, flake8, pylint, manifest, py{36,37,38,py3}
33

44
[testenv:black]
55
basepython = python3.8

0 commit comments

Comments
 (0)