0
0
GraphqlHow-ToBeginner · 4 min read

How to Use Nested Mutation in GraphQL for Database Operations

In GraphQL, nested mutation lets you create or update related records in one request by nesting mutation fields inside each other. This means you can add or modify connected data, like creating a user and their posts together, using a single mutation call.
📐

Syntax

A nested mutation in GraphQL involves including mutation fields inside other mutation fields to handle related data in one operation.

Key parts:

  • Parent mutation field: The main operation like createUser.
  • Nested mutation field: Inside the parent, fields like createPosts to add related data.
  • Input objects: Provide data for both parent and nested fields.
graphql
mutation {
  createUser(data: {
    name: "Alice",
    posts: {
      create: [
        { title: "First Post" },
        { title: "Second Post" }
      ]
    }
  }) {
    id
    name
    posts {
      id
      title
    }
  }
}
💻

Example

This example shows how to create a user and two posts linked to that user in one mutation using nested mutation syntax.

graphql
mutation {
  createUser(data: {
    name: "Alice",
    posts: {
      create: [
        { title: "First Post" },
        { title: "Second Post" }
      ]
    }
  }) {
    id
    name
    posts {
      id
      title
    }
  }
}
Output
{ "data": { "createUser": { "id": "1", "name": "Alice", "posts": [ { "id": "101", "title": "First Post" }, { "id": "102", "title": "Second Post" } ] } } }
⚠️

Common Pitfalls

Common mistakes when using nested mutations include:

  • Not matching the nested field names exactly as defined in the schema.
  • Forgetting to use the correct nested operation keywords like create, connect, or update.
  • Trying to nest mutations on fields that do not support nested operations.
  • Missing required fields in nested input objects.

Example of a wrong nested mutation and the corrected version:

graphql
mutation {
  createUser(data: {
    name: "Bob",
    posts: {
      create: { title: "Single Post" }  # Wrong: should be an array
    }
  }) {
    id
  }
}

# Corrected:
mutation {
  createUser(data: {
    name: "Bob",
    posts: {
      create: [ { title: "Single Post" } ]
    }
  }) {
    id
  }
}
📊

Quick Reference

Nested Mutation KeywordPurposeExample Usage
createCreate new related recordsposts: { create: [{ title: "Post" }] }
connectLink existing records by IDposts: { connect: [{ id: "123" }] }
updateUpdate existing related recordsposts: { update: { where: { id: "123" }, data: { title: "New" } } }
deleteRemove related recordsposts: { delete: [{ id: "123" }] }

Key Takeaways

Nested mutations let you create or update related data in one GraphQL request.
Use keywords like create, connect, update, and delete inside nested fields.
Always check your schema for supported nested mutation fields and input types.
Provide arrays for multiple nested creations, even if creating one item.
Carefully match field names and input structure to avoid errors.