|
1 | 1 | /*
|
2 |
| - * Copyright 2017 Alfresco, Inc. and/or its affiliates. |
| 2 | + * Copyright 2017 IntroPro Ventures, Inc. and/or its affiliates. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 | package com.introproventures.graphql.jpa.query.web;
|
17 | 17 |
|
18 | 18 | import java.io.IOException;
|
| 19 | +import java.util.HashMap; |
19 | 20 | import java.util.Map;
|
20 | 21 |
|
21 | 22 | import javax.validation.Valid;
|
|
25 | 26 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
26 | 27 | import org.springframework.http.MediaType;
|
27 | 28 | import org.springframework.validation.annotation.Validated;
|
| 29 | +import org.springframework.web.bind.annotation.GetMapping; |
| 30 | +import org.springframework.web.bind.annotation.PostMapping; |
28 | 31 | import org.springframework.web.bind.annotation.RequestBody;
|
29 |
| -import org.springframework.web.bind.annotation.RequestMapping; |
30 |
| -import org.springframework.web.bind.annotation.RequestMethod; |
31 | 32 | import org.springframework.web.bind.annotation.RequestParam;
|
32 | 33 | import org.springframework.web.bind.annotation.RestController;
|
33 | 34 |
|
|
38 | 39 | import graphql.ExecutionResult;
|
39 | 40 |
|
40 | 41 | /**
|
41 |
| - * Provides controller with Json and Form POST mapping endpoints for GraphQLExecutor instance |
| 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> |
42 | 45 | *
|
43 |
| - * @author Igor Dianov |
44 |
| - * |
45 | 46 | */
|
46 | 47 | @RestController
|
47 | 48 | @ConditionalOnWebApplication
|
48 | 49 | @ConditionalOnClass(GraphQLExecutor.class)
|
49 | 50 | public class GraphQLController {
|
50 | 51 |
|
51 |
| - private GraphQLExecutor graphQLExecutor; |
52 |
| - private ObjectMapper mapper; |
| 52 | + private static final String PATH = "${spring.graphql.jpa.query.path:/graphql}"; |
| 53 | + public static final String APPLICATION_GRAPHQL_VALUE = "application/graphql"; |
| 54 | + |
| 55 | + private final GraphQLExecutor graphQLExecutor; |
| 56 | + private final ObjectMapper mapper; |
53 | 57 |
|
54 | 58 | /**
|
55 |
| - * Create instance of Spring GraphQLController RestController |
| 59 | + * Creates instance of Spring GraphQLController RestController |
56 | 60 | *
|
57 |
| - * @param graphQLExecutor instance |
58 |
| - * @param mapper instance |
| 61 | + * @param graphQLExecutor {@link GraphQLExecutor} instance |
| 62 | + * @param mapper {@link ObjectMapper} instance |
59 | 63 | */
|
60 | 64 | public GraphQLController(GraphQLExecutor graphQLExecutor, ObjectMapper mapper) {
|
61 | 65 | super();
|
62 | 66 | this.graphQLExecutor = graphQLExecutor;
|
63 | 67 | this.mapper = mapper;
|
64 | 68 | }
|
65 | 69 |
|
66 |
| - @RequestMapping(method = RequestMethod.POST, value = "${spring.graphql.jpa.query.path:/graphql}", consumes = MediaType.APPLICATION_JSON_VALUE) |
67 |
| - public ExecutionResult postJson(@RequestBody @Valid final GraphQLQueryRequest query) throws IOException |
| 70 | + /** |
| 71 | + * Handle standard GraphQL POST request that consumes |
| 72 | + * "application/json" content type with a JSON-encoded body |
| 73 | + * of the following format: |
| 74 | + * <pre> |
| 75 | + * { |
| 76 | + * "query": "...", |
| 77 | + * "variables": { "myVariable": "someValue", ... } |
| 78 | + * } |
| 79 | + * </pre> |
| 80 | + * @param queryRequest object |
| 81 | + * @return {@link ExecutionResult} response |
| 82 | + * @throws IOException |
| 83 | + */ |
| 84 | + @PostMapping(value = PATH, |
| 85 | + consumes = {MediaType.APPLICATION_JSON_VALUE}, |
| 86 | + produces = MediaType.APPLICATION_JSON_VALUE) |
| 87 | + public ExecutionResult postJson(@RequestBody @Valid final GraphQLQueryRequest queryRequest) throws IOException |
68 | 88 | {
|
69 |
| - Map<String, Object> variablesMap = variablesStringToMap(query.getVariables()); |
| 89 | + return graphQLExecutor.execute(queryRequest.getQuery(), queryRequest.getVariables()); |
| 90 | + } |
70 | 91 |
|
71 |
| - return graphQLExecutor.execute(query.getQuery(), variablesMap); |
| 92 | + /** |
| 93 | + * Handle HTTP GET request. |
| 94 | + * The GraphQL query should be specified in the "query" query string. |
| 95 | + * 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 |
| 98 | + * query parameter called variables. |
| 99 | + * |
| 100 | + * @param query encoded JSON string |
| 101 | + * @param variables encoded JSON string |
| 102 | + * @return {@link ExecutionResult} response |
| 103 | + * @throws IOException |
| 104 | + */ |
| 105 | + @GetMapping(value = PATH, |
| 106 | + consumes = {APPLICATION_GRAPHQL_VALUE}, |
| 107 | + 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 |
| 111 | + { |
| 112 | + Map<String, Object> variablesMap = variablesStringToMap(variables); |
| 113 | + |
| 114 | + return graphQLExecutor.execute(query, variablesMap); |
72 | 115 | }
|
| 116 | + |
| 117 | + /** |
| 118 | + * Handle HTTP FORM POST request. |
| 119 | + * 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 |
| 122 | + * query parameter called variables. |
73 | 123 |
|
74 |
| - @RequestMapping(method = RequestMethod.POST, value = "${spring.graphql.jpa.query.path:/graphql}", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) |
| 124 | + * @param query encoded JSON string |
| 125 | + * @param variables encoded JSON string |
| 126 | + * @return {@link ExecutionResult} response |
| 127 | + * @throws IOException |
| 128 | + */ |
| 129 | + @PostMapping(value = PATH, |
| 130 | + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, |
| 131 | + produces=MediaType.APPLICATION_JSON_VALUE) |
75 | 132 | public ExecutionResult postForm(
|
76 |
| - @RequestParam final String query, |
77 |
| - @RequestParam(required = false) final String variables) throws IOException |
| 133 | + @RequestParam(name="query") final String query, |
| 134 | + @RequestParam(name="variables", required = false) final String variables) throws IOException |
78 | 135 | {
|
79 | 136 | Map<String, Object> variablesMap = variablesStringToMap(variables);
|
80 | 137 |
|
81 | 138 | return graphQLExecutor.execute(query, variablesMap);
|
82 | 139 | }
|
83 | 140 |
|
| 141 | + /** |
| 142 | + * Handle POST with the "application/graphql" Content-Type header. |
| 143 | + * Treat the HTTP POST body contents as the GraphQL query string. |
| 144 | + * |
| 145 | + * |
| 146 | + * @param queryRequest a valid {@link GraphQLQueryRequest} input argument |
| 147 | + * @return {@link ExecutionResult} response |
| 148 | + * @throws IOException |
| 149 | + */ |
| 150 | + @PostMapping(value = PATH, |
| 151 | + consumes = APPLICATION_GRAPHQL_VALUE, |
| 152 | + produces=MediaType.APPLICATION_JSON_VALUE) |
| 153 | + public ExecutionResult postApplicationGraphQL( |
| 154 | + @RequestBody final String query) throws IOException |
| 155 | + { |
| 156 | + return graphQLExecutor.execute(query, null); |
| 157 | + } |
| 158 | + |
84 | 159 | /**
|
85 | 160 | * Convert String argument to a Map as expected by {@link GraphQLJpaExecutor#execute(String, Map)}. GraphiQL posts both
|
86 |
| - * query and variables as String, so Spring MVC mapping is useless here. |
| 161 | + * query and variables as JSON encoded String, so Spring MVC mapping is useless here. |
| 162 | + * See: http://graphql.org/learn/serving-over-http/ |
87 | 163 | *
|
88 |
| - * @param variables |
89 |
| - * @return HashMap of parameter key-value pairs |
| 164 | + * @param json JSON encoded string variables |
| 165 | + * @return a {@link HashMap} object of variable key-value pairs |
90 | 166 | * @throws IOException
|
91 | 167 | */
|
92 | 168 | @SuppressWarnings("unchecked")
|
93 |
| - private Map<String, Object> variablesStringToMap(final String variables) throws IOException { |
94 |
| - Map<String, Object> variablesMap = null; |
95 |
| - if (variables != null && !variables.isEmpty()) |
96 |
| - variablesMap = mapper.readValue(variables, Map.class); |
97 |
| - return variablesMap; |
| 169 | + private Map<String, Object> variablesStringToMap(final String json) throws IOException { |
| 170 | + Map<String, Object> variables = null; |
| 171 | + |
| 172 | + if (json != null && !json.isEmpty()) |
| 173 | + variables = mapper.readValue(json, Map.class); |
| 174 | + |
| 175 | + return variables; |
98 | 176 | }
|
99 | 177 |
|
100 | 178 | /**
|
101 |
| - * Query Request Data Transfer Object |
102 |
| - * |
103 |
| - * @author Igor Dianov |
104 |
| - * |
| 179 | + * GraphQL JSON HTTP Request Wrapper Class |
105 | 180 | */
|
106 | 181 | @Validated
|
107 | 182 | public static class GraphQLQueryRequest {
|
108 |
| - |
| 183 | + |
109 | 184 | @NotNull
|
110 | 185 | private String query;
|
| 186 | + |
| 187 | + private Map<String, Object> variables; |
| 188 | + |
| 189 | + GraphQLQueryRequest() {} |
111 | 190 |
|
112 |
| - private String variables; |
| 191 | + /** |
| 192 | + * @param query |
| 193 | + */ |
| 194 | + public GraphQLQueryRequest(String query) { |
| 195 | + super(); |
| 196 | + this.query = query; |
| 197 | + } |
113 | 198 |
|
114 | 199 | /**
|
115 |
| - * Default constructor |
| 200 | + * @return the query |
116 | 201 | */
|
117 |
| - GraphQLQueryRequest() { |
| 202 | + public String getQuery() { |
| 203 | + return this.query; |
118 | 204 | }
|
119 |
| - |
| 205 | + |
120 | 206 | /**
|
121 |
| - * @param query string |
| 207 | + * @param query the query to set |
122 | 208 | */
|
123 |
| - public GraphQLQueryRequest(String query) { |
124 |
| - super(); |
| 209 | + public void setQuery(String query) { |
125 | 210 | this.query = query;
|
126 | 211 | }
|
127 |
| - |
| 212 | + |
128 | 213 | /**
|
129 | 214 | * @return the variables
|
130 | 215 | */
|
131 |
| - public String getVariables() { |
| 216 | + public Map<String, Object> getVariables() { |
132 | 217 | return this.variables;
|
133 | 218 | }
|
134 |
| - |
| 219 | + |
135 | 220 | /**
|
136 |
| - * @param variables string |
137 |
| - * the variables to set |
| 221 | + * @param variables the variables to set |
138 | 222 | */
|
139 |
| - public void setVariables(String variables) { |
| 223 | + public void setVariables(Map<String, Object> variables) { |
140 | 224 | this.variables = variables;
|
141 | 225 | }
|
142 |
| - |
143 |
| - /** |
144 |
| - * @return the query |
145 |
| - */ |
146 |
| - public String getQuery() { |
147 |
| - return this.query; |
148 |
| - } |
149 |
| - |
| 226 | + |
150 | 227 | }
|
| 228 | + |
151 | 229 | }
|
0 commit comments