8000 Improved format execution_results · graphql-python/flask-graphql@15fe6c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 15fe6c5

Browse files
committed
Improved format execution_results
1 parent 3404fad commit 15fe6c5

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

flask_graphql/graphqlview.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from flask.views import View
55

66
from graphql.type.schema import GraphQLSchema
7-
from graphql_server import run_http_query, HttpQueryError, default_format_error, load_json_body
7+
from graphql_server import run_http_query, HttpQueryError, default_format_error, load_json_body, format_execution_result
88

99
from .render_graphiql import render_graphiql
1010

@@ -65,11 +65,11 @@ def dispatch_request(self):
6565

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

68-
result, status_code, all_params = run_http_query(
68+
execution_results, all_params = run_http_query(
6969
self.schema,
7070
request_method,
7171
data,
72-
query_data=request.args.to_dict(),
72+
query_data=request.args,
7373
batch_enabled=self.batch,
7474
catch=catch,
7575
# Execute options
@@ -78,6 +78,16 @@ def dispatch_request(self):
7878
middleware=self.get_middleware(),
7979
executor=self.get_executor(),
8080
)
81+
responses = [
82+
format_execution_result(execution_result, default_format_error)
83+
for execution_result in execution_results
84+
]
85+
result, status_codes = zip(*responses)
86+
status_code = max(status_codes)
87+
88+
# If is not batch
89+
if not isinstance(data, list):
90+
result = result[0]
8191

8292
result = self.json_encode(result, pretty)
8393

@@ -110,14 +120,14 @@ def parse_body(self):
110120
# information provided by content_type
111121
content_type = request.mimetype
112122
if content_type == 'application/graphql':
113-
return {'query': request.data.decode()}
123+
return {'query': request.data.decode('utf8')}
114124

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

118128
elif content_type == 'application/x-www-form-urlencoded' \
119129
or content_type == 'multipart/form-data':
120-
return request.form.to_dict()
130+
return request.form
121131

122132
return {}
123133

graphql_server/__init__.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def default_format_error(error):
3030

3131

3232

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

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

74-
if format_error is None:
75-
format_error = default_format_error
76-
77-
responses = [format_execution_result(get_response(
74+
responses = [get_response(
7875
schema,
7976
params,
8077
catch,
8178
allow_only_query,
8279
**execute_options
83-
), params.id, format_error) for params in all_params]
84-
85-
response, status_codes = zip(*responses)
86-
status_code = max(status_codes)
80+
) for params in all_params]
8781

88-
if not is_batch:
89-
response = response[0]
90-
91-
return response, status_code, all_params
82+
return responses, all_params
9283

9384

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

131122

132-
def format_execution_result(execution_result, id, format_error):
123+
def format_execution_result(execution_result, format_error):
133124
status_code = 200
134125

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

150-
if id:
151-
response['id'] = id
152-
153141
else:
154142
response = None
155143

tests/test_graphqlview.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,16 @@ def test_post_multipart_data(client):
470470
def test_batch_allows_post_with_json_encoding(client):
471471
response = client.post(
472472
url_string(),
473-
data=jl(id=1, query='{test}'),
473+
data=jl(
474+
# id=1,
475+
query='{test}'
476+
),
474477
content_type='application/json'
475478
)
476479

477480
assert response.status_code == 200
478481
assert response_json(response) == [{
479-
'id': 1,
482+
# 'id': 1,
480483
'data': {'test': "Hello World"}
481484
}]
482485

@@ -486,7 +489,7 @@ def test_batch_supports_post_json_query_with_json_variables(client):
486489
response = client.post(
487490
url_string(),
488491
data=jl(
489-
id=1,
492+
# id=1,
490493
query='query helloWho($who: String){ test(who: $who) }',
491494
variables={'who': "Dolly"}
492495
),
@@ -495,7 +498,7 @@ def test_batch_supports_post_json_query_with_json_variables(client):
495498

496499
assert response.status_code == 200
497500
assert response_json(response) == [{
498-
'id': 1,
501+
# 'id': 1,
499502
'data': {'test': "Hello Dolly"}
500503
}]
501504

@@ -505,7 +508,7 @@ def test_batch_allows_post_with_operation_name(client):
505508
response = client.post(
506509
url_string(),
507510
data=jl(
508-
id=1,
511+
# id=1,
509512
query='''
510513
query helloYou { test(who: "You"), ...shared }
511514
query helloWorld { test(who: "World"), ...shared }
@@ -521,7 +524,7 @@ def test_batch_allows_post_with_operation_name(client):
521524

522525
assert response.status_code == 200
523526
assert response_json(response) == [{
524-
'id': 1,
527+
# 'id': 1,
525528
'data': {
526529
'test': 'Hello World',
527530
'shared': 'Hello Everyone'

0 commit comments

Comments
 (0)
0