Discriminated union type #356
-
Is it possible to set up a discriminated union when defining a schema and doing polymorphic queries? Example: // .esdl
scalar type ShapeKind extending enum<Circle, Square>;
abstract type Shape {
require property kind => ShapeKind
}
type Circle extending Shape {
required property kind -> ShapeKind.Circle
required property radius -> decimal
}
type Square extending Shape {
required property kind -> ShapeKind.Circle
required property sideLength -> decimal
} // query
e.select(e.Shape, shape => ({
...e.is(e.Circle, { ...e.Circle['*']}),
...e.is(e.Square, { ...e.Square['*']}),
}));
const result = await query.run(client);
/* (Circle | Square)[] */ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
It's not possible currently, and it's tricky to implement. It's hard to know what the discriminator key should be. I tried to make this work originally, but there are lots of edge cases, e.g. if the type passed into e.select(e.Shape, shape => ({
...e.is(e.Quadrilateral, { ...e.Quadrilateral['*']}),
...e.is(e.Square, { ...e.Square['*']}),
})); You can try using e.select(e.Shape, shape => ({
__type__: { name: true },
...e.is(e.Quadrilateral, { ...e.Quadrilateral['*']}),
...e.is(e.Square, { ...e.Square['*']}),
})); But the value of |
Beta Was this translation helpful? Give feedback.
It's not possible currently, and it's tricky to implement. It's hard to know what the discriminator key should be. I tried to make this work originally, but there are lots of edge cases, e.g. if the type passed into
e.is
is itself generic:You can try using
__type__
to differentiate the options at runtime but it won't be reflected in the type.But the value of
__type__.name
will never beQuadrilateral
,…