The payload
field can be Int
or String
scalar type.
when I write it like union type:
const schema = `
input QuickReply {
content_type: String
title: String
payload: Int | String
image_url: String
}
`
I got an error:
GraphQLError: Syntax Error GraphQL request (45:18) Expected Name, found |
44: title: String
45: payload: Int | String
^
46: image_url: String
It seems GraphQL
does not support union scalar type.
So, how can I solve this situation?
Scalars can't be used as part of unions, since per the specification, unions specifically "represent an object that could be one of a list of GraphQL Object types." Instead, you can use a custom scalar. For example:
This code effectively combines the behaviors for both the Int and String types, while still enforcing the range for 32-bit signed integers. However, you could have whatever type coercion behavior you want. Check out the source code to see how the built-in scalars work, or this article for more details around how custom scalars work.