GraphQL

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
        }
    }
}

id

The ID of the comment.

uid

The UID of the comment.

status

The comment’s stored status, which is approved, pending, spam or removed.

siteId

The ID of the site the comment is on.

siteHandle

The handle of the site the comment is on.

dateCreated

The date the comment’s element was created.

dateUpdated

The date the comment’s element was last updated.

ownerId

The ID of the element the comment was made on. Use that element’s own query for its title or URI.

commentDate

The date the comment was posted.

comment

The comment’s plain text. A rich comment has its text here without formatting.

contentFormat

The way the comment was written, which is plain or rich.

contentHtml

The rendered HTML of a rich comment. It’s null for a plain text comment.

contentDocument

The Editor JSON of a rich comment, for editing it. It’s null for a plain text comment.

editedAt

The date the comment’s content last changed after it was posted, or null if it hasn’t been edited.

moderationState

The comment’s moderation state, such as approved or pending. Public schemas only ever return approved comments.

rootId

The ID of the comment at the top of this comment’s thread.

depth

The number of replies deep the comment is. Top-level comments are at 0.

gifUrl

The URL of the GIF shown with the comment, or null when it doesn’t have one.

name

The name a guest gave. It’s null for comments by signed-in users, so use author for the name the thread shows.

email

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.

author

The person who wrote the comment, as the thread shows them. See the CommentAuthor type.

viewer

A summary of what the person making the request can do with the comment, and how they voted. See the CommentViewer type.

url

The URL of the page the comment was posted from.

commentsData

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.

children

The comment’s direct replies. It accepts the same arguments as the comments query.

parent

The comment this one replies to, or null for a top-level comment.

votes

The comment’s net score, which is its upvotes minus its downvotes.

upvotes

The number of upvotes the comment has.

downvotes

The number of downvotes the comment has.

userId

The ID of the user who wrote the comment, or null for a guest. It’s only on schemas that can view users.

user

The user who wrote the comment. It’s only on schemas that can view users.

flags

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 }
        }
    }
}

name

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.

initials

The first one or two initials of the name, for an avatar without a photo.

avatarUrl

The URL of the author’s photo, or of your placeholder avatar. It’s null when Show Avatar is off, or there’s neither.

isGuest

Whether the comment was posted without an account.

badges

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
        }
    }
}

canReply

Whether they can reply to the comment.

canEdit

Whether they can change the comment with saveComment, as its author within the Author Edit Window.

canDelete

Whether they can delete the comment with deleteComment, as its author.

canVote

Whether they can upvote or like the comment.

canDownvote

Whether they can downvote the comment, which needs the Upvotes and downvotes interaction mode.

canReport

Whether they can report the comment.

vote

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 }
    }
}

ownerId

The ID of the element the conversation is on.

siteId

The ID of the site the conversation is on.

commentCount

The number of published comments.

lastActivityAt

The date the latest published comment was posted, or null when there are none.

open

Whether new comments can be posted.

interactionMode

The way people can vote, which is like, updown for upvotes and downvotes, or none.

maxLength

The most characters a comment can have, from Maximum Comment Length, or null for no limit.

guestPolicy

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.

reportReasons

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.

viewer

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
    }
}

apiVersion

The version of the data API that built the response.

data

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
        }
    }
}

id

The ID of the vote. It’s always null, as votes aren’t exposed by ID.

upvote

Whether the vote is now an upvote.

downvote

Whether the vote is now a downvote. When neither upvote nor downvote is true, the vote was taken back.

commentId

The ID of the comment the vote was made on. It’s only on schemas that can view comments.

comment

The comment the vote was made on. It’s only on schemas that can view comments.

userId

The ID of the user who voted. It’s only on schemas that can view users.

user

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
    }
}

id

The ID of the report. It’s always null, as reports aren’t exposed by ID.

commentId

The ID of the reported comment. It’s only on schemas that can view comments.

comment

The reported comment. It’s only on schemas that can view comments.

userId

The ID of the user who made the report. It’s only on schemas that can view users.

user

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"
    }
}

version

The version of the preparation, from protection.version.

preparation

The signed preparation, from protection.manifest.preparation.

challenges

The tokens your front end collected for the challenges in manifest.challenges, as turnstile or recaptchaV3. Leave it out when no provider is enabled.