Skip to content

GraphQL operations

Operations on content in GraphQL are performed by using mutations. They include creating, updating, and deleting content items.

The schema contains two mutations per content type, for example, createFolder, and updateFolder. You can also make use of the generic deleteContent and uploadFiles mutations.

Creating content

Create a new Folder as a child of Location 2 with:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
mutation createFolder {
  createFolder(
    language: eng_GB
    parentLocationId: 2
    input: {
      name: "New Folder"
    }
  ) {
    id
  }
}

Response:

1
2
3
4
5
6
7
{
  "data": {
    "createFolder": {
      "id": "RG9tYWluQ29udGVudDo2NA=="
    }
  }
}

Updating content

Modify the name of a Folder content item with:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
mutation updateFolder {
  updateFolder(
    language: eng_GB
    contentId: 64
    input: {
      name: "New Folder name"
    }
  ) {
    id
  }
}

Response:

1
2
3
4
5
6
7
{
  "data": {
    "updateFolder": {
      "id": "RG9tYWluQ29udGVudDo2NA=="
    }
  }
}

The input for updating a content item is the same as when creating it, but all fields are optional.

Deleting content

You can delete any content item by providing its contentId (or its GraphQL opaque ID under id):

1
2
3
4
5
6
mutation deleteBlogPost {
  deleteContent(contentId: 64) {
    id
    contentId
  }
}

Response:

1
2
3
4
5
6
7
8
{
  "data": {
    "deleteContent": {
      "id": "Rm9sZGVyQ29udGVudDo2NA==",
      "contentId": 64
    }
  }
}

File upload

Note

Uploading binary files isn't possible through GraphiQL. You can use alternative third-party clients such as Altair GraphQL.

Uploading files makes use of dedicated mutations per content type, for example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
mutation CreateImage($file: FileUpload!) {
  createImage(
    parentLocationId: 51,
    language: eng_GB,
    input: {
      name: "An image created over GraphQL",
      image: {
        alternativeText: "The alternative text",
        file: $file
      }
    }
  ) {
    _info { id mainLocationId }
    name
    image { fileName alternativeText uri }
  }
}

The file is provided as the $file variable, defined as an UploadFile.

You can include this mutation in a cURL request under operations:

1
2
3
4
5
6
curl -v -X POST \
  <yourdomain>/graphql \
  -H "Cookie: $AUTH_COOKIE" \
  -F 'operations={"query":"mutation createFile($file: FileUpload!) { ... }","variables":{"file": null}}' \
  -F 'map={"image":["variables.file"]}' \
  -F "image"=@/path/to/image.png

For example:

1
2
3
4
5
6
curl -v -X POST \
  <yourdomain>/graphql \
  -H "Cookie: $AUTH_COOKIE" \
  -F 'operations={"query":"mutation CreateImage($file: FileUpload!) { createImage( parentLocationId: 51, input: { name: \"An image created over GraphQL\", image: { alternativeText: \"The alternative text\", file: $file } }, language: \"eng-GB\" ) { _info { id mainLocationId } _url name image { fileName alternativeText uri } } }","variables":{"file": null}}' \
  -F 'map={"image":["variables.file"]}' \
  -F "image"=@/path/to/image.png

Authentication

The example above requires you to set your authentication cookie in the $AUTH_COOKIE variable. For more information, see Authentication.

Uploading multiple files

You can upload multiple files with one operation in a similar way by using the uploadFiles mutation. Here the files are provided in a $files variable and listed under map in the cURL request.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
mutation UploadMultipleFiles($files: [FileUpload]!) {
  uploadFiles(
    locationId: 51,
    files: $files,
    language: eng_GB
  ) {
    files {
      _url
      _location {
        id
      }
      ... on ImageContent {
        name
        image {
          uri
        }
      }
      ... on FileContent {
        name
        file {
          uri
        }
      }
      ... on VideoContent {
        name
        file {
          uri
        }
      }
    }
    warnings
  }
}

Include this mutation in a cURL request:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
curl -v -X POST \
  <yourdomain>/graphql \
  -H 'Cookie: $AUTH_COOKIE' \
  -F 'operations={"query": "mutation UploadMultipleFiles($files: [FileUpload]!) { uploadFiles( locationId: 51, files: $files, languageCode: \"eng-GB\" ) { files { _url _location { id } ... on ImageContent { name image { uri } } ... on FileContent { name file { uri } } ... on VideoContent { name file { uri } } } warnings } }", "variables": {"files": [null, null, null, null, null]}}' \
  -F 'map={"image1":["variables.files.0"], "image2":["variables.files.1"], "file1":["variables.files.2"], "file2":["variables.files.3"], "media":["variables.files.4"]}' \
  -F "image1"=@/tmp/files/image1.png \
  -F "image2"=@/tmp/files/image2.png \
  -F "file1"=@/tmp/files/file1.pdf \
  -F "file2"=@/tmp/files/file2.zip \
  -F "media"=@/tmp/files/media.mp4