GraphQL查询并检查返回的数据 [英] GraphQL query and check the returned data
本文介绍了GraphQL查询并检查返回的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个包含支票的查询,例如,在查询用户时,只需要具有非空电子邮件的用户.我如何添加这种支票?谢谢!
I would like to have a query containing a check, for example when querying users, only users that have a non-null email are wanted. How could I add such check? Thanks!
users {
id,
username,
email
}
推荐答案
GraphQL字段可以带有参数.
因此,您可以将一个名为onlyNonNullEmail: boolean
的参数传递给您的users
字段:
GraphQL fields can have arguments.
So you could pass an argument to your users
field, named onlyNonNullEmail: boolean
:
type Query {
users(onlyNonNullEmail: Boolean) {
...
}
}
然后,在您的resolve函数中:
Then, inside your resolve function:
users: (_, args, context, ast) => {
const { onlyNonNullEmail } = args;
// If onlyNonNullEmail is true, return only users with a non-null email
// Else proceed as usual
}
这篇关于GraphQL查询并检查返回的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文