icon | description |
---|---|
upload |
Upload files through the router using GraphQL multipart request. |
This feature is implemented according to the GraphQL multipart request specification, utilizing the multipart/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.
A JSON-encoded operations object with files replaced by null
. This object defines the GraphQL query or mutation to be executed, along with the necessary variables.
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.
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.
You need to define a custom scalar Upload
in your schema. This scalar will be used for the field type where you intend to upload files.
scalar Upload
type Mutation {
singleUpload(file: Upload!): Boolean!
multipleUpload(files: [Upload!]!): Boolean!
}
To upload a single file, use the following curl
command:
curl localhost:3002/graphql \
-F operations='{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) }", "variables": { "file": null } }' \
-F map='{ "0": ["variables.file"] }' \
-F [email protected]
This command sends a multipart request where:
operations
defines the GraphQL mutation with thefile
variable set tonull
.map
associates the form field0
withvariables.file
.[email protected]
uploads the filea.txt
with the form field name0
.
To upload multiple files, use the following curl
command:
curl localhost:3002/graphql \
-F operations='{ "query": "mutation($files: [Upload!]!) { multipleUpload(files: $files) }", "variables": { "files": [null, null] } }' \
-F map='{ "0": ["variables.files.0"], "1": ["variables.files.1"] }' \
-F [email protected] \
-F [email protected]
This command sends a multipart request where:
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.[email protected]
and[email protected]
upload the filesa.txt
andb.txt
with form field names0
and1
.
By following these examples, you can configure your router to handle single and multiple file uploads effectively using GraphQL multipart requests. For more details, refer to the GraphQL multipart request specification.