]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/GraphQL/TreeFirstLevel.hs
[ChangeLog] Node names
[gargantext.git] / src / Gargantext / API / GraphQL / TreeFirstLevel.hs
1 {-# LANGUAGE DeriveAnyClass #-}
2 {-# LANGUAGE DuplicateRecordFields #-}
3
4 module Gargantext.API.GraphQL.TreeFirstLevel where
5
6 import Gargantext.Prelude
7 import Data.Morpheus.Types (GQLType, lift, Resolver, QUERY)
8 import GHC.Generics (Generic)
9 import Data.Text (Text)
10 import Gargantext.API.Prelude (GargM, GargError)
11 import Gargantext.Database.Prelude (HasConnectionPool, HasConfig)
12 import Gargantext.Core.Mail.Types (HasMail)
13 import qualified Gargantext.Database.Query.Tree as T
14 import qualified Gargantext.Database.Schema.Node as N
15 import qualified Gargantext.Database.Admin.Types.Node as NN
16 import Gargantext.Database.Admin.Types.Node (allNodeTypes, NodeId (NodeId))
17 import Gargantext.Core.Types (Tree, NodeTree, NodeType)
18 import Gargantext.Core.Types.Main
19 ( Tree(TreeN), _tn_node, _tn_children, NodeTree(NodeTree, _nt_id, _nt_type), _nt_name )
20 import Gargantext.Database.Query.Table.Node (getNode)
21 import Gargantext.Database.Admin.Config (fromNodeTypeId)
22 import Gargantext.Database.Schema.Node (NodePoly(_node_parent_id))
23
24 data TreeArgs = TreeArgs
25 {
26 root_id :: Int
27 } deriving (Generic, GQLType)
28
29 data TreeNode = TreeNode
30 {
31 name :: Text
32 , id :: Int
33 , node_type :: NodeType
34 , parent_id :: Maybe Int
35 } deriving (Generic, GQLType)
36
37 data TreeFirstLevel m = TreeFirstLevel
38 {
39 root :: TreeNode
40 , parent :: m (Maybe TreeNode)
41 , children :: [TreeNode]
42 } deriving (Generic, GQLType)
43
44 type GqlM e env = Resolver QUERY e (GargM env GargError)
45
46 type ParentId = Maybe NodeId
47
48 resolveTree :: (HasConnectionPool env, HasConfig env, HasMail env) => TreeArgs -> GqlM e env (TreeFirstLevel (GqlM e env))
49 resolveTree TreeArgs { root_id } = dbTree root_id
50
51 dbTree :: (HasConnectionPool env, HasConfig env, HasMail env) => Int -> GqlM e env (TreeFirstLevel (GqlM e env))
52 dbTree root_id = do
53 let rId = NodeId root_id
54 t <- lift $ T.tree T.TreeFirstLevel rId allNodeTypes
55 n <- lift $ getNode $ NodeId root_id
56 let pId = toParentId n
57 pure $ toTree rId pId t
58 where
59 toParentId N.Node { _node_parent_id } = _node_parent_id
60
61
62 toTree :: (HasConnectionPool env, HasConfig env, HasMail env) => NodeId -> ParentId -> Tree NodeTree -> TreeFirstLevel (GqlM e env)
63 toTree rId pId TreeN { _tn_node, _tn_children } = TreeFirstLevel
64 { parent = resolveParent pId
65 , root = toTreeNode pId _tn_node
66 , children = map childrenToTreeNodes $ zip _tn_children $ repeat rId
67 }
68
69 toTreeNode :: ParentId -> NodeTree -> TreeNode
70 toTreeNode pId NodeTree { _nt_name, _nt_id, _nt_type } = TreeNode { name = _nt_name, id = id2int _nt_id, node_type = _nt_type, parent_id = id2int <$> pId}
71 where
72 id2int :: NodeId -> Int
73 id2int (NodeId n) = n
74
75 childrenToTreeNodes :: (Tree NodeTree, NodeId) -> TreeNode
76 childrenToTreeNodes (TreeN {_tn_node}, rId) = toTreeNode (Just rId) _tn_node
77
78 resolveParent :: (HasConnectionPool env, HasConfig env, HasMail env) => Maybe NodeId -> GqlM e env (Maybe TreeNode)
79 resolveParent (Just pId) = do
80 node <- lift $ getNode pId
81 pure $ Just $ nodeToTreeNode node
82 resolveParent Nothing = pure Nothing
83
84
85 nodeToTreeNode :: NN.Node json -> TreeNode
86 nodeToTreeNode N.Node {..} = TreeNode { id = NN.unNodeId _node_id
87 , name = _node_name
88 , node_type = fromNodeTypeId _node_typename
89 , parent_id = NN.unNodeId <$> _node_parent_id
90 }