Comments
Learn about how to interact with Comments using WPGraphQL
Comments are a core part of WordPress, and they are native in the WPGraphQL Schema. WPGraphQL provides the ability to query and mutate comments.
Queries
WPGraphQL provides RootQuery fields built-in to query Comments. You can query for lists of comments or an individual comment. Below are some examples of querying for comments.
Comments on a Post
One of the most common use cases for querying a comment is to query for comments associated with a specific post.
This example shows querying for a specific post, and on that Post, get a list of comments and the comment children (replies).
{
posts(first: 100) {
nodes {
id
title
comments {
nodes {
...CommentFields
replies {
nodes {
...CommentFields
replies {
nodes {
...CommentFields
replies {
nodes {
...CommentFields
replies {
nodes {
...CommentFields
}
}
}
}
}
}
}
}
}
}
}
}
}
fragment CommentFields on Comment {
id
date
type
approved
content
}
Root Comments on Post
The example above shows querying a Post and it's comments. One option would also be to load the Post data initially without comments, then once the page has loaded, make a request from the client to load the comments.
This could be done like so:
query GET_COMMENTS_FOR_POST {
comments(where: { contentId: 1 }) {
nodes {
...CommentFields
replies {
nodes {
...CommentFields
replies {
...CommentFields
replies {
...CommentFields
replies {
...CommentFields
}
}
}
}
}
}
}
}
fragment CommentFields on Comment {
id
date
type
approved
content
}
Mutations
Create comment
This is an example of a mutation to create a Comment.
GraphiQL Loading...