Thay thế WP REST API
Nếu ứng dụng của bạn đang sử dụng WP REST API, bạn có thể dùng Gato GraphQL thay thế.
Với tiện ích Persisted Queries, bạn có thể xuất bản các endpoint theo kiểu REST, được tạo bằng GraphQL.
Với mỗi endpoint REST trong ứng dụng, bạn có thể tạo một endpoint persisted query tương ứng để lấy cùng dữ liệu, và sử dụng endpoint đó thay thế.
Ví dụ, queries GraphQL sau có thể thay thế endpoint REST /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
}
}Nhờ cấu trúc phân cấp API, persisted query có thể được xuất bản tại đường dẫn /graphql-query/wp/v2/posts/, giúp việc ánh xạ các endpoint trở nên dễ dàng.
Để sao chép endpoint REST /wp-json/wp/v2/posts/{id}/, vốn lấy dữ liệu cho bài viết theo ID nhất định, chúng ta có thể cung cấp ID bài viết qua tham số URL postId.
Ví dụ, persisted query sau có thể được gọi tại 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
}
}