12
12
import java .util .Arrays ;
13
13
import java .util .Collections ;
14
14
import java .util .List ;
15
+ import java .util .function .IntFunction ;
15
16
import lombok .Getter ;
16
17
import lombok .NonNull ;
17
18
import org .springframework .beans .factory .annotation .Value ;
@@ -313,24 +314,36 @@ public GraphQLResponse postMultipart(String query, String variables) {
313
314
* will be a part of multipart request. GraphQL Servlet will use <i>map</i> part to walk through
314
315
* variables.files and validate the request in combination with other binary file parts
315
316
*
316
- * <p>-------------- request beginning ---------------
317
+ * <p>----------------------------dummyid
317
318
*
318
- * <p>operations: { "query": "mutation($files:[Upload]!) {uploadFiles(files:$files)}",
319
- * "operationName": "uploadFiles", "variables": { "files": [null, null] } }
319
+ * <p>Content-Disposition: form-data; name="operations"
320
320
*
321
- * <p>-----------------------------------------------
321
+ * <p>{ "query": "mutation($files:[Upload]!) {uploadFiles(files:$files)}", "operationName":
322
+ * "uploadFiles", "variables": { "files": [null, null] } }
323
+ *
324
+ * <p>----------------------------dummyid
325
+ *
326
+ * <p>Content-Disposition: form-data; name="map"
322
327
*
323
328
* <p>map: { "1":["variables.files.0"], "2":["variables.files.1"] }
324
329
*
325
- * <p>-----------------------------------------------
330
+ * <p>----------------------------dummyid
331
+ *
332
+ * <p>Content-Disposition: form-data; name="1"; filename="file1.pdf"
333
+ *
334
+ * <p>Content-Type: application/octet-stream
335
+ *
336
+ * <p>--file 1 binary code--
337
+ *
338
+ * <p>----------------------------dummyid
326
339
*
327
- * <p>1: --file 1 binary code--
340
+ * <p>Content-Disposition: form-data; name="2"; filename="file2.pdf"
328
341
*
329
- * <p>-----------------------------------------------
342
+ * <p>Content-Type: application/octet-stream
330
343
*
331
344
* <p>2: --file 2 binary code--
332
345
*
333
- * <p>-------------- request end ---------------------
346
+ * <p>
334
347
*
335
348
* @param graphqlResource path to the classpath resource containing the GraphQL query
336
349
* @param variables the input variables for the GraphQL query
@@ -344,12 +357,39 @@ public GraphQLResponse postFiles(
344
357
String graphqlResource , ObjectNode variables , List <ClassPathResource > files )
345
358
throws IOException {
346
359
360
+ return postFiles (
361
+ graphqlResource , variables , files , index -> String .format ("variables.files.%d" , index ));
362
+ }
363
+
364
+ /**
365
+ * Handle the multipart files upload request to GraphQL servlet
366
+ *
367
+ * @param graphqlResource path to the classpath resource containing the GraphQL query
368
+ * @param variables the input variables for the GraphQL query
369
+ * @param files ClassPathResource instance for each file that will be uploaded to GraphQL server.
370
+ * When Spring RestTemplate processes the request, it will automatically produce a valid part
371
+ * representing given file inside multipart request (including size, submittedFileName, etc.)
372
+ * @param pathFunc function to generate the path to file inside variables. For example:
373
+ * <ul>
374
+ * <li>index -> String.format("variables.files.%d", index) for multiple files
375
+ * <li>index -> "variables.file" for single file
376
+ * </ul>
377
+ *
378
+ * @return {@link GraphQLResponse} containing the result of query execution
379
+ * @throws IOException if the resource cannot be loaded from the classpath
380
+ */
381
+ public GraphQLResponse postFiles (
382
+ String graphqlResource ,
383
+ ObjectNode variables ,
384
+ List <ClassPathResource > files ,
385
+ IntFunction <String > pathFunc )
386
+ throws IOException {
347
387
MultiValueMap <String , Object > values = new LinkedMultiValueMap <>();
348
388
MultiValueMap <String , Object > map = new LinkedMultiValueMap <>();
349
389
350
390
for (int i = 0 ; i < files .size (); i ++) {
351
391
String valueKey = String .valueOf (i + 1 ); // map value and part index starts at 1
352
- map .add (valueKey , String . format ( "variables.files.%d" , i ));
392
+ map .add (valueKey , pathFunc . apply ( i ));
353
393
354
394
values .add (valueKey , files .get (i ));
355
395
}
0 commit comments