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.Mutation Types
For each non-singleton collection, Directus generates the following mutations:create_<collection>_item- Create a single itemcreate_<collection>_items- Create multiple itemsupdate_<collection>_item- Update a single item by IDupdate_<collection>_items- Update multiple items by IDsupdate_<collection>_batch- Update multiple items with different datadelete_<collection>_item- Delete a single item by IDdelete_<collection>_items- Delete multiple items by IDs
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:Using Variables
Make mutations reusable and secure with variables: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 returnstrue on success:
Return Related Data
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 permissionsINVALID_PAYLOAD- Invalid data providedINVALID_QUERY- Invalid GraphQL syntaxRECORD_NOT_FOUND- Item with specified ID doesn’t existFAILED_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:Best Practices
Always Use Variables
Always Use Variables
Never hardcode values in mutations. Always use variables for security and reusability:
Request Only Needed Fields
Request Only Needed Fields
Only request the fields you need in the response:
Handle Errors Gracefully
Handle Errors Gracefully
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