我收到以下錯誤。當試圖通過graphiql運行突變時。請幫我解決這個問題或指向一個鏈接,我可以找到反應中繼突變的例子。無法設置屬性'clientMutationId'undefined
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;});
}
});
錯誤幾乎肯定是因爲您沒有正確加載用戶。可能是因爲你只是發送一個用戶名作爲輸入。 –
我可以控制檯在mutateAndGetPayload中記錄新用戶,但是在outputFields中奇怪的解析函數沒有執行。 – user1762608
它可能是你不查詢用戶。如果您不請求字段,則不會調用輸出字段的解析函數。 –