Skip to content

Mutations

Mutations allow you to create, update, and delete data. All mutations require authentication, so you will need to obtain an access token before you can use them.

TIP

Unlike queries, where you can exclude the query keyword, mutations require the mutation keyword.

The majority of our mutations have the following prefixes:

  • Save: Create new or update existing data

    We usually determine if you would like to create or update data depending on if you provide an id or not.

  • Delete: Delete data

    This will usually return a Deleted object type. However, some mutations will return an updated version of the sibling data if this is more useful.

  • Toggle: Toggle a boolean value

Authorization needed

In the examples below, you will also need to include your access token in the Authorization header of your request. For the sake of brevity, we will be skipping that step here.

Create

Let's create a new media list entry on our user's list. We'll use Cowboy Bebop, which has an ID of 1, as an example.

Example Query

Apollo Studio

php
$query = '
mutation ($listEntryId: Int, $mediaId: Int, $status: MediaListStatus) {
  SaveMediaListEntry(id: $listEntryId, mediaId: $mediaId, status: $status) {
    id
    status
  }
}
';

$variables = [
    "mediaId" => 1,
    "status" => "CURRENT"
];

$http = new GuzzleHttp\Client;
$response = $http->post('https://graphql.anilist.co', [
    'json' => [
        'query' => $query,
        'variables' => $variables,
    ]
]);

The request in the above example will return the following JSON response:

json
{
  "data": {
    "SaveMediaListEntry": {
        "id": 4,
        "status": "CURRENT"
    }
  }
}

Keep in mind that the id that was returned is the ID of the newly created list entry.

Update

Using the id returned above, we can update the status of the list entry.

Note how the above mutation includes the $listEntryId variable, which is used to identify the list entry to update. It went unused in the example above, but we can make use of it now.

Example Query

Apollo Studio

If using the above studio link, you will need to replace the listEntryId variable with the ID returned from the previous mutation.

php
$query = '
mutation ($listEntryId: Int, $mediaId: Int, $status: MediaListStatus) {
  SaveMediaListEntry(id: $listEntryId, mediaId: $mediaId, status: $status) {
    id
    status
  }
}
';

$variables = [
    "listEntryId" => 4,
    "status" => "COMPLETED"
];

$http = new GuzzleHttp\Client;
$response = $http->post('https://graphql.anilist.co', [
    'json' => [
        'query' => $query,
        'variables' => $variables,
    ]
]);

The request in the above example will return an identical response to the one above, but with the status field updated to COMPLETED.