@@ -32,6 +32,7 @@ class GraphQLView(View):
3232 graphiql_version = None
3333 graphiql_template = None
3434 middleware = None
35+ batch = False
3536
3637 methods = ['GET' , 'POST' , 'PUT' , 'DELETE' ]
3738
@@ -41,6 +42,7 @@ def __init__(self, **kwargs):
4142 if hasattr (self , key ):
4243 setattr (self , key , value )
4344
45+ assert not all ((self .graphiql , self .batch )), 'Use either graphiql or batch processing'
4446 assert isinstance (self .schema , GraphQLSchema ), 'A Schema is required to be provided to GraphQLView.'
4547
4648 # noinspection PyUnusedLocal
@@ -66,33 +68,15 @@ def dispatch_request(self):
6668 data = self .parse_body (request )
6769 show_graphiql = self .graphiql and self .can_display_graphiql (data )
6870
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 ]
9275 else :
93- result = None
76+ result , status_code = self . get_response ( request , data , show_graphiql )
9477
9578 if show_graphiql :
79+ query , variables , operation_name , id = self .get_graphql_params (request , data )
9680 return render_graphiql (
9781 graphiql_version = self .graphiql_version ,
9882 graphiql_template = self .graphiql_template ,
@@ -118,6 +102,43 @@ def dispatch_request(self):
118102 content_type = 'application/json'
119103 )
120104
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+
121142 def json_encode (self , request , d ):
122143 if not self .pretty and not request .args .get ('pretty' ):
123144 return json .dumps (d , separators = (',' , ':' ))
@@ -134,7 +155,10 @@ def parse_body(self, request):
134155 elif content_type == 'application/json' :
135156 try :
136157 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 )
138162 return request_json
139163 except :
140164 raise HttpError (BadRequest ('POST body sent invalid JSON.' ))
@@ -207,6 +231,7 @@ def request_wants_html(cls, request):
207231 def get_graphql_params (request , data ):
208232 query = request .args .get ('query' ) or data .get ('query' )
209233 variables = request .args .get ('variables' ) or data .get ('variables' )
234+ id = request .args .get ('id' ) or data .get ('id' )
210235
211236 if variables and isinstance (variables , six .text_type ):
212237 try :
@@ -216,7 +241,7 @@ def get_graphql_params(request, data):
216241
217242 operation_name = request .args .get ('operationName' ) or data .get ('operationName' )
218243
219- return query , variables , operation_name
244+ return query , variables , operation_name , id
220245
221246 @staticmethod
222247 def format_error (error ):
0 commit comments