2525import java .io .Writer ;
2626import java .util .ArrayList ;
2727import java .util .Arrays ;
28- import java .util .Collections ;
2928import java .util .HashMap ;
3029import java .util .List ;
3130import 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 );
0 commit comments