GraphQL Mutation Body Parser
Asked Answered
K

1

5

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": {}
    }
  }'
Katinka answered 18/12, 2017 at 3:5 Comment(0)
S
8

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": {}
    }
  }'
Strapper answered 18/12, 2017 at 6:55 Comment(1)
Thanks Bless! Adding the Content-Type worked for me. Appreciate your input :)Katinka

© 2022 - 2024 — McMap. All rights reserved.