|
1 | 1 | package graphql.spring.controller; |
2 | 2 |
|
3 | 3 |
|
4 | | -import graphql.ExecutionInput; |
5 | | -import graphql.GraphQL; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import graphql.Internal; |
6 | 6 | import org.springframework.beans.factory.annotation.Autowired; |
7 | 7 | import org.springframework.http.MediaType; |
8 | 8 | import org.springframework.web.bind.annotation.RequestBody; |
9 | 9 | import org.springframework.web.bind.annotation.RequestMapping; |
10 | 10 | import org.springframework.web.bind.annotation.RequestMethod; |
| 11 | +import org.springframework.web.bind.annotation.RequestParam; |
11 | 12 | import org.springframework.web.bind.annotation.RestController; |
| 13 | +import org.springframework.web.context.request.WebRequest; |
12 | 14 |
|
13 | | -import java.util.LinkedHashMap; |
| 15 | +import java.io.IOException; |
14 | 16 | import java.util.Map; |
15 | 17 |
|
16 | 18 | @RestController |
| 19 | +@Internal |
17 | 20 | public class GraphQLController { |
18 | 21 |
|
19 | 22 | @Autowired |
20 | | - private GraphQL graphql; |
| 23 | + private GraphQLInvocation graphQLInvocation; |
21 | 24 |
|
22 | | - |
23 | | - @RequestMapping(value = "${graphql.url:graphql}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) |
24 | | - public Map<String, Object> graphql(@RequestBody Map<String, Object> body) { |
25 | | - String query = (String) body.get("query"); |
| 25 | + @Autowired |
| 26 | + private ObjectMapper objectMapper; |
| 27 | + |
| 28 | + @RequestMapping(value = "${graphql.url:graphql}", |
| 29 | + method = RequestMethod.POST, |
| 30 | + consumes = MediaType.APPLICATION_JSON_VALUE, |
| 31 | + produces = MediaType.APPLICATION_JSON_VALUE) |
| 32 | + public Object graphqlPOST(@RequestBody GraphQLRequestBody body, WebRequest webRequest) { |
| 33 | + String query = body.getQuery(); |
26 | 34 | if (query == null) { |
27 | 35 | query = ""; |
28 | 36 | } |
29 | | - String operationName = (String) body.get("operationName"); |
30 | | - Map<String, Object> variables = (Map<String, Object>) body.get("variables"); |
31 | | - if (variables == null) { |
32 | | - variables = new LinkedHashMap<>(); |
33 | | - } |
34 | | - return executeGraphqlQuery(query, operationName, variables); |
| 37 | + return graphQLInvocation.invoke(new GraphQLInvocationData(query, body.getOperationName(), body.getVariables()), webRequest); |
| 38 | + } |
| 39 | + |
| 40 | + @RequestMapping(value = "${graphql.url:graphql}", |
| 41 | + method = RequestMethod.GET, |
| 42 | + produces = MediaType.APPLICATION_JSON_VALUE) |
| 43 | + public Object graphqlGET( |
| 44 | + @RequestParam("query") String query, |
| 45 | + @RequestParam(value = "operationName", required = false) String operationName, |
| 46 | + @RequestParam(value = "variables", required = false) String variablesJson, |
| 47 | + WebRequest webRequest) { |
| 48 | + return graphQLInvocation.invoke(new GraphQLInvocationData(query, operationName, convertVariablesJson(variablesJson)), webRequest); |
35 | 49 | } |
36 | 50 |
|
37 | | - private Map<String, Object> executeGraphqlQuery(String query, String operationName, Map<String, Object> variables) { |
38 | | - ExecutionInput executionInput = ExecutionInput.newExecutionInput() |
39 | | - .query(query) |
40 | | - .operationName(operationName) |
41 | | - .variables(variables) |
42 | | - .build(); |
43 | | - return this.graphql.execute(executionInput).toSpecification(); |
| 51 | + private Map<String, Object> convertVariablesJson(String jsonMap) { |
| 52 | + try { |
| 53 | + return objectMapper.readValue(jsonMap, Map.class); |
| 54 | + } catch (IOException e) { |
| 55 | + throw new RuntimeException("Could not convert variables GET parameter: expected a JSON map", e); |
| 56 | + } |
| 57 | + |
44 | 58 | } |
45 | 59 |
|
| 60 | + |
46 | 61 | } |
0 commit comments