Mit der GraphQL API interagierenBulk-Mutations ausführen
Bulk-Mutations ausführen
Gato GraphQL bietet „Bulk"-Mutation-Felder für alle Mutations im Schema, mit denen wir mehrere Ressourcen auf einmal mutieren können.
Zum Beispiel erstellt die Mutation createPosts (die Mutation für eine einzelne Ressource lautet createPost) mehrere Beiträge:
mutation CreatePosts {
createPosts(inputs: [
{
title: "First post"
contentAs: {
html: "This is the content for the first post"
}
},
{
title: "Second post"
contentAs: {
html: "Here is another content, for another post"
}
excerpt: "The cup is within reach"
},
{
title: "Third post"
contentAs: {
html: "This is yet another piece of content"
},
authorBy: {
id: 1
},
status: draft
}
]) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
title
content
excerpt
author {
name
}
status
}
}
}Argumente
Alle Bulk-Mutations akzeptieren zwei Argumente:
inputs(erforderlich): Das Array der Eingabe-Elemente, wobei jedes Element die Daten enthält, um eine Ressource zu mutierenstopExecutingMutationItemsOnFirstError(Standardfalse): Gibt an, ob die Ausführung der Mutation für die nachfolgenden Eingaben gestoppt werden soll, falls einer der Eingaben einen Fehler produziert.
Alle Mutations werden in derselben Reihenfolge ausgeführt, die im Argument inputs angegeben ist.
Anwendungsfälle
Bulk-Mutations eröffnen neue Möglichkeiten zur Verwaltung unserer WordPress-Website.
Zum Beispiel verwendet die folgende GraphQL-Query createPosts, um Beiträge zu duplizieren:
query ExportPostData
{
postsToDuplicate: posts {
rawTitle
rawContent
rawExcerpt
postInput: _echo(value: {
title: $__rawTitle
contentAs: {
html: $__rawContent
},
excerpt: $__rawExcerpt
})
@export(as: "postInputs", type: LIST)
@remove
}
}
mutation CreatePosts
@depends(on: "ExportPostData")
{
createPosts(inputs: $postInputs) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
title
content
excerpt
}
}
}