GraphQL:不可为空的数组/列表 [英] GraphQL: Non-nullable array/list

查看:27
本文介绍了GraphQL:不可为空的数组/列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习 GraphQL,在学习教程的过程中,我遇到了一些我无法理解的行为.假设我们已经在模式中定义了类型:

I'm learning GraphQL now and while walking through tutorial I met behavior that I can't understand. Let's say we have defined type in schema:

type Link {
  id: ID!
  url: String!
  description: String!
  postedBy: User
  votes: [Vote!]!
}

由于文档 votes: [Vote!]! 意味着该字段应该是不可为空的,并且数组本身也应该是不可为空的.但就在教程的作者显示查询示例之后,对于某些链接,它为 votes 字段返回空数组.像这样:

Due to docs votes: [Vote!]! means that that field should be a non-nullable and array itself should be non-nullable too. But just after that author of tutorial shows example of query and for some of links it returns empty array for votes field. Like this:

{
 "url": "youtube.com",
 "votes": []
},
{
 "url": "agar.io",
 "votes": []
}

所以我的问题是:在 graphQL 模式中,不可为空"是否意味着空",或者它只是 graphQL 服务器的某种错误行为(我的意思是它返回空数组而没有警告应该有什么到期到架构).

So my question is: Doesn't "non-nullable" means "empty" in graphQL schema or it's just some kind of wrong behavior of graphQL server (I mean it returns array of nothing without warning that there should be something due to schema).

谢谢!

推荐答案

Non-null 的意思就是它听起来的样子——不是 null.空数组不为空——它仍然返回一个值.

Non-null means exactly what it sounds like -- not null. An empty array is not null -- it's still returning a value.

这是一个汇总表:

declaration accepts: | null | []   | [null] | [{foo: 'BAR'}]
------------------------------------------------------------------------
[Vote!]!             | no   | yes  | no     | yes
[Vote]!              | no   | yes  | yes    | yes
[Vote!]              | yes  | yes  | no     | yes
[Vote]               | yes  | yes  | yes    | yes

[Vote!]! 表示该字段(在本例中为 votes)不能返回 null 它必须解析为一个数组并且该数组中的任何单个项目都不能为空.所以 [][{}][{foo: 'BAR'}] 都是有效的(假设 foo 非空).但是,以下抛出:[{foo: 'BAR'}, null]

[Vote!]! means that the field (in this case votes) cannot return null and that it must resolve to an array and that none of the individuals items inside that array can be null. So [] and [{}] and [{foo: 'BAR'}] would all be valid (assuming foo is non-null). However, the following would throw: [{foo: 'BAR'}, null]

[Vote]! 表示该字段不能返回 null,但返回列表中的任何单个项都可以为 null.

[Vote]! means that the field cannot return null, but any individual item in the returned list can be null.

[Vote!] 表示整个字段可以为空,但如果确实有返回值,则需要一个数组,该数组中的每一项都不能为空.

[Vote!] means that the entire field can be null, but if it does return a value, it needs to an array and each item in that array cannot be null.

[Vote] 表示整个字段可以为空,但如果确实返回值,则需要一个数组.但是,数组的任何成员也可能为空.

[Vote] means that the entire field can be null, but if it does return a value, it needs to an array. However, any member of the array may also be null.

如果您需要验证数组是否为空,则必须在解析器逻辑中进行.如果您希望 GraphQL 在数组为空时仍然抛出,只需让您的解析器返回一个被拒绝的 Promise.

If you need to verify whether an array is empty, you have to do so within your resolver logic. If you want GraphQL to still throw when an array is empty, just have your resolver return a rejected Promise.

有关更多信息,您可以阅读列表和非空 GraphQL 介绍部分或查看 规格.

For more information your can read the list and non-null section of the GraphQL introduction or take a look at the spec.

这篇关于GraphQL:不可为空的数组/列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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