2121import com .fasterxml .jackson .databind .ObjectMapper ;
2222import com .fasterxml .jackson .databind .RuntimeJsonMappingException ;
2323import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
24- import com .google .common .io .CharStreams ;
2524import graphql .ExecutionResult ;
25+ import graphql .GraphQL ;
2626import graphql .GraphQLError ;
2727import graphql .InvalidSyntaxError ;
2828import graphql .execution .ExecutionStrategy ;
2929import graphql .execution .instrumentation .Instrumentation ;
30+ import graphql .introspection .IntrospectionQuery ;
3031import graphql .schema .GraphQLFieldDefinition ;
3132import graphql .schema .GraphQLSchema ;
3233import graphql .validation .ValidationError ;
4546import javax .servlet .http .HttpServletResponse ;
4647import java .io .IOException ;
4748import java .io .InputStream ;
48- import java .io .InputStreamReader ;
4949import java .security .AccessController ;
5050import java .security .PrivilegedAction ;
5151import java .util .ArrayList ;
5757import java .util .function .Consumer ;
5858import java .util .stream .Collectors ;
5959
60- import static graphql .GraphQL .newGraphQL ;
61-
6260/**
6361 * @author Andrew Potter
6462 */
65- public abstract class GraphQLServlet extends HttpServlet implements Servlet , GraphQLMBean , GraphQLSchemaProvider {
63+ public abstract class GraphQLServlet extends HttpServlet implements Servlet , GraphQLMBean {
6664
6765 public static final Logger log = LoggerFactory .getLogger (GraphQLServlet .class );
6866
@@ -72,8 +70,10 @@ public abstract class GraphQLServlet extends HttpServlet implements Servlet, Gra
7270
7371 private static final ObjectMapper mapper = new ObjectMapper ();
7472
73+ protected abstract GraphQLSchemaProvider getSchemaProvider ();
7574 protected abstract GraphQLContext createContext (Optional <HttpServletRequest > request , Optional <HttpServletResponse > response );
76- protected abstract ExecutionStrategy getExecutionStrategy ();
75+ protected abstract ExecutionStrategy getQueryExecutionStrategy ();
76+ protected abstract ExecutionStrategy getMutationExecutionStrategy ();
7777 protected abstract Instrumentation getInstrumentation ();
7878 protected abstract Map <String , Object > transformVariables (GraphQLSchema schema , String query , Map <String , Object > variables );
7979
@@ -100,7 +100,7 @@ public GraphQLServlet(List<GraphQLOperationListener> operationListeners, List<Gr
100100 path = request .getServletPath ();
101101 }
102102 if (path .contentEquals ("/schema.json" )) {
103- query (CharStreams . toString ( new InputStreamReader ( GraphQLServlet . class . getResourceAsStream ( "introspectionQuery" ))) , null , new HashMap <>(), getSchema (), request , response , context );
103+ query (IntrospectionQuery . INTROSPECTION_QUERY , null , new HashMap <>(), getSchemaProvider (). getSchema (request ), request , response , context );
104104 } else {
105105 if (request .getParameter ("query" ) != null ) {
106106 final Map <String , Object > variables = new HashMap <>();
@@ -111,7 +111,7 @@ public GraphQLServlet(List<GraphQLOperationListener> operationListeners, List<Gr
111111 if (request .getParameter ("operationName" ) != null ) {
112112 operationName = request .getParameter ("operationName" );
113113 }
114- query (request .getParameter ("query" ), operationName , variables , getReadOnlySchema (), request , response , context );
114+ query (request .getParameter ("query" ), operationName , variables , getSchemaProvider (). getReadOnlySchema (request ), request , response , context );
115115 } else {
116116 response .setStatus (STATUS_BAD_REQUEST );
117117 log .info ("Bad GET request: path was not \" /schema.json\" or no query variable named \" query\" given" );
@@ -184,7 +184,7 @@ public GraphQLServlet(List<GraphQLOperationListener> operationListeners, List<Gr
184184 variables = new HashMap <>();
185185 }
186186
187- query (graphQLRequest .getQuery (), graphQLRequest .getOperationName (), variables , getSchema (), request , response , context );
187+ query (graphQLRequest .getQuery (), graphQLRequest .getOperationName (), variables , getSchemaProvider (). getSchema (request ), request , response , context );
188188 };
189189 }
190190
@@ -206,18 +206,18 @@ public void removeServletListener(GraphQLServletListener servletListener) {
206206
207207 @ Override
208208 public String [] getQueries () {
209- return getSchema ().getQueryType ().getFieldDefinitions ().stream ().map (GraphQLFieldDefinition ::getName ).toArray (String []::new );
209+ return getSchemaProvider (). getSchema ().getQueryType ().getFieldDefinitions ().stream ().map (GraphQLFieldDefinition ::getName ).toArray (String []::new );
210210 }
211211
212212 @ Override
213213 public String [] getMutations () {
214- return getSchema ().getMutationType ().getFieldDefinitions ().stream ().map (GraphQLFieldDefinition ::getName ).toArray (String []::new );
214+ return getSchemaProvider (). getSchema ().getMutationType ().getFieldDefinitions ().stream ().map (GraphQLFieldDefinition ::getName ).toArray (String []::new );
215215 }
216216
217217 @ Override
218218 public String executeQuery (String query ) {
219219 try {
220- final ExecutionResult result = newGraphQL (getSchema ()). instrumentation ( getInstrumentation ()). build ( ).execute (query , createContext (Optional .empty (), Optional .empty ()), new HashMap <>());
220+ final ExecutionResult result = newGraphQL (getSchemaProvider (). getSchema () ).execute (query , createContext (Optional .empty (), Optional .empty ()), new HashMap <>());
221221 return mapper .writeValueAsString (createResultFromDataAndErrors (result .getData (), result .getErrors ()));
222222 } catch (Exception e ) {
223223 return e .getMessage ();
@@ -257,6 +257,13 @@ private Optional<FileItem> getFileItem(Map<String, List<FileItem>> fileItems, St
257257 return items .stream ().findFirst ();
258258 }
259259
260+ private GraphQL newGraphQL (GraphQLSchema schema ) {
261+ return GraphQL .newGraphQL (schema )
262+ .queryExecutionStrategy (getQueryExecutionStrategy ())
263+ .mutationExecutionStrategy (getMutationExecutionStrategy ())
264+ .instrumentation (getInstrumentation ())
265+ .build ();
266+ }
260267
261268 private void query (String query , String operationName , Map <String , Object > variables , GraphQLSchema schema , HttpServletRequest req , HttpServletResponse resp , GraphQLContext context ) throws IOException {
262269 if (Subject .getSubject (AccessController .getContext ()) == null && context .getSubject ().isPresent ()) {
@@ -271,7 +278,7 @@ private void query(String query, String operationName, Map<String, Object> varia
271278 } else {
272279 runListeners (operationListeners , l -> runListener (l , it -> it .beforeGraphQLOperation (context , operationName , query , variables )));
273280
274- final ExecutionResult executionResult = newGraphQL (schema ).queryExecutionStrategy ( getExecutionStrategy ()). instrumentation ( getInstrumentation ()). build (). execute (query , operationName , context , transformVariables (schema , query , variables ));
281+ final ExecutionResult executionResult = newGraphQL (schema ).execute (query , operationName , context , transformVariables (schema , query , variables ));
275282 final List <GraphQLError > errors = executionResult .getErrors ();
276283 final Object data = executionResult .getData ();
277284
0 commit comments