GraphQL APIとのインタラクション
GraphQL APIとのインタラクションバルクmutationの実行

バルクmutationの実行

Gato GraphQLは、スキーマ内のすべてのmutationに対して「バルク」mutationフィールドを提供しており、複数のリソースを一度にmutateすることができます。

たとえば、mutation createPosts(単一リソースのmutationは createPost)は複数の投稿を作成します:

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
    }
  }
}

引数

すべてのバルクmutationは2つの引数を受け付けます:

  • inputs(必須):入力アイテムの配列。各アイテムには1つのリソースをmutateするためのデータが含まれます
  • stopExecutingMutationItemsOnFirstError(デフォルト:false):いずれかの入力がエラーを生成した場合に、以降の入力に対するmutationの実行を停止するかどうかを指定します。

すべてのmutationは inputs 引数で指定された順序と同じ順序で実行されます。

ユースケース

バルクmutationにより、WordPressサイトの管理における様々な可能性が広がります。

たとえば、次のGraphQLクエリは createPosts を使用して投稿を複製します:

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
    }
  }
}