Skip to content

List of Object makes an error (Tried to treat object as if it were leaf field) #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nishirken opened this issue May 11, 2019 · 0 comments

Comments

@nishirken
Copy link

nishirken commented May 11, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant