@@ -32,6 +32,7 @@ class GraphQLView(View):
32
32
graphiql_version = None
33
33
graphiql_template = None
34
34
middleware = None
35
+ batch = False
35
36
36
37
methods = ['GET' , 'POST' , 'PUT' , 'DELETE' ]
37
38
@@ -41,6 +42,7 @@ def __init__(self, **kwargs):
41
42
if hasattr (self , key ):
42
43
setattr (self , key , value )
43
44
45
+ assert not all ((self .graphiql , self .batch )), 'Use either graphiql or batch processing'
44
46
assert isinstance (self .schema , GraphQLSchema ), 'A Schema is required to be provided to GraphQLView.'
45
47
46
48
# noinspection PyUnusedLocal
@@ -66,33 +68,15 @@ def dispatch_request(self):
66
68
data = self .parse_body (request )
67
69
show_graphiql = self .graphiql and self .can_display_graphiql (data )
68
70
69
- query , variables , operation_name = self .get_graphql_params (request , data )
70
-
71
- execution_result = self .execute_graphql_request (
72
- data ,
73
- query ,
74
- variables ,
75
- operation_name ,
76
- show_graphiql
77
- )
78
-
79
- if execution_result :
80
- response = {}
81
-
82
- if execution_result .errors :
83
- response ['errors' ] = [self .format_error (e ) for e in execution_result .errors ]
84
-
85
- if execution_result .invalid :
86
- status_code = 400
87
- else :
88
- status_code = 200
89
- response ['data' ] = execution_result .data
90
-
91
- result = self .json_encode (request , response )
71
+ if self .batch :
72
+ responses = [self .get_response (request , entry ) for entry in data ]
73
+ result = '[{}]' .format (',' .join ([response [0 ] for response in responses ]))
74
+ status_code = max (responses , key = lambda response : response [1 ])[1 ]
92
75
else :
93
- result = None
76
+ result , status_code = self . get_response ( request , data , show_graphiql )
94
77
95
78
if show_graphiql :
79
+ query , variables , operation_name , id = self .get_graphql_params (request , data )
96
80
return render_graphiql (
97
81
graphiql_version = self .graphiql_version ,
98
82
graphiql_template = self .graphiql_template ,
@@ -118,6 +102,43 @@ def dispatch_request(self):
118
102
content_type = 'application/json'
119
103
)
120
104
105
+ def get_response (self , request , data , show_graphiql = False ):
106
+ query , variables , operation_name , id = self .get_graphql_params (request , data )
107
+
108
+ execution_result = self .execute_graphql_request (
109
+ data ,
110
+ query ,
111
+ variables ,
112
+ operation_name ,
113
+ show_graphiql
114
+ )
115
+
116
+ status_code = 200
117
+ if execution_result :
118
+ response = {}
119
+
120
+ if execution_result .errors :
121
+ response ['errors' ] = [self .format_error (e ) for e in execution_result .errors ]
122
+
123
+ if execution_result .invalid :
124
+ status_code = 400
125
+ else :
126
+ status_code = 200
127
+ response ['data' ] = execution_result .data
128
+
129
+ if self .batch :
130
+ response = {
131
+ 'id' : id ,
132
+ 'payload' : response ,
133
+ 'status' : status_code ,
134
+ }
135
+
136
+ result = self .json_encode (request , response )
137
+ else :
138
+ result = None
139
+
140
+ return result , status_code
141
+
121
142
def json_encode (self , request , d ):
122
143
if not self .pretty and not request .args .get ('pretty' ):
123
144
return json .dumps (d , separators = (',' , ':' ))
@@ -134,7 +155,10 @@ def parse_body(self, request):
134
155
elif content_type == 'application/json' :
135
156
try :
136
157
request_json = json .loads (request .data .decode ('utf8' ))
137
- assert isinstance (request_json , dict )
158
+ if self .batch :
159
+ assert isinstance (request_json , list )
160
+ else :
161
+ assert isinstance (request_json , dict )
138
162
return request_json
139
163
except :
140
164
raise HttpError (BadRequest ('POST body sent invalid JSON.' ))
@@ -207,6 +231,7 @@ def request_wants_html(cls, request):
207
231
def get_graphql_params (request , data ):
208
232
query = request .args .get ('query' ) or data .get ('query' )
209
233
variables = request .args .get ('variables' ) or data .get ('variables' )
234
+ id = request .args .get ('id' ) or data .get ('id' )
210
235
211
236
if variables and isinstance (variables , six .text_type ):
212
237
try :
@@ -216,7 +241,7 @@ def get_graphql_params(request, data):
216
241
217
242
operation_name = request .args .get ('operationName' ) or data .get ('operationName' )
218
243
219
- return query , variables , operation_name
244
+ return query , variables , operation_name , id
220
245
221
246
@staticmethod
222
247
def format_error (error ):
0 commit comments