Interfaces & Types
These are the GraphQL interfaces and types Comments adds. Queries and Mutations show them in use.
Comments
The CommentInterface Interface
The interface implemented by every comment. Comments use a single Comment type, which also holds the custom fields from your Comment field layout, requested inside a ... on Comment fragment as shown in Custom Fields. The commenter’s IP address and browser aren’t available, and a guest’s email address is only available as email on a private schema with the View commenters’ email addresses permission.
Craft also adds title, slug and uri to every element type, but comments don’t have a title, slug or URI of their own, so these are always null. Use ownerId to find the element a comment belongs to, and that element’s uri for its page.
{
comments (ownerId: 2460, parentId: ":empty:") {
id
commentDate @formatDateTime (format: "j M Y")
contentHtml
comment
votes
author {
name
avatarUrl
}
viewer {
canReply
vote
}
}
}The ID of the comment.
The UID of the comment.
The comment’s stored status, which is approved, pending, spam or removed.
The ID of the site the comment is on.
The handle of the site the comment is on.
The date the comment’s element was created.
The date the comment’s element was last updated.
The ID of the element the comment was made on. Use that element’s own query for its title or URI.
The date the comment was posted.
The comment’s plain text. A rich comment has its text here without formatting.
The way the comment was written, which is plain or rich.
The rendered HTML of a rich comment. It’s null for a plain text comment.
The Editor JSON of a rich comment, for editing it. It’s null for a plain text comment.
The date the comment’s content last changed after it was posted, or null if it hasn’t been edited.
The comment’s moderation state, such as approved or pending. Public schemas only ever return approved comments.
The ID of the comment at the top of this comment’s thread.
The number of replies deep the comment is. Top-level comments are at 0.
The URL of the GIF shown with the comment, or null when it doesn’t have one.
The name a guest gave. It’s null for comments by signed-in users, so use author for the name the thread shows.
The email address a guest gave. It’s null for comments by signed-in users. Only on a private schema with the View commenters’ email addresses permission.
The person who wrote the comment, as the thread shows them. See the CommentAuthor type.
A summary of what the person making the request can do with the comment, and how they voted. See the CommentViewer type.
The URL of the page the comment was posted from.
The comment as the data API describes it to the person asking, including what they may do with it in viewer. For a moderator on a schema that isn’t public, it also has moderation.
The comment’s direct replies. It accepts the same arguments as the comments query.
The comment this one replies to, or null for a top-level comment.
The comment’s net score, which is its upvotes minus its downvotes.
The number of upvotes the comment has.
The number of downvotes the comment has.
The ID of the user who wrote the comment, or null for a guest. It’s only on schemas that can view users.
The user who wrote the comment. It’s only on schemas that can view users.
The number of open reports on the comment. It’s only on schemas with Moderate comments, and is null unless the request is from someone who can moderate comments, on a schema that isn’t public.
The CommentAuthor Type
Every comment has an author with the name, initials, avatar and badges the thread shows next to it. It’s available on any schema that can view comments, including a public one, so a headless front end can show “Alex Morgan” without giving the schema access to users, which would also expose your users’ details. The user field still needs a schema that can view users, for when you want the account itself.
{
comments (ownerId: 2460, parentId: ":empty:") {
id
comment
author {
name
initials
avatarUrl
isGuest
badges { type label }
}
}
}The name shown on the comment. A guest who didn’t give a name is shown as “Guest”, and a member’s name never falls back to their email address.
The first one or two initials of the name, for an avatar without a photo.
The URL of the author’s photo, or of your placeholder avatar. It’s null when Show Avatar is off, or there’s neither.
Whether the comment was posted without an account.
The comment’s badges, each with a type and a label. Comments adds owner and staff badges with the labels from your badge settings, and a site can add its own through the defineBadges event.
The CommentViewer Type
Every comment also has a viewer, which says what the person making the request can do with it, so a headless front end can show the same controls the default templates would and leave out ones that would fail. These follow the same rules as the default templates and the mutations. The person is whoever is signed in for the request, through a session or a GraphQL Authentication (opens new window) token, or a guest otherwise.
viewer is worked out for all the comments in a response together, so asking for it on a page of comments doesn’t add a query per comment. Because the answer depends on who’s asking, Craft’s GraphQL cache isn’t used for any query that includes a viewer field.
{
comments (ownerId: 2460) {
id
viewer {
canReply
canEdit
canDelete
canVote
canDownvote
canReport
vote
}
}
}Whether they can reply to the comment.
Whether they can change the comment with saveComment, as its author within the Author Edit Window.
Whether they can delete the comment with deleteComment, as its author.
Whether they can upvote or like the comment.
Whether they can downvote the comment, which needs the Upvotes and downvotes interaction mode.
Whether they can report the comment.
The way they voted on the comment, which is upvote or downvote, or null if they haven’t.
Conversations
The CommentConversation Type
The type the commentConversation query returns, describing the discussion on an element.
{
commentConversation (ownerId: 2460) {
commentCount
open
guestPolicy { allowed nameAndEmail }
viewer { canComment }
}
}The ID of the element the conversation is on.
The ID of the site the conversation is on.
The number of published comments.
The date the latest published comment was posted, or null when there are none.
Whether new comments can be posted.
The way people can vote, which is like, updown for upvotes and downvotes, or none.
The most characters a comment can have, from Maximum Comment Length, or null for no limit.
A summary of what guests can do here. allowed is whether they can comment, nameAndEmail is whether they give their name and email (required, optional or hidden), and canVote and canReport are whether they can vote and report.
The list of your Report Reasons, each with the value to send as reason to flagComment, its translated label and its description. It’s empty while reporting is off.
A summary of what the person making the request can do here. isGuest is whether they’re a guest, and canComment, canVote and canReport are whether they can post a new comment, vote and report comments. canComment takes everything into account, including closing dates, whether guests are allowed and Maximum Comments per User.
The CommentsApiData Type
The type the data API queries return.
{
commentsThread(ownerId: 2460) {
apiVersion
data
}
}The version of the data API that built the response.
The data API’s response as a JSON value, without its apiVersion and status.
Votes and Reports
The VoteInterface Interface
The interface of the Vote type, which voteComment returns.
mutation VoteComment($id: ID!) {
voteComment(id: $id, upvote: true) {
upvote
downvote
comment {
votes
}
}
}The ID of the vote. It’s always null, as votes aren’t exposed by ID.
Whether the vote is now an upvote.
Whether the vote is now a downvote. When neither upvote nor downvote is true, the vote was taken back.
The ID of the comment the vote was made on. It’s only on schemas that can view comments.
The comment the vote was made on. It’s only on schemas that can view comments.
The ID of the user who voted. It’s only on schemas that can view users.
The user who voted. It’s only on schemas that can view users.
The FlagInterface Interface
The interface of the Flag type, which flagComment returns. In code, a report is called a flag.
mutation FlagComment($id: ID!) {
flagComment(id: $id, withdraw: false) {
commentId
}
}The ID of the report. It’s always null, as reports aren’t exposed by ID.
The ID of the reported comment. It’s only on schemas that can view comments.
The reported comment. It’s only on schemas that can view comments.
The ID of the user who made the report. It’s only on schemas that can view users.
The user who made the report. It’s only on schemas that can view users.
Inputs
The CommentsProtectionEvidence Input
The input createComment and saveComment take as protection, described under Protection.
{
"version": 1,
"preparation": "SIGNED_PREPARATION",
"challenges": {
"turnstile": "CLIENT_PROVIDER_TOKEN"
}
}The version of the preparation, from protection.version.
The signed preparation, from protection.manifest.preparation.
The tokens your front end collected for the challenges in manifest.challenges, as turnstile or recaptchaV3. Leave it out when no provider is enabled.