33import com .fasterxml .jackson .core .type .TypeReference ;
44import com .fasterxml .jackson .databind .ObjectMapper ;
55import graphql .ExecutionInput ;
6- import graphql .ExecutionResult ;
76import graphql .GraphQL ;
87import org .springframework .beans .factory .annotation .Autowired ;
98import org .springframework .http .MediaType ;
1413import org .springframework .web .bind .annotation .RequestParam ;
1514import org .springframework .web .bind .annotation .RestController ;
1615
17- import javax .servlet .http .HttpServletRequest ;
18- import javax .servlet .http .HttpServletResponse ;
1916import java .io .IOException ;
20- import java .io .PrintWriter ;
2117import java .util .LinkedHashMap ;
2218import java .util .Map ;
2319
@@ -35,58 +31,51 @@ public GraphQLController(GraphQL graphql, ObjectMapper objectMapper) {
3531
3632 @ RequestMapping (value = "/graphql" , method = RequestMethod .GET , produces = MediaType .APPLICATION_JSON_VALUE )
3733 @ CrossOrigin
38- public void graphqlGET (@ RequestParam ("query" ) String query ,
39- @ RequestParam (value = "operationName" , required = false ) String operationName ,
40- @ RequestParam ("variables" ) String variablesJson ,
41- HttpServletResponse httpServletResponse ) throws IOException {
34+ public Map <String , Object > graphqlGET (@ RequestParam ("query" ) String query ,
35+ @ RequestParam (value = "operationName" , required = false ) String operationName ,
36+ @ RequestParam ("variables" ) String variablesJson ) throws IOException {
4237 if (query == null ) {
4338 query = "" ;
4439 }
40+
4541 Map <String , Object > variables = new LinkedHashMap <>();
46- ;
42+
4743 if (variablesJson != null ) {
4844 variables = objectMapper .readValue (variablesJson , new TypeReference <Map <String , Object >>() {
4945 });
5046 }
51- executeGraphqlQuery (httpServletResponse , operationName , query , variables );
47+
48+ return executeGraphqlQuery (operationName , query , variables );
5249 }
5350
5451 @ SuppressWarnings ("unchecked" )
5552 @ RequestMapping (value = "/graphql" , method = RequestMethod .POST , produces = MediaType .APPLICATION_JSON_VALUE )
5653 @ CrossOrigin
57- public void graphql (@ RequestBody Map <String , Object > body , HttpServletRequest httpServletRequest , HttpServletResponse httpServletResponse ) throws IOException {
54+ public Map < String , Object > graphql (@ RequestBody Map <String , Object > body ) {
5855 String query = (String ) body .get ("query" );
56+
5957 if (query == null ) {
6058 query = "" ;
6159 }
60+
6261 String operationName = (String ) body .get ("operationName" );
6362 Map <String , Object > variables = (Map <String , Object >) body .get ("variables" );
63+
6464 if (variables == null ) {
6565 variables = new LinkedHashMap <>();
6666 }
67- executeGraphqlQuery (httpServletResponse , operationName , query , variables );
67+
68+ return executeGraphqlQuery (operationName , query , variables );
6869 }
6970
70- private void executeGraphqlQuery (HttpServletResponse httpServletResponse , String operationName , String query , Map <String , Object > variables ) throws IOException {
71+ private Map <String , Object > executeGraphqlQuery (String operationName ,
72+ String query , Map <String , Object > variables ) {
7173 ExecutionInput executionInput = ExecutionInput .newExecutionInput ()
7274 .query (query )
7375 .variables (variables )
7476 .operationName (operationName )
7577 .build ();
7678
77- ExecutionResult executionResult = graphql .execute (executionInput );
78- handleNormalResponse (httpServletResponse , executionResult );
79- }
80-
81- private void handleNormalResponse (HttpServletResponse httpServletResponse , ExecutionResult executionResult ) throws IOException {
82- Map <String , Object > result = executionResult .toSpecification ();
83- httpServletResponse .setStatus (HttpServletResponse .SC_OK );
84- httpServletResponse .setCharacterEncoding ("UTF-8" );
85- httpServletResponse .setContentType ("application/json" );
86- httpServletResponse .setHeader ("Access-Control-Allow-Origin" , "*" );
87- String body = objectMapper .writeValueAsString (result );
88- PrintWriter writer = httpServletResponse .getWriter ();
89- writer .write (body );
90- writer .close ();
79+ return graphql .execute (executionInput ).toSpecification ();
9180 }
9281}
0 commit comments