Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit de3313c

Browse filesBrowse files
committed
Refactored params
1 parent 3050e0a commit de3313c
Copy full SHA for de3313c

File tree

Expand file treeCollapse file tree

2 files changed

+35
-29
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+35
-29
lines changed

‎flask_graphql/graphqlview.py

Copy file name to clipboardExpand all lines: flask_graphql/graphqlview.py
+26-24Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
from promise import Promise
3+
from collections import namedtuple
34

45
import six
56
from flask import Response, request
@@ -15,6 +16,8 @@
1516
from .render_graphiql import render_graphiql
1617

1718

19+
GraphQLParams = namedtuple('GraphQLParams', 'query,variables,operation_name,id')
20+
1821
class HttpQueryError(Exception):
1922
def __init__(self, status_code, message=None, is_graphql_error=False, headers=None):
2023
self.status_code = status_code
@@ -61,11 +64,12 @@ def get_middleware(self, request):
6164
def get_executor(self, request):
6265
return self.executor
6366

64-
def render_graphiql(self, **kwargs):
67+
def render_graphiql(self, params, result):
6568
return render_graphiql(
69+
params=params,
70+
result=result,
6671
graphiql_version=self.graphiql_version,
6772
graphiql_template=self.graphiql_template,
68-
**kwargs
6973
)
7074

7175
def dispatch_request(self):
@@ -115,11 +119,9 @@ def dispatch_request(self):
115119
result = self.json_encode(response, pretty)
116120

117121
if show_graphiql:
118-
query, variables, operation_name, id = self.get_graphql_params(data[0])
122+
params = self.get_graphql_params(data[0])
119123
return self.render_graphiql(
120-
query=query,
121-
variables=variables,
122-
operation_name=operation_name,
124+
params=params,
123125
result=result
124126
)
125127

@@ -140,23 +142,21 @@ def dispatch_request(self):
140142
)
141143

142144
def get_response(self, execute, data, show_graphiql=False, only_allow_query=False):
143-
query, variables, operation_name, id = self.get_graphql_params(data)
145+
params = self.get_graphql_params(data)
144146
try:
145147
execution_result = self.execute_graphql_request(
146148
self.schema,
147149
execute,
148150
data,
149-
query,
150-
variables,
151-
operation_name,
151+
params,
152152
only_allow_query,
153153
)
154154
except HttpQueryError:
155155
if show_graphiql:
156156
execution_result = None
157157
else:
158158
raise
159-
return self.format_execution_result(execution_result, id, self.format_error)
159+
return self.format_execution_result(execution_result, params.id, self.format_error)
160160

161161
@staticmethod
162162
def format_execution_result(execution_result, id, format_error):
@@ -223,12 +223,12 @@ def execute(self, schema, *args, **kwargs):
223223
)
224224

225225
@staticmethod
226-
def execute_graphql_request(schema, execute, data, query, variables, operation_name, only_allow_query=False):
227-
if not query:
226+
def execute_graphql_request(schema, execute, data, params, only_allow_query=False):
227+
if not params.query:
228228
raise HttpQueryError(400, 'Must provide query string.')
229229

230230
try:
231-
source = Source(query, name='GraphQL request')
231+
source = Source(params.query, name='GraphQL request')
232232
ast = parse(source)
233233
validation_errors = validate(schema, ast)
234234
if validation_errors:
@@ -240,7 +240,7 @@ def execute_graphql_request(schema, execute, data, query, variables, operation_n
240240
return ExecutionResult(errors=[e], invalid=True)
241241

242242
if only_allow_query:
243-
operation_ast = get_operation_ast(ast, operation_name)
243+
operation_ast = get_operation_ast(ast, params.operation_name)
244244
if operation_ast and operation_ast.operation != 'query':
245245
raise HttpQueryError(
246246
405,
@@ -254,8 +254,8 @@ def execute_graphql_request(schema, execute, data, query, variables, operation_n
254254
return execute(
255255
schema,
256256
ast,
257-
operation_name=operation_name,
258-
variable_values=variables,
257+
operation_name=params.operation_name,
258+
variable_values=params.variables,
259259
)
260260
except Exception as e:
261261
return ExecutionResult(errors=[e], invalid=True)
@@ -285,20 +285,22 @@ def request_wants_html(self):
285285
request.accept_mimetypes['application/json']
286286

287287
@staticmethod
288-
def get_graphql_params(data):
289-
query = data.get('query')
290-
variables = data.get('variables')
291-
id = data.get('id')
292-
288+
def get_variables(variables):
293289
if variables and isinstance(variables, six.text_type):
294290
try:
295-
variables = json.loads(variables)
291+
return json.loads(variables)
296292
except:
297293
raise HttpQueryError(400, 'Variables are invalid JSON.')
294+
return variables
298295

296+
@classmethod
297+
def get_graphql_params(cls, data):
298+
query = data.get('query')
299+
variables = cls.get_variables(data.get('variables'))
300+
id = data.get('id')
299301
operation_name = data.get('operationName')
300302

301-
return query, variables, operation_name, id
303+
return GraphQLParams(query, variables, operation_name, id)
302304

303305
@staticmethod
304306
def format_error(error):

‎flask_graphql/render_graphiql.py

Copy file name to clipboardExpand all lines: flask_graphql/render_graphiql.py
+9-5Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@
112112
onEditQuery: onEditQuery,
113113
onEditVariables: onEditVariables,
114114
onEditOperationName: onEditOperationName,
115-
query: {{ query|tojson }},
115+
query: {{ params.query|tojson }},
116116
response: {{ result|tojson }},
117-
variables: {{ variables|tojson }},
118-
operationName: {{ operation_name|tojson }},
117+
variables: {{ params.variables|tojson }},
118+
operationName: {{ params.operation_name|tojson }},
119119
}),
120120
document.body
121121
);
@@ -124,9 +124,13 @@
124124
</html>'''
125125

126126

127-
def render_graphiql(graphiql_version=None, graphiql_template=None, **kwargs):
127+
def render_graphiql(params, result, graphiql_version=None, graphiql_template=None):
128128
graphiql_version = graphiql_version or GRAPHIQL_VERSION
129129
template = graphiql_template or TEMPLATE
130130

131131
return render_template_string(
132-
template, graphiql_version=graphiql_version, **kwargs)
132+
template,
133+
graphiql_version=graphiql_version,
134+
result=result,
135+
params=params
136+
)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.