การสร้างคำสั่งกำกับแบบกำหนดเอง
สร้างคำสั่งกำกับสคีมาของคุณเองเพื่อเพิ่มตรรกะ การตรวจสอบความถูกต้อง หรือการแปลงข้อมูลให้กับฟิลด์ GraphQL
การสร้างคำสั่งกำกับแบบกำหนดเอง เป็นบทเรียน GraphQL APIs with Spring Boot ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน GraphQL APIs with Spring Boot และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Directives: Extending GraphQL
GraphQL directives are powerful tools that allow you to add custom behavior or metadata to your schema. You might already know built-in directives like @deprecated or @skip.
Custom directives let you define your own, extending GraphQL's capabilities to fit your unique application needs.
Power of Custom Directives
Custom directives offer several benefits:
- Reusability: Apply the same logic across multiple fields or types without repeating code.
- Separation of Concerns: Keep business logic separate from schema definitions.
- Cross-Cutting Concerns: Easily add features like logging, authorization, or validation to many parts of your API.
Declaring Your Directive
You define a custom directive using the directive keyword in your Schema Definition Language (SDL).
It specifies the directive's name, arguments (optional), and the locations where it can be applied.
directive @log(message: String = "Access") on FIELD_DEFINITION | FIELDUnderstanding Directive Locations
The on keyword specifies where your directive can be used. Common locations include:
FIELD_DEFINITION: On a field within a type (e.g.,User.name).FIELD: On a field in a query document (client-side).ARGUMENT_DEFINITION: On an argument of a field or input field.OBJECT: On an object type.
Implementing Directive Logic
To make your custom directive do something, you need to "wire" it to your Spring Boot application. This involves telling graphql-java how to handle the directive when it encounters it in the schema.
We'll use SchemaDirectiveWiring to intercept and modify field resolution.
Example: The @log Directive
Let's create a @log directive that prints a message whenever a field it's applied to is resolved. This is a great way to see directives in action.
First, our schema definition:
type Query {
hello: String @log(message: "Hello field accessed")
goodbye: String
}
directive @log(message: String = "Default Log") on FIELD_DEFINITIONWiring the @log Directive (Part 1)
In Spring Boot, you implement SchemaDirectiveWiring and register it. This class will contain the logic to execute when the directive is found.
We'll focus on onField to intercept field resolution.
package com.coddykit.directives;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetcherFactories;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.idl.SchemaDirectiveWiring;
import graphql.schema.idl.SchemaDirectiveWiringEnvironment;
import org.springframework.stereotype.Component;
@Component
public class LogDirective implements SchemaDirectiveWiring {
@Override
public GraphQLFieldDefinition onField(
SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> environment) {
GraphQLFieldDefinition field = environment.getElement();
String message = (String) environment.getDirective()
.getArgument("message")
.getValue();
DataFetcher originalDataFetcher = environment.getFieldDataFetcher();
DataFetcher newDataFetcher = DataFetcherFactories
.wrapDataFetcher(originalDataFetcher, (dataFetchingEnvironment, value) -> {
System.out.println("LOG: " + message + " for field '" + field.getName() + "'");
return value;
});
environment.getFieldAndContainer().setDataFetcher(newDataFetcher);
return field;
}
}Wiring the @log Directive (Part 2)
To ensure our LogDirective is picked up, we need a main application and a resolver. Here's how it all fits together, including a simple data fetcher for our hello field.
Notice how RuntimeWiringConfigurer is used to register directive wirings.
package com.coddykit;
import com.coddykit.directives.LogDirective;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.graphql.execution.RuntimeWiringConfigurer;
import org.springframework.stereotype.Controller;
import org.springframework.graphql.data.method.annotation.QueryMapping;
@SpringBootApplication
@Controller
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@QueryMapping
public String hello() {
return "Hello from CoddyKit!";
}
// This bean registers our custom directive wiring
@Bean
public RuntimeWiringConfigurer runtimeWiringConfigurer(LogDirective logDirective) {
return builder -> builder.directive("log", logDirective);
}
}
// For this application to run, the LogDirective class (from Scene 7)
// must be present in 'com.coddykit.directives' package.Testing the Directive
With the application running, send a GraphQL query like this:
When you query the hello field, you'll see "LOG: Hello field accessed for field 'hello'" printed in your server console, demonstrating our custom directive in action!
query {
hello
}Quick Check: Directive Power
You've seen how custom directives add logic to your GraphQL schema. Which of the following best describes a primary benefit of using custom directives?
Recap: Building Directives
In this lesson, you learned to define and implement custom GraphQL directives in a Spring Boot application. We covered:
- Declaring directives in SDL with
on LOCATION. - Implementing directive logic using
SchemaDirectiveWiring. - Applying a directive to modify field behavior, like our
@logexample.
Custom directives are powerful for adding reusable, cross-cutting concerns to your API.
คำถามที่พบบ่อย
บทเรียน “การสร้างคำสั่งกำกับแบบกำหนดเอง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสร้างคำสั่งกำกับแบบกำหนดเอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส GraphQL APIs with Spring Boot ให้อัปเกรดเป็น CoddyKit PRO คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสร้างคำสั่งกำกับแบบกำหนดเอง”
สร้างคำสั่งกำกับสคีมาของคุณเองเพื่อเพิ่มตรรกะ การตรวจสอบความถูกต้อง หรือการแปลงข้อมูลให้กับฟิลด์ GraphQL คุณปฏิบัติ GraphQL APIs with Spring Boot ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน GraphQL APIs with Spring Boot หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน GraphQL APIs with Spring Boot บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การสร้างคำสั่งกำกับแบบกำหนดเอง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน GraphQL APIs with Spring Boot นี้ได้ไหม
ได้ บทเรียน GraphQL APIs with Spring Boot ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างคำสั่งกำกับแบบกำหนดเอง
- พื้นฐานการต่อสคีมา
- การรวมสคีมา GraphQL หลายรายการ
- การแยกส่วนสคีมาด้วยส่วนขยายชนิดข้อมูล