Les 4: Meerdere blogberichten tegelijk dupliceren
We kunnen de vorige tutorial-les uitbreiden om meerdere berichten te dupliceren met een enkel GraphQL-verzoek.
GraphQL-query om meerdere berichten tegelijk te dupliceren
Om deze GraphQL-query te laten werken, moet de Schemaconfiguratie die op het endpoint is toegepast, Geneste mutaties hebben ingeschakeld
Deze GraphQL-query dupliceert de berichten die zijn opgehaald via de opgegeven variabelen $limit en $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
}
}
}Stap voor stap: de GraphQL-query opbouwen
Hieronder volgt een gedetailleerde analyse van hoe de query werkt.
De tutorial-les "Een blogbericht dupliceren" uitbreiden
De vorige les gebruikt de volgende strategie (in de GraphQL-query van de tweede aanpak):
- Exporteer de resource-ID's uit de velden (inclusief verbindingsvelden):
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")
}
}- Maak het invoerobject voor
createPost(input:)aan op basis van die dynamische variabelen:
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
}) {
# ...
}
}Dankzij de extensie Field to Input kunnen we het invoerobject al in de eerste operatie aanmaken en alle benodigde berichtdata exporteren onder één enkele dynamische variabele:
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")
}
}Daarna ontvangt createPost(input:) in de volgende mutatie rechtstreeks de dynamische variabele $postInputs:
mutation DuplicatePost
@depends(on: "GetPostAndExportData")
{
createPost(input: $postInputs) {
# ...
}
}Meerdere berichten ophalen
We moeten de query omzetten om meerdere te dupliceren berichten op te halen:
- Bevraag de berichten via
posts(pagination: { limit : $limit, offset: $offset}) { ... } - Exporteer
postInputsals een lijst (d.w.z. een array met alle invoergegevens voor de bevraagde berichten)
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
)
}
}Meerdere berichten aanmaken in één GraphQL-query
De dynamische variabele $postInputs bevat nu een array met alle invoerdata voor elk te dupliceren bericht:
[
{
"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"
}
]Ten slotte roepen we de bulk-mutatie createPosts aan om alle berichten aan te maken door de data van de geëxporteerde invoergegevens door te geven:
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
}
}
}Ten slotte roepen we de bulk-mutatie createPosts aan om alle berichten aan te maken door alle invoergegevens door te geven:
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
}
}
}Overbodige data verwijderen
De laatste stap is het verwijderen van alle hulpvelden (waarvan we de uitvoer niet hoeven af te drukken in het antwoord) via @remove.
De geconsolideerde GraphQL-query is:
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
}
}
}