Skip to content

Commit

Permalink
Improved catch and removed base context from the GraphQLView
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Mar 23, 2017
1 parent 4a09ea2 commit d728f80
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
7 changes: 2 additions & 5 deletions flask_graphql/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class GraphQLView(View):
schema = None
executor = None
root_value = None
context = None
pretty = False
graphiql = False
graphiql_version = None
Expand All @@ -38,8 +37,6 @@ def get_root_value(self):
return self.root_value

def get_context(self):
if self.context is not None:
return self.context
return request

def get_middleware(self):
Expand All @@ -65,7 +62,7 @@ def dispatch_request(self):
data = self.parse_body()

show_graphiql = request_method == 'get' and self.should_display_graphiql()
catch = HttpQueryError if show_graphiql else None
catch = show_graphiql

pretty = self.pretty or show_graphiql or request.args.get('pretty')

Expand Down Expand Up @@ -97,8 +94,8 @@ def dispatch_request(self):
)

return Response(
result,
status=status_code,
response=result,
content_type='application/json'
)

Expand Down
13 changes: 7 additions & 6 deletions graphql_server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from collections import namedtuple
from collections import namedtuple, MutableMapping

import six
from promise import Promise
Expand Down Expand Up @@ -28,7 +28,7 @@ def default_format_error(error):
return {'message': six.text_type(error)}


def run_http_query(schema, request_method, data, query_data=None, batch_enabled=False, catch=None, **execute_options):
def run_http_query(schema, request_method, data, query_data=None, batch_enabled=False, catch=False, **execute_options):
if request_method not in ('get', 'post'):
raise HttpQueryError(
405,
Expand All @@ -37,14 +37,17 @@ def run_http_query(schema, request_method, data, query_data=None, batch_enabled=
'Allow': 'GET, POST'
}
)

if catch:
catch = HttpQueryError
else:
catch = SkipException
is_batch = isinstance(data, list)

is_get_request = request_method == 'get'
allow_only_query = is_get_request

if not is_batch:
if not isinstance(data, dict):
if not isinstance(data, (dict, MutableMapping)):
raise HttpQueryError(
400,
'GraphQL params should be a dict. Received {}.'.format(data)
Expand Down Expand Up @@ -124,8 +127,6 @@ def get_graphql_params(data, query_data):


def get_response(schema, params, catch=None, allow_only_query=False, **kwargs):
if catch is None:
catch = SkipException
try:
execution_result = execute_graphql_request(
schema,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def test_passes_request_into_request_context(client):
}


@pytest.mark.parametrize('app', [create_app(context="CUSTOM CONTEXT")])
@pytest.mark.parametrize('app', [create_app(get_context=lambda:"CUSTOM CONTEXT")])
def test_supports_pretty_printing(client):
response = client.get(url_string(query='{context}'))

Expand Down

0 comments on commit d728f80

Please sign in to comment.