0Pricing
GraphQL APIs with Spring Boot · Lesson

GraphQL Mutations: Changing Data

Move beyond reading data: learn to define and implement GraphQL mutations to create, update, and delete data in Spring Boot.

GraphQL Mutations: Changing Data is a free GraphQL APIs with Spring Boot lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the GraphQL APIs with Spring Boot learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

From Reading to Writing

You can now define types, resolvers, and run queries. But real APIs must also change data.

In GraphQL, every write goes through a mutation — the counterpart to a query.

Query vs Mutation

The shapes look similar, but intent differs:

  • Query: read data, no side effects, may run in parallel.
  • Mutation: change data, executed sequentially top to bottom.

Declaring a Mutation in the Schema

Mutations live under the special Mutation type and usually return the affected object.

type Mutation {
  createBook(title: String!, author: String!): Book
}

Input Types for Complex Arguments

For many fields, group arguments into an input type instead of a long argument list.

input BookInput {
  title: String!
  author: String!
}

type Mutation {
  createBook(book: BookInput!): Book
}

Implementing the Resolver

In Spring Boot, annotate the handler with @MutationMapping.

@Controller
class BookMutation {
    @MutationMapping
    Book createBook(@Argument String title, @Argument String author) {
        return new Book(title, author);
    }
}

Returning the Result

Returning the created or updated object lets the client immediately read back any fields it needs.

mutation {
  createBook(title: "Clean Code", author: "Martin") {
    id
    title
  }
}

Update and Delete Mutations

The same pattern covers all writes.

type Mutation {
  updateBook(id: ID!, book: BookInput!): Book
  deleteBook(id: ID!): Boolean
}

A Self-Contained Logic Example

The resolver logic is plain Java; here is the core idea as a runnable snippet.

import java.util.*;
public class Main {
  static Map<Integer,String> store = new HashMap<>();
  static int seq = 0;
  static int createBook(String title){ int id = ++seq; store.put(id, title); return id; }
  public static void main(String[] a){
    int id = createBook("Clean Code");
    System.out.println("Created id=" + id + " title=" + store.get(id));
  }
}

Validating Input

Mutations are the right place to enforce rules: reject empty titles, check permissions, ensure referenced records exist.

Return clear errors so clients can react meaningfully.

Sequential Execution Matters

Unlike query fields, top-level mutation fields run one after another.

This guarantees that if a single request contains several mutations, earlier ones complete before later ones begin — important for consistency.

Best Practices

  • Use input types for multi-field arguments.
  • Return the affected object so clients can re-read fields.
  • Validate and authorize inside resolvers.
  • Name mutations as verbs: createBook, updateBook, deleteBook.

Quick Check

Test your understanding of GraphQL mutations.

Recap

You learned to change data with GraphQL mutations.

  • Define them under the Mutation type, often with input types.
  • Implement with @MutationMapping in Spring Boot.
  • They run sequentially and should validate and return the affected object.

Frequently asked questions

Is the “GraphQL Mutations: Changing Data” lesson free?

Yes — the full text of “GraphQL Mutations: Changing Data” is free to read here on the web, and the GraphQL APIs with Spring Boot course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the GraphQL APIs with Spring Boot course, upgrade to CoddyKit PRO.

What will I learn in “GraphQL Mutations: Changing Data”?

Move beyond reading data: learn to define and implement GraphQL mutations to create, update, and delete data in Spring Boot. You practise GraphQL APIs with Spring Boot with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start GraphQL APIs with Spring Boot?

No prior experience is required. GraphQL APIs with Spring Boot on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “GraphQL Mutations: Changing Data” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this GraphQL APIs with Spring Boot lesson?

Yes. Every GraphQL APIs with Spring Boot lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining Custom Data Types
  2. Implementing Data Resolvers
  3. Executing Simple GraphQL Queries
  4. GraphQL Mutations: Changing Data
← Back to GraphQL APIs with Spring Boot