Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL Mutation Body Parser

Just updated to 'graphql-server-express' version 1.3.0 and now I am getting this error when running any mutations:

POST body missing. Did you forget use body-parser middleware?

When initializing the server, I am including the "body-parser" package so I am not sure what is going on here. Any ideas?

Server Config:

    //GraphQL Server
    graphQLServer.use(

        //GRAPHQL Endpoint
        `/${settings.public.graphql.endpoint}`,

        //Parse JSON
        bodyParser.json(),

        //authenticate
        authenticateRequest,

        //GRAPHQL Server
        graphqlExpress(req => {

            return {
                schema: schema,
                context: req
            }
        })

    );

Example Request:

curl 'http://localhost:5050/graphql' \
  -H 'authorization: Bearer xxxxxxxxxxx.xxxxxxxxxxx' \
  -d '{
    "query": "mutation { sensorData(sensorValue: \u0027asdasdasdasd \u0027)}",
    "variables": {}
    }
  }'
like image 883
matt Avatar asked Dec 18 '17 03:12

matt


1 Answers

Set Content-Type: application/json header in the request (docs)

curl 'http://localhost:5050/graphql' \
  -H "Content-Type: application/json" \
  -H 'authorization: Bearer xxxxxxxxxxx.xxxxxxxxxxx' \
  -d '{
    "query": "mutation { sensorData(sensorValue: \u0027asdasdasdasd \u0027)}",
    "variables": {}
    }
  }'
like image 92
Bless Avatar answered Oct 03 '22 02:10

Bless