การใช้ Nested Mutations
Nested mutations ช่วยให้สามารถดำเนินการ mutation บน type อื่นที่ไม่ใช่ root type ใน GraphQL ได้
Query ด้านล่างนี้ดำเนินการ mutation แบบมาตรฐาน โดยใช้ mutation field updatePost จาก root type:
mutation {
updatePost(input: {
id: 5,
title: "New title"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
title
}
}
}Query ข้างต้นสามารถดำเนินการผ่าน nested mutation ได้เช่นกัน โดย query post object ผ่าน field post ก่อน จากนั้นจึงนำ mutation field update ซึ่งเป็นของ type Post มาใช้กับ post object นั้น:
mutation {
post(by: {id: 5}) {
update(input: {
title: "New title"
}) {
status
post {
title
}
}
}
}Mutations ยังสามารถซ้อนกันได้ โดยแก้ไขข้อมูลบนผลลัพธ์จาก mutation อื่น:
mutation {
createPost(input: {
title: "First title"
}) {
status
postID
post {
update(input: {
title: "Second title",
contentAs: { html: "Some content" }
}) {
status
post {
title
content
addComment(input: {
commentAs: { html: "My first comment" }
}) {
status
commentID
comment {
content
date
}
}
}
}
}
}
}Root type แบบย่อ
Nested mutations เปลี่ยน root type จาก QueryRoot และ MutationRoot ให้เป็น type Root เดียวที่จัดการทั้ง queries และ mutations:

การแสดงผล mutation fields
ใช้ Voyager client เพื่อแสดงผลว่า field ใดบ้างที่เป็น mutation fields
เมื่อใช้ nested mutations ทุก type ใน schema สามารถมีทั้ง query fields และ mutation fields ได้ เพื่อแยกความแตกต่าง คำอธิบายของ mutation field จะมีป้ายกำกับ "[Mutation] " นำหน้า
ตัวอย่างเช่น นี่คือ fields สำหรับ type Root:

การใช้ Nested Mutations ใน Endpoints
มี 2 ระดับที่สามารถกำหนดได้ว่า schema จะใช้ nested mutations หรือไม่ เรียงตามลำดับความสำคัญ:
1. บน Schema Configuration
การกำหนดให้ custom endpoint หรือ persisted query ใช้ nested mutations สามารถกำหนดได้ผ่าน schema configuration ที่เกี่ยวข้อง:

2. โหมดเริ่มต้น กำหนดใน Settings
หาก schema configuration มีค่าเป็น "Default" ระบบจะใช้โหมดที่กำหนดใน Settings:

การกำหนดค่า Nested Mutations
มีพฤติกรรมสามแบบที่สามารถเลือกได้สำหรับ schema:
1. ไม่เปิดใช้งาน Nested Mutations
ตัวเลือกนี้ปิดใช้งาน nested mutations (ใช้พฤติกรรมมาตรฐานแทน) สำหรับ schema
2. เปิดใช้งาน Nested Mutations โดยเก็บ Mutation Fields ทั้งหมดไว้ใน Root
เมื่อ nested mutations เปิดใช้งาน mutation fields อาจถูกเพิ่มสองครั้งใน schema:
- ครั้งหนึ่งภายใต้ type
Root - ครั้งหนึ่งภายใต้ type เฉพาะ
ตัวอย่างเช่น:
Root.updatePostPost.update
เมื่อใช้ตัวเลือกนี้ mutation fields ที่ "ซ้ำกัน" จาก root type จะถูกเก็บไว้
3. เปิดใช้งาน Nested Mutations โดยลบ Mutation Fields ที่ซ้ำซ้อนออกจาก Root
ตัวเลือกเดียวกับข้างต้น แต่ลบ mutation fields ที่ "ซ้ำกัน" ออกจาก root type
ตัวอย่างเช่น:
Root.updatePostถูกลบออกPost.updateพร้อมใช้งาน
GraphQL Spec
ฟีเจอร์นี้ยังไม่ได้เป็นส่วนหนึ่งของ GraphQL spec ในปัจจุบัน แต่มีการร้องขอไว้แล้ว: