22
22
import javax .validation .Valid ;
23
23
import javax .validation .constraints .NotNull ;
24
24
25
+ import com .fasterxml .jackson .databind .ObjectMapper ;
26
+ import com .introproventures .graphql .jpa .query .schema .GraphQLExecutor ;
27
+ import com .introproventures .graphql .jpa .query .schema .impl .GraphQLJpaExecutor ;
28
+ import graphql .ExecutionResult ;
25
29
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
26
30
import org .springframework .boot .autoconfigure .condition .ConditionalOnWebApplication ;
27
31
import org .springframework .http .MediaType ;
32
36
import org .springframework .web .bind .annotation .RequestParam ;
33
37
import org .springframework .web .bind .annotation .RestController ;
34
38
35
- import com .fasterxml .jackson .databind .ObjectMapper ;
36
- import com .introproventures .graphql .jpa .query .schema .GraphQLExecutor ;
37
- import com .introproventures .graphql .jpa .query .schema .impl .GraphQLJpaExecutor ;
38
-
39
- import graphql .ExecutionResult ;
40
-
41
39
/**
42
- * Spring Boot GraphQL Query Rest Controller with HTTP mapping endpoints for GraphQLExecutor relay
43
- *
44
- * @see <a href="http://graphql.org/learn/serving-over-http/">Serving GraphQL over HTTP</a>
45
- *
40
+ * Spring Boot GraphQL Query Rest Controller with HTTP mapping endpoints for GraphQLExecutor relay
41
+ *
42
+ * @see <a href="http://graphql.org/learn/serving-over-http/">Serving GraphQL over HTTP</a>
43
+ *
46
44
*/
47
45
@ RestController
48
46
@ ConditionalOnWebApplication
49
47
@ ConditionalOnClass (GraphQLExecutor .class )
50
48
public class GraphQLController {
51
-
49
+
52
50
private static final String PATH = "${spring.graphql.jpa.query.path:/graphql}" ;
53
51
public static final String APPLICATION_GRAPHQL_VALUE = "application/graphql" ;
54
-
52
+
55
53
private final GraphQLExecutor graphQLExecutor ;
56
54
private final ObjectMapper mapper ;
57
55
58
56
/**
59
57
* Creates instance of Spring GraphQLController RestController
60
- *
58
+ *
61
59
* @param graphQLExecutor {@link GraphQLExecutor} instance
62
60
* @param mapper {@link ObjectMapper} instance
63
61
*/
@@ -66,10 +64,10 @@ public GraphQLController(GraphQLExecutor graphQLExecutor, ObjectMapper mapper) {
66
64
this .graphQLExecutor = graphQLExecutor ;
67
65
this .mapper = mapper ;
68
66
}
69
-
67
+
70
68
/**
71
- * Handle standard GraphQL POST request that consumes
72
- * "application/json" content type with a JSON-encoded body
69
+ * Handle standard GraphQL POST request that consumes
70
+ * "application/json" content type with a JSON-encoded body
73
71
* of the following format:
74
72
* <pre>
75
73
* {
@@ -79,12 +77,12 @@ public GraphQLController(GraphQLExecutor graphQLExecutor, ObjectMapper mapper) {
79
77
* </pre>
80
78
* @param queryRequest object
81
79
* @return {@link ExecutionResult} response
82
- * @throws IOException
80
+ * @throws IOException exception
83
81
*/
84
- @ PostMapping (value = PATH ,
85
- consumes = {MediaType .APPLICATION_JSON_VALUE },
82
+ @ PostMapping (value = PATH ,
83
+ consumes = {MediaType .APPLICATION_JSON_VALUE },
86
84
produces = MediaType .APPLICATION_JSON_VALUE )
87
- public ExecutionResult postJson (@ RequestBody @ Valid final GraphQLQueryRequest queryRequest ) throws IOException
85
+ public ExecutionResult postJson (@ RequestBody @ Valid final GraphQLQueryRequest queryRequest ) throws IOException
88
86
{
89
87
return graphQLExecutor .execute (queryRequest .getQuery (), queryRequest .getVariables ());
90
88
}
@@ -93,90 +91,90 @@ public ExecutionResult postJson(@RequestBody @Valid final GraphQLQueryRequest qu
93
91
* Handle HTTP GET request.
94
92
* The GraphQL query should be specified in the "query" query string.
95
93
* i.e. <pre> http://server/graphql?query={query{name}}</pre>
96
- *
97
- * Query variables can be sent as a JSON-encoded string in an additional
94
+ *
95
+ * Query variables can be sent as a JSON-encoded string in an additional
98
96
* query parameter called variables.
99
- *
97
+ *
100
98
* @param query encoded JSON string
101
99
* @param variables encoded JSON string
102
100
* @return {@link ExecutionResult} response
103
- * @throws IOException
101
+ * @throws IOException exception
104
102
*/
105
- @ GetMapping (value = PATH ,
106
- consumes = {APPLICATION_GRAPHQL_VALUE },
103
+ @ GetMapping (value = PATH ,
104
+ consumes = {APPLICATION_GRAPHQL_VALUE },
107
105
produces =MediaType .APPLICATION_JSON_VALUE )
108
- public ExecutionResult getQuery (
109
- @ RequestParam (name ="query" ) final String query ,
110
- @ RequestParam (name ="variables" , required = false ) final String variables ) throws IOException
106
+ public ExecutionResult getQuery (
107
+ @ RequestParam (name ="query" ) final String query ,
108
+ @ RequestParam (name ="variables" , required = false ) final String variables ) throws IOException
111
109
{
112
110
Map <String , Object > variablesMap = variablesStringToMap (variables );
113
-
111
+
114
112
return graphQLExecutor .execute (query , variablesMap );
115
113
}
116
-
114
+
117
115
/**
118
116
* Handle HTTP FORM POST request.
119
117
* The GraphQL query should be specified in the "query" query parameter string.
120
- *
121
- * Query variables can be sent as a JSON-encoded string in an additional
118
+ *
119
+ * Query variables can be sent as a JSON-encoded string in an additional
122
120
* query parameter called variables.
123
121
124
122
* @param query encoded JSON string
125
123
* @param variables encoded JSON string
126
124
* @return {@link ExecutionResult} response
127
- * @throws IOException
125
+ * @throws IOException exception
128
126
*/
129
- @ PostMapping (value = PATH ,
130
- consumes = MediaType .APPLICATION_FORM_URLENCODED_VALUE ,
127
+ @ PostMapping (value = PATH ,
128
+ consumes = MediaType .APPLICATION_FORM_URLENCODED_VALUE ,
131
129
produces =MediaType .APPLICATION_JSON_VALUE )
132
- public ExecutionResult postForm (
133
- @ RequestParam (name ="query" ) final String query ,
134
- @ RequestParam (name ="variables" , required = false ) final String variables ) throws IOException
130
+ public ExecutionResult postForm (
131
+ @ RequestParam (name ="query" ) final String query ,
132
+ @ RequestParam (name ="variables" , required = false ) final String variables ) throws IOException
135
133
{
136
134
Map <String , Object > variablesMap = variablesStringToMap (variables );
137
-
135
+
138
136
return graphQLExecutor .execute (query , variablesMap );
139
137
}
140
138
141
139
/**
142
140
* Handle POST with the "application/graphql" Content-Type header.
143
141
* Treat the HTTP POST body contents as the GraphQL query string.
144
- *
145
- *
146
- * @param queryRequest a valid {@link GraphQLQueryRequest} input argument
142
+ *
143
+ *
144
+ * @param query a valid {@link GraphQLQueryRequest} input argument
147
145
* @return {@link ExecutionResult} response
148
- * @throws IOException
146
+ * @throws IOException exception
149
147
*/
150
- @ PostMapping (value = PATH ,
151
- consumes = APPLICATION_GRAPHQL_VALUE ,
148
+ @ PostMapping (value = PATH ,
149
+ consumes = APPLICATION_GRAPHQL_VALUE ,
152
150
produces =MediaType .APPLICATION_JSON_VALUE )
153
- public ExecutionResult postApplicationGraphQL (
154
- @ RequestBody final String query ) throws IOException
151
+ public ExecutionResult postApplicationGraphQL (
152
+ @ RequestBody final String query ) throws IOException
155
153
{
156
154
return graphQLExecutor .execute (query , null );
157
155
}
158
-
156
+
159
157
/**
160
158
* Convert String argument to a Map as expected by {@link GraphQLJpaExecutor#execute(String, Map)}. GraphiQL posts both
161
159
* query and variables as JSON encoded String, so Spring MVC mapping is useless here.
162
160
* See: http://graphql.org/learn/serving-over-http/
163
161
*
164
162
* @param json JSON encoded string variables
165
163
* @return a {@link HashMap} object of variable key-value pairs
166
- * @throws IOException
164
+ * @throws IOException exception
167
165
*/
168
166
@ SuppressWarnings ("unchecked" )
169
167
private Map <String , Object > variablesStringToMap (final String json ) throws IOException {
170
168
Map <String , Object > variables = null ;
171
-
169
+
172
170
if (json != null && !json .isEmpty ())
173
171
variables = mapper .readValue (json , Map .class );
174
-
172
+
175
173
return variables ;
176
174
}
177
175
178
176
/**
179
- * GraphQL JSON HTTP Request Wrapper Class
177
+ * GraphQL JSON HTTP Request Wrapper Class
180
178
*/
181
179
@ Validated
182
180
public static class GraphQLQueryRequest {
@@ -187,9 +185,9 @@ public static class GraphQLQueryRequest {
187
185
private Map <String , Object > variables ;
188
186
189
187
GraphQLQueryRequest () {}
190
-
188
+
191
189
/**
192
- * @param query
190
+ * @param query string
193
191
*/
194
192
public GraphQLQueryRequest (String query ) {
195
193
super ();
@@ -225,5 +223,5 @@ public void setVariables(Map<String, Object> variables) {
225
223
}
226
224
227
225
}
228
-
226
+
229
227
}
0 commit comments