gpt4 book ai didi

没有子部分的 GraphQL 突变

转载 作者:行者123 更新时间:2023-12-03 12:58:28 27 4
gpt4 key购买 nike

我想发送没有子部分的 graphql 突变请求

mutation _ {
updateCurrentUser(fullName: "Syava", email: "fake@gmail.com")
}

我得到

{
"errors": [
{
"message": "Field \"updateCurrentUser\" of type \"User\" must have a sub selection.",
...
}
]
}

添加 { id } 请求工作正常,但我不想要

还有架构代码
const userType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: { type: new GraphQLNonNull(GraphQLString) },
fullName: { type: GraphQLString },
email: { type: GraphQLString },
}),
});

type: userType,
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);

...

return user;
},

最佳答案

您将字段的类型定义为 UserType。即使它是一个突变,它仍然遵循与查询相同的规则和行为。因为 UserType 是一种对象类型,所以它需要嵌套字段。

mutation _ {
updateCurrentUser(fullName: "Syava", email: "fake@gmail.com") {
fullName
email
}
}
// would respond with { fullName: 'Syava', email: 'fake@gmail.com' }

如果您不希望突变返回用户,则可以将其类型声明为 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
}
}

关于没有子部分的 GraphQL 突变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34075018/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com