无法设置未定义的属性“clientMutationId”

时间:2015-11-20 16:48:15

标签: relayjs

我收到了以下错误。当试图通过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;});
  }
});

1 个答案:

答案 0 :(得分:11)

你必须从<{1}}返回 - 在你的情况下,这是一个承诺。

mutateAndGetPayload