Open sunwukonga opened 6 years ago
I have solved this for myself, but I need feedback before I presume to push it as a PR.
I included a [t]
as an instance of HasAnnotatedInputType
in API.hs
:
instance forall t. (HasAnnotatedInputType t) => HasAnnotatedInputType [t] where
getAnnotatedInputType = TypeList . ListType <$> getAnnotatedInputType @t
the same definition as for List t
^^^.
Also included a Defaultable
instance for [a]
in Resolver.hs
:
instance Defaultable [a]
Which allows the following:
type User = Object "User" '[]
'[ Argument "friendNames" [Text] :> Field "friends" (List Text) ]
myFriends :: [Text] -> Handler IO (List Text)
myFriends friends = pure $ map pure friends
I feel like I should have been able to convince the compiler that List a
and [a] are the same for HasAnnotatedInputType a
, but I am having trouble even grokking how GraphQL.API.List leaves the realm of Types and becomes a usable value.
@teh is probably best placed to help with this issue. Our type gymnastics are a bit much for me.
It heartens me that I'm not the only one ;)
@sunwukonga I had a go at this last weekend and I'm not so sure what I did any more either! I feel you are on the right track (we forgot to implement HasAnnotatedInputType
for lists) but right now I'm not 100% sure yet that your solution is correct (I'm 90% sure though!). I'm sorry it's taking a while.
@teh any update on whether the solution suggested by @sunwukonga can be pushed as a fix? Even if you are not fully sure we can perhaps try it and see if users find any problem with it.
I tried this solution and so far it is working fine for me.
@harendra-kumar if it's working for you and @sunwukonga we should include it. Do you want to open a PR?
With the information provided in this post, I have been using the following patch to enable user input :: [input]
that behaves accordingly in my handlers. I import the Patch
where I would otherwise do so for GraphQL.API
.
module Patch
(
module Patch
, module GraphQL.API
)
where
import GraphQL.API
import Protolude hiding (Enum, TypeError)
import qualified GraphQL.Internal.Schema as Schema
instance Defaultable [a]
instance forall t. (HasAnnotatedInputType t)
=> HasAnnotatedInputType [t] where
getAnnotatedInputType = Schema.TypeList . Schema.ListType <$> getAnnotatedInputType @t
The following query works:
against a
toApplication @RootQuery rootHandler request respond
on the following:However, I am having trouble doing this with an argument like:
This results in an error:
The issue lies here ...............................................................................^^^^^ The types say that the argument needs to be
List Text
, but the handler function takes[Text]
If I set the type signature of the handler `myFriends' to:I get the following error:
Which is no surprise, given this from GraphQl/Resolver.hs:
Ultimately, what I interpret this to mean is that
type User
should be defined as:Which results in the error: