🎉 เปิดตัว Gato GraphQL v0.7 — รองรับ mutations และ nested mutations แล้ว!
เวอร์ชัน 0.7 ของ Gato GraphQL ได้รับการเปิดตัวแล้ว รองรับ mutations และ nested mutations! 🎉

นี่คือทัวร์แสดงฟีเจอร์ใหม่ที่เพิ่มเข้ามา
1. Mutations! 🚀
GraphQL mutations ช่วยให้สามารถแก้ไขข้อมูล (กล่าวคือ ดำเนินการผลข้างเคียง) ผ่าน query ได้
Mutation คือสิ่งสำคัญที่ยังขาดอยู่ใน Gato GraphQL จนถึงตอนนี้ เมื่อเพิ่มเข้ามาแล้ว ฉันสามารถกล่าวได้ว่า GraphQL server นี้ครบถ้วนสมบูรณ์เกือบทั้งหมดแล้ว (เหลือเพียง subscriptions เท่านั้น และฉันกำลังคิดถึงวิธีการเพิ่มอยู่แล้ว)

มาดูตัวอย่างการเพิ่มความคิดเห็นกัน แต่ก่อนอื่น เราต้องรัน mutation อีกตัวหนึ่งเพื่อล็อกอิน เพื่อให้คุณสามารถเพิ่มความคิดเห็นได้ กดปุ่ม "Run" บน GraphiQL client ด้านล่าง เพื่อรัน mutation field loginUser ด้วยผู้ใช้ทดสอบที่สร้างไว้ล่วงหน้า:
mutation LogUserIn {
loginUser(
by: { credentials: { usernameOrEmail: "test", password: "pass" } }
) {
id
name
}
}ตอนนี้มาเพิ่มความคิดเห็นกัน กดปุ่ม Run ด้านล่าง เพื่อเพิ่มความคิดเห็นในโพสต์โดยรัน mutation field addCommentToCustomPost (คุณสามารถแก้ไขข้อความความคิดเห็นได้ด้วย):
mutation AddComment {
addCommentToCustomPost(
input: { customPostID: 1459, comment: "Adding a comment: bla bla bla" }
) {
id
content
date
}
}ในรีลีสแรกนี้ ปลั๊กอินมาพร้อมกับ mutations ดังต่อไปนี้:
✅ createPost
✅ updatePost
✅ setFeaturedImageforCustomPost
✅ removeFeaturedImageforCustomPost
✅ addCommentToCustomPost
✅ replyComment
✅ loginUser
✅ logoutUser
2. Nested Mutations! 🚀🚀
Nested mutations คือความสามารถในการดำเนินการ mutations บน type อื่นที่ไม่ใช่ root type ใน GraphQL
ฟีเจอร์นี้ได้ถูกร้องขอสำหรับ GraphQL spec แต่ยังไม่ได้รับการอนุมัติ (และอาจไม่มีวันได้รับการอนุมัติ) ดังนั้น Gato GraphQL จึงเพิ่มการรองรับในรูปแบบ opt-in ผ่านโมดูล Nested Mutations
ปลั๊กอินรองรับพฤติกรรม 2 แบบ:
- พฤติกรรม GraphQL มาตรฐาน (กล่าวคือ การเพิ่ม mutation fields ไปที่ root type) โดยค่าเริ่มต้น
- Nested mutations ในรูปแบบ opt-in
ตัวอย่างเช่น query จากด้านบนสามารถรันด้วย query ต่อไปนี้ได้ด้วย ซึ่งเราจะดึง post ผ่าน Root.post ก่อน แล้วจึงเพิ่มความคิดเห็นผ่าน Post.addComment:
mutation AddComment {
post(by: { id: 1459 }) {
addComment(
input: {
comment: "Notice how field `addCommentToCustomPost` under the `Root` type is renamed as `addComment` under the `Post` type? The schema got neater!"
}
) {
id
content
date
}
}
}Mutations ยังสามารถแก้ไขข้อมูลบนผลลัพธ์จาก mutation อื่นได้ด้วย ใน query ด้านล่าง เราจะดึง post ผ่าน Root.post ก่อน จากนั้นรัน mutation Post.addComment บนมันและรับ comment object ที่สร้างขึ้น และสุดท้ายรัน mutation Comment.reply บน comment นั้น:
mutation AddCommentAndResponse {
post(by: { id: 1459 }) {
id
title
addComment(input: { comment: "Isn't this awesome?" }) {
id
date
content
reply(input: { comment: "I think so!" }) {
id
date
content
}
}
}
}มีประโยชน์มากจริงๆ! 😍 (วิธีอื่นในการสร้างพฤติกรรมเดียวกันในคิวรีเดียว คือการใช้ directive @export... ฉันจะเปรียบเทียบทั้งสองวิธีในบล็อกโพสต์ถัดไป)
ในรีลีสแรกนี้ ปลั๊กอินมาพร้อมกับ mutations ดังต่อไปนี้:
✅ CustomPost.update
✅ CustomPost.setFeaturedImage
✅ CustomPost.removeFeaturedImage
✅ CustomPost.addComment
✅ Comment.reply
มาตรฐานหรือ nested? หรือทั้งสองอย่าง?
คุณอาจมี GraphQL API ที่ใช้ในแอปพลิเคชันของคุณเอง และยังเปิดให้สาธารณชนใช้งานสำหรับลูกค้าด้วย คุณอาจต้องการเปิดใช้งาน nested mutations แต่เฉพาะในแอปพลิเคชันของคุณเอง ไม่ใช่สำหรับลูกค้า เพราะนี่เป็นฟีเจอร์ที่ไม่เป็นมาตรฐาน
ข่าวดี: คุณทำได้
ฉันได้เพิ่มส่วน "Mutation Scheme" ใน Schema Configuration ซึ่งใช้สำหรับปรับแต่ง schema สำหรับ Custom Endpoints และ Persisted Queries:

ดังนั้น คุณสามารถปิดใช้งาน nested mutations ทุกที่ แต่เปิดใช้งานเฉพาะสำหรับ custom endpoint เฉพาะที่แอปพลิเคชันของคุณเท่านั้นจะใช้ 💪
การลบ fields ที่ซ้ำซ้อนออกจาก root type
เมื่อใช้ nested mutations mutation fields อาจถูกเพิ่มสองครั้งใน schema:
- ครั้งหนึ่งภายใต้ root type
- ครั้งหนึ่งภายใต้ type เฉพาะ
ตัวอย่างเช่น fields เหล่านี้ถือได้ว่าเป็น "สำเนา" ของกันและกัน:
Root.updatePostPost.update
Gato GraphQL ช่วยให้คุณเก็บทั้งสองไว้ หรือลบ fields ที่ซ้ำซ้อนออกจาก root type ได้
3 schemas ต่อไปนี้:
- พฤติกรรมมาตรฐาน:
ใช้ typeQueryRootเพื่อจัดการ queries และMutationRootเพื่อจัดการ mutations - Nested mutations ที่เก็บ mutation fields ที่ซ้ำกันไว้:
typeRootเดียวจัดการทั้ง queries และ mutations และ mutation fields ที่ซ้ำซ้อนใน type นี้ยังคงอยู่ - Nested mutations ที่ลบ mutation fields ที่ซ้ำซ้อนออกจาก root type:
เหมือนกับข้างต้น แต่ลบ mutation fields ที่ซ้ำซ้อนทั้งหมดออกจาก typeRoot
✱ อนึ่ง 1, schemas ทั้ง 3 นี้ใช้ endpoint เดียวกัน แต่เปลี่ยน URL param ?mutation_scheme เป็นค่า standard, nested และ lean_nested ซึ่งเป็นไปได้เพราะ GraphQL server ปฏิบัติตามแนวทาง code-first 🤟
✱ อนึ่ง 2, ตัวเลือกเหล่านี้สามารถเลือกได้ในส่วน "Mutation Scheme" ใน Schema configuration (แสดงด้านบน) ดังนั้นคุณยังสามารถตัดสินใจพฤติกรรมที่จะใช้สำหรับ custom endpoints และ persisted queries แต่ละรายการได้ด้วย 👏
ถึงเวลาเริ่มเตรียมการสำหรับ v0.8 แล้ว! 🙏