2222import javax .validation .Valid ;
2323import javax .validation .constraints .NotNull ;
2424
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 ;
2529import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
2630import org .springframework .boot .autoconfigure .condition .ConditionalOnWebApplication ;
2731import org .springframework .http .MediaType ;
3236import org .springframework .web .bind .annotation .RequestParam ;
3337import org .springframework .web .bind .annotation .RestController ;
3438
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-
4139/**
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+ *
4644 */
4745@ RestController
4846@ ConditionalOnWebApplication
4947@ ConditionalOnClass (GraphQLExecutor .class )
5048public class GraphQLController {
51-
49+
5250 private static final String PATH = "${spring.graphql.jpa.query.path:/graphql}" ;
5351 public static final String APPLICATION_GRAPHQL_VALUE = "application/graphql" ;
54-
52+
5553 private final GraphQLExecutor graphQLExecutor ;
5654 private final ObjectMapper mapper ;
5755
5856 /**
5957 * Creates instance of Spring GraphQLController RestController
60- *
58+ *
6159 * @param graphQLExecutor {@link GraphQLExecutor} instance
6260 * @param mapper {@link ObjectMapper} instance
6361 */
@@ -66,10 +64,10 @@ public GraphQLController(GraphQLExecutor graphQLExecutor, ObjectMapper mapper) {
6664 this .graphQLExecutor = graphQLExecutor ;
6765 this .mapper = mapper ;
6866 }
69-
67+
7068 /**
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
7371 * of the following format:
7472 * <pre>
7573 * {
@@ -79,12 +77,12 @@ public GraphQLController(GraphQLExecutor graphQLExecutor, ObjectMapper mapper) {
7977 * </pre>
8078 * @param queryRequest object
8179 * @return {@link ExecutionResult} response
82- * @throws IOException
80+ * @throws IOException exception
8381 */
84- @ PostMapping (value = PATH ,
85- consumes = {MediaType .APPLICATION_JSON_VALUE },
82+ @ PostMapping (value = PATH ,
83+ consumes = {MediaType .APPLICATION_JSON_VALUE },
8684 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
8886 {
8987 return graphQLExecutor .execute (queryRequest .getQuery (), queryRequest .getVariables ());
9088 }
@@ -93,90 +91,90 @@ public ExecutionResult postJson(@RequestBody @Valid final GraphQLQueryRequest qu
9391 * Handle HTTP GET request.
9492 * The GraphQL query should be specified in the "query" query string.
9593 * 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
9896 * query parameter called variables.
99- *
97+ *
10098 * @param query encoded JSON string
10199 * @param variables encoded JSON string
102100 * @return {@link ExecutionResult} response
103- * @throws IOException
101+ * @throws IOException exception
104102 */
105- @ GetMapping (value = PATH ,
106- consumes = {APPLICATION_GRAPHQL_VALUE },
103+ @ GetMapping (value = PATH ,
104+ consumes = {APPLICATION_GRAPHQL_VALUE },
107105 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
111109 {
112110 Map <String , Object > variablesMap = variablesStringToMap (variables );
113-
111+
114112 return graphQLExecutor .execute (query , variablesMap );
115113 }
116-
114+
117115 /**
118116 * Handle HTTP FORM POST request.
119117 * 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
122120 * query parameter called variables.
123121
124122 * @param query encoded JSON string
125123 * @param variables encoded JSON string
126124 * @return {@link ExecutionResult} response
127- * @throws IOException
125+ * @throws IOException exception
128126 */
129- @ PostMapping (value = PATH ,
130- consumes = MediaType .APPLICATION_FORM_URLENCODED_VALUE ,
127+ @ PostMapping (value = PATH ,
128+ consumes = MediaType .APPLICATION_FORM_URLENCODED_VALUE ,
131129 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
135133 {
136134 Map <String , Object > variablesMap = variablesStringToMap (variables );
137-
135+
138136 return graphQLExecutor .execute (query , variablesMap );
139137 }
140138
141139 /**
142140 * Handle POST with the "application/graphql" Content-Type header.
143141 * 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
147145 * @return {@link ExecutionResult} response
148- * @throws IOException
146+ * @throws IOException exception
149147 */
150- @ PostMapping (value = PATH ,
151- consumes = APPLICATION_GRAPHQL_VALUE ,
148+ @ PostMapping (value = PATH ,
149+ consumes = APPLICATION_GRAPHQL_VALUE ,
152150 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
155153 {
156154 return graphQLExecutor .execute (query , null );
157155 }
158-
156+
159157 /**
160158 * Convert String argument to a Map as expected by {@link GraphQLJpaExecutor#execute(String, Map)}. GraphiQL posts both
161159 * query and variables as JSON encoded String, so Spring MVC mapping is useless here.
162160 * See: http://graphql.org/learn/serving-over-http/
163161 *
164162 * @param json JSON encoded string variables
165163 * @return a {@link HashMap} object of variable key-value pairs
166- * @throws IOException
164+ * @throws IOException exception
167165 */
168166 @ SuppressWarnings ("unchecked" )
169167 private Map <String , Object > variablesStringToMap (final String json ) throws IOException {
170168 Map <String , Object > variables = null ;
171-
169+
172170 if (json != null && !json .isEmpty ())
173171 variables = mapper .readValue (json , Map .class );
174-
172+
175173 return variables ;
176174 }
177175
178176 /**
179- * GraphQL JSON HTTP Request Wrapper Class
177+ * GraphQL JSON HTTP Request Wrapper Class
180178 */
181179 @ Validated
182180 public static class GraphQLQueryRequest {
@@ -187,9 +185,9 @@ public static class GraphQLQueryRequest {
187185 private Map <String , Object > variables ;
188186
189187 GraphQLQueryRequest () {}
190-
188+
191189 /**
192- * @param query
190+ * @param query string
193191 */
194192 public GraphQLQueryRequest (String query ) {
195193 super ();
@@ -225,5 +223,5 @@ public void setVariables(Map<String, Object> variables) {
225223 }
226224
227225 }
228-
226+
229227}
0 commit comments