Description
Typed Scalars
Some time before i started writing subset language of GQL (with name iris) which would take best parts of GQL and simplify and generalize it. although it made a lot of fun writing it, i think best way to solve problems would be to introduce this features directly in GQL, one of which is typed scalars (with name data
).
motivation
- support input unions with non breaking changes
- support tree types (see why GQL needs typed scalars)
- GQL can be used for
server-to-server
requests (likeRPC
)
hypothetically it can make enums/inputObjects
redundant and reduce schema complexity.
I know the requirement for new features in the language is high, but I think this feature can be beneficial and removes unnecessary limitations for the language. for now, instead of modeling tree types in GQL, people either go to another language or model them with untyped JSON objects, which is not such a good option. same problem is with input unions.
definition
core idea of typed scalars is to extend GQL type system with typed JSON objects. this new kind of data
types will not resolve any value (no dedicated type resolvers) but only check their validity.
in my opinion use of algebraic data types for their definitions would be most suitable (syntax is described in language definition iris).
data Image = { src: String! } # standalone variant so called "variant type"
data RichText
= NewLine {} # can accept string literal "NewLine" or {__typename:"NewLine"}
| Text { text : String! } # inline variant is only accessable inside Richtext
| Node { children: [RichText!]! } # inline variant is only accessable inside Richtext
| Image # reference of variant type
since there is already implementation for this feature in iris, i could freely open PR for that.
non breaking
client with old GQL version will read it as scalar types with type definition as JSDoc annotations (PR in apollo) in description. however for new clients it will provide field dataVariants
: which will include description of variant definitions for the type.