简体   繁体   中英

Multiple builtin type for graphql

How can I create a multiple type for 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. 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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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