@@ -62,8 +62,10 @@ class GraphQLView(View):
62
62
middleware = None
63
63
root_value = None
64
64
pretty = False
65
+ batch = False
65
66
66
- def __init__ (self , schema = None , executor = None , middleware = None , root_value = None , graphiql = False , pretty = False ):
67
+ def __init__ (self , schema = None , executor = None , middleware = None , root_value = None , graphiql = False , pretty = False ,
68
+ batch = False ):
67
69
if not schema :
68
70
schema = graphene_settings .SCHEMA
69
71
@@ -77,8 +79,10 @@ def __init__(self, schema=None, executor=None, middleware=None, root_value=None,
77
79
self .root_value = root_value
78
80
self .pretty = pretty
79
81
self .graphiql = graphiql
82
+ self .batch = batch
80
83
81
84
assert isinstance (self .schema , GraphQLSchema ), 'A Schema is required to be provided to GraphQLView.'
85
+ assert not all ((graphiql , batch )), 'Use either graphiql or batch processing'
82
86
83
87
# noinspection PyUnusedLocal
84
88
def get_root_value (self , request ):
@@ -99,34 +103,15 @@ def dispatch(self, request, *args, **kwargs):
99
103
data = self .parse_body (request )
100
104
show_graphiql = self .graphiql and self .can_display_graphiql (request , data )
101
105
102
- query , variables , operation_name = self .get_graphql_params (request , data )
103
-
104
- execution_result = self .execute_graphql_request (
105
- request ,
106
- data ,
107
- query ,
108
- variables ,
109
- operation_name ,
110
- show_graphiql
111
- )
112
-
113
- if execution_result :
114
- response = {}
115
-
116
- if execution_result .errors :
117
- response ['errors' ] = [self .format_error (e ) for e in execution_result .errors ]
118
-
119
- if execution_result .invalid :
120
- status_code = 400
121
- else :
122
- status_code = 200
123
- response ['data' ] = execution_result .data
124
-
125
- result = self .json_encode (request , response , pretty = show_graphiql )
106
+ if self .batch :
107
+ responses = [self .get_response (request , entry ) for entry in data ]
108
+ result = '[{}]' .format (',' .join ([response [0 ] for response in responses ]))
109
+ status_code = max (responses , key = lambda response : response [1 ])[1 ]
126
110
else :
127
- result = None
111
+ result , status_code = self . get_response ( request , data , show_graphiql )
128
112
129
113
if show_graphiql :
114
+ query , variables , operation_name , id = self .get_graphql_params (request , data )
130
115
return self .render_graphiql (
131
116
request ,
132
117
graphiql_version = self .graphiql_version ,
@@ -150,6 +135,43 @@ def dispatch(self, request, *args, **kwargs):
150
135
})
151
136
return response
152
137
138
+ def get_response (self , request , data , show_graphiql = False ):
139
+ query , variables , operation_name , id = self .get_graphql_params (request , data )
140
+
141
+ execution_result = self .execute_graphql_request (
142
+ request ,
143
+ data ,
144
+ query ,
145
+ variables ,
146
+ operation_name ,
147
+ show_graphiql
148
+ )
149
+
150
+ status_code = 200
151
+ if execution_result :
152
+ response = {}
153
+
154
+ if execution_result .errors :
155
+ response ['errors' ] = [self .format_error (e ) for e in execution_result .errors ]
156
+
157
+ if execution_result .invalid :
158
+ status_code = 400
159
+ else :
160
+ response ['data' ] = execution_result .data
161
+
162
+ if self .batch :
163
+ response = {
164
+ 'id' : id ,
165
+ 'payload' : response ,
166
+ 'status' : status_code ,
167
+ }
168
+
169
+ result = self .json_encode (request , response , pretty = show_graphiql )
170
+ else :
171
+ result = None
172
+
173
+ return result , status_code
174
+
153
175
def render_graphiql (self , request , ** data ):
154
176
return render (request , self .graphiql_template , data )
155
177
@@ -170,7 +192,10 @@ def parse_body(self, request):
170
192
elif content_type == 'application/json' :
171
193
try :
172
194
request_json = json .loads (request .body .decode ('utf-8' ))
173
- assert isinstance (request_json , dict )
195
+ if self .batch :
196
+ assert isinstance (request_json , list )
197
+ else :
198
+ assert isinstance (request_json , dict )
174
199
return request_json
175
200
except :
176
201
raise HttpError (HttpResponseBadRequest ('POST body sent invalid JSON.' ))
@@ -242,6 +267,7 @@ def request_wants_html(cls, request):
242
267
def get_graphql_params (request , data ):
243
268
query = request .GET .get ('query' ) or data .get ('query' )
244
269
variables = request .GET .get ('variables' ) or data .get ('variables' )
270
+ id = request .GET .get ('id' ) or data .get ('id' )
245
271
246
272
if variables and isinstance (variables , six .text_type ):
247
273
try :
@@ -251,7 +277,7 @@ def get_graphql_params(request, data):
251
277
252
278
operation_name = request .GET .get ('operationName' ) or data .get ('operationName' )
253
279
254
- return query , variables , operation_name
280
+ return query , variables , operation_name , id
255
281
256
282
@staticmethod
257
283
def format_error (error ):
0 commit comments