Skip to main content

Introduction

GraphQL mutations allow you to modify data in your EverShop application. Unlike queries which are read-only, mutations perform write operations such as creating, updating, or deleting data.

Mutation Structure

Mutations in EverShop follow the standard GraphQL mutation syntax:

Defining Mutations

Mutations are defined in your GraphQL schema files alongside queries:

Mutation Examples

Create Mutation

Create a new resource in the system.
String!
required
The name of the new Foo object
String
Optional description for the Foo object
Foo
Returns the newly created Foo object

Update Mutation

Update an existing resource.
ID!
required
The ID of the Foo object to update
String
New name for the object (optional)
String
New description for the object (optional)
Foo
Returns the updated Foo object

Delete Mutation

Remove a resource from the system.
ID!
required
The ID of the Foo object to delete
Boolean
Returns true if the deletion was successful

Input Types

For complex mutations, use input types to group related fields:

Mutation Context

Mutations have access to the same context object as queries:

Authorization

Implement authorization checks in your mutation resolvers:

Validation

Validate input data before processing:

Database Integration

Integrate mutations with database operations:

Best Practices

Use Descriptive Names

Name your mutations clearly: createProduct, updateUser, deleteOrder

Return Created Objects

Return the full object after creation/update for immediate use

Validate Input

Always validate input data before processing mutations

Handle Errors

Provide clear, actionable error messages

Use Transactions

Wrap related database operations in transactions

Check Authorization

Verify user permissions before allowing mutations

Error Handling

Mutations should provide clear error messages:

Testing Mutations

Test your mutations thoroughly:

Next Steps

GraphQL Queries

Learn about querying data with GraphQL

GraphQL Overview

Review GraphQL architecture and concepts