Skip to content

Commit ba4e732

Browse files
committed
Updated readme in line with new doc
1 parent 1f7a7a8 commit ba4e732

File tree

8 files changed

+178
-51
lines changed

8 files changed

+178
-51
lines changed

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

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.io.Writer;
2626
import java.util.ArrayList;
2727
import java.util.Arrays;
28-
import java.util.Collections;
2928
import java.util.HashMap;
3029
import java.util.List;
3130
import java.util.Map;
@@ -51,31 +50,40 @@ public abstract class AbstractGraphQLHttpServlet extends HttpServlet implements
5150
private static final GraphQLRequest INTROSPECTION_REQUEST = new GraphQLRequest(IntrospectionQuery.INTROSPECTION_QUERY, new HashMap<>(), null);
5251
private static final String[] MULTIPART_KEYS = new String[]{"operations", "graphql", "query"};
5352

53+
private GraphQLConfiguration configuration;
54+
5455
/**
55-
* @deprecated use {@link #getConfiguration()} instead
56+
* @deprecated override {@link #getConfiguration()} instead
5657
*/
5758
@Deprecated
5859
protected abstract GraphQLQueryInvoker getQueryInvoker();
5960

6061
/**
61-
* @deprecated use {@link #getConfiguration()} instead
62+
* @deprecated override {@link #getConfiguration()} instead
6263
*/
6364
@Deprecated
6465
protected abstract GraphQLInvocationInputFactory getInvocationInputFactory();
6566

6667
/**
67-
* @deprecated use {@link #getConfiguration()} instead
68+
* @deprecated override {@link #getConfiguration()} instead
6869
*/
6970
@Deprecated
7071
protected abstract GraphQLObjectMapper getGraphQLObjectMapper();
7172

7273
/**
73-
* @deprecated use {@link #getConfiguration()} instead
74+
* @deprecated override {@link #getConfiguration()} instead
7475
*/
7576
@Deprecated
7677
protected abstract boolean isAsyncServletMode();
7778

78-
protected abstract GraphQLConfiguration getConfiguration();
79+
protected GraphQLConfiguration getConfiguration() {
80+
return GraphQLConfiguration.with(getInvocationInputFactory())
81+
.with(getQueryInvoker())
82+
.with(getGraphQLObjectMapper())
83+
.with(isAsyncServletMode())
84+
.with(listeners)
85+
.build();
86+
}
7987

8088
/**
8189
* @deprecated use {@link #getConfiguration()} instead
@@ -95,13 +103,13 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners) {
95103
}
96104

97105
@Override
98-
public void init(ServletConfig config) {
99-
GraphQLConfiguration configuration = getConfiguration();
106+
public void init(ServletConfig servletConfig) {
107+
this.configuration = getConfiguration();
100108

101109
this.getHandler = (request, response) -> {
102-
GraphQLInvocationInputFactory invocationInputFactory = getInvocationInputFactory();
103-
GraphQLObjectMapper graphQLObjectMapper = getGraphQLObjectMapper();
104-
GraphQLQueryInvoker queryInvoker = getQueryInvoker();
110+
GraphQLInvocationInputFactory invocationInputFactory = configuration.getInvocationInputFactory();
111+
GraphQLObjectMapper graphQLObjectMapper = configuration.getObjectMapper();
112+
GraphQLQueryInvoker queryInvoker = configuration.getQueryInvoker();
105113

106114
String path = request.getPathInfo();
107115
if (path == null) {
@@ -133,9 +141,9 @@ public void init(ServletConfig config) {
133141
};
134142

135143
this.postHandler = (request, response) -> {
136-
GraphQLInvocationInputFactory invocationInputFactory = getInvocationInputFactory();
137-
GraphQLObjectMapper graphQLObjectMapper = getGraphQLObjectMapper();
138-
GraphQLQueryInvoker queryInvoker = getQueryInvoker();
144+
GraphQLInvocationInputFactory invocationInputFactory = configuration.getInvocationInputFactory();
145+
GraphQLObjectMapper graphQLObjectMapper = configuration.getObjectMapper();
146+
GraphQLQueryInvoker queryInvoker = configuration.getQueryInvoker();
139147

140148
try {
141149
if (APPLICATION_GRAPHQL.equals(request.getContentType())) {
@@ -148,7 +156,7 @@ public void init(ServletConfig config) {
148156

149157
for (String key : MULTIPART_KEYS) {
150158
// Check to see if there is a part under the key we seek
151-
if(!fileItems.containsKey(key)) {
159+
if (!fileItems.containsKey(key)) {
152160
continue;
153161
}
154162

@@ -174,7 +182,7 @@ public void init(ServletConfig config) {
174182
return;
175183
} else {
176184
GraphQLRequest graphQLRequest;
177-
if("query".equals(key)) {
185+
if ("query".equals(key)) {
178186
graphQLRequest = buildRequestFromQuery(inputStream, graphQLObjectMapper, fileItems);
179187
} else {
180188
graphQLRequest = graphQLObjectMapper.readGraphQLRequest(inputStream);
@@ -217,8 +225,7 @@ private static InputStream asMarkableInputStream(InputStream inputStream) {
217225

218226
private GraphQLRequest buildRequestFromQuery(InputStream inputStream,
219227
GraphQLObjectMapper graphQLObjectMapper,
220-
Map<String, List<Part>> fileItems) throws IOException
221-
{
228+
Map<String, List<Part>> fileItems) throws IOException {
222229
GraphQLRequest graphQLRequest;
223230
String query = new String(ByteStreams.toByteArray(inputStream));
224231

@@ -240,49 +247,48 @@ private GraphQLRequest buildRequestFromQuery(InputStream inputStream,
240247

241248
private void mapMultipartVariables(GraphQLRequest request,
242249
Map<String, List<String>> variablesMap,
243-
Map<String, List<Part>> fileItems)
244-
{
250+
Map<String, List<Part>> fileItems) {
245251
Map<String, Object> variables = request.getVariables();
246252

247253
variablesMap.forEach((partName, objectPaths) -> {
248254
Part part = getFileItem(fileItems, partName)
249-
.orElseThrow(() -> new RuntimeException("unable to find part name " +
250-
partName +
251-
" as referenced in the variables map"));
255+
.orElseThrow(() -> new RuntimeException("unable to find part name " +
256+
partName +
257+
" as referenced in the variables map"));
252258

253259
objectPaths.forEach(objectPath -> VariableMapper.mapVariable(objectPath, variables, part));
254260
});
255261
}
256262

257263
public void addListener(GraphQLServletListener servletListener) {
258-
listeners.add(servletListener);
264+
configuration.add(servletListener);
259265
}
260266

261267
public void removeListener(GraphQLServletListener servletListener) {
262-
listeners.remove(servletListener);
268+
configuration.remove(servletListener);
263269
}
264270

265271
@Override
266272
public String[] getQueries() {
267-
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);
268274
}
269275

270276
@Override
271277
public String[] getMutations() {
272-
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);
273279
}
274280

275281
@Override
276282
public String executeQuery(String query) {
277283
try {
278-
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))));
279285
} catch (Exception e) {
280286
return e.getMessage();
281287
}
282288
}
283289

284290
private void doRequestAsync(HttpServletRequest request, HttpServletResponse response, HttpRequestHandler handler) {
285-
if (isAsyncServletMode()) {
291+
if (configuration.isAsyncServletModeEnabled()) {
286292
AsyncContext asyncContext = request.startAsync();
287293
HttpServletRequest asyncRequest = (HttpServletRequest) asyncContext.getRequest();
288294
HttpServletResponse asyncResponse = (HttpServletResponse) asyncContext.getResponse();
@@ -351,11 +357,7 @@ private void queryBatched(GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper
351357
}
352358

353359
private <R> List<R> runListeners(Function<? super GraphQLServletListener, R> action) {
354-
if (listeners == null) {
355-
return Collections.emptyList();
356-
}
357-
358-
return listeners.stream()
360+
return configuration.getListeners().stream()
359361
.map(listener -> {
360362
try {
361363
return action.apply(listener);
Lines changed: 18 additions & 0 deletions
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+
}

src/main/java/graphql/servlet/DefaultGraphQLServlet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
public class DefaultGraphQLServlet extends AbstractGraphQLHttpServlet {
66

77
@Override
8-
public void init(ServletConfig config) {
8+
public void init(ServletConfig servletConfig) {
99

10-
super.init(config);
10+
super.init(servletConfig);
1111
}
1212

1313
@Override

src/main/java/graphql/servlet/GraphQLConfiguration.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.ArrayList;
66
import java.util.List;
7+
import java.util.Objects;
78

89
public class GraphQLConfiguration {
910

@@ -21,10 +22,50 @@ public static GraphQLConfiguration.Builder with(GraphQLSchemaProvider schemaProv
2122
return new Builder(GraphQLInvocationInputFactory.newBuilder(schemaProvider));
2223
}
2324

25+
static GraphQLConfiguration.Builder with(GraphQLInvocationInputFactory invocationInputFactory) {
26+
return new Builder(invocationInputFactory);
27+
}
28+
29+
private GraphQLConfiguration(GraphQLInvocationInputFactory invocationInputFactory, GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper objectMapper, List<GraphQLServletListener> listeners, boolean asyncServletModeEnabled) {
30+
this.invocationInputFactory = invocationInputFactory;
31+
this.queryInvoker = queryInvoker;
32+
this.objectMapper = objectMapper;
33+
this.listeners = listeners;
34+
this.asyncServletModeEnabled = asyncServletModeEnabled;
35+
}
36+
37+
public GraphQLInvocationInputFactory getInvocationInputFactory() {
38+
return invocationInputFactory;
39+
}
40+
41+
public GraphQLQueryInvoker getQueryInvoker() {
42+
return queryInvoker;
43+
}
44+
45+
public GraphQLObjectMapper getObjectMapper() {
46+
return objectMapper;
47+
}
48+
49+
public List<GraphQLServletListener> getListeners() {
50+
return new ArrayList<>(listeners);
51+
}
52+
53+
public boolean isAsyncServletModeEnabled() {
54+
return asyncServletModeEnabled;
55+
}
56+
57+
public void add(GraphQLServletListener listener) {
58+
listeners.add(listener);
59+
}
60+
61+
public boolean remove(GraphQLServletListener listener) {
62+
return listeners.remove(listener);
63+
}
2464

2565
public static class Builder {
2666

2767
private GraphQLInvocationInputFactory.Builder invocationInputFactoryBuilder;
68+
private GraphQLInvocationInputFactory invocationInputFactory;
2869
private GraphQLQueryInvoker queryInvoker = GraphQLQueryInvoker.newBuilder().build();
2970
private GraphQLObjectMapper objectMapper = GraphQLObjectMapper.newBuilder().build();
3071
private List<GraphQLServletListener> listeners = new ArrayList<>();
@@ -34,6 +75,10 @@ private Builder(GraphQLInvocationInputFactory.Builder invocationInputFactoryBuil
3475
this.invocationInputFactoryBuilder = invocationInputFactoryBuilder;
3576
}
3677

78+
private Builder(GraphQLInvocationInputFactory invocationInputFactory) {
79+
this.invocationInputFactory = invocationInputFactory;
80+
}
81+
3782
public Builder with(GraphQLQueryInvoker queryInvoker) {
3883
this.queryInvoker = queryInvoker;
3984
return this;
@@ -44,13 +89,24 @@ public Builder with(GraphQLObjectMapper objectMapper) {
4489
return this;
4590
}
4691

92+
public Builder with(List<GraphQLServletListener> listeners) {
93+
this.listeners = Objects.requireNonNull(listeners, "listeners must not be null");
94+
return this;
95+
}
96+
4797
public Builder with(boolean asyncServletModeEnabled) {
4898
this.asyncServletModeEnabled = asyncServletModeEnabled;
4999
return this;
50100
}
51101

52102
public GraphQLConfiguration build() {
53-
return null;
103+
return new GraphQLConfiguration(
104+
this.invocationInputFactory != null ? this.invocationInputFactory : invocationInputFactoryBuilder.build(),
105+
queryInvoker,
106+
objectMapper,
107+
listeners,
108+
asyncServletModeEnabled
109+
);
54110
}
55111

56112
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package graphql.servlet;
2+
3+
import graphql.schema.GraphQLSchema;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Objects;
8+
9+
/**
10+
* @author Michiel Oliemans
11+
*/
12+
public abstract class GraphQLHttpServlet extends AbstractGraphQLHttpServlet {
13+
14+
public static GraphQLHttpServlet with(GraphQLSchema schema) {
15+
return new ConfiguredGraphQLHttpServlet(GraphQLConfiguration.with(schema).build());
16+
}
17+
18+
public static GraphQLHttpServlet with(GraphQLConfiguration configuration) {
19+
return new ConfiguredGraphQLHttpServlet(configuration);
20+
}
21+
22+
@Override
23+
protected abstract GraphQLConfiguration getConfiguration();
24+
25+
@Override
26+
protected GraphQLQueryInvoker getQueryInvoker() {
27+
throw new UnsupportedOperationException();
28+
}
29+
30+
@Override
31+
protected GraphQLInvocationInputFactory getInvocationInputFactory() {
32+
throw new UnsupportedOperationException();
33+
}
34+
35+
@Override
36+
protected GraphQLObjectMapper getGraphQLObjectMapper() {
37+
throw new UnsupportedOperationException();
38+
}
39+
40+
@Override
41+
protected boolean isAsyncServletMode() {
42+
throw new UnsupportedOperationException();
43+
}
44+
45+
}

0 commit comments

Comments
 (0)