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

การอัปเดตและลบข้อมูล

พัฒนามิวเทชันสำหรับแก้ไขข้อมูลเดิมและลบระเบียนออกจากระบบ

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

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

Changing Your Data

So far, you've learned to fetch data with queries and add new data with mutations. But what about modifying or removing existing records?

In this lesson, we'll dive into how GraphQL mutations allow you to update and delete data, making your applications fully dynamic.

GraphQL Update Mutation (SDL)

An update mutation modifies an existing record. It usually requires an identifier (like an ID) to specify which record to change, and then the fields you want to update.

Here's how you might define an updateBook mutation in your GraphQL schema (SDL):

type Mutation {
updateBook(id: ID!, title: String, author: String, year: Int): Book
}

Designing Update Input Types

For cleaner mutations, especially with many fields, it's good practice to use an Input Type. This bundles all arguments into a single object.

An Input Type is similar to a regular type but uses the input keyword.

input UpdateBookInput {
id: ID!
title: String
author: String
year: Int
}

type Mutation {
updateBook(input: UpdateBookInput!): Book
}

Spring Boot: Update Resolver

In your Spring Boot application, you'll create a Java method that acts as the resolver for the updateBook mutation.

This method will take the necessary arguments (like the book's ID and new details) and perform the update logic, often interacting with a database or service.

public Book updateBook(String id, String title, String author, Integer year) {
// ... logic to find and update the book ...
return updatedBook;
}

Implementing Update Logic

Let's see a simple Java example of how an update might work. We'll use an in-memory list to simulate a data store.

Notice how we find the book by ID and then modify its properties.

public class Main {
  static class Book {
    String id;
    String title;
    public Book(String id, String title) {
      this.id = id;
      this.title = title;
    }
    public String getId() { return id; }
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
    @Override
    public String toString() { return "Book[id=" + id + ", title='" + title + "']"; }
  }

  private static java.util.List<Book> books = new java.util.ArrayList<>();

  public static void main(String[] args) {
    books.add(new Book("1", "Original Title"));
    books.add(new Book("2", "Another Book"));

    System.out.println("Before update:");
    books.forEach(System.out::println);

    // Simulate update logic
    String targetId = "1";
    String newTitle = "Updated Title";
    
    books.stream()
         .filter(b -> b.getId().equals(targetId))
         .findFirst()
         .ifPresent(book -> book.setTitle(newTitle));

    System.out.println("\nAfter update:");
    books.forEach(System.out::println);
  }
}

GraphQL Delete Mutation (SDL)

Deleting data is simpler. You typically only need to provide the unique ID of the record you wish to remove.

The delete mutation usually returns the ID of the deleted item, or a boolean indicating success.

type Mutation {
deleteBook(id: ID!): ID
}

Spring Boot: Delete Resolver

Similar to updating, a delete mutation in Spring Boot will have a corresponding Java method.

This method will receive the ID of the item to be deleted and perform the removal operation.

public String deleteBook(String id) {
// ... logic to find and delete the book ...
return id;
}

Implementing Delete Logic

Here's a basic Java example of a delete operation. We'll remove a book from our in-memory list based on its ID.

The removeIf method is a concise way to delete elements that match a condition.

public class Main {
  static class Book {
    String id;
    String title;
    public Book(String id, String title) {
      this.id = id;
      this.title = title;
    }
    public String getId() { return id; }
    public String getTitle() { return title; }
    @Override
    public String toString() { return "Book[id=" + id + ", title='" + title + "']"; }
  }

  private static java.util.List<Book> books = new java.util.ArrayList<>();

  public static void main(String[] args) {
    books.add(new Book("1", "Book to Delete"));
    books.add(new Book("2", "Another Book"));

    System.out.println("Before delete:");
    books.forEach(System.out::println);

    // Simulate delete logic
    String targetId = "1";
    books.removeIf(book -> book.getId().equals(targetId));

    System.out.println("\nAfter delete:");
    books.forEach(System.out::println);
  }
}

Handling Missing Records

What happens if you try to update or delete a record that doesn't exist?

  • Your resolver should check if the ID exists.
  • If not found, it can return null or throw a specific GraphQL error (more on error handling in a later course!).
  • This prevents unexpected behavior and informs the client.

Quick Check: Mutation Operations

You've learned about different types of GraphQL operations. Let's test your understanding!

Recap: Mastered Data Changes!

Congratulations! You've learned how to implement powerful data modification features:

  • Update Mutations: Used to modify existing data records, identified by a unique ID.
  • Delete Mutations: Used to remove data records from your system, also identified by a unique ID.
  • Spring Boot Resolvers: How to map these mutations to Java methods for backend logic.

With queries, create, update, and delete mutations, you now have a full set of tools to build dynamic GraphQL APIs!

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

บทเรียน “การอัปเดตและลบข้อมูล” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การอัปเดตและลบข้อมูล”

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

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

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

บทเรียน “การอัปเดตและลบข้อมูล” ใช้เวลานานแค่ไหน

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

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

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

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

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