เริ่มต้นใช้งาน
เริ่มต้นใช้งานการแทนที่ WP REST API

การแทนที่ WP REST API

หากแอปพลิเคชันของคุณกำลังใช้ WP REST API คุณสามารถใช้ Gato GraphQL แทนได้

ด้วยส่วนขยาย Persisted Queries คุณสามารถเผยแพร่ endpoint แบบ REST ที่สร้างขึ้นด้วย GraphQL ได้

สำหรับ REST endpoint แต่ละรายการในแอปพลิเคชันของคุณ คุณสามารถสร้าง persisted query endpoint ที่สอดคล้องกันซึ่งดึงข้อมูลเดียวกัน และใช้ endpoint นั้นแทนได้

ตัวอย่างเช่น GraphQL query ต่อไปนี้สามารถแทนที่ REST endpoint /wp-json/wp/v2/posts/ ได้:

{
  posts {
    id
    date: dateStr(format: "Y-m-d\\TH:i:s")
    modified: modifiedDateStr(format: "Y-m-d\\TH:i:s")
    slug
    status
    link: url
    title: self {
      rendered: title
    }
    content: self {
      rendered: content
    },
    excerpt: self {
      rendered: excerpt
    }
    author
    featured_media: featuredImage
    sticky: isSticky
    categories
    tags
  }
}

ด้วย API hierarchy persisted query สามารถเผยแพร่ภายใต้ path /graphql-query/wp/v2/posts/ ซึ่งทำให้การ mapping endpoint ทำได้ง่าย

เพื่อจำลอง REST endpoint /wp-json/wp/v2/posts/{id}/ ที่ดึงข้อมูลสำหรับโพสต์ที่มี ID ที่กำหนด เราสามารถระบุ post ID ใต้ URL param postId ได้

ตัวอย่างเช่น persisted query ต่อไปนี้สามารถเรียกใช้ภายใต้ endpoint /graphql-query/wp/v2/posts/single/?postId={id}:

query GetPost($postId: ID!) {
  post(by: { id: $postId }) {
    id
    date: dateStr(format: "Y-m-d\\TH:i:s")
    modified: modifiedDateStr(format: "Y-m-d\\TH:i:s")
    slug
    status
    link: url
    title: self {
      rendered: title
    }
    content: self {
      rendered: content
    },
    excerpt: self {
      rendered: excerpt
    }
    author
    featured_media: featuredImage
    sticky: isSticky
    categories
    tags
  }
}