Cannot set property 'clientMutationId' of undefined

一笑奈何 提交于 2019-12-23 10:07:12

问题


I am getting following error. when trying to run a mutation via graphiql. Please help me resolve this issue or point to a link where I can find react relay mutations example.

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
        }
      ]
    }
  ]
}

Here is the mutation definition

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:


You have to return something from mutateAndGetPayload – in your case, a promise.

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


来源:https://stackoverflow.com/questions/33831911/cannot-set-property-clientmutationid-of-undefined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!