From 36f5ec21e1d7d7f81ddcb774e49257fbc57a2e82 Mon Sep 17 00:00:00 2001 From: Serge Huber Date: Wed, 12 Oct 2016 11:40:50 +0200 Subject: [PATCH] Add support for operationName that was not implemented. We can now build GraphQL code such as : query UserQuery { dx { user(userKey: "root") { id properties { key value } } } } mutation CreateNode { createNodeByPath(name:"test", parentPath: "/") { identifier path parentIdentifier } } And choose which operation to execute using GraphiQL's play button. --- .../java/graphql/servlet/GraphQLServlet.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/graphql/servlet/GraphQLServlet.java b/src/main/java/graphql/servlet/GraphQLServlet.java index cdc3e85e..8b12695f 100644 --- a/src/main/java/graphql/servlet/GraphQLServlet.java +++ b/src/main/java/graphql/servlet/GraphQLServlet.java @@ -182,16 +182,20 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se path = req.getServletPath(); } if (path.contentEquals("/schema.json")) { - query(CharStreams.toString(new InputStreamReader(GraphQLServlet.class.getResourceAsStream("introspectionQuery"))), new HashMap<>(), schema, req, resp, context); + query(CharStreams.toString(new InputStreamReader(GraphQLServlet.class.getResourceAsStream("introspectionQuery"))), null, new HashMap<>(), schema, req, resp, context); } else { if (req.getParameter("q") != null) { - query(req.getParameter("q"), new HashMap<>(), readOnlySchema, req, resp, context); + query(req.getParameter("q"), null, new HashMap<>(), readOnlySchema, req, resp, context); } else if (req.getParameter("query") != null) { Map variables = new HashMap<>(); if (req.getParameter("variables") != null) { variables.putAll(new ObjectMapper().readValue(req.getParameter("variables"), new TypeReference>() {})); } - query(req.getParameter("query"), variables, readOnlySchema, req, resp, context); + String operationName = null; + if (req.getParameter("operationName") != null) { + operationName = req.getParameter("operationName"); + } + query(req.getParameter("query"), operationName, variables, readOnlySchema, req, resp, context); } } } @@ -226,20 +230,20 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S if (variables == null) { variables = new HashMap<>(); } - query(request.query, variables, schema, req, resp, context); + query(request.query, request.operationName, variables, schema, req, resp, context); } - private void query(String query, Map variables, GraphQLSchema schema, HttpServletRequest req, HttpServletResponse resp, GraphQLContext context) throws IOException { + private void query(String query, String operationName, Map variables, GraphQLSchema schema, HttpServletRequest req, HttpServletResponse resp, GraphQLContext context) throws IOException { if (Subject.getSubject(AccessController.getContext()) == null && context.getSubject().isPresent()) { Subject.doAs(context.getSubject().get(), new PrivilegedAction() { @Override @SneakyThrows public Void run() { - query(query, variables, schema, req, resp, context); + query(query, operationName, variables, schema, req, resp, context); return null; } }); } else { - ExecutionResult result = new GraphQL(schema, executionStrategyProvider.getExecutionStrategy()).execute(query, context, variables); + ExecutionResult result = new GraphQL(schema, executionStrategyProvider.getExecutionStrategy()).execute(query, operationName, context, variables); resp.setContentType("application/json"); if (result.getErrors().isEmpty()) { Map dict = new HashMap<>();