Skip to content

Commit

Permalink
Improved format execution_results
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Mar 20, 2017
1 parent 3404fad commit 15fe6c5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
20 changes: 15 additions & 5 deletions flask_graphql/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask.views import View

from graphql.type.schema import GraphQLSchema
from graphql_server import run_http_query, HttpQueryError, default_format_error, load_json_body
from graphql_server import run_http_query, HttpQueryError, default_format_error, load_json_body, format_execution_result

from .render_graphiql import render_graphiql

Expand Down Expand Up @@ -65,11 +65,11 @@ def dispatch_request(self):

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

result, status_code, all_params = run_http_query(
execution_results, all_params = run_http_query(
self.schema,
request_method,
data,
query_data=request.args.to_dict(),
query_data=request.args,
batch_enabled=self.batch,
catch=catch,
# Execute options
Expand All @@ -78,6 +78,16 @@ def dispatch_request(self):
middleware=self.get_middleware(),
executor=self.get_executor(),
)
responses = [
format_execution_result(execution_result, default_format_error)
for execution_result in execution_results
]
result, status_codes = zip(*responses)
status_code = max(status_codes)

# If is not batch
if not isinstance(data, list):
result = result[0]

result = self.json_encode(result, pretty)

Expand Down Expand Up @@ -110,14 +120,14 @@ def parse_body(self):
# information provided by content_type
content_type = request.mimetype
if content_type == 'application/graphql':
return {'query': request.data.decode()}
return {'query': request.data.decode('utf8')}

elif content_type == 'application/json':
return load_json_body(request.data.decode('utf8'))

elif content_type == 'application/x-www-form-urlencoded' \
or content_type == 'multipart/form-data':
return request.form.to_dict()
return request.form

return {}

Expand Down
22 changes: 5 additions & 17 deletions graphql_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def default_format_error(error):



def run_http_query(schema, request_method, data, query_data=None, batch_enabled=False, format_error=None, catch=None, **execute_options):
def run_http_query(schema, request_method, data, query_data=None, batch_enabled=False, catch=None, **execute_options):
if request_method not in ('get', 'post'):
raise HttpQueryError(
405,
Expand Down Expand Up @@ -71,24 +71,15 @@ def run_http_query(schema, request_method, data, query_data=None, batch_enabled=

all_params = [get_graphql_params(entry, extra_data) for entry in data]

if format_error is None:
format_error = default_format_error

responses = [format_execution_result(get_response(
responses = [get_response(
schema,
params,
catch,
allow_only_query,
**execute_options
), params.id, format_error) for params in all_params]

response, status_codes = zip(*responses)
status_code = max(status_codes)
) for params in all_params]

if not is_batch:
response = response[0]

return response, status_code, all_params
return responses, all_params


def load_json_variables(variables):
Expand Down Expand Up @@ -129,7 +120,7 @@ def get_response(schema, params, catch=None, allow_only_query=False, **kwargs):
return execution_result


def format_execution_result(execution_result, id, format_error):
def format_execution_result(execution_result, format_error):
status_code = 200

if isinstance(execution_result, Promise):
Expand All @@ -147,9 +138,6 @@ def format_execution_result(execution_result, id, format_error):
status_code = 200
response['data'] = execution_result.data

if id:
response['id'] = id

else:
response = None

Expand Down
15 changes: 9 additions & 6 deletions tests/test_graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,16 @@ def test_post_multipart_data(client):
def test_batch_allows_post_with_json_encoding(client):
response = client.post(
url_string(),
data=jl(id=1, query='{test}'),
data=jl(
# id=1,
query='{test}'
),
content_type='application/json'
)

assert response.status_code == 200
assert response_json(response) == [{
'id': 1,
# 'id': 1,
'data': {'test': "Hello World"}
}]

Expand All @@ -486,7 +489,7 @@ def test_batch_supports_post_json_query_with_json_variables(client):
response = client.post(
url_string(),
data=jl(
id=1,
# id=1,
query='query helloWho($who: String){ test(who: $who) }',
variables={'who': "Dolly"}
),
Expand All @@ -495,7 +498,7 @@ def test_batch_supports_post_json_query_with_json_variables(client):

assert response.status_code == 200
assert response_json(response) == [{
'id': 1,
# 'id': 1,
'data': {'test': "Hello Dolly"}
}]

Expand All @@ -505,7 +508,7 @@ def test_batch_allows_post_with_operation_name(client):
response = client.post(
url_string(),
data=jl(
id=1,
# id=1,
query='''
query helloYou { test(who: "You"), ...shared }
query helloWorld { test(who: "World"), ...shared }
Expand All @@ -521,7 +524,7 @@ def test_batch_allows_post_with_operation_name(client):

assert response.status_code == 200
assert response_json(response) == [{
'id': 1,
# 'id': 1,
'data': {
'test': 'Hello World',
'shared': 'Hello Everyone'
Expand Down

0 comments on commit 15fe6c5

Please sign in to comment.