graphql-api
graphql-api copied to clipboard
List of Object makes an error (Tried to treat object as if it were leaf field)
this code:
type Friend = Object "Friend" '[]
'[ Field "id" Text.Text, Field "email" Text.Text ]
type User = Object "User" '[]
'[ Field "id" Text.Text, Field "email" Text.Text, Field "friends" (List Friend) ]
type UserQuery = Object "UserQuery" '[]
'[ Argument "id" Text.Text :> Field "user" (Maybe User) ]
friendHandler :: DbUser.DbUser -> Handler IO Friend
friendHandler DbUser.DbUser {..} = pure $ pure ((Text.pack . show) _id) :<> pure _email
friendsHandler :: [DbUser.DbUser] -> Handler IO (List Friend)
friendsHandler friends = pure $ map friendHandler friends
userHandler :: Int -> Text.Text -> [DbUser.DbUser] -> Handler IO User
userHandler id email friends = pure $
pure ((Text.pack . show) id) :<> pure email :<> friendsHandler friends
userQueryHandler :: PSQL.Connection -> Handler IO UserQuery
userQueryHandler dbConn = pure $ \userId -> do
result <- Db.getUserById dbConn (read @Int $ Text.unpack userId)
case result of
[DbUser.DbUser {..}] -> do
friends <- Db.getUsersByIds dbConn _friendsIds
pure $ Just $ userHandler _id _email friends
_ -> pure Nothing
and query:
{
user(id: "1") {
id
email
friends
}
}
compiled successfully, but with errors in a response:
{
"data": {
"user": {
"email": "[email protected]",
"id": "1",
"friends": [
null
]
}
},
"errors": [
{
"message": "Tried to treat object as if it were leaf field."
}
]
}
But if i change type
type User = Object "User" '[]
'[ Field "id" Text.Text, Field "email" Text.Text, Field "friends" (List Text.Text) ]
everything works
So, what I did wrong and what this error means? Thanks for any help.
ghc - 8.6.5 graphql-api-0.3.0