การ Query ข้อมูล WordPress
การ Query ข้อมูล WordPressความคิดเห็น

ความคิดเห็น

ต่อไปนี้คือตัวอย่าง queries สำหรับดึงข้อมูลและเพิ่มความคิดเห็น

การดึงข้อมูลความคิดเห็น

ความคิดเห็นจากโพสต์:

query {
  post(by: { id: 1 }) {
    comments {
      id
      content
      author {
        name
      }
      parent {
        id
      }
    }
  }
}

ความคิดเห็นและการตอบกลับ สำหรับหลายระดับ:

query {
  post(by: { id: 1499 }) {
    comments(pagination: { limit: 5 }) {
      ...CommentFields
      responses {
        ...CommentFields
        responses {
          ...CommentFields
        }
      }
    }
  }
}
 
fragment CommentFields on Comment {
  id
  date
  content
}

การกรองความคิดเห็น:

{
  posts {
    title
    comments(
      filter: { search: "insight" }
    ) {
      id
      content
    }
  }
}

การนับจำนวนความคิดเห็น:

{
  posts {
    id
    commentCount
  }
}

การแบ่งหน้าความคิดเห็น:

{
  posts {
    id
    comments(
      pagination: {
        limit: 3,
        offset: 3
      }
    ) {
      id
      date
      content
    }
  }
}

ความคิดเห็นทั้งหมดบนไซต์จากผู้ใช้เฉพาะราย:

{
  commentCount(filter: { authorIDs: [1], parentID: null })
  comments(filter: { authorIDs: [1], parentID: null }, pagination: { limit: -1 }) {
    id
    date
    content
  }
}

ความคิดเห็นเฉพาะรายการ:

{
  comment(by: { id: 272 }) {
    id
    date
    content
    author {
      id
      name
    }
  }
}

การดึงค่า meta:

{
  posts {
    id
    comments{
      id
      metaValue(
        key:"someKey"
      )
    }
  }
}

การเพิ่มความคิดเห็น

ผู้ใช้ที่เข้าสู่ระบบหรือไม่ได้เข้าสู่ระบบสามารถเพิ่มความคิดเห็นได้:

mutation {
  addCommentToCustomPost(
    input: { customPostID: 1459, commentAs: { html: "Lovely tango!" } }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    commentID
    comment {
      date
      content
      author {
        id
        name
      }
    }
  }
}

เราสามารถใช้ nested mutations ได้เช่นกัน:

mutation {
  post(by: { id: 1459 }) {
    id
    title
    addComment(input: { commentAs: { html: "Lovely tango!" } }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      commentID
      comment {
        date
        content
        author {
          id
          name
        }
      }
    }
  }
}

การตอบกลับความคิดเห็น

คล้ายกับการเพิ่มความคิดเห็น แต่ต้องระบุอาร์กิวเมนต์ parentCommentID เพิ่มเติม:

mutation {
  addCommentToCustomPost(
    input: {
      customPostID: 1459
      parentCommentID: 272
      commentAs: { html: "Hi to you too" }
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    commentID
    comment {
      date
      content
      author {
        id
        name
      }
    }
  }
}

หรือใช้ฟิลด์ replyComment ที่เฉพาะเจาะจงกว่า:

mutation {
  replyComment(input: { parentCommentID: 272, commentAs: { html: "Hi to you too" } }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    commentID
    comment {
      date
      content
      author {
        id
        name
      }
    }
  }
}

หรือนำทางไปยัง parent comment โดยใช้ nested mutations:

mutation {
  post(by: { id: 1459 }) {
    comments(filter: { ids: 272 }) {
      id
      content
      reply(input: { commentAs: { html: "Everything good?" } }) {
        status
        errors {
          __typename
          ...on ErrorPayload {
            message
          }
        }
        commentID
        comment {
          date
          content
        }
      }
    }
  }
}