Skip to main content

Introduction

Mutations in Directus GraphQL allow you to modify data in your collections. Each collection automatically gets mutation operations for creating, updating, and deleting items.
Mutations must be sent via POST requests. Attempting to send mutations via GET will result in an error.

Mutation Types

For each non-singleton collection, Directus generates the following mutations:
  • create_<collection>_item - Create a single item
  • create_<collection>_items - Create multiple items
  • update_<collection>_item - Update a single item by ID
  • update_<collection>_items - Update multiple items by IDs
  • update_<collection>_batch - Update multiple items with different data
  • delete_<collection>_item - Delete a single item by ID
  • delete_<collection>_items - Delete multiple items by IDs
For singleton collections:
  • update_<collection> - Upsert the singleton item
Read-only system collections like directus_activity and directus_revisions don’t have mutation operations.

Create Operations

Create Single Item

Create a single item and return specified fields:

Create Multiple Items

Create multiple items in a single mutation:

Create with Relationships

Create an item with related items:

Create with Existing Relations

Link to existing related items:

Update Operations

Update Single Item

Update a single item by its ID:

Update Multiple Items

Update multiple items with the same data:

Batch Update

Update multiple items with different data for each:

Update Singleton

For singleton collections, use the upsert operation:

Update Relationships

Update related items:

Delete Operations

Delete Single Item

Delete a single item by its ID:

Delete Multiple Items

Delete multiple items by their IDs:
Delete operations are permanent and cannot be undone. Consider implementing a “soft delete” pattern with a status field instead.

Using Variables

Make mutations reusable and secure with variables:
Variables payload:

Return Values

You can control what data is returned after a mutation:

Return Specific Fields

Return Boolean

If you don’t request any fields, the mutation returns true on success:
Response:
Return nested relationship data:

System Collection Mutations

Use the /graphql/system endpoint to mutate system collections:

Create User

Update Role Permissions

File Uploads

To upload files via GraphQL, you need to use multipart form data. Most GraphQL clients support this:

Using Apollo Client

Mutation with File Upload

Error Handling

Mutations return errors in the standard GraphQL format:

Common Error Codes

  • FORBIDDEN - Insufficient permissions
  • INVALID_PAYLOAD - Invalid data provided
  • INVALID_QUERY - Invalid GraphQL syntax
  • RECORD_NOT_FOUND - Item with specified ID doesn’t exist
  • FAILED_VALIDATION - Data doesn’t meet validation rules

Combining Multiple Mutations

Execute multiple mutations in a single request:
Mutations in a single request are executed sequentially in the order they appear.

Example: Complete CRUD Operations

Here’s a comprehensive example showing create, read, update, and delete:
Variables:

Best Practices

Never hardcode values in mutations. Always use variables for security and reusability:
Only request the fields you need in the response:
Always check for errors in mutation responses:

Next Steps

GraphQL Queries

Learn how to query your data

GraphQL Subscriptions

Get real-time updates with subscriptions