【问题标题】:Cannot set property 'clientMutationId' of undefined无法设置未定义的属性“clientMutationId”
【发布时间】:2016-02-23 05:59:14
【问题描述】:

我收到以下错误。尝试通过 graphiql 运行突变时。请帮助我解决此问题或指向我可以找到反应中继突变示例的链接。

mutation {
  createUser(input: {username: "Hamza Khan", clientMutationId: ""}) {
    user {
      id
      username
    }
  }
}

{
  "data": {
    "createUser": null
  },
  "errors": [
    {
      "message": "Cannot set property 'clientMutationId' of undefined",
      "locations": [
        {
          "line": 17,
          "column": 2
        }
      ]
    }
  ]
}

这里是突变定义

import {
  GraphQLString,
  GraphQLInt,
  GraphQLFloat,
  GraphQLList,
  GraphQLObjectType,
  GraphQLID,
  GraphQLNonNull
} from 'graphql';

import {
  connectionArgs,
  connectionDefinitions,
  connectionFromArray,
  fromGlobalId,
  globalIdField,
  mutationWithClientMutationId,
  nodeDefinitions,
} from 'graphql-relay';

import {User} from './usermodel';
import {UserType} from './usertype';

export const UserMutations = {};
UserMutations.createUser = mutationWithClientMutationId({
  name: 'CreateUser',
  inputFields: {
    username: {type: new GraphQLNonNull(GraphQLString)}
  },
  outputFields: {
    user: {
      type: UserType,
      resolve: (payload) => {
        return User.getUserById(payload.userId);
      }
    }
  },
  mutateAndGetPayload: (args) => {
    let newUser = new User({ username: args.username });
    newUser.save().then((user) => {
      return {userId: user.id};
    }).error((err) => {return null;});
  }
});

【问题讨论】:

  • 该错误几乎可以肯定是因为您没有正确加载用户。可能是因为您只是将用户名作为输入发送。
  • 我可以在 mutateAndGetPayload 中控制台登录新用户,但奇怪的是 outputFields 中的解析函数没有执行。
  • 可能是您没有查询用户。如果您不请求字段,则不会调用输出字段的解析函数。
  • 我正在使用两个字段 Id 和用户名查询用户。
  • 您可以在为您的架构定义突变类型的位置添加代码吗?例如UserMutations.createUser 引用在哪里?

标签: relayjs


【解决方案1】:

你必须返回来自mutateAndGetPayload的东西——在你的情况下,一个承诺。

mutateAndGetPayload: ({username}) => {
  const newUser = new User({username});
  return newUser.save().then(user => ({userId: user.id}));
}

【讨论】:

  • 此外,如果出现错误,我不确定您是否要返回空有效负载。您可能想要 throw 以便错误可以通过您的 webstack 冒泡并输出到 Relay,以便客户端突变可以知道突变已失败。
  • 就我而言,我在返回的承诺中还有第二个承诺。第二个承诺被拒绝。这会导致再次出现错误“无法设置未定义的属性 'clientMutationId'”。不知道什么是最好的处理方式。
  • 好吧,我的错。我的代码中有一个愚蠢的错误。回报在我承诺的“当时”之内。我没有从主流中返回。
猜你喜欢
  • 2017-04-26
  • 2021-11-27
  • 2022-10-30
  • 2023-02-10
  • 2015-12-07
  • 2013-05-28
  • 2011-11-20
相关资源
最近更新 更多