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

查看:97
本文介绍了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!]!
}

由于docs 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).

谢谢!

推荐答案

非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 ,并且它必须解析为数组,并且该数组中的单个项目可以为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]表示整个字段可以为空,但如果它确实返回一个值,则需要一个数组.但是,数组的任何成员也可以为null.

[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.

有关更多信息,您可以阅读列表和非空部分,或查看

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天全站免登陆