嵌套结构上的GraphQL变异(GraphQL,JSON,Express,Axios)

时间:2018-02-05 19:27:23

标签: json express graphql axios

我遇到Mutation的问题。我可以查询数据。我喜欢实现“addEntity”函数来添加一个带有嵌套数据的新实体(primaryContact),但似乎无法正确设置它。

依赖项:express,express-graphql,graphql,axios,json-server

schema.js

    // Mutations
    const mutation = new GraphQLObjectType({
        name:'EntityMutation',
        fields:{
            addEntity:{
                type:EntityType,
                args:{
                    name: {type: new GraphQLNonNull(GraphQLString)},
                    primaryContact: {type: PrimaryContactType}

                },
                resolve(parentValue, args){
                    return axios.post('http://localhost:3000/entities', {
                        name: args.name,
                        primaryContact: {
                          name: args.primaryContact.name,
                          phone: args.primaryContact.phone
                        }
                    })
                    .then(res => res.data);
                }
            }
        }
    });

GraphQL条目

mutation{
  addEntity(
    name: "Tom d. Harry"
    primaryContact:{
      name: "Tommy"
      phone: "111-222-3333"
    })
  {
    name,
    primaryContact{
      name,
      phone
    }
  }
}

预期结果

{
    name: "Tom d. Harry",
    primaryContact: {
        name: "Tommy",
        phone: "111-222-3333",
    }
}

错误消息

{
  "errors": [
    {
      "message": "Cannot read property 'name' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "addEntity"
      ]
    }
  ],
  "data": {
    "addEntity": null
  }
}

0 个答案:

没有答案