@@ -8,6 +8,7 @@ use graph::prelude::serde_json;
8
8
use graph:: prelude:: serde_json:: json;
9
9
use graph:: prelude:: * ;
10
10
use graph:: semver:: VersionReq ;
11
+ use graph:: url:: form_urlencoded;
11
12
use graph:: { components:: server:: query:: GraphQLServerError , data:: query:: QueryTarget } ;
12
13
use http:: header;
13
14
use http:: header:: {
@@ -248,7 +249,22 @@ where
248
249
}
249
250
. boxed ( )
250
251
}
252
+ fn handle_mutations ( & self ) -> GraphQLServiceResponse {
253
+ async {
254
+ let response_obj = json ! ( {
255
+ "error" : "Can't use mutations with GET method"
256
+ } ) ;
257
+ let response_str = serde_json:: to_string ( & response_obj) . unwrap ( ) ;
251
258
259
+ Ok ( Response :: builder ( )
260
+ . status ( 400 )
261
+ . header ( CONTENT_TYPE , "application/json" )
262
+ . header ( ACCESS_CONTROL_ALLOW_ORIGIN , "*" )
263
+ . body ( Body :: from ( response_str) )
264
+ . unwrap ( ) )
265
+ }
266
+ . boxed ( )
267
+ }
252
268
/// Handles requests without content type.
253
269
fn handle_requests_without_content_type ( & self ) -> GraphQLServiceResponse {
254
270
async {
@@ -275,15 +291,14 @@ where
275
291
let response_str = serde_json:: to_string ( & response_obj) . unwrap ( ) ;
276
292
277
293
Ok ( Response :: builder ( )
278
- . status ( StatusCode :: BAD_REQUEST )
294
+ . status ( 400 )
279
295
. header ( CONTENT_TYPE , "application/json" )
280
296
. header ( ACCESS_CONTROL_ALLOW_ORIGIN , "*" )
281
297
. body ( Body :: from ( response_str) )
282
298
. unwrap ( ) )
283
299
}
284
300
. boxed ( )
285
301
}
286
-
287
302
fn has_request_body ( & self , req : & Request < Body > ) -> bool {
288
303
if let Some ( length) = req. headers ( ) . get ( hyper:: header:: CONTENT_LENGTH ) {
289
304
if let Ok ( length) = length. to_str ( ) {
@@ -319,6 +334,19 @@ where
319
334
return self . handle_requests_without_body ( ) . boxed ( ) ;
320
335
}
321
336
337
+ let is_mutation = req
338
+ . uri ( )
339
+ . query ( )
340
+ . and_then ( |query_str| {
341
+ form_urlencoded:: parse ( query_str. as_bytes ( ) )
342
+ . find ( |( key, _) | key == "query" )
343
+ . map ( |( _, value) | value. into_owned ( ) )
344
+ } )
345
+ . unwrap_or_else ( || String :: new ( ) )
346
+ . trim ( )
347
+ . to_lowercase ( )
348
+ . starts_with ( "mutation" ) ;
349
+
322
350
match ( method, path_segments. as_slice ( ) ) {
323
351
( Method :: GET , [ "" ] ) => self . index ( ) . boxed ( ) ,
324
352
( Method :: GET , & [ "subgraphs" , "id" , _, "graphql" ] )
@@ -327,6 +355,9 @@ where
327
355
| ( Method :: GET , & [ "subgraphs" , "network" , _, _, "graphql" ] )
328
356
| ( Method :: GET , & [ "subgraphs" , "graphql" ] ) => self . handle_graphiql ( ) ,
329
357
358
+ ( Method :: GET , path @ [ "subgraphs" , "name" , _, _] ) if is_mutation => {
359
+ self . handle_mutations ( )
360
+ }
330
361
( Method :: GET , path @ [ "subgraphs" , "id" , _] )
331
362
| ( Method :: GET , path @ [ "subgraphs" , "name" , _] )
332
363
| ( Method :: GET , path @ [ "subgraphs" , "name" , _, _] )
@@ -393,7 +424,7 @@ where
393
424
let response_str = serde_json:: to_string ( & response_obj) . unwrap ( ) ;
394
425
395
426
Ok ( Response :: builder ( )
396
- . status ( 200 )
427
+ . status ( 400 )
397
428
. header ( CONTENT_TYPE , "application/json" )
398
429
. header ( ACCESS_CONTROL_ALLOW_ORIGIN , "*" )
399
430
. body ( Body :: from ( response_str) )
@@ -408,7 +439,7 @@ where
408
439
let response_str = serde_json:: to_string ( & response_obj) . unwrap ( ) ;
409
440
410
441
Ok ( Response :: builder ( )
411
- . status ( 200 )
442
+ . status ( 400 )
412
443
. header ( CONTENT_TYPE , "application/json" )
413
444
. header ( ACCESS_CONTROL_ALLOW_ORIGIN , "*" )
414
445
. body ( Body :: from ( response_str) )
@@ -593,7 +624,6 @@ mod tests {
593
624
. unwrap ( )
594
625
. expect ( "Should return a response" ) ;
595
626
596
- println ! ( "{:?}" , response) ;
597
627
let data = test_utils:: assert_successful_response ( response) ;
598
628
599
629
// The body should match the simulated query result
0 commit comments