我收到了以下错误。当试图通过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;});
}
});
答案 0 :(得分:11)
你必须从<{1}}返回 - 在你的情况下,这是一个承诺。
mutateAndGetPayload