Skip to content

Commit e19ab31

Browse files
authored
Merge pull request #127 from graphql-java-kickstart/alternative-construction
Alternative construction
2 parents bc93775 + 4ab1a96 commit e19ab31

11 files changed

+339
-77
lines changed

gradle.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
version = 6.2.1-SNAPSHOT
1+
version = 7.0.0-SNAPSHOT
22
group = com.graphql-java-kickstart
33

44
LIB_GRAPHQL_JAVA_VER = 11.0
5-
LIB_JACKSON_VER = 2.8.11
5+
LIB_JACKSON_VER = 2.9.7

src/main/java/graphql/servlet/AbstractGraphQLHttpServlet.java

+69-40
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import javax.servlet.AsyncContext;
1414
import javax.servlet.Servlet;
15+
import javax.servlet.ServletConfig;
1516
import javax.servlet.ServletException;
1617
import javax.servlet.http.HttpServlet;
1718
import javax.servlet.http.HttpServletRequest;
@@ -24,7 +25,6 @@
2425
import java.io.Writer;
2526
import java.util.ArrayList;
2627
import java.util.Arrays;
27-
import java.util.Collections;
2828
import java.util.HashMap;
2929
import java.util.List;
3030
import java.util.Map;
@@ -50,31 +50,66 @@ public abstract class AbstractGraphQLHttpServlet extends HttpServlet implements
5050
private static final GraphQLRequest INTROSPECTION_REQUEST = new GraphQLRequest(IntrospectionQuery.INTROSPECTION_QUERY, new HashMap<>(), null);
5151
private static final String[] MULTIPART_KEYS = new String[]{"operations", "graphql", "query"};
5252

53+
private GraphQLConfiguration configuration;
54+
55+
/**
56+
* @deprecated override {@link #getConfiguration()} instead
57+
*/
58+
@Deprecated
5359
protected abstract GraphQLQueryInvoker getQueryInvoker();
5460

61+
/**
62+
* @deprecated override {@link #getConfiguration()} instead
63+
*/
64+
@Deprecated
5565
protected abstract GraphQLInvocationInputFactory getInvocationInputFactory();
5666

67+
/**
68+
* @deprecated override {@link #getConfiguration()} instead
69+
*/
70+
@Deprecated
5771
protected abstract GraphQLObjectMapper getGraphQLObjectMapper();
5872

59-
private final List<GraphQLServletListener> listeners;
73+
/**
74+
* @deprecated override {@link #getConfiguration()} instead
75+
*/
76+
@Deprecated
77+
protected abstract boolean isAsyncServletMode();
78+
79+
protected GraphQLConfiguration getConfiguration() {
80+
return GraphQLConfiguration.with(getInvocationInputFactory())
81+
.with(getQueryInvoker())
82+
.with(getGraphQLObjectMapper())
83+
.with(isAsyncServletMode())
84+
.with(listeners)
85+
.build();
86+
}
6087

61-
private final HttpRequestHandler getHandler;
62-
private final HttpRequestHandler postHandler;
88+
/**
89+
* @deprecated use {@link #getConfiguration()} instead
90+
*/
91+
@Deprecated
92+
private final List<GraphQLServletListener> listeners;
6393

64-
private final boolean asyncServletMode;
94+
private HttpRequestHandler getHandler;
95+
private HttpRequestHandler postHandler;
6596

6697
public AbstractGraphQLHttpServlet() {
67-
this(null, false);
98+
this(null);
6899
}
69100

70-
public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolean asyncServletMode) {
101+
public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners) {
71102
this.listeners = listeners != null ? new ArrayList<>(listeners) : new ArrayList<>();
72-
this.asyncServletMode = asyncServletMode;
103+
}
104+
105+
@Override
106+
public void init(ServletConfig servletConfig) {
107+
this.configuration = getConfiguration();
73108

74109
this.getHandler = (request, response) -> {
75-
GraphQLInvocationInputFactory invocationInputFactory = getInvocationInputFactory();
76-
GraphQLObjectMapper graphQLObjectMapper = getGraphQLObjectMapper();
77-
GraphQLQueryInvoker queryInvoker = getQueryInvoker();
110+
GraphQLInvocationInputFactory invocationInputFactory = configuration.getInvocationInputFactory();
111+
GraphQLObjectMapper graphQLObjectMapper = configuration.getObjectMapper();
112+
GraphQLQueryInvoker queryInvoker = configuration.getQueryInvoker();
78113

79114
String path = request.getPathInfo();
80115
if (path == null) {
@@ -106,22 +141,22 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolea
106141
};
107142

108143
this.postHandler = (request, response) -> {
109-
GraphQLInvocationInputFactory invocationInputFactory = getInvocationInputFactory();
110-
GraphQLObjectMapper graphQLObjectMapper = getGraphQLObjectMapper();
111-
GraphQLQueryInvoker queryInvoker = getQueryInvoker();
144+
GraphQLInvocationInputFactory invocationInputFactory = configuration.getInvocationInputFactory();
145+
GraphQLObjectMapper graphQLObjectMapper = configuration.getObjectMapper();
146+
GraphQLQueryInvoker queryInvoker = configuration.getQueryInvoker();
112147

113148
try {
114149
if (APPLICATION_GRAPHQL.equals(request.getContentType())) {
115150
String query = CharStreams.toString(request.getReader());
116151
query(queryInvoker, graphQLObjectMapper, invocationInputFactory.create(new GraphQLRequest(query, null, null)), response);
117152
} else if (request.getContentType() != null && request.getContentType().startsWith("multipart/form-data") && !request.getParts().isEmpty()) {
118153
final Map<String, List<Part>> fileItems = request.getParts()
119-
.stream()
120-
.collect(Collectors.groupingBy(Part::getName));
154+
.stream()
155+
.collect(Collectors.groupingBy(Part::getName));
121156

122157
for (String key : MULTIPART_KEYS) {
123158
// Check to see if there is a part under the key we seek
124-
if(!fileItems.containsKey(key)) {
159+
if (!fileItems.containsKey(key)) {
125160
continue;
126161
}
127162

@@ -134,28 +169,28 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolea
134169
InputStream inputStream = asMarkableInputStream(queryItem.get().getInputStream());
135170

136171
final Optional<Map<String, List<String>>> variablesMap =
137-
getFileItem(fileItems, "map").map(graphQLObjectMapper::deserializeMultipartMap);
172+
getFileItem(fileItems, "map").map(graphQLObjectMapper::deserializeMultipartMap);
138173

139174
if (isBatchedQuery(inputStream)) {
140175
List<GraphQLRequest> graphQLRequests =
141-
graphQLObjectMapper.readBatchedGraphQLRequest(inputStream);
176+
graphQLObjectMapper.readBatchedGraphQLRequest(inputStream);
142177
variablesMap.ifPresent(map -> graphQLRequests.forEach(r -> mapMultipartVariables(r, map, fileItems)));
143178
GraphQLBatchedInvocationInput invocationInput =
144-
invocationInputFactory.create(graphQLRequests, request, response);
179+
invocationInputFactory.create(graphQLRequests, request, response);
145180
invocationInput.getContext().setParts(fileItems);
146181
queryBatched(queryInvoker, graphQLObjectMapper, invocationInput, response);
147182
return;
148183
} else {
149184
GraphQLRequest graphQLRequest;
150-
if("query".equals(key)) {
185+
if ("query".equals(key)) {
151186
graphQLRequest = buildRequestFromQuery(inputStream, graphQLObjectMapper, fileItems);
152187
} else {
153188
graphQLRequest = graphQLObjectMapper.readGraphQLRequest(inputStream);
154189
}
155190

156191
variablesMap.ifPresent(m -> mapMultipartVariables(graphQLRequest, m, fileItems));
157192
GraphQLSingleInvocationInput invocationInput =
158-
invocationInputFactory.create(graphQLRequest, request, response);
193+
invocationInputFactory.create(graphQLRequest, request, response);
159194
invocationInput.getContext().setParts(fileItems);
160195
query(queryInvoker, graphQLObjectMapper, invocationInput, response);
161196
return;
@@ -190,8 +225,7 @@ private static InputStream asMarkableInputStream(InputStream inputStream) {
190225

191226
private GraphQLRequest buildRequestFromQuery(InputStream inputStream,
192227
GraphQLObjectMapper graphQLObjectMapper,
193-
Map<String, List<Part>> fileItems) throws IOException
194-
{
228+
Map<String, List<Part>> fileItems) throws IOException {
195229
GraphQLRequest graphQLRequest;
196230
String query = new String(ByteStreams.toByteArray(inputStream));
197231

@@ -213,49 +247,48 @@ private GraphQLRequest buildRequestFromQuery(InputStream inputStream,
213247

214248
private void mapMultipartVariables(GraphQLRequest request,
215249
Map<String, List<String>> variablesMap,
216-
Map<String, List<Part>> fileItems)
217-
{
250+
Map<String, List<Part>> fileItems) {
218251
Map<String, Object> variables = request.getVariables();
219252

220253
variablesMap.forEach((partName, objectPaths) -> {
221254
Part part = getFileItem(fileItems, partName)
222-
.orElseThrow(() -> new RuntimeException("unable to find part name " +
223-
partName +
224-
" as referenced in the variables map"));
255+
.orElseThrow(() -> new RuntimeException("unable to find part name " +
256+
partName +
257+
" as referenced in the variables map"));
225258

226259
objectPaths.forEach(objectPath -> VariableMapper.mapVariable(objectPath, variables, part));
227260
});
228261
}
229262

230263
public void addListener(GraphQLServletListener servletListener) {
231-
listeners.add(servletListener);
264+
configuration.add(servletListener);
232265
}
233266

234267
public void removeListener(GraphQLServletListener servletListener) {
235-
listeners.remove(servletListener);
268+
configuration.remove(servletListener);
236269
}
237270

238271
@Override
239272
public String[] getQueries() {
240-
return getInvocationInputFactory().getSchemaProvider().getSchema().getQueryType().getFieldDefinitions().stream().map(GraphQLFieldDefinition::getName).toArray(String[]::new);
273+
return configuration.getInvocationInputFactory().getSchemaProvider().getSchema().getQueryType().getFieldDefinitions().stream().map(GraphQLFieldDefinition::getName).toArray(String[]::new);
241274
}
242275

243276
@Override
244277
public String[] getMutations() {
245-
return getInvocationInputFactory().getSchemaProvider().getSchema().getMutationType().getFieldDefinitions().stream().map(GraphQLFieldDefinition::getName).toArray(String[]::new);
278+
return configuration.getInvocationInputFactory().getSchemaProvider().getSchema().getMutationType().getFieldDefinitions().stream().map(GraphQLFieldDefinition::getName).toArray(String[]::new);
246279
}
247280

248281
@Override
249282
public String executeQuery(String query) {
250283
try {
251-
return getGraphQLObjectMapper().serializeResultAsJson(getQueryInvoker().query(getInvocationInputFactory().create(new GraphQLRequest(query, new HashMap<>(), null))));
284+
return configuration.getObjectMapper().serializeResultAsJson(configuration.getQueryInvoker().query(configuration.getInvocationInputFactory().create(new GraphQLRequest(query, new HashMap<>(), null))));
252285
} catch (Exception e) {
253286
return e.getMessage();
254287
}
255288
}
256289

257290
private void doRequestAsync(HttpServletRequest request, HttpServletResponse response, HttpRequestHandler handler) {
258-
if (asyncServletMode) {
291+
if (configuration.isAsyncServletModeEnabled()) {
259292
AsyncContext asyncContext = request.startAsync();
260293
HttpServletRequest asyncRequest = (HttpServletRequest) asyncContext.getRequest();
261294
HttpServletResponse asyncResponse = (HttpServletResponse) asyncContext.getResponse();
@@ -324,11 +357,7 @@ private void queryBatched(GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper
324357
}
325358

326359
private <R> List<R> runListeners(Function<? super GraphQLServletListener, R> action) {
327-
if (listeners == null) {
328-
return Collections.emptyList();
329-
}
330-
331-
return listeners.stream()
360+
return configuration.getListeners().stream()
332361
.map(listener -> {
333362
try {
334363
return action.apply(listener);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package graphql.servlet;
2+
3+
import java.util.Objects;
4+
5+
class ConfiguredGraphQLHttpServlet extends GraphQLHttpServlet {
6+
7+
private GraphQLConfiguration configuration;
8+
9+
ConfiguredGraphQLHttpServlet(GraphQLConfiguration configuration) {
10+
this.configuration = Objects.requireNonNull(configuration, "configuration is required");
11+
}
12+
13+
@Override
14+
protected GraphQLConfiguration getConfiguration() {
15+
return configuration;
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package graphql.servlet;
2+
3+
import javax.servlet.ServletConfig;
4+
5+
public class DefaultGraphQLServlet extends AbstractGraphQLHttpServlet {
6+
7+
@Override
8+
public void init(ServletConfig servletConfig) {
9+
10+
super.init(servletConfig);
11+
}
12+
13+
@Override
14+
protected GraphQLInvocationInputFactory getInvocationInputFactory() {
15+
return null;
16+
}
17+
18+
@Override
19+
protected GraphQLQueryInvoker getQueryInvoker() {
20+
return GraphQLQueryInvoker.newBuilder().build();
21+
}
22+
23+
@Override
24+
protected GraphQLObjectMapper getGraphQLObjectMapper() {
25+
return GraphQLObjectMapper.newBuilder().build();
26+
}
27+
28+
@Override
29+
protected boolean isAsyncServletMode() {
30+
return false;
31+
}
32+
33+
}

0 commit comments

Comments
 (0)