Truy vấn dữ liệu WordPress
Truy vấn dữ liệu WordPressBình luận

Bình luận

Đây là các ví dụ về queries để lấy và thêm bình luận.

Lấy bình luận

Bình luận từ một bài viết:

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

Bình luận và các phản hồi của chúng, qua nhiều cấp độ:

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

Lọc bình luận:

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

Đếm số lượng bình luận:

{
  posts {
    id
    commentCount
  }
}

Phân trang bình luận:

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

Tất cả bình luận trên site từ một người dùng cụ thể:

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

Một bình luận cụ thể:

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

Lấy giá trị meta:

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

Thêm bình luận

Người dùng đã đăng nhập hoặc chưa đăng nhập đều có thể thêm bình luận:

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

Chúng ta cũng có thể sử dụng 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
        }
      }
    }
  }
}

Trả lời bình luận

Tương tự như thêm bình luận, nhưng cũng cung cấp thêm đối số 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
      }
    }
  }
}

Hoặc sử dụng trường cụ thể hơn là 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
      }
    }
  }
}

Hoặc điều hướng đến bình luận cha bằng cách sử dụng 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
        }
      }
    }
  }
}