-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgraphqlview.py
192 lines (155 loc) · 6.33 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
from functools import partial
from cgi import parse_header
from promise import Promise
from sanic.response import HTTPResponse
from sanic.views import HTTPMethodView
from graphql.type.schema import GraphQLSchema
from graphql.execution.executors.asyncio import AsyncioExecutor
from graphql_server import (HttpQueryError, default_format_error,
encode_execution_results, json_encode,
load_json_body, run_http_query)
from .render_graphiql import render_graphiql
class GraphQLView(HTTPMethodView):
schema = None
executor = None
root_value = None
context = None
pretty = False
graphiql = False
graphiql_version = None
graphiql_template = None
middleware = None
batch = False
jinja_env = None
cors = False
max_age = 86400
_enable_async = True
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)
self._enable_async = self._enable_async and isinstance(self.executor, AsyncioExecutor)
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):
context = self.context or {}
if isinstance(context, dict) and 'request' not in context:
context.update({'request': request})
return context
def get_middleware(self, request):
return self.middleware
def get_executor(self, request):
return self.executor
def render_graphiql(self, params, result):
return render_graphiql(
jinja_env=self.jinja_env,
params=params,
result=result,
graphiql_version=self.graphiql_version,
graphiql_template=self.graphiql_template,
)
format_error = staticmethod(default_format_error)
encode = staticmethod(json_encode)
async def dispatch_request(self, request, *args, **kwargs):
try:
request_method = request.method.lower()
data = self.parse_body(request)
show_graphiql = request_method == 'get' and self.should_display_graphiql(request)
catch = show_graphiql
pretty = self.pretty or show_graphiql or request.args.get('pretty')
if request_method != 'options':
execution_results, all_params = run_http_query(
self.schema,
request_method,
data,
query_data=request.args,
batch_enabled=self.batch,
catch=catch,
# Execute options
return_promise=self._enable_async,
root_value=self.get_root_value(request),
context_value=self.get_context(request),
middleware=self.get_middleware(request),
executor=self.get_executor(request),
)
awaited_execution_results = await Promise.all(execution_results)
result, status_code = encode_execution_results(
awaited_execution_results,
is_batch=isinstance(data, list),
format_error=self.format_error,
encode=partial(self.encode, pretty=pretty)
)
if show_graphiql:
return await self.render_graphiql(
params=all_params[0],
result=result
)
return HTTPResponse(
result,
status=status_code,
content_type='application/json'
)
else:
return self.process_preflight(request)
except HttpQueryError as e:
return HTTPResponse(
self.encode({
'errors': [default_format_error(e)]
}),
status=e.status_code,
headers=e.headers,
content_type='application/json'
)
# noinspection PyBroadException
def parse_body(self, request):
content_type = self.get_mime_type(request)
if content_type == 'application/graphql':
return {'query': request.body.decode('utf8')}
elif content_type == 'application/json':
return load_json_body(request.body.decode('utf8'))
elif content_type in ('application/x-www-form-urlencoded', 'multipart/form-data'):
return request.form
return {}
@staticmethod
def get_mime_type(request):
# We use mimetype here since we don't need the other
# information provided by content_type
if 'content-type' not in request.headers:
return None
mimetype, _ = parse_header(request.headers['content-type'])
return mimetype
def should_display_graphiql(self, request):
if not self.graphiql or 'raw' in request.args:
return False
return self.request_wants_html(request)
def request_wants_html(self, request):
accept = request.headers.get('accept', {})
return 'text/html' in accept or '*/*' in accept
def process_preflight(self, request):
""" Preflight request support for apollo-client
https://www.w3.org/TR/cors/#resource-preflight-requests """
origin = request.headers.get('Origin', '')
method = request.headers.get('Access-Control-Request-Method', '').upper()
headers = request.headers.get('Access-Control-Request-Headers', '')
if method and method in self.methods:
if self.cors:
headers={
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Methods': ', '.join(self.methods),
'Access-Control-Allow-Headers': headers,
'Access-Control-Allow-Age': str(self.max_age),
}
else:
headers = {}
return HTTPResponse(
status=200,
headers=headers,
)
else:
return HTTPResponse(
status=400,
)