-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathgraphqlview.py
268 lines (219 loc) · 8.97 KB
/
graphqlview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# -*- coding: utf-8 -*-
import json
import six
from flask import request
from flask import Response
from flask.views import View
from graphql import execute
from graphql import parse
from graphql import Source
from graphql import validate
from graphql.error import format_error as format_graphql_error
from graphql.error import GraphQLError
from graphql.execution import ExecutionResult
from graphql.type.schema import GraphQLSchema
from graphql.utils.get_operation_ast import get_operation_ast
from werkzeug.exceptions import BadRequest
from werkzeug.exceptions import MethodNotAllowed
from .render_graphiql import render_graphiql
class HttpError(Exception):
def __init__(self, response, message=None, *args, **kwargs):
self.response = response
self.message = message = message or response.description
super(HttpError, self).__init__(message, *args, **kwargs)
class GraphQLView(View):
schema = None
executor = None
root_value = None
context = None
pretty = False
graphiql = False
graphiql_version = None
graphiql_template = None
middleware = None
batch = False
methods = ['GET', 'POST', 'PUT', 'DELETE']
def __init__(self, **kwargs):
super(GraphQLView, self).__init__()
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)
assert not all((self.graphiql, self.batch)), 'Use either graphiql or batch processing'
assert isinstance(self.schema, GraphQLSchema), 'A Schema is required to be provided to GraphQLView.'
# noinspection PyUnusedLocal
def get_root_value(self, request):
return self.root_value
def get_context(self, request):
if self.context is not None:
return self.context
return request
def get_middleware(self, request):
return self.middleware
def get_executor(self, request):
return self.executor
def render_graphiql(self, **kwargs):
return render_graphiql(
graphiql_version=self.graphiql_version,
graphiql_template=self.graphiql_template,
**kwargs
)
def dispatch_request(self):
try:
if request.method.lower() not in ('get', 'post'):
raise HttpError(MethodNotAllowed(['GET', 'POST'], 'GraphQL only supports GET and POST requests.'))
data = self.parse_body(request)
show_graphiql = self.graphiql and self.can_display_graphiql(data)
if self.batch:
responses = [self.get_response(request, entry) for entry in data]
result = '[{}]'.format(','.join([response[0] for response in responses]))
status_code = max(responses, key=lambda response: response[1])[1]
else:
result, status_code = self.get_response(request, data, show_graphiql)
if show_graphiql:
query, variables, operation_name, id = self.get_graphql_params(request, data)
return self.render_graphiql(
query=query,
variables=variables,
operation_name=operation_name,
result=result
)
return Response(
status=status_code,
response=result,
content_type='application/json'
)
except HttpError as e:
return Response(
self.json_encode(request, {
'errors': [self.format_error(e)]
}),
status=e.response.code,
headers={'Allow': ['GET, POST']},
content_type='application/json'
)
def get_response(self, request, data, show_graphiql=False):
query, variables, operation_name, id = self.get_graphql_params(request, data)
execution_result = self.execute_graphql_request(
data,
query,
variables,
operation_name,
show_graphiql
)
status_code = 200
if execution_result:
response = {}
if execution_result.errors:
response['errors'] = [self.format_error(e) for e in execution_result.errors]
if execution_result.invalid:
status_code = 400
else:
status_code = 200
response['data'] = execution_result.data
if self.batch:
response = {
'id': id,
'payload': response,
'status': status_code,
}
result = self.json_encode(request, response, show_graphiql)
else:
result = None
return result, status_code
def json_encode(self, request, d, show_graphiql=False):
pretty = self.pretty or show_graphiql or request.args.get('pretty')
if not pretty:
return json.dumps(d, separators=(',', ':'))
return json.dumps(d, sort_keys=True,
indent=2, separators=(',', ': '))
# noinspection PyBroadException
def parse_body(self, request):
content_type = self.get_content_type(request)
if content_type == 'application/graphql':
return {'query': request.data.decode()}
elif content_type == 'application/json':
try:
request_json = json.loads(request.data.decode('utf8'))
if self.batch:
assert isinstance(request_json, list)
else:
assert isinstance(request_json, dict)
return request_json
except:
raise HttpError(BadRequest('POST body sent invalid JSON.'))
elif content_type == 'application/x-www-form-urlencoded':
return request.form
elif content_type == 'multipart/form-data':
return request.form
return {}
def execute(self, *args, **kwargs):
return execute(self.schema, *args, **kwargs)
def execute_graphql_request(self, data, query, variables, operation_name, show_graphiql=False):
if not query:
if show_graphiql:
return None
raise HttpError(BadRequest('Must provide query string.'))
try:
source = Source(query, name='GraphQL request')
ast = parse(source)
validation_errors = validate(self.schema, ast)
if validation_errors:
return ExecutionResult(
errors=validation_errors,
invalid=True,
)
except Exception as e:
return ExecutionResult(errors=[e], invalid=True)
if request.method.lower() == 'get':
operation_ast = get_operation_ast(ast, operation_name)
if operation_ast and operation_ast.operation != 'query':
if show_graphiql:
return None
raise HttpError(MethodNotAllowed(
['POST'], 'Can only perform a {} operation from a POST request.'.format(operation_ast.operation)
))
try:
return self.execute(
ast,
root_value=self.get_root_value(request),
variable_values=variables or {},
operation_name=operation_name,
context_value=self.get_context(request),
middleware=self.get_middleware(request),
executor=self.get_executor(request)
)
except Exception as e:
return ExecutionResult(errors=[e], invalid=True)
@classmethod
def can_display_graphiql(cls, data):
raw = 'raw' in request.args or 'raw' in data
return not raw and cls.request_wants_html(request)
@classmethod
def request_wants_html(cls, request):
best = request.accept_mimetypes \
.best_match(['application/json', 'text/html'])
return best == 'text/html' and \
request.accept_mimetypes[best] > \
request.accept_mimetypes['application/json']
@staticmethod
def get_graphql_params(request, data):
query = request.args.get('query') or data.get('query')
variables = request.args.get('variables') or data.get('variables')
id = request.args.get('id') or data.get('id')
if variables and isinstance(variables, six.text_type):
try:
variables = json.loads(variables)
except:
raise HttpError(BadRequest('Variables are invalid JSON.'))
operation_name = request.args.get('operationName') or data.get('operationName')
return query, variables, operation_name, id
@staticmethod
def format_error(error):
if isinstance(error, GraphQLError):
return format_graphql_error(error)
return {'message': six.text_type(error)}
@staticmethod
def get_content_type(request):
# We use mimetype here since we don't need the other
# information provided by content_type
return request.mimetype