2121import com .fasterxml .jackson .databind .ObjectMapper ;
2222import com .fasterxml .jackson .databind .SerializationFeature ;
2323import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
24+ import graphql .ExecutionInput ;
2425import graphql .ExecutionResult ;
2526import graphql .GraphQL ;
2627import graphql .GraphQLError ;
@@ -72,6 +73,7 @@ public abstract class GraphQLServlet extends HttpServlet implements Servlet, Gra
7273
7374 protected abstract GraphQLSchemaProvider getSchemaProvider ();
7475 protected abstract GraphQLContext createContext (Optional <HttpServletRequest > request , Optional <HttpServletResponse > response );
76+ protected abstract Object createRootObject (Optional <HttpServletRequest > request , Optional <HttpServletResponse > response );
7577 protected abstract ExecutionStrategyProvider getExecutionStrategyProvider ();
7678 protected abstract Instrumentation getInstrumentation ();
7779 protected abstract Map <String , Object > transformVariables (GraphQLSchema schema , String query , Map <String , Object > variables );
@@ -93,12 +95,13 @@ public GraphQLServlet(List<GraphQLServletListener> listeners, FileItemFactory fi
9395
9496 this .getHandler = (request , response ) -> {
9597 final GraphQLContext context = createContext (Optional .of (request ), Optional .of (response ));
98+ final Object rootObject = createRootObject (Optional .of (request ), Optional .of (response ));
9699 String path = request .getPathInfo ();
97100 if (path == null ) {
98101 path = request .getServletPath ();
99102 }
100103 if (path .contentEquals ("/schema.json" )) {
101- query (IntrospectionQuery .INTROSPECTION_QUERY , null , new HashMap <>(), getSchemaProvider ().getSchema (request ), request , response , context );
104+ query (IntrospectionQuery .INTROSPECTION_QUERY , null , new HashMap <>(), getSchemaProvider ().getSchema (request ), request , response , context , rootObject );
102105 } else {
103106 if (request .getParameter ("query" ) != null ) {
104107 final Map <String , Object > variables = new HashMap <>();
@@ -109,7 +112,7 @@ public GraphQLServlet(List<GraphQLServletListener> listeners, FileItemFactory fi
109112 if (request .getParameter ("operationName" ) != null ) {
110113 operationName = request .getParameter ("operationName" );
111114 }
112- query (request .getParameter ("query" ), operationName , variables , getSchemaProvider ().getReadOnlySchema (request ), request , response , context );
115+ query (request .getParameter ("query" ), operationName , variables , getSchemaProvider ().getReadOnlySchema (request ), request , response , context , rootObject );
113116 } else {
114117 response .setStatus (STATUS_BAD_REQUEST );
115118 log .info ("Bad GET request: path was not \" /schema.json\" or no query variable named \" query\" given" );
@@ -119,6 +122,7 @@ public GraphQLServlet(List<GraphQLServletListener> listeners, FileItemFactory fi
119122
120123 this .postHandler = (request , response ) -> {
121124 final GraphQLContext context = createContext (Optional .of (request ), Optional .of (response ));
125+ final Object rootObject = createRootObject (Optional .of (request ), Optional .of (response ));
122126 GraphQLRequest graphQLRequest = null ;
123127
124128 try {
@@ -182,7 +186,7 @@ public GraphQLServlet(List<GraphQLServletListener> listeners, FileItemFactory fi
182186 variables = new HashMap <>();
183187 }
184188
185- query (graphQLRequest .getQuery (), graphQLRequest .getOperationName (), variables , getSchemaProvider ().getSchema (request ), request , response , context );
189+ query (graphQLRequest .getQuery (), graphQLRequest .getOperationName (), variables , getSchemaProvider ().getSchema (request ), request , response , context , rootObject );
186190 };
187191 }
188192
@@ -259,13 +263,13 @@ private GraphQL newGraphQL(GraphQLSchema schema) {
259263 .build ();
260264 }
261265
262- private void query (String query , String operationName , Map <String , Object > variables , GraphQLSchema schema , HttpServletRequest req , HttpServletResponse resp , GraphQLContext context ) throws IOException {
266+ private void query (String query , String operationName , Map <String , Object > variables , GraphQLSchema schema , HttpServletRequest req , HttpServletResponse resp , GraphQLContext context , Object rootObject ) throws IOException {
263267 if (operationName != null && operationName .isEmpty ()) {
264- query (query , null , variables , schema , req , resp , context );
268+ query (query , null , variables , schema , req , resp , context , rootObject );
265269 } else if (Subject .getSubject (AccessController .getContext ()) == null && context .getSubject ().isPresent ()) {
266270 Subject .doAs (context .getSubject ().get (), (PrivilegedAction <Void >) () -> {
267271 try {
268- query (query , operationName , variables , schema , req , resp , context );
272+ query (query , operationName , variables , schema , req , resp , context , rootObject );
269273 } catch (IOException e ) {
270274 throw new RuntimeException (e );
271275 }
@@ -274,7 +278,7 @@ private void query(String query, String operationName, Map<String, Object> varia
274278 } else {
275279 List <GraphQLServletListener .OperationCallback > operationCallbacks = runListeners (l -> l .onOperation (context , operationName , query , variables ));
276280
277- final ExecutionResult executionResult = newGraphQL (schema ).execute (query , operationName , context , transformVariables (schema , query , variables ));
281+ final ExecutionResult executionResult = newGraphQL (schema ).execute (new ExecutionInput ( query , operationName , context , rootObject , transformVariables (schema , query , variables ) ));
278282 final List <GraphQLError > errors = executionResult .getErrors ();
279283 final Object data = executionResult .getData ();
280284
0 commit comments