Skip to content

Commit

Permalink
Merge pull request #9 from dpnova/fix/context-request-bug
Browse files Browse the repository at this point in the history
Copy the initial context rather than updating it. #8
  • Loading branch information
grazor authored Sep 4, 2017
2 parents 560f1cd + ca920b6 commit 36a9403
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
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

0 comments on commit 36a9403

Please sign in to comment.