GraphQL查询:如果不为null,则仅包含字段 [英] GraphQL query: only include field if not null

查看:43
本文介绍了GraphQL查询:如果不为null,则仅包含字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有当该字段不是 null 时,GraphQL才有可能让客户端告诉服务器它想要一个字段吗?

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?

推荐答案

据我所知这是不可能的,其中有

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查询:如果不为null,则仅包含字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆