Developer
Jahia 8.1
How to upload a file with GraphQL and Jahia 8.1?
Question
How to upload a file with GraphQL and Jahia 8.1?
Answer
Here is the GraphQL mutation extracted from a request when upload a file in jContent:
mutation uploadFile($nameInJCR: String!, $path: String!, $mimeType: String!, $fileHandle: String!) {
jcr {
addNode(name: $nameInJCR, parentPathOrId: $path, primaryNodeType: "jnt:file") {
addChild(name: "jcr:content", primaryNodeType: "jnt:resource") {
content: mutateProperty(name: "jcr:data") {
setValue(type: BINARY, value: $fileHandle)
__typename
}
contentType: mutateProperty(name: "jcr:mimeType") {
setValue(value: $mimeType)
__typename
}
__typename
}
createVersion
uuid
__typename
}
__typename
}
}
With the following variables:
{
"fileHandle":"0.v6tm3tg8iy",
"nameInJCR":"chat.js",
"path":"/sites/systemsite/files",
"mimeType":"application/x-javascript"
}
As you can see, the variable fileHandle has a random filename, related to the upload process.
So, in addition to this data, you have also to transfer the file.
With curl, it would mean doing the following:
curl -u root:root 'http://localhost:8080/modules/graphql' -H 'Accept: */*' -H 'Origin: http://localhost:8080' -F query=@payload.json -F fileToUpload=@2025-03-06_10-01.png
where the file payload.json has this content:
[
{
"operationName": "uploadFile",
"variables": {
"fileHandle": "fileToUpload",
"nameInJCR": "2025-03-06_10-01.png",
"path": "/sites/systemsite/files",
"mimeType": "image/png"
},
"query": "mutation uploadFile($nameInJCR: String!, $path: String!, $mimeType: String!, $fileHandle: String!) { jcr { addNode(name: $nameInJCR, parentPathOrId: $path, primaryNodeType: \"jnt:file\") { addChild(name: \"jcr:content\", primaryNodeType: \"jnt:resource\") { content: mutateProperty(name: \"jcr:data\") { setValue(type: BINARY, value: $fileHandle) __typename } contentType: mutateProperty(name: \"jcr:mimeType\") { setValue(value: $mimeType) __typename } __typename } createVersion uuid __typename } __typename }}"
}
]