comments icon indicating copy to clipboard operation
comments copied to clipboard

GraphQL - cannot query comments using element uri or slug

Open bzin opened this issue 3 years ago • 11 comments

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

  1. Create a couple of comments and approve these (in my case, the comments were done with guest enabled)
  2. Fetch these with element slug or uri return empty array

Craft CMS version

4.2.4

Plugin version

2.0.2.1

Multi-site?

No

Additional context

No response

bzin avatar Sep 19 '22 13:09 bzin

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.

engram-design avatar Sep 19 '22 22:09 engram-design

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

bzin avatar Sep 20 '22 10:09 bzin

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.

bzin avatar Sep 20 '22 10:09 bzin

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?

engram-design avatar Sep 20 '22 10:09 engram-design

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.

bzin avatar Sep 20 '22 14:09 bzin

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

engram-design avatar Sep 20 '22 21:09 engram-design

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! :)

bzin avatar Sep 21 '22 14:09 bzin

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

bzin avatar Sep 21 '22 14:09 bzin

Strange. status is a property inherited from the ElementInterface

engram-design avatar Sep 21 '22 21:09 engram-design

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?

bzin avatar Sep 22 '22 07:09 bzin

Not quite sure, looking into it though! I'm also on the same Craft 4 version as you.

engram-design avatar Sep 22 '22 22:09 engram-design