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

การนำการรวมชุดคำขอและการแคชมาใช้

ผสาน DataLoaders เข้ากับตัวแก้ข้อมูลของ 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 บทเรียน

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

DataLoaders: Time to Implement!

Welcome back! In previous lessons, we learned about the N+1 problem and how DataLoaders provide an elegant solution through batching and caching.

Today, we'll get hands-on and integrate DataLoaders into a Spring Boot GraphQL application. We'll see how to define a BatchLoader, register it, and use it in our GraphQL resolvers.

Simulating a Data Service

First, let's set up a simple mock service that simulates fetching users from a database. This service will be called by our DataLoaders.

Notice the System.out.println, which will help us observe when the actual 'database' call happens, demonstrating batching later.

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// A simple User data class
class User {
  String id;
  String name;
  User(String id, String name) { 
    this.id = id; this.name = name; 
  }
  public String getId() { return id; }
  public String getName() { return name; }
}

// Mock service to fetch users (simulates DB call)
class MockUserService {
  private final Map<String, User> users = Map.of(
    "1", new User("1", "Alice"),
    "2", new User("2", "Bob"),
    "3", new User("3", "Charlie"),
    "4", new User("4", "David")
  );

  public List<User> findAllByIds(List<String> ids) {
    System.out.println("DB Call: Fetching users for IDs: " + ids);
    return ids.stream()
              .map(users::get)
              .filter(user -> user != null)
              .collect(Collectors.toList());
  }
}

public class Main {
  public static void main(String[] args) {
    MockUserService service = new MockUserService();
    List<User> foundUsers = service.findAllByIds(List.of("1", "3"));
    System.out.println("Found: " + foundUsers.size() + " users.");
  }
}

The BatchLoader Interface

The core of DataLoader's batching mechanism is the BatchLoader interface. It defines a single method: load(List<K> keys).

  • It takes a list of keys (e.g., user IDs).
  • It returns a CompletionStage (a future-like object) of a list of values (e.g., users).
  • Crucially, this method is called once for all keys requested in a short window, allowing you to fetch them in a single optimized operation (like a single database query).

Implementing Your BatchLoader

Now, let's create our UserBatchLoader. It will implement the BatchLoader<String, User> interface, meaning it takes a String (user ID) as a key and returns a User object.

It uses our MockUserService to perform the actual batch fetch.

import org.dataloader.BatchLoader;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

// (Assume User and MockUserService are defined elsewhere)
// For runnable example, they would be in the same file.

public class UserBatchLoader implements BatchLoader<String, User> {
  private final MockUserService userService;

  public UserBatchLoader(MockUserService userService) {
    this.userService = userService;
  }

  @Override
  public CompletionStage<List<User>> load(List<String> userIds) {
    // This method is called once with all requested IDs.
    // We delegate to our mock service to fetch them in one go.
    return CompletableFuture.supplyAsync(
      () -> userService.findAllByIds(userIds)
    );
  }
}

public class Main {
  public static void main(String[] args) {
    MockUserService userService = new MockUserService();
    UserBatchLoader batchLoader = new UserBatchLoader(userService);
    System.out.println("UserBatchLoader created!");
  }
}

The DataLoaderRegistry

In a Spring Boot GraphQL application, DataLoader instances are managed within a DataLoaderRegistry. This registry acts as a container for all DataLoaders available during a GraphQL request execution.

  • Each DataLoader is registered with a unique name (e.g., "userDataLoader").
  • Resolvers retrieve the necessary DataLoader from this registry using its name.
  • Spring for GraphQL typically handles the lifecycle and scope of this registry per request.

Setting up DataLoaders

Here's how you create a DataLoader instance from your BatchLoader and register it in a DataLoaderRegistry. This setup is crucial for making DataLoaders accessible to your resolvers.

import org.dataloader.DataLoader;
import org.dataloader.DataLoaderRegistry;

// (Assume User, MockUserService, UserBatchLoader are defined)

public class Main {
  public static void main(String[] args) {
    MockUserService userService = new MockUserService();
    UserBatchLoader userBatchLoader = new UserBatchLoader(userService);

    // Create a DataLoader instance
    DataLoader<String, User> userDataLoader =
      DataLoader.newDataLoader(userBatchLoader);

    // Register it in a DataLoaderRegistry
    DataLoaderRegistry registry = new DataLoaderRegistry();
    registry.register("userDataLoader", userDataLoader);

    System.out.println("DataLoader registered as 'userDataLoader'!");
    // In a real app, this registry would be part of the GraphQLContext.
    // Resolvers would then retrieve DataLoaders from it.
  }
}

Using DataLoader in Resolvers

Once your DataLoader is registered, you can use it within your GraphQL resolvers. Instead of directly calling your service, you'll call dataLoader.load(id).

  • The load() method returns a CompletionStage, not the direct object.
  • The DataLoader collects all load() calls within the current execution frame before dispatching them to the BatchLoader.
  • Spring for GraphQL automatically dispatches the DataLoaders at the end of a data fetching cycle.

Resolver Integration in Action

This example simulates a GraphQL query where multiple user IDs are requested. Watch the "DB Call" message in the output to see how DataLoaders batch these requests into a single operation, even if they appear separate in the resolver logic.

import org.dataloader.DataLoader;
import org.dataloader.DataLoaderRegistry;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;

// User, MockUserService, UserBatchLoader definitions (from previous scenes)
class User {
  String id; String name;
  User(String id, String name) { this.id = id; this.name = name; }
  public String getId() { return id; }
  public String getName() { return name; }
}

class MockUserService {
  private final Map<String, User> users = Map.of(
    "1", new User("1", "Alice"), "2", new User("2", "Bob"),
    "3", new User("3", "Charlie"), "4", new User("4", "David")
  );
  public List<User> findAllByIds(List<String> ids) {
    System.out.println("DB Call: Fetching users for IDs: " + ids);
    return ids.stream().map(users::get)
              .filter(user -> user != null).collect(Collectors.toList());
  }
}

class UserBatchLoader implements BatchLoader<String, User> {
  private final MockUserService userService;
  public UserBatchLoader(MockUserService userService) { this.userService = userService; }
  @Override
  public CompletionStage<List<User>> load(List<String> userIds) {
    return CompletableFuture.supplyAsync(() -> userService.findAllByIds(userIds));
  }
}

// Simulate a GraphQL Query Resolver method
public class UserResolver {
  private final DataLoaderRegistry dataLoaderRegistry;

  public UserResolver(DataLoaderRegistry registry) {
    this.dataLoaderRegistry = registry;
  }

  public CompletionStage<User> getUserById(String id) {
    DataLoader<String, User> userDataLoader =
      dataLoaderRegistry.getDataLoader("userDataLoader");
    return userDataLoader.load(id);
  }

  public static void main(String[] args) {
    MockUserService userService = new MockUserService();
    UserBatchLoader userBatchLoader = new UserBatchLoader(userService);
    DataLoader<String, User> userDataLoader =
      DataLoader.newDataLoader(userBatchLoader);

    DataLoaderRegistry registry = new DataLoaderRegistry();
    registry.register("userDataLoader", userDataLoader);

    UserResolver resolver = new UserResolver(registry);

    System.out.println("Requesting User 1 and User 2 concurrently...");

    // Simulate two concurrent GraphQL field fetches
    CompletableFuture<User> user1Future =
      (CompletableFuture<User>) resolver.getUserById("1");
    CompletableFuture<User> user2Future =
      (CompletableFuture<User>) resolver.getUserById("2");

    CompletableFuture.allOf(user1Future, user2Future)
      .thenRun(() -> {
        try {
          System.out.println("Fetched User 1: " + user1Future.join().getName());
          System.out.println("Fetched User 2: " + user2Future.join().getName());
        } catch (Exception e) {
          e.printStackTrace();
        }
        // For this manual demo, we dispatch the DataLoader.
        // In Spring GraphQL, this is handled automatically.
        userDataLoader.dispatch();
      }).join();
  }
}

DataLoader's Built-in Caching

Beyond batching, DataLoaders also provide request-scoped caching out-of-the-box. This means:

  • If dataLoader.load("1") is called multiple times within the same GraphQL request, the BatchLoader's fetch function for ID "1" will only be invoked once.
  • Subsequent calls for the same ID within that request will return the already fetched (cached) result.
  • This prevents redundant database calls for the same data within a single GraphQL operation, further boosting performance.

Check Your Understanding

You've learned how to implement DataLoaders for batching and caching. Let's test your knowledge!

Recap: Batching & Caching

Great job! You've successfully learned how to implement DataLoaders in Spring Boot GraphQL.

  • We created a MockUserService to simulate data fetching.
  • We built a UserBatchLoader to handle batching multiple ID requests.
  • We saw how to initialize and register DataLoader instances in a DataLoaderRegistry.
  • Finally, we integrated DataLoaders into a resolver, observing how requests are batched and how caching prevents redundant calls.

By using DataLoaders, you significantly optimize your GraphQL API's performance, especially when dealing with complex data graphs and nested objects.

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

บทเรียน “การนำการรวมชุดคำขอและการแคชมาใช้” ฟรีหรือไม่

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

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

ผสาน DataLoaders เข้ากับตัวแก้ข้อมูลของ Spring Boot เพื่อเพิ่มประสิทธิภาพการดึงข้อมูลและลดการเดินทางไปกลับของฐานข้อมูล คุณปฏิบัติ 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. อธิบายปัญหา N+1
  2. แนะนำ GraphQL DataLoaders
  3. การนำการรวมชุดคำขอและการแคชมาใช้
  4. DataLoaders พร้อมบริบท Spring และการทำงานแบบอะซิงโครนัส
← กลับไปที่ GraphQL APIs with Spring Boot