Skip to content
Advertisement

Node Fetch Post Request using Graphql Query

I’m trying to make a POST request with a GraphQL query, but it’s returning the error Must provide query string, even though my request works in PostMan.

Here is how I have it running in PostMan:

enter image description here

enter image description here

And here is the code I’m running in my application:

JavaScript

Any ideas what I’m doing wrong? Is it possible to make it so that the body attribute I’m passing in with the fetch request is formatted as Text like I’ve specified in the PostMan request’s body?

Advertisement

Answer

The body is expected to have a query property, containing the query string. Another variable property can be passed as well, to submit GraphQL variables for the query as well.

This should work in your case:

JavaScript

This is how to submit GraphQL variables:

JavaScript

I created a complete example on GitHub.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement