verbb / comments

A Craft CMS plugin for managing comments directly within the CMS.
Other
137 stars 33 forks source link

How can I add a new comment via GraphQL #276

Closed Jones-S closed 1 year ago

Jones-S commented 1 year ago

Question

I am trying to add a new comment via graphql. I can see that mutations were added at some point (https://github.com/verbb/comments/pull/227).

I am using a craft plugin to retrieve a JSON web token I want to use for authentication when doing the mutation. (https://graphql-authentication.jamesedmonston.co.uk/).

Now with that JWT I should be able to make an apollo mutation to the graphql service and sending my JWT as Authentication header (Bearer) or am I missing something?

I currently can't figure out how I can add a comment via graphql. Unfortunately this example here (https://github.com/verbb/comments/issues/242) is not ready, but it would have helped a lot.

Thanks for your help in advance. Cheers

Additional context

No response

engram-design commented 1 year ago

Be sure to check out the mutations docs if you haven't already?

Jones-S commented 1 year ago

Thank you for the hint. Of course I did :). But even when using that in my GraphiQL I get the error that I can't add a comment.

image

I don't really understand the error message, so I am not sure if the error refers to something else than the login-issue. But even when I am removing the Authorization headers I get the same message.

Of course I don't know if I even get an authorization problem when doing things in GraphiQL, because obviously I am logged in. But if the problem results from something else I don't know what setting is missing.

Any help appreciated, there are quite a few ends where I may make mistakes...

Jones-S commented 1 year ago

Hm I think I need to configure my nginx.conf instead of my .htaccess because I don't have Apache. Right now I don't know why my calls go through anyway, but 🤷‍♂️

When trying to mutate via apollo I now don't get an authorization issue anymore, but the response looks like this:

image

No idea why it's German, but it says that comments are disabled for that element. As far as I know I have comments enable for all kind of entries and assets.

image

What am I missing?

Jones-S commented 1 year ago

Another approach: I simplified things to reduce options for mistakes. I now send the mutation with the app insomnia. I created a GraphQL token in the CP and I am able to create new entries.

So far so good, but now I am trying to send the NewComment mutation and I still get Comments are disabled for this element.

But how the hell can I enable them? As shown above all things should be commentable. I am currently sending these variables:

{
    "newParentId": null,
    "ownderId": 69, <---- double checked and this entry exists in the CP
    "name": "Nickname",
    "email": "test@test.com",
    "comment": "test comment"
}

Where the hell do I have to enable these comments? I just can't figure it out....

engram-design commented 1 year ago

I can't comment on the Nginx/Apache issues (maybe something to do with JWT?) but that'll likely be more a Craft issue.

That is indeed a bit strange. Is your site a multi-site? Just thinking maybe the entry isn't available in the site you're targeting. Otherwise, I agree it should be that simple.

Jones-S commented 1 year ago

Well that Apache/Nginx aside, I can debug that as soon as I have a state where things run smoothly :).

So no, it is not a multi-site. I also tried different IDs. I tried to comment on assets, different types of entries etc. And it just does not work.

I also made sure that the schema which the token is assigned to, has all possible rights. So it can't be that either...

Oh and what about GraphiQL. Is there a way I should be able to create a comment via GraphiQL? Cause the I mean I should be just able to paste in the newComment mutation and add some variables and boom... (which does not work, no matter if I send Bearer token or not in headers)

engram-design commented 1 year ago

Oh and what about GraphiQL

Yep, should be the same principle, and what I'm primarily testing with. I'm wondering if you might be able to add some debugging code to the vendor files just for testing (and test with GraphiQL for convenience).

At this line add the following line:

Craft::dd(($element->id ?? null));

And then run the query with GraphiQL. It looks like for some reason, it's struggling to find the owner element (which seems very strange given the simplistic code).

Jones-S commented 1 year ago

I did that and I see this:

image

I have no idea what this means... can't be the thing you hoped for, right?

I am pretty sure I added the code in the correct place:

image

Oh and my variables look like this:

{
    "newParentId": null,
    "ownderId": 69,
    "name": "name",
    "email": "test@dystpoioe.ch",
    "comment": "test"
}

But I also tried to add some random ParentIds. It all looks the same though...

Jones-S commented 1 year ago

Now I just returned true within the checkPermissions function. It does now create comments via GraphiQL:

image

But you can see that it always thinks that the comment belongs to a deleted element...

engram-design commented 1 year ago

Yeah, just as I thought, it's reporting back null which means that it's failing to find the owner element.

Also, just want to check if that's a typo in your variables - "ownderId": 69, should be "ownerId": 69,?

Jones-S commented 1 year ago

Omg, it can't be that!!! I even removed that typo once, because I realized it was in there. Unfortunately I went back in the GraphQL history and the typo was back in...

It looks like it works. With insomnia as well. Thank you so much. 👍 Such a stupid mistake 🙈

engram-design commented 1 year ago

Haha, all good and easy mistake! Glad we got to the bottom of it.