Graphql post body "Must provide query string."
ExpressGraphqlGraphql JsExpress Problem Overview
I use Express-graphql middleware. I send the following request in the body line:
POST /graphql HTTP/1.1
Host: local:8083
Content-Type: application/graphql
Cache-Control: no-cache
Postman-Token: d71a7ea9-5502-d5fe-2e36-0ae49c635a29
{
testing {
pass(id: 1) {
idn
}
}
}
and have error
{
"errors": [
{
"message": "Must provide query string."
}
]
}
in graphql i can send update in URL.
URL string is too short. i must send update model like
mutation {
update(id: 2, x1: "zazaza", x2: "zazaza", x3: "zazaza" ...(more more fields)...) {
idn
}
}
i think its must be in request body. How can i send 'update' query or that i'm doing wrong?
Express Solutions
Solution 1 - Express
Post request needs to manage headers info.
-
Using Http client: Content-Type application/json
-
Using Postman client: Content-Type application/graphql
but request body looks like string
{"query":"mutation{update(id:1,x1:\"zazaz\",x2:\"zazaz\"......){id x1 x2}}"}
Solution 2 - Express
If you are using graphql and want to test it using postman or any other Rest client do this.
In postman, select POST
method and enter your URL
and set Content-Type
as application/graphql
then pass your query in the body.
Example:
http://localhost:8080/graphql
Mehtod: POST
Content-Type: application/graphql
Body:
query{
FindAllGames{
_id
title
company
price
year
url
}
}
Thats it you will get the response.
Solution 3 - Express
Using Postman Version 7.2.2 I had a similar issue. This version of Postman supports Graphql out of the box. Changing the Content-type to application/json
fixed it for me.
Solution 4 - Express
for me worked like as following:
In the body
In the Headers
Don't forget mark GraphQl [x] on Body settings
And how was quoted before changes the verb to POST.
Solution 5 - Express
This generally occurs when your 'express-graphql' doest receive any params. You need to added a json/applicaton parser in your application.
npm install body-parser
eg -
const bodyParser = require('body-parser');
app.use(bodyParser.json()); // application/json
Solution 6 - Express
Switch content type to JSON. Like this