Overview
This feature is implemented according to the GraphQL multipart request specification, utilizing themultipart/form-data
content type. Cosmo supports both single and multiple file uploads, but batch uploads are not supported.
The router can be configured for file uploads with these options.
Multipart Request Structure
operations
A JSON-encoded operations object with files replaced bynull
. This object defines the GraphQL query or mutation to be executed, along with the necessary variables.
map
A JSON-encoded map indicating where files appear in the operations. For each file, the key is the file’s multipart form field name, and the value is an array of operations paths. This map ensures the correct association between the files and their respective variables in the GraphQL operation.File Fields
Each file extracted from the operations object is assigned a unique, arbitrary field name. These fields are included in the multipart form data and correspond to the file paths specified in the map.Sample Setup
Define Custom Scalar
You need to define a custom scalarUpload
in your schema. This scalar will be used for the field type where you intend to upload files.
Single File Upload
To upload a single file, use the followingcurl
command:
-
operations
defines the GraphQL mutation with thefile
variable set tonull
. -
map
associates the form field0
withvariables.file
. -
0=@a.txt
uploads the filea.txt
with the form field name0
.
Multiple Files Upload
To upload multiple files, use the followingcurl
command:
-
operations
defines the GraphQL mutation with thefiles
variable set to an array ofnull
values. -
map
associates the form fields0
and1
withvariables.files.0
andvariables.files.1
respectively. -
0=@a.txt
and1=@b.txt
upload the filesa.txt
andb.txt
with form field names0
and1
.