1 {-# LANGUAGE DeriveAnyClass #-}
2 {-# LANGUAGE DuplicateRecordFields #-}
4 module Gargantext.API.GraphQL.Node where
7 import Data.Either (Either(..))
8 import qualified Data.HashMap.Strict as HashMap
9 import Data.Morpheus.Types
15 import Data.Text (Text)
16 import qualified Data.Text as T
17 import Gargantext.API.Prelude (GargM, GargError)
18 import Gargantext.Database.Admin.Types.Node (NodeId(..), NodeType)
19 import qualified Gargantext.Database.Admin.Types.Node as NN
20 import Gargantext.Database.Query.Table.Node (getClosestParentIdByType, getNode)
21 import Gargantext.Database.Prelude (CmdCommon) -- , JSONB)
22 import qualified Gargantext.Database.Schema.Node as N
23 import Gargantext.Prelude
24 import GHC.Generics (Generic)
25 import qualified Prelude
26 import qualified PUBMED.Types as PUBMED
27 import Text.Read (readEither)
32 , parent_id :: Maybe Int
33 , pubmedAPIKey :: Maybe PUBMED.APIKey
35 } deriving (Show, Generic, GQLType)
40 , parent_id :: Maybe Int
42 } deriving (Show, Generic, GQLType)
47 } deriving (Generic, GQLType)
52 } deriving (Generic, GQLType)
54 type GqlM e env = Resolver QUERY e (GargM env GargError)
56 -- | Function to resolve user from a query.
59 => NodeArgs -> GqlM e env [Node]
60 resolveNodes NodeArgs { node_id } = dbNodes node_id
64 => CorpusArgs -> GqlM e env [Corpus]
65 resolveNodesCorpus CorpusArgs { corpus_id } = dbNodesCorpus corpus_id
69 => Int -> GqlM e env [Node]
71 node <- lift $ getNode $ NodeId node_id
76 => Int -> GqlM e env [Corpus]
77 dbNodesCorpus corpus_id = do
78 corpus <- lift $ getNode $ NodeId corpus_id
79 pure [toCorpus corpus]
85 } deriving (Generic, GQLType)
89 => NodeParentArgs -> GqlM e env [Node]
90 resolveNodeParent NodeParentArgs { node_id, parent_type } = dbParentNodes node_id parent_type
94 => Int -> Text -> GqlM e env [Node]
95 dbParentNodes node_id parent_type = do
96 let mParentType = readEither (T.unpack parent_type) :: Either Prelude.String NodeType
99 lift $ printDebug "[dbParentNodes] error reading parent type" (T.pack err)
101 Right parentType -> do
102 mNodeId <- lift $ getClosestParentIdByType (NodeId node_id) parentType -- (fromNodeTypeId parent_type_id)
106 node <- lift $ getNode id
109 toNode :: NN.Node json -> Node
110 toNode N.Node { .. } = Node { id = NN.unNodeId _node_id
112 , parent_id = NN.unNodeId <$> _node_parent_id
113 , type_id = _node_typename }
115 toCorpus :: NN.Node Value -> Corpus
116 toCorpus N.Node { .. } = Corpus { id = NN.unNodeId _node_id
118 , parent_id = NN.unNodeId <$> _node_parent_id
119 , pubmedAPIKey = pubmedAPIKeyFromValue _node_hyperdata
120 , type_id = _node_typename }
122 pubmedAPIKeyFromValue :: Value -> Maybe PUBMED.APIKey
123 pubmedAPIKeyFromValue (Object kv) =
124 case HashMap.lookup "pubmed_api_key" kv of
126 Just v -> case fromJSON v of
128 Success v' -> Just v'
129 pubmedAPIKeyFromValue _ = Nothing