`:union` type
zachdaniel:
Hey all, I’ve added a relatively experimental new type to ash core called
:union
(or
Ash.Type.Union
). Its in
main
currently. Some work will be done in the future to support rendering this automatically in
ash_graphql
(but not for input types, there are no union input types in ash_graphql so you’ll have to write your own input types there or just use
:json
or something for now). Its not as nice/easy as something like a typescript union, because this we are actually retaining the fact that this is a union type. Additionally, you won’t be able to filter on unions as intuitively as you like because unions will be
:map
(json) under the hood. Still, it could be useful for others and the ergonomics of it can be improved. Here is an example usage:
# using `tag` and `tag_value` to union maps (or embedded resources
attribute :metadata, :union do
constraints types: [
foo: [
type: MyApp.Foo,
tag: :type,
tag_value: :foo
],
bar: [
type: MyApp.Bar,
tag: :type,
tag_value: :bar
]
]
end
# using type casting (we try to cast it as each type in the list) for literals
attribute :width, :union do
constraints types: [
int: [
type: :integer
constraints: [
max: 10
]
],
string: [
type: :string,
constraints: [
match: ~r/regex/
]
]
]
end
Anyone using main is free to give it a try and let me know what you think 🙂
jharton:
Thanks for this <@197905764424089601> this is awesome. I’ll give it a whirl soon enough
zachdaniel:
The union type has shipped in
2.6.4
and graphql support for union types is in
main