1 {-# LANGUAGE DeriveAnyClass #-}
2 {-# LANGUAGE DuplicateRecordFields #-}
4 module Gargantext.API.GraphQL.Node where
6 import Data.Either (Either(..))
7 import Data.Morpheus.Types
13 import Data.Text (Text)
14 import qualified Data.Text as T
15 import Gargantext.API.Prelude (GargM, GargError)
16 import Gargantext.Core.Mail.Types (HasMail)
17 import Gargantext.Database.Admin.Types.Node (NodeId(..), NodeType)
18 import qualified Gargantext.Database.Admin.Types.Node as NN
19 import Gargantext.Database.Query.Table.Node (getClosestParentIdByType, getNode)
20 import Gargantext.Database.Prelude (HasConnectionPool, HasConfig)
21 import qualified Gargantext.Database.Schema.Node as N
22 import Gargantext.Prelude
23 import GHC.Generics (Generic)
24 import qualified Prelude
25 import Text.Read (readEither)
30 , parent_id :: Maybe Int
32 } deriving (Show, Generic, GQLType)
37 } deriving (Generic, GQLType)
39 type GqlM e env = Resolver QUERY e (GargM env GargError)
41 -- | Function to resolve user from a query.
43 :: (HasConnectionPool env, HasConfig env, HasMail env)
44 => NodeArgs -> GqlM e env [Node]
45 resolveNodes NodeArgs { node_id } = dbNodes node_id
48 :: (HasConnectionPool env, HasConfig env, HasMail env)
49 => Int -> GqlM e env [Node]
51 node <- lift $ getNode $ NodeId node_id
58 } deriving (Generic, GQLType)
61 :: (HasConnectionPool env, HasConfig env, HasMail env)
62 => NodeParentArgs -> GqlM e env [Node]
63 resolveNodeParent NodeParentArgs { node_id, parent_type } = dbParentNodes node_id parent_type
66 :: (HasConnectionPool env, HasConfig env, HasMail env)
67 => Int -> Text -> GqlM e env [Node]
68 dbParentNodes node_id parent_type = do
69 let mParentType = readEither (T.unpack parent_type) :: Either Prelude.String NodeType
72 lift $ printDebug "[dbParentNodes] error reading parent type" (T.pack err)
74 Right parentType -> do
75 mNodeId <- lift $ getClosestParentIdByType (NodeId node_id) parentType -- (fromNodeTypeId parent_type_id)
79 node <- lift $ getNode id
82 toNode :: NN.Node json -> Node
83 toNode (N.Node { .. }) = Node { id = NN.unNodeId _node_id
85 , parent_id = NN.unNodeId <$> _node_parent_id
86 , type_id = _node_typename }