Skip to content
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. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy the initial context rather than updating it. #8 #9

Merged
merged 2 commits into from
Sep 4, 2017
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
Diff view
10 changes: 8 additions & 2 deletions sanic_graphql/graphqlview.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import Mapping
from functools import partial
from cgi import parse_header

Expand Down Expand Up @@ -47,8 +48,13 @@ def get_root_value(self, request):
return self.root_value

def get_context(self, request):
context = self.context or {}
if isinstance(context, dict) and 'request' not in context:
context = (
self.context.copy()
if self.context and
isinstance(self.context, Mapping)
else {}
)
if isinstance(context, Mapping) and 'request' not in context:
context.update({'request': request})
return context

Expand Down
13 changes: 5 additions & 8 deletions tests/test_graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,11 +484,8 @@ def test_supports_pretty_printing(app):


assert response.status == 200
assert response_json(response) == {
'data': {
'context': 'CUSTOM CONTEXT'
}
}
assert 'data' in response_json(response)
assert response_json(response)['data']['context'] == "{'request': {}}"


@parametrize_sync_async_app_test('app')
Expand All @@ -499,9 +496,9 @@ def test_post_multipart_data(app):
'Content-Disposition: form-data; name="query"\r\n' +
'\r\n' +
query + '\r\n' +
'------sanicgraphql--\r\n' +
'Content-Type: text/plain; charset=utf-8\r\n' +
'Content-Disposition: form-data; name="file"; filename="text1.txt"; filename*=utf-8\'\'text1.txt\r\n' +
'------sanicgraphql--\r\n' +
'Content-Type: text/plain; charset=utf-8\r\n' +
'Content-Disposition: form-data; name="file"; filename="text1.txt"; filename*=utf-8\'\'text1.txt\r\n' +
'\r\n' +
'\r\n' +
'------sanicgraphql--\r\n'
Expand Down