diff --git a/sanic_graphql/graphqlview.py b/sanic_graphql/graphqlview.py index 4177890..7c19753 100644 --- a/sanic_graphql/graphqlview.py +++ b/sanic_graphql/graphqlview.py @@ -1,3 +1,4 @@ +from collections import Mapping from functools import partial from cgi import parse_header @@ -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 diff --git a/tests/test_graphqlview.py b/tests/test_graphqlview.py index b659539..4ad3918 100644 --- a/tests/test_graphqlview.py +++ b/tests/test_graphqlview.py @@ -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') @@ -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'