本文介绍了GraphQL 查询:如果不为空则仅包含字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
GraphQL 是否有可能让客户端告诉服务器它只在该字段不是 null
时才需要该字段?
Does GraphQL have the possibility for the client to tell the server that it wants a field only if that field is not null
?
给定查询
query HeroAndFriends {
hero {
name
friends {
name
}
}
}
响应应该是这样的
{
"data": {
"hero": {
"friends": [
{
"name": "Luke Skywalker"
},
{
"name": "Han Solo"
},
{
"name": "Leia Organa"
}
]
}
}
}
代替
{
"data": {
"hero": {
"name": null,
"friends": [
{
"name": "Luke Skywalker"
},
{
"name": "Han Solo"
},
{
"name": "Leia Organa"
}
]
}
}
}
在不违反 GraphQL 规范的情况下这可能吗?
Is this possible without violating the GraphQL specification?
推荐答案
据我所知这是不可能的,有指令,如@skip 和@include.指令,但它们需要一个变量,我认为您可以与 graphql 团队一起说明情况,将指令扩展为仅包含不为空的字段.
As far as I know this is not possible, there are directives like @skip and @include. Directives but they need a variable, I think you can make your case with the graphql team to extend the directives to only include the field if it's not null.
这篇关于GraphQL 查询:如果不为空则仅包含字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!