GraphQL Mutation Body Parser

时间:2017-12-18 03:05:07

标签: curl post graphql express-graphql

刚刚更新为'graphql-server-express'版本1.3.0,现在我在运行任何突变时遇到此错误:

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

初始化服务器时,我包含“body-parser”包,所以我不确定这里发生了什么。有什么想法吗?

服务器配置:

    //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
            }
        })

    );

示例请求:

curl 'http://localhost:5050/graphql' \
  -H 'authorization: Bearer xxxxxxxxxxx.xxxxxxxxxxx' \
  -d '{
    "query": "mutation { sensorData(sensorValue: \u0027asdasdasdasd \u0027)}",
    "variables": {}
    }
  }'

1 个答案:

答案 0 :(得分:4)

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": {}
    }
  }'