【问题标题】:How do I specify a graphql type that takes multiple types?如何指定采用多种类型的 graphql 类型?
【发布时间】:2018-11-21 09:04:18
【问题描述】:

我想创建一个可以返回Array of IntegersString 的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 IntegersString,并且不是在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


【解决方案1】:

关注这个https://github.com/facebook/graphql/issues/215,Graphql 目前不支持缩放联合类型,你可以这样做

union IntOrString = IntBox | StringBox

type IntBox {
  value: Int
}

type StringBox {
  value: String
}

或者你可以有你的自定义类型,看到这个graphql, union scalar type?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2018-08-03
    • 2018-07-20
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多