GraphQL Mutation Body Parser

问题描述 投票:2回答:1

刚刚更新为'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": {}
    }
  }'
curl post graphql express-graphql
1个回答
4
投票

在请求中设置Content-Type: application/json标头(docs

curl 'http://localhost:5050/graphql' \
  -H "Content-Type: application/json" \
  -H 'authorization: Bearer xxxxxxxxxxx.xxxxxxxxxxx' \
  -d '{
    "query": "mutation { sensorData(sensorValue: \u0027asdasdasdasd \u0027)}",
    "variables": {}
    }
  }'
© www.soinside.com 2019 - 2024. All rights reserved.