22
33import graphql .kickstart .execution .GraphQLObjectMapper ;
44import graphql .kickstart .execution .GraphQLRequest ;
5- import java .io .IOException ;
6- import java .util .Collections ;
7- import java .util .Map ;
85import lombok .RequiredArgsConstructor ;
96import org .springframework .http .HttpHeaders ;
107import org .springframework .http .HttpStatus ;
118import org .springframework .http .MediaType ;
9+ import org .springframework .lang .Nullable ;
10+ import org .springframework .web .bind .annotation .GetMapping ;
11+ import org .springframework .web .bind .annotation .PostMapping ;
1212import org .springframework .web .bind .annotation .RequestBody ;
1313import org .springframework .web .bind .annotation .RequestHeader ;
14- import org .springframework .web .bind .annotation .RequestMapping ;
15- import org .springframework .web .bind .annotation .RequestMethod ;
1614import org .springframework .web .bind .annotation .RequestParam ;
1715import org .springframework .web .server .ResponseStatusException ;
1816import org .springframework .web .server .ServerWebExchange ;
1917
18+ import java .io .IOException ;
19+ import java .util .Collections ;
20+ import java .util .Map ;
21+
2022@ RequiredArgsConstructor
2123public abstract class AbstractGraphQLController {
2224
2325 private final GraphQLObjectMapper objectMapper ;
2426
25- @ RequestMapping (value = "${graphql.url:graphql}" ,
26- method = RequestMethod .POST ,
27- produces = MediaType .APPLICATION_JSON_VALUE )
27+ @ PostMapping (value = "${graphql.url:graphql}" ,
28+ consumes = MediaType .ALL_VALUE , produces = MediaType .APPLICATION_JSON_VALUE )
2829 public Object graphqlPOST (
29- @ RequestHeader (value = HttpHeaders .CONTENT_TYPE , required = false ) String contentType ,
30- @ RequestParam (value = "query" , required = false ) String query ,
31- @ RequestParam (value = "operationName" , required = false ) String operationName ,
32- @ RequestParam (value = "variables" , required = false ) String variablesJson ,
33- @ RequestBody (required = false ) String body ,
30+ @ RequestHeader (HttpHeaders .CONTENT_TYPE ) final MediaType contentType ,
31+ @ Nullable @ RequestParam (value = "query" , required = false ) String query ,
32+ @ Nullable @ RequestParam (value = "operationName" , required = false ) String operationName ,
33+ @ Nullable @ RequestParam (value = "variables" , required = false ) String variablesJson ,
34+ @ Nullable @ RequestBody (required = false ) String body ,
3435 ServerWebExchange serverWebExchange ) throws IOException {
3536
3637 if (body == null ) {
@@ -48,7 +49,7 @@ public Object graphqlPOST(
4849 // "variables": { "myVariable": "someValue", ... }
4950 // }
5051
51- if (MediaType .APPLICATION_JSON_VALUE . equals (contentType )) {
52+ if (MediaType .APPLICATION_JSON . isCompatibleWith (contentType )) {
5253 GraphQLRequest request = objectMapper .readGraphQLRequest (body );
5354 if (request .getQuery () == null ) {
5455 request .setQuery ("" );
@@ -68,20 +69,19 @@ public Object graphqlPOST(
6869 // * If the "application/graphql" Content-Type header is present,
6970 // treat the HTTP POST body contents as the GraphQL query string.
7071
71- if ("application/graphql" .equals (contentType )) {
72- return executeRequest (body , null , null , serverWebExchange );
72+ if ("application/graphql" .equals (contentType . toString ()) || "application/graphql; charset=utf-8" . equals ( contentType . toString () )) {
73+ return executeRequest (body , null , Collections . emptyMap () , serverWebExchange );
7374 }
7475
7576 throw new ResponseStatusException (HttpStatus .UNPROCESSABLE_ENTITY , "Could not process GraphQL request" );
7677 }
7778
78- @ RequestMapping (value = "${graphql.url:graphql}" ,
79- method = RequestMethod .GET ,
79+ @ GetMapping (value = "${graphql.url:graphql}" ,
8080 produces = MediaType .APPLICATION_JSON_VALUE )
8181 public Object graphqlGET (
82- @ RequestParam ("query" ) String query ,
83- @ RequestParam (value = "operationName" , required = false ) String operationName ,
84- @ RequestParam (value = "variables" , required = false ) String variablesJson ,
82+ @ Nullable @ RequestParam ("query" ) String query ,
83+ @ Nullable @ RequestParam (value = "operationName" , required = false ) String operationName ,
84+ @ Nullable @ RequestParam (value = "variables" , required = false ) String variablesJson ,
8585 ServerWebExchange serverWebExchange ) {
8686
8787 // https://graphql.org/learn/serving-over-http/#get-request
0 commit comments