การตรวจสอบอาร์กิวเมนต์อินพุตของมิวเทชัน
เรียนรู้การตรวจสอบอาร์กิวเมนต์มิวเทชันขาเข้าใน API GraphQL ของ Spring Boot โดยใช้ Bean Validation เพื่อให้มีเพียงข้อมูลที่สะอาดและอยู่ในรูปแบบถูกต้องเท่านั้นที่ไปถึงชั้นข้อมูล
การตรวจสอบอาร์กิวเมนต์อินพุตของมิวเทชัน เป็นบทเรียน GraphQL APIs with Spring Boot ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน GraphQL APIs with Spring Boot และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Validate Mutation Input?
Mutations change your data, so the arguments they receive must be trustworthy. Without validation, clients could send empty names, negative prices, or malformed emails straight into your database.
Input validation rejects bad data early and returns a clear error before any persistence happens.
Validation in the GraphQL Lifecycle
GraphQL already validates the shape of your input against the schema (types, required fields). But it does not check business rules like 'price must be positive'.
That semantic layer is your responsibility, and Spring's Bean Validation fits perfectly here.
Bean Validation Annotations
Spring Boot ships with Jakarta Bean Validation. Common annotations:
@NotNull,@NotBlankfor required values@Sizefor length bounds@Min,@Max,@Positivefor numbers@Emailfor email format
Annotating an Input Class
Map your GraphQL input type to a Java record or class and add constraints to its fields.
public record CreateBookInput(
@NotBlank String title,
@Size(min = 2, max = 60) String author,
@Positive double price
) {}Triggering Validation in a Resolver
Add @Valid to the argument in your mutation method. Spring then checks every constraint before the method body runs.
@MutationMapping
public Book createBook(@Argument @Valid CreateBookInput input) {
return bookService.create(input);
}What Happens on Failure
If a constraint fails, Spring throws a ConstraintViolationException before your service code runs. The mutation does not persist anything, protecting your data layer from invalid state.
Custom Error Messages
Each annotation accepts a message attribute so clients see helpful feedback instead of a generic failure.
@NotBlank(message = "Title is required")
String title;Mapping Violations to GraphQL Errors
Use a DataFetcherExceptionResolver to turn validation exceptions into clean GraphQL errors with field-level detail, so clients know exactly which input was wrong.
@Override
public GraphQLError resolveToSingleError(Throwable ex, DataFetchingEnvironment env) {
return GraphqlErrorBuilder.newError(env)
.errorType(ErrorType.BAD_REQUEST)
.message(ex.getMessage())
.build();
}Custom Validators
For rules the built-in annotations cannot express (like 'ISBN must be unique'), write a custom constraint implementing ConstraintValidator and annotate fields with it.
public class IsbnValidator implements ConstraintValidator<ValidIsbn, String> {
public boolean isValid(String value, ConstraintValidatorContext ctx) {
return value != null && value.matches("\\d{13}");
}
}Validating Nested Inputs
When an input contains another object, mark the nested field with @Valid too. Validation then cascades into the child object.
public record OrderInput(
@NotNull @Valid AddressInput shippingAddress,
@Positive int quantity
) {}Best Practices
Keep validation reliable:
- Validate at the boundary, before any business logic
- Return field-specific messages clients can act on
- Reuse input classes across related mutations
- Keep custom validators stateless and fast
Quick Check
Test your understanding of mutation validation.
Recap
You learned to validate mutation input:
- GraphQL checks shape; you enforce business rules
- Annotate input classes with Bean Validation constraints
- Use
@Validin resolvers to trigger checks - Map violations to clean GraphQL errors
- Write custom validators and cascade with nested
@Valid
Validated mutations keep your data layer safe and clean.
เรียนรู้ GraphQL APIs with Spring Boot ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การตรวจสอบอาร์กิวเมนต์อินพุตของมิวเทชัน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การตรวจสอบอาร์กิวเมนต์อินพุตของมิวเทชัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส GraphQL APIs with Spring Boot ให้อัปเกรดเป็น CoddyKit PRO คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การตรวจสอบอาร์กิวเมนต์อินพุตของมิวเทชัน”
เรียนรู้การตรวจสอบอาร์กิวเมนต์มิวเทชันขาเข้าใน API GraphQL ของ Spring Boot โดยใช้ Bean Validation เพื่อให้มีเพียงข้อมูลที่สะอาดและอยู่ในรูปแบบถูกต้องเท่านั้นที่ไปถึงชั้นข้อมูล คุณปฏิบัติ GraphQL APIs with Spring Boot ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน GraphQL APIs with Spring Boot หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน GraphQL APIs with Spring Boot บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การตรวจสอบอาร์กิวเมนต์อินพุตของมิวเทชัน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน GraphQL APIs with Spring Boot นี้ได้ไหม
ได้ บทเรียน GraphQL APIs with Spring Boot ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจมิวเทชัน GraphQL
- การสร้างข้อมูลด้วยมิวเทชัน
- การอัปเดตและลบข้อมูล
- การตรวจสอบอาร์กิวเมนต์อินพุตของมิวเทชัน