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.Database.Admin.Types.Node (NodeId(..), NodeType)
17 import qualified Gargantext.Database.Admin.Types.Node as NN
18 import Gargantext.Database.Query.Table.Node (getClosestParentIdByType, getNode)
19 import Gargantext.Database.Prelude (CmdCommon)
20 import qualified Gargantext.Database.Schema.Node as N
21 import Gargantext.Prelude
22 import GHC.Generics (Generic)
23 import qualified Prelude
24 import Text.Read (readEither)
29 , parent_id :: Maybe Int
31 } deriving (Show, Generic, GQLType)
36 } deriving (Generic, GQLType)
38 type GqlM e env = Resolver QUERY e (GargM env GargError)
40 -- | Function to resolve user from a query.
43 => NodeArgs -> GqlM e env [Node]
44 resolveNodes NodeArgs { node_id } = dbNodes node_id
48 => Int -> GqlM e env [Node]
50 node <- lift $ getNode $ NodeId node_id
57 } deriving (Generic, GQLType)
61 => NodeParentArgs -> GqlM e env [Node]
62 resolveNodeParent NodeParentArgs { node_id, parent_type } = dbParentNodes node_id parent_type
66 => Int -> Text -> GqlM e env [Node]
67 dbParentNodes node_id parent_type = do
68 let mParentType = readEither (T.unpack parent_type) :: Either Prelude.String NodeType
71 lift $ printDebug "[dbParentNodes] error reading parent type" (T.pack err)
73 Right parentType -> do
74 mNodeId <- lift $ getClosestParentIdByType (NodeId node_id) parentType -- (fromNodeTypeId parent_type_id)
78 node <- lift $ getNode id
81 toNode :: NN.Node json -> Node
82 toNode (N.Node { .. }) = Node { id = NN.unNodeId _node_id
84 , parent_id = NN.unNodeId <$> _node_parent_id
85 , type_id = _node_typename }