An interoperable multipart form field structure for GraphQL requests, used by various file upload client/server implementations.
It’s possible to implement:
- Nesting files anywhere within operations (usually in
variables
). - Operation batching.
- File deduplication.
- File upload streams in resolvers.
- Aborting file uploads in resolvers.
An “operations object” is an Apollo GraphQL POST request (or array of requests if batching). An “operations path” is an object-path
string to locate a file within an operations object.
So operations can be resolved while the files are still uploading, the fields are ordered:
operations
: A JSON encoded operations object with files replaced withnull
.map
: A JSON encoded map of where files occurred in the operations. For each file, the key is the file multipart form field name and the value is an array of operations paths.- File fields: Each file extracted from the operations object with a unique, arbitrary field name.
{
query: `
mutation($file: Upload!) {
uploadFile(file: $file) {
id
}
}
`,
variables: {
file: File // a.txt
}
}
curl localhost:3001/graphql \
-F operations='{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) { id } }", "variables": { "file": null } }' \
-F map='{ "file0": ["variables.file"] }' \
-F [email protected]
--------------------------cec8e8123c05ba25
Content-Disposition: form-data; name="operations"
{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) { id } }", "variables": { "file": null } }
--------------------------cec8e8123c05ba25
Content-Disposition: form-data; name="map"
{ "file0": ["variables.file"] }
--------------------------cec8e8123c05ba25
Content-Disposition: form-data; name="file0"; filename="a.txt"
Content-Type: text/plain
Alpha file content.
--------------------------cec8e8123c05ba25--
{
query: `
mutation($files: [Upload!]!) {
multipleUpload(files: $files) {
id
}
}
`,
variables: {
files: [
File, // b.txt
File // c.txt
]
}
}
curl localhost:3001/graphql \
-F operations='{ "query": "mutation($files: [Upload!]!) { multipleUpload(files: $files) { id } }", "variables": { "files": [null, null] } }' \
-F map='{ "file0": ["variables.files.0"], "file1": ["variables.files.1"] }' \
-F [email protected] \
-F [email protected]
--------------------------ec62457de6331cad
Content-Disposition: form-data; name="operations"
{ "query": "mutation($files: [Upload!]!) { multipleUpload(files: $files) { id } }", "variables": { "files": [null, null] } }
--------------------------ec62457de6331cad
Content-Disposition: form-data; name="map"
{ "file0": ["variables.files.0"], "file1": ["variables.files.1"] }
--------------------------ec62457de6331cad
Content-Disposition: form-data; name="file0"; filename="b.txt"
Content-Type: text/plain
Bravo file content.
--------------------------ec62457de6331cad
Content-Disposition: form-data; name="file1"; filename="c.txt"
Content-Type: text/plain
Charlie file content.
--------------------------ec62457de6331cad--
;[
{
query: `
mutation($file: Upload!) {
uploadFile(file: $file) {
id
}
}
`,
variables: {
file: File // a.txt
}
},
{
query: `
mutation($files: [Upload!]!) {
multipleUpload(files: $files) {
id
}
}
`,
variables: {
files: [
File, // b.txt
File // c.txt
]
}
}
]
curl localhost:3001/graphql \
-F operations='[{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) { id } }", "variables": { "file": null } }, { "query": "mutation($files: [Upload!]!) { multipleUpload(files: $files) { id } }", "variables": { "files": [null, null] } }]' \
-F map='{ "file0": ["0.variables.file"], "file1": ["1.variables.files.0"], "file2": ["1.variables.files.1"] }' \
-F [email protected] \
-F [email protected] \
-F [email protected]
--------------------------627436eaefdbc285
Content-Disposition: form-data; name="operations"
[{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) { id } }", "variables": { "file": null } }, { "query": "mutation($files: [Upload!]!) { multipleUpload(files: $files) { id } }", "variables": { "files": [null, null] } }]
--------------------------627436eaefdbc285
Content-Disposition: form-data; name="map"
{ "file0": ["0.variables.file"], "file1": ["1.variables.files.0"], "file2": ["1.variables.files.1"] }
--------------------------627436eaefdbc285
Content-Disposition: form-data; name="file0"; filename="a.txt"
Content-Type: text/plain
Alpha file content.
--------------------------627436eaefdbc285
Content-Disposition: form-data; name="file1"; filename="b.txt"
Content-Type: text/plain
Bravo file content.
--------------------------627436eaefdbc285
Content-Disposition: form-data; name="file2"; filename="c.txt"
Content-Type: text/plain
Charlie file content.
--------------------------627436eaefdbc285--
Pull requests adding either experimental or mature implementations to these lists are welcome!
- jaydenseric/graphql-react (JS: npm)
- jaydenseric/apollo-upload-client (JS: npm)
- jaydenseric/extract-files (JS: npm)
apollo-fetch-upload(JS: npm)
- jaydenseric/graphql-upload (JS: npm)
- apollographql/apollo-server (JS: npm)
jaydenseric/apollo-upload-server(JS: npm)- jetruby/apollo_upload_server-ruby (Ruby: Gem)
- Ecodev/graphql-upload (PHP: Composer)
- rebing/graphql-laravel (PHP: Composer)
- lmcgartland/graphene-file-upload (Python: PyPi)