GraphQL APIs with Spring Boot · บทเรียน

มิวเทชัน GraphQL: การเปลี่ยนแปลงข้อมูล

ก้าวข้ามการอ่านข้อมูล โดยเรียนรู้การกำหนดและใช้งานมิวเทชัน GraphQL เพื่อสร้าง อัปเดต และลบข้อมูลใน Spring Boot

บทเรียน 4 จาก 413 ขั้นตอน

มิวเทชัน GraphQL: การเปลี่ยนแปลงข้อมูล เป็นบทเรียน GraphQL APIs with Spring Boot ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน GraphQL APIs with Spring Boot และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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.
เริ่มต้นได้ฟรี

เรียนรู้ GraphQL APIs with Spring Boot ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “มิวเทชัน GraphQL: การเปลี่ยนแปลงข้อมูล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “มิวเทชัน GraphQL: การเปลี่ยนแปลงข้อมูล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส GraphQL APIs with Spring Boot ให้อัปเกรดเป็น CoddyKit PRO คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “มิวเทชัน GraphQL: การเปลี่ยนแปลงข้อมูล”

ก้าวข้ามการอ่านข้อมูล โดยเรียนรู้การกำหนดและใช้งานมิวเทชัน GraphQL เพื่อสร้าง อัปเดต และลบข้อมูลใน Spring Boot คุณปฏิบัติ GraphQL APIs with Spring Boot ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน GraphQL APIs with Spring Boot หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน GraphQL APIs with Spring Boot บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “มิวเทชัน GraphQL: การเปลี่ยนแปลงข้อมูล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน GraphQL APIs with Spring Boot นี้ได้ไหม

ได้ บทเรียน GraphQL APIs with Spring Boot ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การกำหนดชนิดข้อมูลแบบกำหนดเอง
  2. การนำตัวแก้ข้อมูลมาใช้
  3. การเรียกใช้คิวรี GraphQL อย่างง่าย
  4. มิวเทชัน GraphQL: การเปลี่ยนแปลงข้อมูล
← กลับไปที่ GraphQL APIs with Spring Boot