Commit b947678a authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[graphql] some user work

NOTE: Does not compile
parent 7b9de040
......@@ -14,6 +14,7 @@ import Data.ByteString.Lazy.Char8
( ByteString
)
import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.Maybe (fromMaybe)
import Data.Morpheus
( App
, deriveApp )
......@@ -48,16 +49,18 @@ import Data.Text (Text)
import qualified Data.Text.Lazy as LT
import Data.Text.Lazy.Encoding (decodeUtf8)
import Data.Typeable (Typeable)
import Gargantext.API.GraphQL.User
import Gargantext.API.Prelude (GargServerT, GargM, GargError)
import Gargantext.Database.Prelude (Cmd, HasConnectionPool, HasConfig)
import Gargantext.Database.Query.Table.User (getUsersWithId)
import Gargantext.Database.Schema.User (UserPoly(..), UserLight)
import Gargantext.Prelude
import GHC.Generics (Generic)
import GHC.TypeLits
import Network.HTTP.Media ((//), (/:))
import Network.WebSockets
( ServerApp,
)
import qualified Prelude as Prelude
import Servant
( (:<|>) (..),
(:>),
......@@ -70,18 +73,11 @@ import Servant
ReqBody,
ServerT,
)
import Prelude
-- | Represents possible GraphQL queries.
data Query m
= Query
{ user :: UserArgs -> m UserLight
} deriving (Generic, GQLType)
-- | Arguments to the "user" query.
data UserArgs
= UserArgs
{ user_id :: Int
{ users :: UserArgs -> m [UserLight]
} deriving (Generic, GQLType)
-- | Possible GraphQL Events, i.e. here we describe how we will
......@@ -96,7 +92,7 @@ data Channel
-- | This type describes what data we will operate on.
data Contet
= UserContet UserLight
= UserContet [UserLight]
-- | The main GraphQL resolver: how queries, mutations and
......@@ -106,30 +102,10 @@ rootResolver
=> RootResolver (GargM env GargError) EVENT Query Undefined Undefined
rootResolver =
RootResolver
{ queryResolver = Query { user = resolveUser }
{ queryResolver = Query { users = resolveUsers }
, mutationResolver = Undefined
, subscriptionResolver = Undefined }
-- | Function to resolve user from a query.
resolveUser
:: (HasConnectionPool env, HasConfig env)
=> UserArgs -> ResolverQ e (GargM env GargError) UserLight
resolveUser UserArgs { user_id } = do
liftEither $ dbUser user_id
-- user <- lift $ dbUser user_id
-- case user of
-- --Left err -> failure $ msg err
-- Left err -> error "fail"
-- Right u -> pure u
-- | Inner function to fetch the user from DB.
dbUser :: Int -> Cmd err (Either String UserLight)
dbUser user_id = do
users <- getUsersWithId user_id
case users of
[] -> pure $ Left "User not found"
(user:_) -> pure $ Right user
-- | Main GraphQL "app".
app
:: (Typeable env, HasConnectionPool env, HasConfig env)
......
{-# LANGUAGE DeriveAnyClass #-}
module Gargantext.API.GraphQL.User where
import Data.Either (Either(..))
import Data.Maybe (fromMaybe)
import Data.Morpheus.Types
( GQLType
, ResolverQ
, liftEither
)
import Gargantext.API.Prelude (GargM, GargError)
import Gargantext.Database.Prelude (Cmd, HasConnectionPool, HasConfig)
import Gargantext.Database.Query.Table.User (getUsersWithId)
import Gargantext.Database.Schema.User (UserLight)
import Gargantext.Prelude
import GHC.Generics (Generic)
import qualified Prelude as Prelude
-- | Arguments to the "user" query.
data UserArgs
= UserArgs
{ user_id :: Int
, includeHyperdata :: Maybe Bool
} deriving (Generic, GQLType)
-- | Function to resolve user from a query.
resolveUsers
:: (HasConnectionPool env, HasConfig env)
=> UserArgs -> ResolverQ e (GargM env GargError) [UserLight]
resolveUsers UserArgs { user_id, includeHyperdata } = do
let _hyp = fromMaybe False includeHyperdata
liftEither $ dbUsers user_id
-- user <- lift $ dbUser user_id
-- case user of
-- --Left err -> failure $ msg err
-- Left err -> error "fail"
-- Right u -> pure u
-- | Inner function to fetch the user from DB.
dbUsers :: Int -> Cmd err (Either Prelude.String [UserLight])
dbUsers user_id = do
users <- getUsersWithId user_id
pure $ Right users
......@@ -107,7 +107,6 @@ getUsersWithId i = map toUserLight <$> runOpaQuery (selectUsersLightWithId i)
returnA -< row
queryUserTable :: Query UserRead
queryUserTable = selectTable userTable
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment