Lekcja 4: Duplikowanie wielu postów na blogu jednocześnie
Możemy rozszerzyć poprzednią lekcję tutorialu, aby zduplikować wiele postów za pomocą jednego żądania GraphQL.
Query GraphQL do duplikowania wielu postów jednocześnie
Aby ta query GraphQL działała, Konfiguracja schematu zastosowana do endpointu musi mieć włączone Zagnieżdżone mutacje
Ta query GraphQL duplikuje posty pobrane za pomocą podanych zmiennych $limit i $offset:
query InitializeDynamicVariables
@configureWarningsOnExportingDuplicateVariable(enabled: false)
{
postInputs: _echo(value: [])
@export(as: "postInputs")
@remove
}
query GetPostsAndExportData($limit: Int! = 5, $offset: Int! = 0)
@depends(on: "InitializeDynamicVariables")
{
postsToDuplicate: posts(
pagination: {
limit : $limit
offset: $offset
}
sort: {
by: ID,
order: ASC
}
) {
# Fields not to be duplicated
id
slug
date
status
# Fields to be duplicated
author {
id
}
categories {
id
}
rawContent
excerpt
featuredImage {
id
}
tags {
id
}
title
# Already create (and export) the inputs for the mutation
postInputs: _echo(value: {
status: draft,
authorBy: {
id: $__author
},
categoriesBy: {
ids: $__categories
},
contentAs: {
html: $__rawContent
},
excerpt: $__excerpt
featuredImageBy: {
id: $__featuredImage
},
tagsBy: {
ids: $__tags
},
title: $__title
})
@export(as: "postInputs", type: LIST)
@remove
}
}
mutation DuplicatePosts
@depends(on: "GetPostsAndExportData")
{
createPosts(inputs: $postInputs) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
# Fields not to be duplicated
id
slug
date
status
# Fields to be duplicated
author {
id
}
categories {
id
}
rawContent
excerpt
featuredImage {
id
}
tags {
id
}
title
}
}
}Krok po kroku: tworzenie query GraphQL
Poniżej znajduje się szczegółowa analiza działania tej query.
Rozszerzenie lekcji tutorialu "Duplikowanie posta na blogu"
Poprzednia lekcja stosuje następującą strategię (w query GraphQL drugiego podejścia):
- Wyeksportuj identyfikatory zasobów z pól (w tym pól połączeń):
query GetPostAndExportData {
post {
author @export(as: "authorID") {
id
}
categories @export(as: "categoryIDs") {
id
}
rawContent @export(as: "rawContent")
rawExcerpt @export(as: "excerpt")
featuredImage @export(as: "featuredImageID") {
id
}
tags @export(as: "tagIDs") {
id
}
rawTitle @export(as: "title")
}
}- Utwórz obiekt wejściowy dla
createPost(input:)z tych zmiennych dynamicznych:
mutation DuplicatePost
@depends(on: "GetPostAndExportData")
{
createPost(input: {
status: draft,
authorBy: {
id: $authorID
},
categoriesBy: {
ids: $categoryIDs
},
contentAs: {
html: $rawContent
},
excerpt: $excerpt
featuredImageBy: {
id: $featuredImageID
},
tagsBy: {
ids: $tagIDs
},
title: $title
}) {
# ...
}
}Dzięki rozszerzeniu Field to Input możemy utworzyć obiekt wejściowy już w pierwszej operacji i wyeksportować wszystkie wymagane dane posta w jednej zmiennej dynamicznej:
query GetPostAndExportData {
post {
author {
id
}
categories {
id
}
rawContent
excerpt
featuredImage {
id
}
tags {
id
}
title
postInputs: _echo(value: {
status: draft,
authorBy: {
id: $__author
},
categoriesBy: {
ids: $__categories
},
contentAs: {
html: $__rawContent
},
excerpt: $__excerpt
featuredImageBy: {
id: $__featuredImage
},
tagsBy: {
ids: $__tags
},
title: $__title
})
@export(as: "postInputs")
}
}Następnie w kolejnej mutacji createPost(input:) bezpośrednio otrzymuje zmienną dynamiczną $postInputs:
mutation DuplicatePost
@depends(on: "GetPostAndExportData")
{
createPost(input: $postInputs) {
# ...
}
}Pobieranie wielu postów
Musimy przekształcić query, aby pobrać wiele postów do zduplikowania:
- Wykonaj zapytanie o posty przez
posts(pagination: { limit : $limit, offset: $offset}) { ... } - Wyeksportuj
postInputsjako listę (tj. tablicę zawierającą wszystkie dane wejściowe dla zapytanych postów)
query GetPostsAndExportData($limit: Int! = 5, $offset: Int! = 0)
@depends(on: "InitializeDynamicVariables")
{
postsToDuplicate: posts(
pagination: {
limit : $limit
offset: $offset
}
sort: {
by: ID,
order: ASC
}
) {
# ...
postInputs: _echo(value: {
# ...
})
@export(
as: "postInputs",
type: LIST
)
}
}Tworzenie wielu postów w jednej query GraphQL
Zmienna dynamiczna $postInputs zawiera teraz tablicę ze wszystkimi danymi wejściowymi dla każdego z postów do zduplikowania:
[
{
"status": "draft",
"authorBy": {
"id": "2"
},
"categoryIDs": [
1
],
"contentAs": {
"html": "<!-- wp:paragraph -->\n<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n<!-- /wp:paragraph -->"
},
"excerpt": "Welcome to WordPress. This is your first post. Edit or delete it, then start writing!",
"featuredImageBy": {
"id": null
},
"tagsBy": {
"ids": []
},
"title": "Hello world!"
},
{
"status": "draft",
"authorBy": {
"id": "3"
},
"categoryIDs": [
3
],
"contentAs": {
"html": "<!-- wp:paragraph -->\n<p>This is a paragraph block. Professionally productize highly efficient results with world-class core competencies. Objectively matrix leveraged architectures vis-a-vis error-free applications. Completely maximize customized portals via fully researched metrics. Enthusiastically generate premier action items through web-enabled e-markets. Efficiently parallel task holistic intellectual capital and client-centric markets.<br><br></p>\n<!-- /wp:paragraph -->\n\n<!-- wp:heading -->\n<h2>Image Block (Standard)</h2>\n<!-- /wp:heading -->\n\n<!-- wp:image {\"id\":1755} -->\n<figure class=\"wp-block-image\"><img src=\"https://d.pr/i/8pTmgY+\" alt=\"\" class=\"wp-image-1755\"/></figure>\n<!-- /wp:image -->"
},
"excerpt": "This is a paragraph block. Professionally productize highly efficient results with world-class core competencies. Objectively matrix leveraged architectures vis-a-vis error-free applications. Completely maximize customized portals via fully researched metrics. Enthusiastically generate premier action items through web-enabled e-markets. Efficiently parallel task holistic intellectual capital and client-centric markets. Image Block (Standard)",
"featuredImageBy": {
"id": 361
},
"tagsBy": {
"ids": [
11,
10
]
},
"title": "Released v0.6, check it out"
}
]Na koniec wywołujemy masową mutację createPosts, aby utworzyć wszystkie posty, przekazując dane wyeksportowanych danych wejściowych:
mutation DuplicatePosts
@depends(on: "GetPostsAndExportData")
{
createPosts(inputs: $postInputs) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
# Fields not to be duplicated
id
slug
date
status
# Fields to be duplicated
author {
id
}
categories {
id
}
rawContent
excerpt
featuredImage {
id
}
tags {
id
}
title
}
}
}Na koniec wywołujemy masową mutację createPosts, aby utworzyć wszystkie posty, przekazując wszystkie dane wejściowe:
mutation DuplicatePosts
@depends(on: "GetPostsAndExportData")
{
createPosts(inputs: $postInputs) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
# Fields not to be duplicated
id
slug
date
status
# Fields to be duplicated
author {
id
}
categories {
id
}
rawContent
excerpt
featuredImage {
id
}
tags {
id
}
title
}
}
}Usuwanie zbędnych danych
Ostatnim krokiem jest usunięcie wszystkich pól pomocniczych (których wyniku nie musimy drukować w odpowiedzi) za pomocą @remove.
Skonsolidowana query GraphQL to:
query InitializeDynamicVariables
@configureWarningsOnExportingDuplicateVariable(enabled: false)
{
postInputs: _echo(value: [])
@export(as: "postInputs")
@remove
}
query GetPostsAndExportData($limit: Int! = 5, $offset: Int! = 0)
@depends(on: "InitializeDynamicVariables")
{
postsToDuplicate: posts(
pagination: {
limit : $limit
offset: $offset
}
sort: {
by: ID,
order: ASC
}
) {
# Fields not to be duplicated
id
slug
date
status
# Fields to be duplicated
author {
id
}
categories {
id
}
rawContent
excerpt
featuredImage {
id
}
tags {
id
}
title
# Already create (and export) the inputs for the mutation
postInputs: _echo(value: {
status: draft,
authorBy: {
id: $__author
},
categoriesBy: {
ids: $__categories
},
contentAs: {
html: $__rawContent
},
excerpt: $__excerpt
featuredImageBy: {
id: $__featuredImage
},
tagsBy: {
ids: $__tags
},
title: $__title
})
@export(as: "postInputs", type: LIST)
@remove
}
}
mutation DuplicatePosts
@depends(on: "GetPostsAndExportData")
{
createPosts(inputs: $postInputs) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
# Fields not to be duplicated
id
slug
date
status
# Fields to be duplicated
author {
id
}
categories {
id
}
rawContent
excerpt
featuredImage {
id
}
tags {
id
}
title
}
}
}