แนวคิด ไอเดีย และกลยุทธ์
แนวคิด ไอเดีย และกลยุทธ์กลยุทธ์สำหรับลำดับชั้น API

กลยุทธ์สำหรับลำดับชั้น API

เราสามารถนำแนวคิดต่างๆ มาประยุกต์ใช้เพื่อสร้างลำดับชั้นสำหรับ API endpoint ของเรา

Queries ก่อน แล้วค่อย override schema

เราสามารถกำหนดให้ parent มี GraphQL query ที่ใช้ร่วมกัน แล้วจึงขยายต่อสำหรับแต่ละแอปพลิเคชัน

ตัวอย่างเช่น parent persisted query /graphql-query/posts/ จะกำหนด GraphQL query ดังนี้:

query GetPosts {
  posts {
    id
    title
    url
  }
}

และ child persisted queries สำหรับเว็บไซต์และแอปมือถือ จะกำหนด schema configuration ที่สอดคล้องกัน:

  • /graphql-query/posts/website/ => ใช้ schema configuration "Website"
  • /graphql-query/posts/mobile-app/ => ใช้ schema configuration "Mobile app"

Schema ก่อน แล้วค่อย override query

อีกทางเลือกหนึ่ง เราสามารถประกาศ schema configuration ที่ระดับ parent และให้ children ทุกตัวสืบทอดมัน จากนั้นจึง implement เฉพาะ GraphQL query:

  • /graphql-query/mobile-app/posts/
  • /graphql-query/mobile-app/users/
  • /graphql-query/website/posts/
  • /graphql-query/website/users/

แปล endpoint

แปลเนื้อหาใน endpoint โดยระบุรหัสภาษาผ่าน variable ซึ่งสามารถ override ได้โดย child endpoint

ตัวอย่างเช่น เราสามารถสร้าง persisted query /graphql-query/posts/ พร้อม GraphQL query ดังนี้:

query GetTranslatedPosts($lang: String!) {
  posts {
    title @strTranslate(from: "en", to: $lang)
    url
  }
}

เราไม่จำเป็นต้องเรียกใช้ endpoint นี้โดยตรง จึงสามารถปิดการใช้งานได้

จากนั้น เราสร้าง child persisted query /graphql-query/posts/french/ ซึ่ง override ตัวแปร GraphQL:

{
  "lang": "fr"
}