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
34 } deriving (Show, Generic, GQLType)
39 , parent_id :: Maybe Int
41 } deriving (Show, Generic, GQLType)
46 } deriving (Generic, GQLType)
51 } deriving (Generic, GQLType)
53 type GqlM e env = Resolver QUERY e (GargM env GargError)
55 -- | Function to resolve user from a query.
58 => NodeArgs -> GqlM e env [Node]
59 resolveNodes NodeArgs { node_id } = dbNodes node_id
63 => CorpusArgs -> GqlM e env [Corpus]
64 resolveNodesCorpus CorpusArgs { corpus_id } = dbNodesCorpus corpus_id
68 => Int -> GqlM e env [Node]
70 node <- lift $ getNode $ NodeId node_id
75 => Int -> GqlM e env [Corpus]
76 dbNodesCorpus corpus_id = do
77 corpus <- lift $ getNode $ NodeId corpus_id
78 pure [toCorpus corpus]
84 } deriving (Generic, GQLType)
88 => NodeParentArgs -> GqlM e env [Node]
89 resolveNodeParent NodeParentArgs { node_id, parent_type } = dbParentNodes node_id parent_type
93 => Int -> Text -> GqlM e env [Node]
94 dbParentNodes node_id parent_type = do
95 let mParentType = readEither (T.unpack parent_type) :: Either Prelude.String NodeType
98 lift $ printDebug "[dbParentNodes] error reading parent type" (T.pack err)
100 Right parentType -> do
101 mNodeId <- lift $ getClosestParentIdByType (NodeId node_id) parentType -- (fromNodeTypeId parent_type_id)
105 node <- lift $ getNode id
108 toNode :: NN.Node json -> Node
109 toNode N.Node { .. } = Node { id = NN.unNodeId _node_id
111 , parent_id = NN.unNodeId <$> _node_parent_id
112 , type_id = _node_typename }
114 toCorpus :: NN.Node Value -> Corpus
115 toCorpus N.Node { .. } = Corpus { id = NN.unNodeId _node_id
117 , parent_id = NN.unNodeId <$> _node_parent_id
118 , type_id = _node_typename }
120 pubmedAPIKeyFromValue :: Value -> Maybe PUBMED.APIKey
121 pubmedAPIKeyFromValue (Object kv) =
122 case HashMap.lookup "pubmed_api_key" kv of
124 Just v -> case fromJSON v of
126 Success v' -> Just v'
127 pubmedAPIKeyFromValue _ = Nothing