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