Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set property 'clientMutationId' of undefined

Tags:

relayjs

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;});
  }
});
like image 977
user1762608 Avatar asked Nov 20 '15 16:11

user1762608


1 Answers

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}));
}
like image 108
steveluscher Avatar answered Nov 22 '22 15:11

steveluscher