Skip to content

Commit 1f7a7a8

Browse files
committed
Alternative construction
1 parent 20e1fa5 commit 1f7a7a8

File tree

6 files changed

+178
-43
lines changed

6 files changed

+178
-43
lines changed

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

Lines changed: 41 additions & 14 deletions
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;
@@ -50,26 +51,52 @@ public abstract class AbstractGraphQLHttpServlet extends HttpServlet implements
5051
private static final GraphQLRequest INTROSPECTION_REQUEST = new GraphQLRequest(IntrospectionQuery.INTROSPECTION_QUERY, new HashMap<>(), null);
5152
private static final String[] MULTIPART_KEYS = new String[]{"operations", "graphql", "query"};
5253

54+
/**
55+
* @deprecated use {@link #getConfiguration()} instead
56+
*/
57+
@Deprecated
5358
protected abstract GraphQLQueryInvoker getQueryInvoker();
5459

60+
/**
61+
* @deprecated use {@link #getConfiguration()} instead
62+
*/
63+
@Deprecated
5564
protected abstract GraphQLInvocationInputFactory getInvocationInputFactory();
5665

66+
/**
67+
* @deprecated use {@link #getConfiguration()} instead
68+
*/
69+
@Deprecated
5770
protected abstract GraphQLObjectMapper getGraphQLObjectMapper();
5871

59-
private final List<GraphQLServletListener> listeners;
72+
/**
73+
* @deprecated use {@link #getConfiguration()} instead
74+
*/
75+
@Deprecated
76+
protected abstract boolean isAsyncServletMode();
77+
78+
protected abstract GraphQLConfiguration getConfiguration();
6079

61-
private final HttpRequestHandler getHandler;
62-
private final HttpRequestHandler postHandler;
80+
/**
81+
* @deprecated use {@link #getConfiguration()} instead
82+
*/
83+
@Deprecated
84+
private final List<GraphQLServletListener> listeners;
6385

64-
private final boolean asyncServletMode;
86+
private HttpRequestHandler getHandler;
87+
private HttpRequestHandler postHandler;
6588

6689
public AbstractGraphQLHttpServlet() {
67-
this(null, false);
90+
this(null);
6891
}
6992

70-
public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolean asyncServletMode) {
93+
public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners) {
7194
this.listeners = listeners != null ? new ArrayList<>(listeners) : new ArrayList<>();
72-
this.asyncServletMode = asyncServletMode;
95+
}
96+
97+
@Override
98+
public void init(ServletConfig config) {
99+
GraphQLConfiguration configuration = getConfiguration();
73100

74101
this.getHandler = (request, response) -> {
75102
GraphQLInvocationInputFactory invocationInputFactory = getInvocationInputFactory();
@@ -116,8 +143,8 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolea
116143
query(queryInvoker, graphQLObjectMapper, invocationInputFactory.create(new GraphQLRequest(query, null, null)), response);
117144
} else if (request.getContentType() != null && request.getContentType().startsWith("multipart/form-data") && !request.getParts().isEmpty()) {
118145
final Map<String, List<Part>> fileItems = request.getParts()
119-
.stream()
120-
.collect(Collectors.groupingBy(Part::getName));
146+
.stream()
147+
.collect(Collectors.groupingBy(Part::getName));
121148

122149
for (String key : MULTIPART_KEYS) {
123150
// Check to see if there is a part under the key we seek
@@ -134,14 +161,14 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolea
134161
InputStream inputStream = asMarkableInputStream(queryItem.get().getInputStream());
135162

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

139166
if (isBatchedQuery(inputStream)) {
140167
List<GraphQLRequest> graphQLRequests =
141-
graphQLObjectMapper.readBatchedGraphQLRequest(inputStream);
168+
graphQLObjectMapper.readBatchedGraphQLRequest(inputStream);
142169
variablesMap.ifPresent(map -> graphQLRequests.forEach(r -> mapMultipartVariables(r, map, fileItems)));
143170
GraphQLBatchedInvocationInput invocationInput =
144-
invocationInputFactory.create(graphQLRequests, request, response);
171+
invocationInputFactory.create(graphQLRequests, request, response);
145172
invocationInput.getContext().setParts(fileItems);
146173
queryBatched(queryInvoker, graphQLObjectMapper, invocationInput, response);
147174
return;
@@ -155,7 +182,7 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolea
155182

156183
variablesMap.ifPresent(m -> mapMultipartVariables(graphQLRequest, m, fileItems));
157184
GraphQLSingleInvocationInput invocationInput =
158-
invocationInputFactory.create(graphQLRequest, request, response);
185+
invocationInputFactory.create(graphQLRequest, request, response);
159186
invocationInput.getContext().setParts(fileItems);
160187
query(queryInvoker, graphQLObjectMapper, invocationInput, response);
161188
return;
@@ -255,7 +282,7 @@ public String executeQuery(String query) {
255282
}
256283

257284
private void doRequestAsync(HttpServletRequest request, HttpServletResponse response, HttpRequestHandler handler) {
258-
if (asyncServletMode) {
285+
if (isAsyncServletMode()) {
259286
AsyncContext asyncContext = request.startAsync();
260287
HttpServletRequest asyncRequest = (HttpServletRequest) asyncContext.getRequest();
261288
HttpServletResponse asyncResponse = (HttpServletResponse) asyncContext.getResponse();
Lines changed: 33 additions & 0 deletions
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 config) {
9+
10+
super.init(config);
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+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package graphql.servlet;
2+
3+
import graphql.schema.GraphQLSchema;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class GraphQLConfiguration {
9+
10+
private GraphQLInvocationInputFactory invocationInputFactory;
11+
private GraphQLQueryInvoker queryInvoker = GraphQLQueryInvoker.newBuilder().build();
12+
private GraphQLObjectMapper objectMapper = GraphQLObjectMapper.newBuilder().build();
13+
private List<GraphQLServletListener> listeners;
14+
private boolean asyncServletModeEnabled;
15+
16+
public static GraphQLConfiguration.Builder with(GraphQLSchema schema) {
17+
return with(new DefaultGraphQLSchemaProvider(schema));
18+
}
19+
20+
public static GraphQLConfiguration.Builder with(GraphQLSchemaProvider schemaProvider) {
21+
return new Builder(GraphQLInvocationInputFactory.newBuilder(schemaProvider));
22+
}
23+
24+
25+
public static class Builder {
26+
27+
private GraphQLInvocationInputFactory.Builder invocationInputFactoryBuilder;
28+
private GraphQLQueryInvoker queryInvoker = GraphQLQueryInvoker.newBuilder().build();
29+
private GraphQLObjectMapper objectMapper = GraphQLObjectMapper.newBuilder().build();
30+
private List<GraphQLServletListener> listeners = new ArrayList<>();
31+
private boolean asyncServletModeEnabled = false;
32+
33+
private Builder(GraphQLInvocationInputFactory.Builder invocationInputFactoryBuilder) {
34+
this.invocationInputFactoryBuilder = invocationInputFactoryBuilder;
35+
}
36+
37+
public Builder with(GraphQLQueryInvoker queryInvoker) {
38+
this.queryInvoker = queryInvoker;
39+
return this;
40+
}
41+
42+
public Builder with(GraphQLObjectMapper objectMapper) {
43+
this.objectMapper = objectMapper;
44+
return this;
45+
}
46+
47+
public Builder with(boolean asyncServletModeEnabled) {
48+
this.asyncServletModeEnabled = asyncServletModeEnabled;
49+
return this;
50+
}
51+
52+
public GraphQLConfiguration build() {
53+
return null;
54+
}
55+
56+
}
57+
58+
}

src/main/java/graphql/servlet/OsgiGraphQLHttpServlet.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ protected GraphQLObjectMapper getGraphQLObjectMapper() {
8383
return graphQLObjectMapper;
8484
}
8585

86+
@Override
87+
protected boolean isAsyncServletMode() {
88+
return false;
89+
}
90+
8691
public OsgiGraphQLHttpServlet() {
8792
updateSchema();
8893

src/main/java/graphql/servlet/SimpleAbstractGraphQLServlet.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/main/java/graphql/servlet/SimpleGraphQLHttpServlet.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,48 @@
33
import graphql.schema.GraphQLSchema;
44

55
import java.util.List;
6+
import java.util.Objects;
67

78
/**
89
* @author Andrew Potter
910
*/
1011
public class SimpleGraphQLHttpServlet extends AbstractGraphQLHttpServlet {
1112

12-
private final GraphQLInvocationInputFactory invocationInputFactory;
13-
private final GraphQLQueryInvoker queryInvoker;
14-
private final GraphQLObjectMapper graphQLObjectMapper;
13+
private GraphQLConfiguration configuration;
1514

16-
// protected to allow class to be extended
17-
protected SimpleGraphQLHttpServlet(GraphQLInvocationInputFactory invocationInputFactory, GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper graphQLObjectMapper, List<GraphQLServletListener> listeners, boolean asyncServletMode) {
18-
super(listeners, asyncServletMode);
19-
this.invocationInputFactory = invocationInputFactory;
20-
this.queryInvoker = queryInvoker;
21-
this.graphQLObjectMapper = graphQLObjectMapper;
15+
// private GraphQLInvocationInputFactory invocationInputFactory;
16+
// private GraphQLQueryInvoker queryInvoker;
17+
// private GraphQLObjectMapper graphQLObjectMapper;
18+
// private boolean asyncServletMode;
19+
20+
public SimpleGraphQLHttpServlet() {
21+
super(null);
22+
}
23+
24+
/**
25+
* @param invocationInputFactory
26+
* @param queryInvoker
27+
* @param graphQLObjectMapper
28+
* @param listeners
29+
* @param asyncServletMode
30+
* @deprecated
31+
*/
32+
@Deprecated
33+
public SimpleGraphQLHttpServlet(GraphQLInvocationInputFactory invocationInputFactory, GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper graphQLObjectMapper, List<GraphQLServletListener> listeners, boolean asyncServletMode) {
34+
super(listeners);
35+
this.configuration = GraphQLConfiguration.with(invocationInputFactory)
36+
.with(queryInvoker)
37+
.with(graphQLObjectMapper)
38+
.with(asyncServletMode);
39+
}
40+
41+
private SimpleGraphQLHttpServlet(GraphQLConfiguration configuration) {
42+
this.configuration = Objects.requireNonNull(configuration, "configuration is required");
43+
}
44+
45+
@Override
46+
protected GraphQLConfiguration getConfiguration() {
47+
return configuration;
2248
}
2349

2450
@Override
@@ -36,6 +62,11 @@ protected GraphQLObjectMapper getGraphQLObjectMapper() {
3662
return graphQLObjectMapper;
3763
}
3864

65+
@Override
66+
protected boolean isAsyncServletMode() {
67+
return asyncServletMode;
68+
}
69+
3970
public static Builder newBuilder(GraphQLSchema schema) {
4071
return new Builder(GraphQLInvocationInputFactory.newBuilder(schema).build());
4172
}
@@ -79,6 +110,7 @@ public Builder withListeners(List<GraphQLServletListener> listeners) {
79110
return this;
80111
}
81112

113+
@Deprecated
82114
public SimpleGraphQLHttpServlet build() {
83115
return new SimpleGraphQLHttpServlet(invocationInputFactory, queryInvoker, graphQLObjectMapper, listeners, asyncServletMode);
84116
}

0 commit comments

Comments
 (0)