purescript-graphql
purescript-graphql copied to clipboard
Using variant for Unions
I am currently struggling with union types. One idea was to return a tuple of output type and value from a resolve type function. The problem is, that this does not work out in the type signature:
resolveType :: forall t. OutputType m t => a -> forall b. Tupe (t b) b
Furthermore, it is not introspectable which output types are potentially returned from the function.
Another idea could be to require a record of possible output types in the union type definition:
aType :: GQL.ObjectType Context AValue
aType = -- ...
bType :: GQL.ObjectType Context BValue
bType = -- ...
myUnionType :: GQL.UnionType Context (Variant (a :: AValue, b :: BValue))
myUnionType = GQL.union "MyUnion" { a: aType, b: bType }
Now this type's parent value has type: (Variant (a :: AValue, b :: BValue)). We can now make usage of this type more convenient using cmap as described in #23:
myUnionType :: GQL.UnionType Context (Either AValue BValue)
myUnionType =
cmap (either (inj (SProxy :: SProxy "a")) (inj (SProxy :: SProxy "b"))) $
GQL.union "MyUnion" { a: aType, b: bType }
Instead of Either we could use any other sum type.