บล็อก

🏁 ตอนนี้คุณสามารถ mutate ค่า meta ใน Gato GraphQL ได้แล้ว

Leonardo Losoviz
โดย Leonardo Losoviz ·

วันนี้ Gato GraphQL v11.3 ได้เปิดตัวแล้ว พร้อมรองรับฟีเจอร์สำคัญ: Meta mutations!

ตอนนี้คุณสามารถเพิ่ม อัปเดต และลบค่า meta สำหรับ custom posts, tags, categories, comments และ users ได้แล้ว

ด้านล่างนี้คือตัวอย่าง queries สำหรับการ mutate meta

การเพิ่ม meta

คุณสามารถเพิ่ม meta entries ให้กับ custom posts, tags, categories, comments และ users ได้

Query นี้เพิ่ม meta entry ให้กับ post ที่มี ID 4:

mutation {
  addCustomPostMeta(input: {
    id: 4
    key: "some_key"
    value: "Some value"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      id
      metaValue(key: "some_key") 
    }
  }
}

Query นี้เพิ่ม meta key เดียวกันพร้อมค่าที่แตกต่างกันให้กับหลาย posts พร้อมกันในคราวเดียว:

mutation {
  addCustomPostMetas(inputs: [
    {
      id: 4
      key: "some_key"
      value: "Some value"
    },
    {
      id: 5
      key: "some_key"
      value: "Some other value"
    },
    {
      id: 6
      key: "some_key"
      value: "Yet another value"
    }
  ]) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      id
      metaValue(key: "some_key") 
    }
  }
}

การอัปเดต meta

อัปเดต meta entry ของ category:

mutation {
  updateCategoryMeta(input: {
    id: 20
    key: "_source"
    value: "Updated source value"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      __typename
      id
      metaValue(key: "_source") 
    }
  }
}

Query นี้ใช้ nested mutations เพื่ออัปเดตค่า meta ใน post:

mutation {
  post(by: {id: 1}) {
    updateMeta(input: {
      key: "some_key"
      value: "Updated description"
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        metaValue(key: "single_meta_key") 
      }
    }
  }
}

การลบ meta

ลบ meta entry จาก post:

mutation {
  deletePostMeta(input: {
    id: 5
    key: "some_key"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      metaValue(key: "some_key") 
    }
  }
}

ลบ meta entry เดียวกันจากหลาย posts พร้อมกันในคราวเดียว:

mutation {
  deletePostMetas(inputs: [
    {
      id: 5
      key: "some_key"
    },
    {
      id: 6
      key: "some_key"
    }
  ]) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      metaValue(key: "some_key") 
    }
  }
}

การตั้งค่า meta entries หลายรายการพร้อมกัน

คุณสามารถตั้งค่า meta entries หลายรายการพร้อมกันได้ โดยส่ง JSON ไปยัง mutations set{Entity}Meta ต่างๆ:

mutation {
  setCustomPostMeta(input: {
    id: 4
    entries: {
      single_meta_key: [
        "This is a single entry",
      ],
      object_meta_key: [
        {
          key: "This is a key",
          value: "This is a value",
        },
      ],
      array_meta_key: [
        "This is a string",
        "This is another string",
      ],
      object_array_meta_key: [
        [
          {
            key: "This is a key 1",
            value: "This is a value 1",
          },
          {
            key: "This is a key 2",
            value: "This is a value 2",
          },
        ]
      ],
    }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      id
      meta(keys: ["single_meta_key", "object_meta_key", "array_meta_key", "object_array_meta_key"])
    }
  }
}

การตั้งค่า meta entries ขณะสร้าง/อัปเดต entity

คุณสามารถกำหนด meta entries โดยตรงเมื่อสร้างหรืออัปเดต custom post, tag, category หรือ comment ผ่านพารามิเตอร์ meta

Query นี้ตั้งค่า meta ขณะเพิ่ม comment:

mutation {
  addCommentToCustomPost(input: {
    customPostID: 1130
    commentAs: { html: "New comment" }
    meta: {
      some_meta_key: [
        "This is a single entry",
      ],
      another_meta_key: [
        "This is an array entry 1",
        "This is an array entry 2",
      ],
    }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    comment {
      id
      meta(keys: ["some_meta_key", "another_meta_key"]) 
    }
  }
}

Query นี้แทรก meta เข้าไปใน nested mutation Post.update:

mutation {
  post(by: {id: 1}) {
    update(input: {
      meta: {
        single_meta_key: [
          "This is an updated value",
        ]
      }
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        metaValue(key: "single_meta_key") 
      }
    }
  }
}

สมัครรับจดหมายข่าวของเรา

ติดตามการอัปเดตทั้งหมดของ Gato GraphQL