|
6 | 6 | import graphql.introspection.IntrospectionQuery; |
7 | 7 | import graphql.schema.GraphQLFieldDefinition; |
8 | 8 | import graphql.servlet.internal.GraphQLRequest; |
| 9 | +import graphql.servlet.internal.VariableMapper; |
9 | 10 | import org.slf4j.Logger; |
10 | 11 | import org.slf4j.LoggerFactory; |
11 | 12 |
|
|
16 | 17 | import javax.servlet.http.HttpServletRequest; |
17 | 18 | import javax.servlet.http.HttpServletResponse; |
18 | 19 | import javax.servlet.http.Part; |
19 | | -import java.io.*; |
20 | | -import java.util.*; |
| 20 | +import java.io.BufferedInputStream; |
| 21 | +import java.io.ByteArrayOutputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.io.Writer; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Objects; |
| 31 | +import java.util.Optional; |
21 | 32 | import java.util.function.BiConsumer; |
22 | 33 | import java.util.function.Consumer; |
23 | 34 | import java.util.function.Function; |
@@ -109,7 +120,37 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolea |
109 | 120 | Collections::singletonList, |
110 | 121 | (l1, l2) -> Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList()))); |
111 | 122 |
|
112 | | - if (fileItems.containsKey("graphql")) { |
| 123 | + if(fileItems.containsKey("operations")) { |
| 124 | + final Optional<Part> queryItem = getFileItem(fileItems, "operations"); |
| 125 | + if (queryItem.isPresent()) { |
| 126 | + InputStream inputStream = queryItem.get().getInputStream(); |
| 127 | + |
| 128 | + if (!inputStream.markSupported()) { |
| 129 | + inputStream = new BufferedInputStream(inputStream); |
| 130 | + } |
| 131 | + |
| 132 | + final Optional<Map<String, List<String>>> variablesMap = |
| 133 | + getFileItem(fileItems, "map") |
| 134 | + .map(graphQLObjectMapper::deserializeMultipartMap); |
| 135 | + |
| 136 | + if (isBatchedQuery(inputStream)) { |
| 137 | + List<GraphQLRequest> graphQLRequests = graphQLObjectMapper.readBatchedGraphQLRequest(inputStream); |
| 138 | + variablesMap.ifPresent(map -> graphQLRequests.forEach(r -> mapMultipartVariables(r, map, fileItems))); |
| 139 | + GraphQLBatchedInvocationInput invocationInput = invocationInputFactory.create(graphQLRequests, request); |
| 140 | + invocationInput.getContext().setFiles(fileItems); |
| 141 | + queryBatched(queryInvoker, graphQLObjectMapper, invocationInput, response); |
| 142 | + return; |
| 143 | + } else { |
| 144 | + GraphQLRequest graphQLRequest = graphQLObjectMapper.readGraphQLRequest(inputStream); |
| 145 | + variablesMap.ifPresent(m -> mapMultipartVariables(graphQLRequest, m, fileItems)); |
| 146 | + GraphQLSingleInvocationInput invocationInput = |
| 147 | + invocationInputFactory.create(graphQLRequest, request); |
| 148 | + invocationInput.getContext().setFiles(fileItems); |
| 149 | + query(queryInvoker, graphQLObjectMapper, invocationInput, response); |
| 150 | + return; |
| 151 | + } |
| 152 | + } |
| 153 | + } else if (fileItems.containsKey("graphql")) { |
113 | 154 | final Optional<Part> graphqlItem = getFileItem(fileItems, "graphql"); |
114 | 155 | if (graphqlItem.isPresent()) { |
115 | 156 | InputStream inputStream = graphqlItem.get().getInputStream(); |
@@ -190,6 +231,22 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolea |
190 | 231 | }; |
191 | 232 | } |
192 | 233 |
|
| 234 | + private void mapMultipartVariables(GraphQLRequest request, |
| 235 | + Map<String, List<String>> variablesMap, |
| 236 | + Map<String, List<Part>> fileItems) |
| 237 | + { |
| 238 | + Map<String, Object> variables = request.getVariables(); |
| 239 | + |
| 240 | + variablesMap.forEach((partName, objectPaths) -> { |
| 241 | + Part part = getFileItem(fileItems, partName) |
| 242 | + .orElseThrow(() -> new RuntimeException("unable to find part name " + |
| 243 | + partName + |
| 244 | + " as referenced in the variables map")); |
| 245 | + |
| 246 | + objectPaths.forEach(objectPath -> VariableMapper.mapVariable(objectPath, variables, part)); |
| 247 | + }); |
| 248 | + } |
| 249 | + |
193 | 250 | public void addListener(GraphQLServletListener servletListener) { |
194 | 251 | listeners.add(servletListener); |
195 | 252 | } |
|
0 commit comments