Query data WordPress
Query data WordPressKomentar

Komentar

Berikut adalah contoh query untuk mengambil dan menambahkan komentar.

Mengambil komentar

Komentar dari sebuah postingan:

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

Komentar dan balasannya, untuk beberapa level:

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

Memfilter komentar:

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

Menghitung hasil komentar:

{
  posts {
    id
    commentCount
  }
}

Melakukan paginasi komentar:

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

Semua komentar di situs dari pengguna tertentu:

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

Komentar tertentu:

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

Mengambil nilai meta:

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

Menambahkan komentar

Pengguna yang sudah login maupun yang belum login dapat menambahkan komentar:

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

Kita juga dapat menggunakan 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
        }
      }
    }
  }
}

Membalas komentar

Mirip dengan menambahkan komentar, tetapi juga menyertakan argumen 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
      }
    }
  }
}

Atau gunakan field replyComment yang lebih spesifik:

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

Atau navigasikan ke komentar induk menggunakan 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
        }
      }
    }
  }
}