From 7ac6115761185941c2a34880367085ae367dff86 Mon Sep 17 00:00:00 2001 From: "David P. Novakovic" Date: Sun, 3 Sep 2017 22:32:01 +1000 Subject: [PATCH 1/2] Copy the initial context rather than updating it. #8 --- sanic_graphql/graphqlview.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic_graphql/graphqlview.py b/sanic_graphql/graphqlview.py index 4177890..fc10378 100644 --- a/sanic_graphql/graphqlview.py +++ b/sanic_graphql/graphqlview.py @@ -47,7 +47,7 @@ def get_root_value(self, request): return self.root_value def get_context(self, request): - context = self.context or {} + context = self.context.copy() if self.context else {} if isinstance(context, dict) and 'request' not in context: context.update({'request': request}) return context From ca920b67b66ced9e049cbf48c00536531155ce4a Mon Sep 17 00:00:00 2001 From: "David P. Novakovic" Date: Sun, 3 Sep 2017 22:46:46 +1000 Subject: [PATCH 2/2] Fix broken tests. --- sanic_graphql/graphqlview.py | 10 ++++++++-- tests/test_graphqlview.py | 13 +++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/sanic_graphql/graphqlview.py b/sanic_graphql/graphqlview.py index fc10378..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.copy() if self.context else {} - 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'