haskell-graphql / graphql-api

Write type-safe GraphQL services in Haskell
BSD 3-Clause "New" or "Revised" License
406 stars 35 forks source link

Is there a way to read-in the type of user input (Union Type) #191

Closed EdmundsEcho closed 6 years ago

EdmundsEcho commented 6 years ago

I have a Union type defined in GraphQL. Is there a way to define a AnnotatedInputType that pattern matches based on the the specific type provided by the user? I'm now using three optional Arguments, one for each of the types in my Union. It seems clunky, but works. Thanks for letting me know.

Update 1

In the starwars tests, the code instantiates human using a case statement that pattern matches

... \case ->
[Argument "episode" (ValueInt n)] -> ... fromIntegral n
[Argument "episode" (ValueEnum "NEWHOPE")] -> ... 
...
_ -> empty

How does this approach play out in the latest iteration of the library?

Update 2

The GraphQL specification does not currently support a Union Type for an Input Object. However, it turns out I'm in good company regarding this question. There is an open RFC to discuss the benefits of implementing the feature in the specification. Here is a link.

In the meantime, there are several alternative approaches. One of them being, as the currently library allows, is to use several Arguments each with a unique field name, with optional input, one for each input type.

- E