【发布时间】:2018-11-21 09:04:18
【问题描述】:
我想创建一个可以返回Array of Integers 或String 的graphql 类型。
我已经尝试在表单中使用 union
union CustomVal = [Int] | String,但这会返回错误。
架构声明是:
union CustomValues = [Int] | String
type Data {
name: String
slug: String
selected: Boolean
values: CustomValues
}
错误是:
node_modules/graphql/error/syntaxError.js:24
return new _GraphQLError.GraphQLError('Syntax Error: ' + description, undefined, source, [position]);
Syntax Error: Expected Name, found [
GraphQL request (81:23)
80:
81: union CustomValues = [Int] | String
这可以在graphql中做到吗?如果没有,您能否提出一个替代方案。
我问这个,因为工会文档说Note that members of a union type need to be concrete object types; you can't create a union type out of interfaces or other unions.
任何解决方案都会非常有帮助。
【问题讨论】:
-
您可以尝试从
[Int]中删除[]吗? -
仍然抛出错误,我希望该类型采用
Array of Integers或String,并且不是在graphql 中使用[]声明的数组类型?错误是Union type CustomValues can only include Object types, it cannot include Int. Union type CustomValues can only include Object types, it cannot include String. -
如果你想要一个列表(数组),你需要使用
[],但是如果你想在不同的输入上返回不同的类型,那么你必须在你的解析器函数中定义如何做到这一点.
标签: graphql graphql-js