Skip to content

Commit 6306ffb

Browse files
authored
Upgrade to graphql-java 5.0 (#23)
* Upgrade to graphql-java v5.0 * Remove @deprecated GraphQL.execute method * Polish GraphQLRestController handler methods * Upgrade to GraphIQL 0.11.2
1 parent c2ab8af commit 6306ffb

File tree

22 files changed

+42175
-265
lines changed

22 files changed

+42175
-265
lines changed

.gitignore

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Eclipse
22
#.classpath
3-
#.project
3+
.project/
44
.settings/
55
build/
66
.factorypath
@@ -15,4 +15,9 @@ build/
1515

1616
# Maven
1717
log/
18-
target/
18+
target/
19+
.factorypath
20+
21+
graphql-jpa-query-annotations/\.project
22+
23+
graphql-jpa-query-schema/\.project

graphql-jpa-query-annotations/.project

-40
This file was deleted.

graphql-jpa-query-boot-starter/.project

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
<arguments>
1616
</arguments>
1717
</buildCommand>
18+
<buildCommand>
19+
<name>org.hibernate.eclipse.console.hibernateBuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
1823
<buildCommand>
1924
<name>org.springframework.ide.eclipse.core.springbuilder</name>
2025
<arguments>
@@ -30,11 +35,22 @@
3035
<arguments>
3136
</arguments>
3237
</buildCommand>
38+
<buildCommand>
39+
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
40+
<arguments>
41+
</arguments>
42+
</buildCommand>
43+
<buildCommand>
44+
<name>org.eclipse.m2e.core.maven2Builder</name>
45+
<arguments>
46+
</arguments>
47+
</buildCommand>
3348
</buildSpec>
3449
<natures>
3550
<nature>org.springframework.ide.eclipse.core.springnature</nature>
3651
<nature>org.eclipse.m2e.core.maven2Nature</nature>
3752
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
3853
<nature>org.eclipse.jdt.core.javanature</nature>
54+
<nature>org.hibernate.eclipse.console.hibernateNature</nature>
3955
</natures>
4056
</projectDescription>

graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/EnableGraphQLJpaQuery.java

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2017 IntroPro Ventures, Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package com.introproventures.graphql.jpa.query.boot.autoconfigure;
217

318
import static java.lang.annotation.ElementType.TYPE;

graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 Alfresco, Inc. and/or its affiliates.
2+
* Copyright 2017 IntroPro Ventures, Inc. and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryProperties.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 Alfresco, Inc. and/or its affiliates.
2+
* Copyright 2017 IntroPro Ventures, Inc. and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 Alfresco, Inc. and/or its affiliates.
2+
* Copyright 2017 IntroPro Ventures, Inc. and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package com.introproventures.graphql.jpa.query.web;
1717

1818
import java.io.IOException;
19+
import java.util.HashMap;
1920
import java.util.Map;
2021

2122
import javax.validation.Valid;
@@ -25,9 +26,9 @@
2526
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2627
import org.springframework.http.MediaType;
2728
import org.springframework.validation.annotation.Validated;
29+
import org.springframework.web.bind.annotation.GetMapping;
30+
import org.springframework.web.bind.annotation.PostMapping;
2831
import org.springframework.web.bind.annotation.RequestBody;
29-
import org.springframework.web.bind.annotation.RequestMapping;
30-
import org.springframework.web.bind.annotation.RequestMethod;
3132
import org.springframework.web.bind.annotation.RequestParam;
3233
import org.springframework.web.bind.annotation.RestController;
3334

@@ -38,114 +39,191 @@
3839
import graphql.ExecutionResult;
3940

4041
/**
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>
4245
*
43-
* @author Igor Dianov
44-
*
4546
*/
4647
@RestController
4748
@ConditionalOnWebApplication
4849
@ConditionalOnClass(GraphQLExecutor.class)
4950
public class GraphQLController {
5051

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;
5357

5458
/**
55-
* Create instance of Spring GraphQLController RestController
59+
* Creates instance of Spring GraphQLController RestController
5660
*
57-
* @param graphQLExecutor instance
58-
* @param mapper instance
61+
* @param graphQLExecutor {@link GraphQLExecutor} instance
62+
* @param mapper {@link ObjectMapper} instance
5963
*/
6064
public GraphQLController(GraphQLExecutor graphQLExecutor, ObjectMapper mapper) {
6165
super();
6266
this.graphQLExecutor = graphQLExecutor;
6367
this.mapper = mapper;
6468
}
6569

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
6888
{
69-
Map<String, Object> variablesMap = variablesStringToMap(query.getVariables());
89+
return graphQLExecutor.execute(queryRequest.getQuery(), queryRequest.getVariables());
90+
}
7091

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);
72115
}
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.
73123
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)
75132
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
78135
{
79136
Map<String, Object> variablesMap = variablesStringToMap(variables);
80137

81138
return graphQLExecutor.execute(query, variablesMap);
82139
}
83140

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+
84159
/**
85160
* 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/
87163
*
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
90166
* @throws IOException
91167
*/
92168
@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;
98176
}
99177

100178
/**
101-
* Query Request Data Transfer Object
102-
*
103-
* @author Igor Dianov
104-
*
179+
* GraphQL JSON HTTP Request Wrapper Class
105180
*/
106181
@Validated
107182
public static class GraphQLQueryRequest {
108-
183+
109184
@NotNull
110185
private String query;
186+
187+
private Map<String, Object> variables;
188+
189+
GraphQLQueryRequest() {}
111190

112-
private String variables;
191+
/**
192+
* @param query
193+
*/
194+
public GraphQLQueryRequest(String query) {
195+
super();
196+
this.query = query;
197+
}
113198

114199
/**
115-
* Default constructor
200+
* @return the query
116201
*/
117-
GraphQLQueryRequest() {
202+
public String getQuery() {
203+
return this.query;
118204
}
119-
205+
120206
/**
121-
* @param query string
207+
* @param query the query to set
122208
*/
123-
public GraphQLQueryRequest(String query) {
124-
super();
209+
public void setQuery(String query) {
125210
this.query = query;
126211
}
127-
212+
128213
/**
129214
* @return the variables
130215
*/
131-
public String getVariables() {
216+
public Map<String, Object> getVariables() {
132217
return this.variables;
133218
}
134-
219+
135220
/**
136-
* @param variables string
137-
* the variables to set
221+
* @param variables the variables to set
138222
*/
139-
public void setVariables(String variables) {
223+
public void setVariables(Map<String, Object> variables) {
140224
this.variables = variables;
141225
}
142-
143-
/**
144-
* @return the query
145-
*/
146-
public String getQuery() {
147-
return this.query;
148-
}
149-
226+
150227
}
228+
151229
}

0 commit comments

Comments
 (0)