简体   繁体   English

graphql的多个内置类型

[英]Multiple builtin type for graphql

How can I create a multiple type for graphql? 如何为graphql创建多重类型? I have this given code: 我有这个给定的代码:

status: {
  type: GraphQLString || GraphQLBoolean,
  resolver: Blahblah
}

obviously this doesn't work coz it converts the boolean to string, I want to support both boolean and string. 显然,这不起作用,因为它将布尔值转换为字符串,我想同时支持布尔值和字符串。

I can't just make them string and adjust the code because other projects are using them and it will take sometime after I can contact the other devs, And yes we have a bad data on our database. 我不能仅仅使它们成为字符串并调整代码,因为其他项目正在使用它们,因此我可能需要一段时间才能联系其他开发人员。是的,我们的数据库中有错误的数据。

Thanks in advance. 提前致谢。

GraphQL is all about specificity. GraphQL与特异性有关。 There is intentionally very little room for ambiguity in the spec, even when it eliminates convenient shortcuts, I think one solution to the problem here could be a Union type, whereby you define a type that is a union of other types, so for example 规范中故意存在歧义的空间很小,即使消除了便捷的快捷方式,我认为这里的一个解决方案可能是Union类型,即您定义的类型是其他类型的Union,例如

type StringType {
  fieldName: String
}

type BooleanType {
  fieldName: Boolean
}

union StringBool = StringType | BooleanType

myQuery(someArg: String!): StringBool

Keep in mind that this is only an acceptable type as a return value for queries. 请记住,这只是可接受的类型,可以作为查询的返回值。 It is not an acceptable input type for mutations. 对于突变,这不是可接受的输入类型。 When you specify the fields you want returned from your query, you will have to "fragment" on the different types, and explain which fields you want back from each possible member of the union: 当您指定要从查询中返回的字段时,您将不得不“碎片化”不同的类型,并说明要从联合的每个可能成员中返回的字段:

... on StringType {
  fieldName
}
... on BooleanType {
  fieldName
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM