如何指定采用多种类型的graphql类型?

时间:2018-06-12 04:13:39

标签: graphql graphql-js

我想创建一个可以返回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.

任何解决方案都非常有用。

1 个答案:

答案 0 :(得分:3)

遵循此https://github.com/facebook/graphql/issues/215,Graphql目前不支持scaler union类型,你可以这样做

union IntOrString = IntBox | StringBox

type IntBox {
  value: Int
}

type StringBox {
  value: String
}

或者您可以拥有自定义类型,请参阅此graphql, union scalar type?

相关问题