Commit 926c855f authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[graphql] node fixes for graphql

parent 8b7790c2
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
module Gargantext.API.GraphQL.Node where module Gargantext.API.GraphQL.Node where
import Data.Either (Either(..))
import Data.Morpheus.Types import Data.Morpheus.Types
( GQLType ( GQLType
, Resolver , Resolver
...@@ -11,16 +12,18 @@ import Data.Morpheus.Types ...@@ -11,16 +12,18 @@ import Data.Morpheus.Types
, lift , lift
) )
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T
import Gargantext.API.Prelude (GargM, GargError) import Gargantext.API.Prelude (GargM, GargError)
import Gargantext.Core.Mail.Types (HasMail) import Gargantext.Core.Mail.Types (HasMail)
import Gargantext.Database.Admin.Config (fromNodeTypeId) import Gargantext.Database.Admin.Types.Node (NodeId(..), NodeType)
import Gargantext.Database.Admin.Types.Node (NodeId(..))
import qualified Gargantext.Database.Admin.Types.Node as NN import qualified Gargantext.Database.Admin.Types.Node as NN
import Gargantext.Database.Query.Table.Node (getClosestParentIdByType, getNode) import Gargantext.Database.Query.Table.Node (getClosestParentIdByType, getNode)
import Gargantext.Database.Prelude (HasConnectionPool, HasConfig) import Gargantext.Database.Prelude (HasConnectionPool, HasConfig)
import qualified Gargantext.Database.Schema.Node as N import qualified Gargantext.Database.Schema.Node as N
import Gargantext.Prelude import Gargantext.Prelude
import GHC.Generics (Generic) import GHC.Generics (Generic)
import qualified Prelude as Prelude
import Text.Read (readEither)
data Node = Node data Node = Node
{ id :: Int { id :: Int
...@@ -51,25 +54,31 @@ dbNodes node_id = do ...@@ -51,25 +54,31 @@ dbNodes node_id = do
data NodeParentArgs data NodeParentArgs
= NodeParentArgs = NodeParentArgs
{ node_id :: Int { node_id :: Int
, parent_type_id :: Int , parent_type :: Text
} deriving (Generic, GQLType) } deriving (Generic, GQLType)
resolveNodeParent resolveNodeParent
:: (HasConnectionPool env, HasConfig env, HasMail env) :: (HasConnectionPool env, HasConfig env, HasMail env)
=> NodeParentArgs -> GqlM e env [Node] => NodeParentArgs -> GqlM e env [Node]
resolveNodeParent NodeParentArgs { node_id, parent_type_id } = dbParentNodes node_id parent_type_id resolveNodeParent NodeParentArgs { node_id, parent_type } = dbParentNodes node_id parent_type
dbParentNodes dbParentNodes
:: (HasConnectionPool env, HasConfig env, HasMail env) :: (HasConnectionPool env, HasConfig env, HasMail env)
=> Int -> Int -> GqlM e env [Node] => Int -> Text -> GqlM e env [Node]
dbParentNodes node_id parent_type_id = do dbParentNodes node_id parent_type = do
mNodeId <- lift $ getClosestParentIdByType (NodeId node_id) (fromNodeTypeId parent_type_id) let mParentType = readEither (T.unpack parent_type) :: Either Prelude.String NodeType
case mNodeId of case mParentType of
Nothing -> pure [] Left err -> do
Just id -> do lift $ printDebug "[dbParentNodes] error reading parent type" (T.pack err)
node <- lift $ getNode id pure []
pure [toNode node] Right parentType -> do
mNodeId <- lift $ getClosestParentIdByType (NodeId node_id) parentType -- (fromNodeTypeId parent_type_id)
case mNodeId of
Nothing -> pure []
Just id -> do
node <- lift $ getNode id
pure [toNode node]
toNode :: NN.Node json -> Node toNode :: NN.Node json -> Node
toNode (N.Node { .. }) = Node { id = NN.unNodeId _node_id toNode (N.Node { .. }) = Node { id = NN.unNodeId _node_id
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment