Commit 79ff0460 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[graphql] implement user with hyperdata

parent b947678a
Pipeline #1989 failed with stage
in 10 minutes and 28 seconds
...@@ -52,7 +52,7 @@ import Data.Typeable (Typeable) ...@@ -52,7 +52,7 @@ import Data.Typeable (Typeable)
import Gargantext.API.GraphQL.User import Gargantext.API.GraphQL.User
import Gargantext.API.Prelude (GargServerT, GargM, GargError) import Gargantext.API.Prelude (GargServerT, GargM, GargError)
import Gargantext.Database.Prelude (Cmd, HasConnectionPool, HasConfig) import Gargantext.Database.Prelude (Cmd, HasConnectionPool, HasConfig)
import Gargantext.Database.Schema.User (UserPoly(..), UserLight) import Gargantext.Database.Schema.User (UserPoly(..))
import Gargantext.Prelude import Gargantext.Prelude
import GHC.Generics (Generic) import GHC.Generics (Generic)
import GHC.TypeLits import GHC.TypeLits
...@@ -77,7 +77,7 @@ import Servant ...@@ -77,7 +77,7 @@ import Servant
-- | Represents possible GraphQL queries. -- | Represents possible GraphQL queries.
data Query m data Query m
= Query = Query
{ users :: UserArgs -> m [UserLight] { users :: UserArgs -> m [User]
} deriving (Generic, GQLType) } deriving (Generic, GQLType)
-- | Possible GraphQL Events, i.e. here we describe how we will -- | Possible GraphQL Events, i.e. here we describe how we will
...@@ -92,7 +92,7 @@ data Channel ...@@ -92,7 +92,7 @@ data Channel
-- | This type describes what data we will operate on. -- | This type describes what data we will operate on.
data Contet data Contet
= UserContet [UserLight] = UserContet [User]
-- | The main GraphQL resolver: how queries, mutations and -- | The main GraphQL resolver: how queries, mutations and
......
...@@ -3,35 +3,40 @@ ...@@ -3,35 +3,40 @@
module Gargantext.API.GraphQL.User where module Gargantext.API.GraphQL.User where
import Data.Either (Either(..)) import Data.Either (Either(..))
import Data.Maybe (fromMaybe)
import Data.Morpheus.Types import Data.Morpheus.Types
( GQLType ( GQLType
, ResolverQ , ResolverQ
, liftEither , liftEither
) )
import Data.Text (Text)
import Gargantext.API.Prelude (GargM, GargError) import Gargantext.API.Prelude (GargM, GargError)
import Gargantext.Database.Admin.Types.Hyperdata (HyperdataUser(..))
import Gargantext.Database.Prelude (Cmd, HasConnectionPool, HasConfig) import Gargantext.Database.Prelude (Cmd, HasConnectionPool, HasConfig)
import Gargantext.Database.Query.Table.User (getUsersWithId) import Gargantext.Database.Query.Table.User (getUsersWithId, getUserHyperdata)
import Gargantext.Database.Schema.User (UserLight) import Gargantext.Database.Schema.User (UserLight(..))
import Gargantext.Prelude import Gargantext.Prelude
import GHC.Generics (Generic) import GHC.Generics (Generic)
import qualified Prelude as Prelude import qualified Prelude as Prelude
data User = User
{ u_email :: Text
, u_hyperdata :: Maybe HyperdataUser
, u_id :: Int
, u_username :: Text }
deriving (Show, Generic, GQLType)
-- | Arguments to the "user" query. -- | Arguments to the "user" query.
data UserArgs data UserArgs
= UserArgs = UserArgs
{ user_id :: Int { user_id :: Int
, includeHyperdata :: Maybe Bool
} deriving (Generic, GQLType) } deriving (Generic, GQLType)
-- | Function to resolve user from a query. -- | Function to resolve user from a query.
resolveUsers resolveUsers
:: (HasConnectionPool env, HasConfig env) :: (HasConnectionPool env, HasConfig env)
=> UserArgs -> ResolverQ e (GargM env GargError) [UserLight] => UserArgs -> ResolverQ e (GargM env GargError) [User]
resolveUsers UserArgs { user_id, includeHyperdata } = do resolveUsers UserArgs { user_id } = do
let _hyp = fromMaybe False includeHyperdata
liftEither $ dbUsers user_id liftEither $ dbUsers user_id
-- user <- lift $ dbUser user_id -- user <- lift $ dbUser user_id
-- case user of -- case user of
...@@ -40,7 +45,24 @@ resolveUsers UserArgs { user_id, includeHyperdata } = do ...@@ -40,7 +45,24 @@ resolveUsers UserArgs { user_id, includeHyperdata } = do
-- Right u -> pure u -- Right u -> pure u
-- | Inner function to fetch the user from DB. -- | Inner function to fetch the user from DB.
dbUsers :: Int -> Cmd err (Either Prelude.String [UserLight]) dbUsers :: Int -> Cmd err (Either Prelude.String [User])
dbUsers user_id = do dbUsers user_id = do
users <- getUsersWithId user_id users <- getUsersWithId user_id
pure $ Right users -- users' <- if includeHyperdata
-- then mapM injectHyperdata (toUser <$> users)
-- else (pure $ toUser <$> users)
users' <- mapM injectHyperdata $ toUser <$> users
pure $ Right users'
toUser :: UserLight -> User
toUser (UserLight { .. }) = User { u_email = userLight_email
, u_hyperdata = Nothing
, u_id = userLight_id
, u_username = userLight_username }
injectHyperdata :: User -> Cmd err User
injectHyperdata user@(User { .. }) = do
hyperdata <- getUserHyperdata u_id
case hyperdata of
[] -> pure $ user
(h:_) -> pure $ User { u_hyperdata = Just h, .. }
...@@ -9,14 +9,17 @@ Portability : POSIX ...@@ -9,14 +9,17 @@ Portability : POSIX
-} -}
{-# LANGUAGE DeriveAnyClass #-}
module Gargantext.Core module Gargantext.Core
where where
import Data.Text (Text)
import Data.Aeson import Data.Aeson
import Data.Either(Either(Left)) import Data.Either(Either(Left))
import Data.Hashable (Hashable) import Data.Hashable (Hashable)
import Data.Morpheus.Types (GQLType)
import Data.Swagger import Data.Swagger
import Data.Text (Text)
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Gargantext.Prelude import Gargantext.Prelude
import Servant.API import Servant.API
...@@ -38,7 +41,7 @@ import Servant.API ...@@ -38,7 +41,7 @@ import Servant.API
-- | All languages supported -- | All languages supported
-- TODO : DE | SP | CH -- TODO : DE | SP | CH
data Lang = EN | FR | All data Lang = EN | FR | All
deriving (Show, Eq, Ord, Bounded, Enum, Generic) deriving (Show, Eq, Ord, Bounded, Enum, Generic, GQLType)
instance ToJSON Lang instance ToJSON Lang
instance FromJSON Lang instance FromJSON Lang
......
...@@ -9,9 +9,10 @@ Portability : POSIX ...@@ -9,9 +9,10 @@ Portability : POSIX
-} -}
{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE NoImplicitPrelude #-}
...@@ -23,11 +24,12 @@ Portability : POSIX ...@@ -23,11 +24,12 @@ Portability : POSIX
module Gargantext.Database.Admin.Types.Hyperdata.Contact module Gargantext.Database.Admin.Types.Hyperdata.Contact
where where
import Data.Morpheus.Types (GQLType(..))
import Data.Time.Segment (jour) import Data.Time.Segment (jour)
import Data.Time (UTCTime)
import Gargantext.Core.Text (HasText(..)) import Gargantext.Core.Text (HasText(..))
import Gargantext.Database.Admin.Types.Hyperdata.Prelude import Gargantext.Database.Admin.Types.Hyperdata.Prelude
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Utils.UTCTime
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
data HyperdataContact = data HyperdataContact =
...@@ -40,7 +42,7 @@ data HyperdataContact = ...@@ -40,7 +42,7 @@ data HyperdataContact =
, _hc_uniqIdBdd :: Maybe Text , _hc_uniqIdBdd :: Maybe Text
, _hc_uniqId :: Maybe Text , _hc_uniqId :: Maybe Text
} deriving (Eq, Show, Generic) } deriving (Eq, Show, Generic, GQLType)
instance HasText HyperdataContact instance HasText HyperdataContact
where where
...@@ -87,7 +89,7 @@ data ContactWho = ...@@ -87,7 +89,7 @@ data ContactWho =
, _cw_lastName :: Maybe Text , _cw_lastName :: Maybe Text
, _cw_keywords :: [Text] , _cw_keywords :: [Text]
, _cw_freetags :: [Text] , _cw_freetags :: [Text]
} deriving (Eq, Show, Generic) } deriving (Eq, Show, Generic, GQLType)
type FirstName = Text type FirstName = Text
type LastName = Text type LastName = Text
...@@ -102,8 +104,6 @@ contactWho fn ln = ContactWho Nothing ...@@ -102,8 +104,6 @@ contactWho fn ln = ContactWho Nothing
[] []
[] []
data ContactWhere = data ContactWhere =
ContactWhere { _cw_organization :: [Text] ContactWhere { _cw_organization :: [Text]
, _cw_labTeamDepts :: [Text] , _cw_labTeamDepts :: [Text]
...@@ -116,9 +116,9 @@ data ContactWhere = ...@@ -116,9 +116,9 @@ data ContactWhere =
, _cw_touch :: Maybe ContactTouch , _cw_touch :: Maybe ContactTouch
, _cw_entry :: Maybe UTCTime , _cw_entry :: Maybe NUTCTime
, _cw_exit :: Maybe UTCTime , _cw_exit :: Maybe NUTCTime
} deriving (Eq, Show, Generic) } deriving (Eq, Show, Generic, GQLType)
defaultContactWhere :: ContactWhere defaultContactWhere :: ContactWhere
defaultContactWhere = ContactWhere ["Organization X"] defaultContactWhere = ContactWhere ["Organization X"]
...@@ -128,14 +128,14 @@ defaultContactWhere = ContactWhere ["Organization X"] ...@@ -128,14 +128,14 @@ defaultContactWhere = ContactWhere ["Organization X"]
(Just "Country") (Just "Country")
(Just "City") (Just "City")
(Just defaultContactTouch) (Just defaultContactTouch)
(Just $ jour 01 01 2020) (Just $ NUTCTime $ jour 01 01 2020)
(Just $ jour 01 01 2029) (Just $ NUTCTime $ jour 01 01 2029)
data ContactTouch = data ContactTouch =
ContactTouch { _ct_mail :: Maybe Text ContactTouch { _ct_mail :: Maybe Text
, _ct_phone :: Maybe Text , _ct_phone :: Maybe Text
, _ct_url :: Maybe Text , _ct_url :: Maybe Text
} deriving (Eq, Show, Generic) } deriving (Eq, Show, Generic, GQLType)
defaultContactTouch :: ContactTouch defaultContactTouch :: ContactTouch
defaultContactTouch = ContactTouch (Just "email@data.com") defaultContactTouch = ContactTouch (Just "email@data.com")
......
...@@ -11,6 +11,7 @@ Portability : POSIX ...@@ -11,6 +11,7 @@ Portability : POSIX
{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE MultiParamTypeClasses #-}
...@@ -23,6 +24,7 @@ Portability : POSIX ...@@ -23,6 +24,7 @@ Portability : POSIX
module Gargantext.Database.Admin.Types.Hyperdata.User module Gargantext.Database.Admin.Types.Hyperdata.User
where where
import Data.Morpheus.Types (GQLType)
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Core (Lang(..)) import Gargantext.Core (Lang(..))
import Gargantext.Database.Admin.Types.Hyperdata.Prelude import Gargantext.Database.Admin.Types.Hyperdata.Prelude
...@@ -35,19 +37,19 @@ data HyperdataUser = ...@@ -35,19 +37,19 @@ data HyperdataUser =
HyperdataUser { _hu_private :: !(Maybe HyperdataPrivate) HyperdataUser { _hu_private :: !(Maybe HyperdataPrivate)
, _hu_shared :: !(Maybe HyperdataContact) , _hu_shared :: !(Maybe HyperdataContact)
, _hu_public :: !(Maybe HyperdataPublic) , _hu_public :: !(Maybe HyperdataPublic)
} deriving (Eq, Show, Generic) } deriving (Eq, Show, Generic, GQLType)
data HyperdataPrivate = data HyperdataPrivate =
HyperdataPrivate { _hpr_password :: !Text HyperdataPrivate { _hpr_password :: !Text
, _hpr_lang :: !Lang , _hpr_lang :: !Lang
} }
deriving (Eq, Show, Generic) deriving (Eq, Show, Generic, GQLType)
data HyperdataPublic = data HyperdataPublic =
HyperdataPublic { _hpu_pseudo :: !Text HyperdataPublic { _hpu_pseudo :: !Text
, _hpu_publications :: ![DocumentId] , _hpu_publications :: ![DocumentId]
} }
deriving (Eq, Show, Generic) deriving (Eq, Show, Generic, GQLType)
-- | Default -- | Default
defaultHyperdataUser :: HyperdataUser defaultHyperdataUser :: HyperdataUser
......
...@@ -25,6 +25,7 @@ import Data.Aeson ...@@ -25,6 +25,7 @@ import Data.Aeson
import Data.Aeson.TH (deriveJSON) import Data.Aeson.TH (deriveJSON)
import Data.Either import Data.Either
import Data.Hashable (Hashable) import Data.Hashable (Hashable)
import Data.Morpheus.Types (GQLType)
import Data.Swagger import Data.Swagger
import Data.Text (Text, unpack) import Data.Text (Text, unpack)
import Data.Time (UTCTime) import Data.Time (UTCTime)
...@@ -152,6 +153,7 @@ pgNodeId = O.sqlInt4 . id2int ...@@ -152,6 +153,7 @@ pgNodeId = O.sqlInt4 . id2int
------------------------------------------------------------------------ ------------------------------------------------------------------------
newtype NodeId = NodeId Int newtype NodeId = NodeId Int
deriving (Read, Generic, Num, Eq, Ord, Enum, ToJSONKey, FromJSONKey, ToJSON, FromJSON, Hashable) deriving (Read, Generic, Num, Eq, Ord, Enum, ToJSONKey, FromJSONKey, ToJSON, FromJSON, Hashable)
instance GQLType NodeId
instance Show NodeId where instance Show NodeId where
show (NodeId n) = "nodeId-" <> show n show (NodeId n) = "nodeId-" <> show n
instance Serialise NodeId instance Serialise NodeId
......
...@@ -23,6 +23,7 @@ module Gargantext.Database.Query.Table.User ...@@ -23,6 +23,7 @@ module Gargantext.Database.Query.Table.User
, deleteUsers , deleteUsers
, updateUserDB , updateUserDB
, queryUserTable , queryUserTable
, getUserHyperdata
, getUser , getUser
, insertNewUsers , insertNewUsers
, selectUsersLightWith , selectUsersLightWith
...@@ -36,13 +37,16 @@ module Gargantext.Database.Query.Table.User ...@@ -36,13 +37,16 @@ module Gargantext.Database.Query.Table.User
where where
import Control.Arrow (returnA) import Control.Arrow (returnA)
import Control.Lens ((^.))
import Data.List (find) import Data.List (find)
import Data.Text (Text) import Data.Text (Text)
import Data.Time (UTCTime) import Data.Time (UTCTime)
import Gargantext.Core.Types.Individu import Gargantext.Core.Types.Individu
import qualified Gargantext.Prelude.Crypto.Auth as Auth import qualified Gargantext.Prelude.Crypto.Auth as Auth
import Gargantext.Database.Schema.User import Gargantext.Database.Admin.Types.Hyperdata (HyperdataUser)
import Gargantext.Database.Prelude import Gargantext.Database.Prelude
import Gargantext.Database.Schema.Node (node_hyperdata, node_id, queryNodeTable)
import Gargantext.Database.Schema.User
import Gargantext.Prelude import Gargantext.Prelude
import Opaleye import Opaleye
...@@ -110,6 +114,16 @@ getUsersWithId i = map toUserLight <$> runOpaQuery (selectUsersLightWithId i) ...@@ -110,6 +114,16 @@ getUsersWithId i = map toUserLight <$> runOpaQuery (selectUsersLightWithId i)
queryUserTable :: Query UserRead queryUserTable :: Query UserRead
queryUserTable = selectTable userTable queryUserTable = selectTable userTable
----------------------------------------------------------------------
getUserHyperdata :: Int -> Cmd err [HyperdataUser]
getUserHyperdata i = do
runOpaQuery (selectUserHyperdataWithId i)
where
selectUserHyperdataWithId :: Int -> Query (Column PGJsonb)
selectUserHyperdataWithId i' = proc () -> do
row <- queryNodeTable -< ()
restrict -< row^.node_id .== (sqlInt4 i')
returnA -< row^.node_hyperdata
------------------------------------------------------------------ ------------------------------------------------------------------
-- | Select User with some parameters -- | Select User with some parameters
-- Not optimized version -- Not optimized version
...@@ -128,7 +142,6 @@ userLightWithUsername t xs = userWith userLight_username t xs ...@@ -128,7 +142,6 @@ userLightWithUsername t xs = userWith userLight_username t xs
userLightWithId :: Int -> [UserLight] -> Maybe UserLight userLightWithId :: Int -> [UserLight] -> Maybe UserLight
userLightWithId t xs = userWith userLight_id t xs userLightWithId t xs = userWith userLight_id t xs
---------------------------------------------------------------------- ----------------------------------------------------------------------
users :: Cmd err [UserDB] users :: Cmd err [UserDB]
users = runOpaQuery queryUserTable users = runOpaQuery queryUserTable
......
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