/

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
}
Query FragmentsHere's an example query that shows how to query deeply nested data. One limitation with GraphQL that is worth noting, is that there's no way to do infinite recursion. You have to specify how many levels deep you want to query.

In this case, we query for the top level comments, and 4 levels of replies.

While this may seem like a flaw of GraphQL, many would argue that it's actually a benefit, in that you have complete control over what you're querying and what your application will support.

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...
ID InputsAt the time of writing this, some inputs, like the one above, that ask for an ID expect the Integer ID (the database ID), while others throughout the Schema expect the Global ID.

We've got an open issue we hope to address soon that will allow all the ID fields to accept either kind of ID and process appropriately.

Edit on GitHub

Comments

  • Queries
  • Comments on a Post
  • Root Comments on Post
  • Mutations
  • Create comment

Edit on GitHub

Discuss on Spectrum