การโต้ตอบกับ GraphQL API
การโต้ตอบกับ GraphQL APIการดำเนินการ bulk mutations

การดำเนินการ bulk mutations

Gato GraphQL มี "bulk" mutation fields สำหรับ mutations ทั้งหมดใน schema ทำให้เราสามารถ 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
    }
  }
}

อาร์กิวเมนต์

bulk mutations ทั้งหมดรับอาร์กิวเมนต์สองตัว:

  • inputs (จำเป็น): อาร์เรย์ของรายการ input โดยแต่ละรายการมีข้อมูลสำหรับ mutate ทรัพยากรหนึ่งรายการ
  • stopExecutingMutationItemsOnFirstError (ค่าเริ่มต้น false): ระบุว่าหาก input รายการใดรายการหนึ่งเกิดข้อผิดพลาด ควรหยุดดำเนินการ mutation สำหรับ inputs รายการถัดไปหรือไม่

mutations ทั้งหมดจะดำเนินการตามลำดับที่ระบุไว้ในอาร์กิวเมนต์ inputs

กรณีการใช้งาน

Bulk mutations เปิดโอกาสให้จัดการเว็บไซต์ WordPress ได้หลากหลายรูปแบบ

ตัวอย่างเช่น GraphQL query ต่อไปนี้ใช้ 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
    }
  }
}