Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Function for GraphQLTestTemplate to upload files using Upload scalar #712

Conversation

tmkhanh
Copy link

@tmkhanh tmkhanh commented Sep 14, 2021

Came across GraphQL file upload functionality and recognised the GraphQLTestTemplate is missing function to handle multi-part file upload for IntegrationTest scenarios.

The current multipart post function for GraphQLTestTemplate is not handling file upload case using built-in Apollo Upload scalar from GraphQL Servlet.

Please note that we can't embed binary data into json. Clients library supporting GraphQL file upload will set variable.files to null for every element inside the array to make it conformed with json blob spec for operations part. However, each file will be a part of multipart request with its binary data. For anyone interested, can check out this library for React https://github.com/jaydenseric/apollo-upload-client

GraphQL Servlet will use map part to walk through variables.files and validate the request in combination with other binary file parts

Example of an Http Request to upload two files using GraphQL:

----------------------------dummyid
Content-Disposition: form-data; name="operations"

{ 
      "query": "mutation($files:[Upload]!) {uploadFiles(files:$files)}", 
      "operationName": "uploadFiles", 
      "variables": { "files": [null, null] } 
}
----------------------------dummyid
Content-Disposition: form-data; name="map"

{ 
     "1":["variables.files.0"], 
     "2":["variables.files.1"] 
}
----------------------------dummyid
Content-Disposition: form-data; name="1"; filename="file1.pdf"
Content-Type: application/octet-stream

<file 1 binary code>
----------------------------dummyid
Content-Disposition: form-data; name="2"; filename="file2.pdf"
Content-Type: application/octet-stream

<file 2 binary code>

First time raising PR so please bear with me.

@tmkhanh
Copy link
Author

tmkhanh commented Nov 16, 2021

@oliemansm please review or advise what else I need to do for the PR. Sorry that this is first PR raised so I'm not sure I'm missing anything

@oliemansm
Copy link
Member

@tmkhanh Thanks for the contribution, sorry for the late response!

@oliemansm oliemansm merged commit 39bba96 into graphql-java-kickstart:master Jan 22, 2022
@oliemansm oliemansm added this to the 12.0.1 milestone Jan 22, 2022
@tmkhanh tmkhanh deleted the feature/graphql-test-template-file-upload-support branch February 2, 2022 08:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants