Skip to content

Commit d76450a

Browse files
Allow empty custom context in GraphQLView (#106)
* Allow empty custom context in GraphQLView * chore: black --------- Co-authored-by: Kien Dang <[email protected]>
1 parent 8b9639e commit d76450a

15 files changed

+110
-5
lines changed

graphql_server/aiohttp/graphqlview.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def get_root_value(self):
7676
def get_context(self, request):
7777
context = (
7878
copy.copy(self.context)
79-
if self.context and isinstance(self.context, MutableMapping)
79+
if self.context is not None and isinstance(self.context, MutableMapping)
8080
else {}
8181
)
8282
if isinstance(context, MutableMapping) and "request" not in context:

graphql_server/flask/graphqlview.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def get_root_value(self):
7373
def get_context(self):
7474
context = (
7575
copy.copy(self.context)
76-
if self.context and isinstance(self.context, MutableMapping)
76+
if self.context is not None and isinstance(self.context, MutableMapping)
7777
else {}
7878
)
7979
if isinstance(context, MutableMapping) and "request" not in context:

graphql_server/quart/graphqlview.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def get_root_value(self):
7777
def get_context(self):
7878
context = (
7979
copy.copy(self.context)
80-
if self.context and isinstance(self.context, MutableMapping)
80+
if self.context is not None and isinstance(self.context, MutableMapping)
8181
else {}
8282
)
8383
if isinstance(context, MutableMapping) and "request" not in context:

graphql_server/sanic/graphqlview.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def get_root_value(self):
7878
def get_context(self, request):
7979
context = (
8080
copy.copy(self.context)
81-
if self.context and isinstance(self.context, MutableMapping)
81+
if self.context is not None and isinstance(self.context, MutableMapping)
8282
else {}
8383
)
8484
if isinstance(context, MutableMapping) and "request" not in context:

graphql_server/webob/graphqlview.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def get_root_value(self):
7272
def get_context(self, request):
7373
context = (
7474
copy.copy(self.context)
75-
if self.context and isinstance(self.context, MutableMapping)
75+
if self.context is not None and isinstance(self.context, MutableMapping)
7676
else {}
7777
)
7878
if isinstance(context, MutableMapping) and "request" not in context:

tests/aiohttp/schema.py

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def resolve_raises(*_):
3535
GraphQLNonNull(GraphQLString),
3636
resolve=lambda obj, info: info.context["request"],
3737
),
38+
"property": GraphQLField(
39+
GraphQLString, resolve=lambda obj, info: info.context.property
40+
),
3841
},
3942
),
4043
resolve=lambda obj, info: info.context,

tests/aiohttp/test_graphqlview.py

+18
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,24 @@ async def test_context_remapped_if_not_mapping(app, client):
539539
assert "Request" in _json["data"]["context"]["request"]
540540

541541

542+
class CustomContext(dict):
543+
property = "A custom property"
544+
545+
546+
@pytest.mark.asyncio
547+
@pytest.mark.parametrize("app", [create_app(context=CustomContext())])
548+
async def test_allow_empty_custom_context(app, client):
549+
response = await client.get(url_string(query="{context { property request }}"))
550+
551+
_json = await response.json()
552+
assert response.status == 200
553+
assert "data" in _json
554+
assert "request" in _json["data"]["context"]
555+
assert "property" in _json["data"]["context"]
556+
assert "A custom property" == _json["data"]["context"]["property"]
557+
assert "Request" in _json["data"]["context"]["request"]
558+
559+
542560
@pytest.mark.asyncio
543561
@pytest.mark.parametrize("app", [create_app(context={"request": "test"})])
544562
async def test_request_not_replaced(app, client):

tests/flask/schema.py

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def resolve_raises(*_):
2929
GraphQLNonNull(GraphQLString),
3030
resolve=lambda obj, info: info.context["request"],
3131
),
32+
"property": GraphQLField(
33+
GraphQLString, resolve=lambda obj, info: info.context.property
34+
),
3235
},
3336
),
3437
resolve=lambda obj, info: info.context,

tests/flask/test_graphqlview.py

+17
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,23 @@ def test_context_remapped_if_not_mapping(app, client):
499499
assert "Request" in res["data"]["context"]["request"]
500500

501501

502+
class CustomContext(dict):
503+
property = "A custom property"
504+
505+
506+
@pytest.mark.parametrize("app", [create_app(context=CustomContext())])
507+
def test_allow_empty_custom_context(app, client):
508+
response = client.get(url_string(app, query="{context { property request }}"))
509+
510+
assert response.status_code == 200
511+
res = response_json(response)
512+
assert "data" in res
513+
assert "request" in res["data"]["context"]
514+
assert "property" in res["data"]["context"]
515+
assert "A custom property" == res["data"]["context"]["property"]
516+
assert "Request" in res["data"]["context"]["request"]
517+
518+
502519
def test_post_multipart_data(app, client):
503520
query = "mutation TestMutation { writeTest { test } }"
504521
response = client.post(

tests/quart/schema.py

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def resolve_raises(*_):
3232
GraphQLNonNull(GraphQLString),
3333
resolve=lambda obj, info: info.context["request"],
3434
),
35+
"property": GraphQLField(
36+
GraphQLString, resolve=lambda obj, info: info.context.property
37+
),
3538
},
3639
),
3740
resolve=lambda obj, info: info.context,

tests/quart/test_graphqlview.py

+19
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,25 @@ async def test_context_remapped_if_not_mapping(app: Quart, client: TestClientPro
633633
assert "Request" in res["data"]["context"]["request"]
634634

635635

636+
class CustomContext(dict):
637+
property = "A custom property"
638+
639+
640+
@pytest.mark.asyncio
641+
@pytest.mark.parametrize("app", [create_app(context=CustomContext())])
642+
async def test_allow_empty_custom_context(app: Quart, client: TestClientProtocol):
643+
response = await execute_client(app, client, query="{context { property request }}")
644+
645+
assert response.status_code == 200
646+
result = await response.get_data(as_text=True)
647+
res = response_json(result)
648+
assert "data" in res
649+
assert "request" in res["data"]["context"]
650+
assert "property" in res["data"]["context"]
651+
assert "A custom property" == res["data"]["context"]["property"]
652+
assert "Request" in res["data"]["context"]["request"]
653+
654+
636655
# @pytest.mark.asyncio
637656
# async def test_post_multipart_data(app: Quart, client: TestClientProtocol):
638657
# query = "mutation TestMutation { writeTest { test } }"

tests/sanic/schema.py

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def resolve_raises(*_):
3232
GraphQLNonNull(GraphQLString),
3333
resolve=lambda obj, info: info.context["request"],
3434
),
35+
"property": GraphQLField(
36+
GraphQLString, resolve=lambda obj, info: info.context.property
37+
),
3538
},
3639
),
3740
resolve=lambda obj, info: info.context,

tests/sanic/test_graphqlview.py

+19
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,25 @@ def test_passes_custom_context_into_context(app):
469469
assert "Request" in res["data"]["context"]["request"]
470470

471471

472+
class CustomContext(dict):
473+
property = "A custom property"
474+
475+
476+
@pytest.mark.parametrize("app", [create_app(context=CustomContext())])
477+
def test_allow_empty_custom_context(app):
478+
_, response = app.test_client.get(
479+
uri=url_string(query="{context { property request }}")
480+
)
481+
482+
assert response.status_code == 200
483+
res = response_json(response)
484+
assert "data" in res
485+
assert "request" in res["data"]["context"]
486+
assert "property" in res["data"]["context"]
487+
assert "A custom property" == res["data"]["context"]["property"]
488+
assert "Request" in res["data"]["context"]["request"]
489+
490+
472491
@pytest.mark.parametrize("app", [create_app(context="CUSTOM CONTEXT")])
473492
def test_context_remapped_if_not_mapping(app):
474493
_, response = app.test_client.get(

tests/webob/schema.py

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def resolve_raises(*_):
3030
GraphQLNonNull(GraphQLString),
3131
resolve=lambda obj, info: info.context["request"],
3232
),
33+
"property": GraphQLField(
34+
GraphQLString, resolve=lambda obj, info: info.context.property
35+
),
3336
},
3437
),
3538
resolve=lambda obj, info: info.context,

tests/webob/test_graphqlview.py

+17
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,23 @@ def test_context_remapped_if_not_mapping(client, settings):
461461
assert "request" in res["data"]["context"]["request"]
462462

463463

464+
class CustomContext(dict):
465+
property = "A custom property"
466+
467+
468+
@pytest.mark.parametrize("settings", [dict(context=CustomContext())])
469+
def test_allow_empty_custom_context(client, settings):
470+
response = client.get(url_string(query="{context { property request }}"))
471+
472+
assert response.status_code == 200
473+
res = response_json(response)
474+
assert "data" in res
475+
assert "request" in res["data"]["context"]
476+
assert "property" in res["data"]["context"]
477+
assert "A custom property" == res["data"]["context"]["property"]
478+
assert "request" in res["data"]["context"]["request"]
479+
480+
464481
def test_post_multipart_data(client):
465482
query = "mutation TestMutation { writeTest { test } }"
466483
data = (

0 commit comments

Comments
 (0)