3
3
import com .fasterxml .jackson .core .type .TypeReference ;
4
4
import com .fasterxml .jackson .databind .ObjectMapper ;
5
5
import graphql .ExecutionInput ;
6
- import graphql .ExecutionResult ;
7
6
import graphql .GraphQL ;
8
7
import org .springframework .beans .factory .annotation .Autowired ;
9
8
import org .springframework .http .MediaType ;
14
13
import org .springframework .web .bind .annotation .RequestParam ;
15
14
import org .springframework .web .bind .annotation .RestController ;
16
15
17
- import javax .servlet .http .HttpServletRequest ;
18
- import javax .servlet .http .HttpServletResponse ;
19
16
import java .io .IOException ;
20
- import java .io .PrintWriter ;
21
17
import java .util .LinkedHashMap ;
22
18
import java .util .Map ;
23
19
@@ -35,58 +31,51 @@ public GraphQLController(GraphQL graphql, ObjectMapper objectMapper) {
35
31
36
32
@ RequestMapping (value = "/graphql" , method = RequestMethod .GET , produces = MediaType .APPLICATION_JSON_VALUE )
37
33
@ 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 {
42
37
if (query == null ) {
43
38
query = "" ;
44
39
}
40
+
45
41
Map <String , Object > variables = new LinkedHashMap <>();
46
- ;
42
+
47
43
if (variablesJson != null ) {
48
44
variables = objectMapper .readValue (variablesJson , new TypeReference <Map <String , Object >>() {
49
45
});
50
46
}
51
- executeGraphqlQuery (httpServletResponse , operationName , query , variables );
47
+
48
+ return executeGraphqlQuery (operationName , query , variables );
52
49
}
53
50
54
51
@ SuppressWarnings ("unchecked" )
55
52
@ RequestMapping (value = "/graphql" , method = RequestMethod .POST , produces = MediaType .APPLICATION_JSON_VALUE )
56
53
@ 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 ) {
58
55
String query = (String ) body .get ("query" );
56
+
59
57
if (query == null ) {
60
58
query = "" ;
61
59
}
60
+
62
61
String operationName = (String ) body .get ("operationName" );
63
62
Map <String , Object > variables = (Map <String , Object >) body .get ("variables" );
63
+
64
64
if (variables == null ) {
65
65
variables = new LinkedHashMap <>();
66
66
}
67
- executeGraphqlQuery (httpServletResponse , operationName , query , variables );
67
+
68
+ return executeGraphqlQuery (operationName , query , variables );
68
69
}
69
70
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 ) {
71
73
ExecutionInput executionInput = ExecutionInput .newExecutionInput ()
72
74
.query (query )
73
75
.variables (variables )
74
76
.operationName (operationName )
75
77
.build ();
76
78
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 ();
91
80
}
92
81
}
0 commit comments