gpt4 book ai didi

javascript - 即使缺少所需模式的部分,GraphQL POST 也会通过

转载 作者:行者123 更新时间:2023-11-30 21:01:54 25 4
gpt4 key购买 nike

我有一个这样定义的突变:

# POST a new Translation
createTranslation(
name: String!
options: [OptionInput!]!
): Translation

根据我的理解,[OptionInput!]! 使用两个引号 means that an array is REQUIRED, and an object of the type OptionInput is also REQUIRED.

但是,从我的客户端,我可以在 Options 数组中没有 任何 对象的情况下成功发布:

const createTranslation = gql`
mutation createTranslation(
$name: String!,
$options: [OptionInput!]!
) {
createTranslation(
name: $name,
options: $options
) {
_id
}
}
`;

即使我期望开箱即用的验证错误(示例响应):

{
"data": {
"translations": [
{
"_id": "59fa1b7a3d4d4805ed6f7539",
"name": "Sample Translation Name!!!",
"options": []
}
]
}
}

确保使用对象数组的子字段不包含任何空值的正确方法是什么?

PS:这是我的输入OptionInput

input OptionInput {
text: String!
value: String!
}

最佳答案

如文档所述,如果列表中的类型为非空:

This means that the list itself can be null, but it can't have any null members.

这意味着空数组仍然有效,但任何项目本身为 null 的数组则无效。

[OptionInput] 表示所有这些都是有效的:

  • [{},null]
  • [{},{}]
  • []

[OptionInput!] 表示所有这些都是有效的:

  • [{},{}]
  • []

[OptionInput]! 表示所有这些都是有效的:

  • [{},null]
  • [{},{}]
  • []

[OptionInput!]! 表示所有这些都是有效的:

  • [{},{}]
  • []

如果您需要阻止客户端提供空数组,您需要在解析器中检查参数是否为空数组,然后抛出错误。

关于javascript - 即使缺少所需模式的部分,GraphQL POST 也会通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47062082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com