25
25
import java .io .Writer ;
26
26
import java .util .ArrayList ;
27
27
import java .util .Arrays ;
28
- import java .util .Collections ;
29
28
import java .util .HashMap ;
30
29
import java .util .List ;
31
30
import java .util .Map ;
@@ -51,31 +50,40 @@ public abstract class AbstractGraphQLHttpServlet extends HttpServlet implements
51
50
private static final GraphQLRequest INTROSPECTION_REQUEST = new GraphQLRequest (IntrospectionQuery .INTROSPECTION_QUERY , new HashMap <>(), null );
52
51
private static final String [] MULTIPART_KEYS = new String []{"operations" , "graphql" , "query" };
53
52
53
+ private GraphQLConfiguration configuration ;
54
+
54
55
/**
55
- * @deprecated use {@link #getConfiguration()} instead
56
+ * @deprecated override {@link #getConfiguration()} instead
56
57
*/
57
58
@ Deprecated
58
59
protected abstract GraphQLQueryInvoker getQueryInvoker ();
59
60
60
61
/**
61
- * @deprecated use {@link #getConfiguration()} instead
62
+ * @deprecated override {@link #getConfiguration()} instead
62
63
*/
63
64
@ Deprecated
64
65
protected abstract GraphQLInvocationInputFactory getInvocationInputFactory ();
65
66
66
67
/**
67
- * @deprecated use {@link #getConfiguration()} instead
68
+ * @deprecated override {@link #getConfiguration()} instead
68
69
*/
69
70
@ Deprecated
70
71
protected abstract GraphQLObjectMapper getGraphQLObjectMapper ();
71
72
72
73
/**
73
- * @deprecated use {@link #getConfiguration()} instead
74
+ * @deprecated override {@link #getConfiguration()} instead
74
75
*/
75
76
@ Deprecated
76
77
protected abstract boolean isAsyncServletMode ();
77
78
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
+ }
79
87
80
88
/**
81
89
* @deprecated use {@link #getConfiguration()} instead
@@ -95,13 +103,13 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners) {
95
103
}
96
104
97
105
@ Override
98
- public void init (ServletConfig config ) {
99
- GraphQLConfiguration configuration = getConfiguration ();
106
+ public void init (ServletConfig servletConfig ) {
107
+ this . configuration = getConfiguration ();
100
108
101
109
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 ();
105
113
106
114
String path = request .getPathInfo ();
107
115
if (path == null ) {
@@ -133,9 +141,9 @@ public void init(ServletConfig config) {
133
141
};
134
142
135
143
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 ();
139
147
140
148
try {
141
149
if (APPLICATION_GRAPHQL .equals (request .getContentType ())) {
@@ -148,7 +156,7 @@ public void init(ServletConfig config) {
148
156
149
157
for (String key : MULTIPART_KEYS ) {
150
158
// Check to see if there is a part under the key we seek
151
- if (!fileItems .containsKey (key )) {
159
+ if (!fileItems .containsKey (key )) {
152
160
continue ;
153
161
}
154
162
@@ -174,7 +182,7 @@ public void init(ServletConfig config) {
174
182
return ;
175
183
} else {
176
184
GraphQLRequest graphQLRequest ;
177
- if ("query" .equals (key )) {
185
+ if ("query" .equals (key )) {
178
186
graphQLRequest = buildRequestFromQuery (inputStream , graphQLObjectMapper , fileItems );
179
187
} else {
180
188
graphQLRequest = graphQLObjectMapper .readGraphQLRequest (inputStream );
@@ -217,8 +225,7 @@ private static InputStream asMarkableInputStream(InputStream inputStream) {
217
225
218
226
private GraphQLRequest buildRequestFromQuery (InputStream inputStream ,
219
227
GraphQLObjectMapper graphQLObjectMapper ,
220
- Map <String , List <Part >> fileItems ) throws IOException
221
- {
228
+ Map <String , List <Part >> fileItems ) throws IOException {
222
229
GraphQLRequest graphQLRequest ;
223
230
String query = new String (ByteStreams .toByteArray (inputStream ));
224
231
@@ -240,49 +247,48 @@ private GraphQLRequest buildRequestFromQuery(InputStream inputStream,
240
247
241
248
private void mapMultipartVariables (GraphQLRequest request ,
242
249
Map <String , List <String >> variablesMap ,
243
- Map <String , List <Part >> fileItems )
244
- {
250
+ Map <String , List <Part >> fileItems ) {
245
251
Map <String , Object > variables = request .getVariables ();
246
252
247
253
variablesMap .forEach ((partName , objectPaths ) -> {
248
254
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" ));
252
258
253
259
objectPaths .forEach (objectPath -> VariableMapper .mapVariable (objectPath , variables , part ));
254
260
});
255
261
}
256
262
257
263
public void addListener (GraphQLServletListener servletListener ) {
258
- listeners .add (servletListener );
264
+ configuration .add (servletListener );
259
265
}
260
266
261
267
public void removeListener (GraphQLServletListener servletListener ) {
262
- listeners .remove (servletListener );
268
+ configuration .remove (servletListener );
263
269
}
264
270
265
271
@ Override
266
272
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 );
268
274
}
269
275
270
276
@ Override
271
277
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 );
273
279
}
274
280
275
281
@ Override
276
282
public String executeQuery (String query ) {
277
283
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 ))));
279
285
} catch (Exception e ) {
280
286
return e .getMessage ();
281
287
}
282
288
}
283
289
284
290
private void doRequestAsync (HttpServletRequest request , HttpServletResponse response , HttpRequestHandler handler ) {
285
- if (isAsyncServletMode ()) {
291
+ if (configuration . isAsyncServletModeEnabled ()) {
286
292
AsyncContext asyncContext = request .startAsync ();
287
293
HttpServletRequest asyncRequest = (HttpServletRequest ) asyncContext .getRequest ();
288
294
HttpServletResponse asyncResponse = (HttpServletResponse ) asyncContext .getResponse ();
@@ -351,11 +357,7 @@ private void queryBatched(GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper
351
357
}
352
358
353
359
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 ()
359
361
.map (listener -> {
360
362
try {
361
363
return action .apply (listener );
0 commit comments