GraphQL - cannot query comments using element uri or slug
Describe the bug
Executing a GraphQL query to fetch comments using uri or slug does not seems to be working.
On the documentation it is mentioned we can use element's uri or slug, but when I try to fetch comments with these I receive an empty array.
Using ownerId the comments do appear after executing the query.
Steps to reproduce
- Create a couple of comments and approve these (in my case, the comments were done with guest enabled)
- Fetch these with element
slugorurireturn empty array
Craft CMS version
4.2.4
Plugin version
2.0.2.1
Multi-site?
No
Additional context
No response
This is actually a mistake in the docs, sorry! Comments don't have a uri or slug so they can't be queried by them.
Hi @engram-design , I believe there's more things like this in documentation.
I just found out that you mention status in the docs but I am also not able to fetch anything if I use this.
Use case could be to show certain users pending comments.
query Page($uri: [String] = "__home__") {
entry(uri: $uri) {
id
title
}
comments(ownerId: 1, status: "null") {
uid
id
parent {
id
title
}
name
email
comment
}
}
{
"errors": [
{
"message": "Unknown argument \"status\" on field \"comments\" of type \"Query\".",
"extensions": {
"category": "graphql"
},
"locations": [
{
"line": 6,
"column": 24
}
]
}
]
}
By the way, replying to your previous comment, it would be quite nice to fetch using uri or slug on GraphQL. It's quite common to fetch pages using these. And if I can use only ownerId I will need to execute two graphql queries, one to fetch entry data, the other to fetch comments related to the entry.
Querying for pending comments should be fine:
{
comments(status: "pending") {
uid
id
}
}
The issue is, comments don't have a value for uri or slug. Do you mean to say you want to query based on the owner's uri or slug?
Yes, it would be great to get this based on the owner uri. Another solution would be fetching inside the entries or entry but I believe that is not possible right? The CommentInterface is only available on the root.
I'll see what I can do about that. And yes, we can't modify the properties available on an EntryInterface.
You could use the relatedToEntries.
{
comments(status: "pending") {
uid
id
relatedToEntries: {slug: "my-entry"}
}
}
Hi @engram-design was looking into this right now but unless I am doing something wrong or I am using an old version I am not able to use status to fetch comments.
The CommentInterface does not seem to have it:
{
"errors": [
{
"message": "Unknown argument \"status\" on field \"comments\" of type \"Query\".",
"extensions": {
"category": "graphql"
},
"locations": [
{
"line": 3,
"column": 46
}
]
}
]
}
My graphql query is:
query Comments($site: [String] = "enUS", $ownerId: [QueryArgument] = 1, $status: [QueryArgument] = "approved") {
comments(ownerId: $ownerId, site: $site, status: $status) {
... on Comment {
status
uid
id
dateCreated
dateUpdated
parent {
id
title
}
name
email
comment
profession # custom field
}
}
}
PS: will also look at the relatedToEntries suggestion you just made! :)
Tried your suggestion and aloso follow the EntryCriteriaInput which is what it's being use on relatedToEntries, comments returns an empty array.
{
comments(relatedToEntries: [{ uri: $uri, site: $site }]) {
id
uid
title
}
}
Strange. status is a property inherited from the ElementInterface
Shall I open a issue regarding this? Can it be that something change in a recent Craft version and Comments might not work as it should?
Not quite sure, looking into it though! I'm also on the same Craft 4 version as you.