您将字段的类型定义为UserType.即使它是一个变异,它仍然遵循与查询相同的规则和行为.因为UserType是对象类型,所以它需要嵌套字段.
mutation _ { updateCurrentUser(fullName: "Syava", email: "fake@gmail.com") { fullName email } } // would respond with { fullName: 'Syava', email: 'fake@gmail.com' }
如果您不希望变异返回User,则可以将其类型声明为GraphQLBoolean - 例如,这是一个标量,并且没有任何嵌套字段.
{ type: GraphQLBoolean, args: { fullName: { type: GraphQLString }, email: { type: new GraphQLNonNull(emailType) }, password: { type: GraphQLString }, }, resolve: async (root, { fullName, email, password }, { rootValue }) => { const user = await User.findById(rootValue.req.user.id); user.fullName = fullName; user.password = password; // or hashed to not store plain text passwords return user.save(); // assuming save returns boolean; depends on the library you use } }