Endpoints.purs 5.89 KB
Newer Older
1 2
module Gargantext.Components.GraphQL.Endpoints where

3 4
import Gargantext.Prelude

5 6 7 8 9
import Data.Array as A
import Data.Either (Either(..))
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
10
import Gargantext.Components.GraphQL (getClient, queryGql)
11 12
import Gargantext.Components.GraphQL.Contact (AnnuaireContact, annuaireContactQuery)
import Gargantext.Components.GraphQL.Context as GQLCTX
13
import Gargantext.Components.GraphQL.IMT as GQLIMT
14
import Gargantext.Components.GraphQL.Node (Node, nodeParentQuery, nodesQuery)
Karen Konou's avatar
Karen Konou committed
15
import Gargantext.Components.GraphQL.Team (Team, teamQuery)
16 17
import Gargantext.Components.GraphQL.Tree (TreeFirstLevel, treeFirstLevelQuery)
import Gargantext.Components.GraphQL.User (UserInfo, userInfoQuery)
18
import Gargantext.Config.REST (RESTError(..), AffRESTError)
19
import Gargantext.Sessions (Session(..))
20
import Gargantext.Types (NodeType)
21
import Gargantext.Utils.Reactix as R2
22 23
import GraphQL.Client.Args (onlyArgs)
import GraphQL.Client.Query (mutation)
24 25 26 27 28
import GraphQL.Client.Variables (withVars)

here :: R2.Here
here = R2.here "Gargantext.Components.GraphQL.Endpoints"

29 30 31 32 33 34 35
getIMTSchools :: Session -> AffRESTError (Array GQLIMT.School)
getIMTSchools session = do
  { imt_schools } <- queryGql session "get imt schools" $
                         GQLIMT.schoolsQuery
  liftEffect $ here.log2 "[getIMTSchools] imt_schools" imt_schools
  pure $ Right imt_schools

36 37 38 39 40 41 42 43 44
getNode :: Session -> Int -> AffRESTError Node
getNode session nodeId = do
  { nodes } <- queryGql session "get nodes" $
              nodesQuery `withVars` { id: nodeId }
  liftEffect $ here.log2 "[getNode] node" nodes
  pure $ case A.head nodes of
    Nothing -> Left (CustomError $ "node with id" <> show nodeId <>" not found")
    Just node -> Right node

45 46 47 48 49 50
getNodeParent :: Session -> Int -> NodeType -> Aff (Array Node)
getNodeParent session nodeId parentType = do
  { node_parent } <- queryGql session "get node parent" $
                     nodeParentQuery `withVars` { id: nodeId
                                                , parent_type: show parentType }  -- TODO: remove "show"
  liftEffect $ here.log2 "[getNodeParent] node_parent" node_parent
51
  pure node_parent
52

53
getUserInfo :: Session -> Int -> AffRESTError UserInfo
54 55 56 57 58 59 60
getUserInfo session id = do
  { user_infos } <- queryGql session "get user infos" $ userInfoQuery `withVars` { id }
  liftEffect $ here.log2 "[getUserInfo] user infos" user_infos
  pure $ case A.head user_infos of
    Nothing -> Left (CustomError $ "user with id " <> show id <> " not found")
    -- NOTE Contact is at G.C.N.A.U.C.Types
    Just ui -> Right ui
61

62 63 64 65 66 67 68 69 70
getAnnuaireContact :: Session -> Int -> AffRESTError AnnuaireContact
getAnnuaireContact session id = do
  { annuaire_contacts } <- queryGql session "get annuaire contact" $
    annuaireContactQuery `withVars` { id }
  liftEffect $ here.log2 "[getAnnuaireContact] data" annuaire_contacts
  pure $ case A.head annuaire_contacts of
    Nothing -> Left (CustomError $ "contact id=" <> show id <> " not found")
    Just r  -> Right r

71 72 73 74 75
getTreeFirstLevel :: Session -> Int -> AffRESTError TreeFirstLevel
getTreeFirstLevel session id = do
  { tree } <- queryGql session "get tree first level" $ treeFirstLevelQuery `withVars` { id }
  liftEffect $ here.log2 "[getTreeFirstLevel] tree first level" tree
  pure $ Right tree -- TODO: error handling
76

Karen Konou's avatar
Karen Konou committed
77
getTeam :: Session -> Int -> AffRESTError Team
78 79 80
getTeam session id = do
  { team } <- queryGql session "get team" $ teamQuery `withVars` { id }
  liftEffect $ here.log2 "[getTree] data" team
Karen Konou's avatar
Karen Konou committed
81
  pure $ Right team
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

type SharedFolderId = Int
type TeamNodeId = Int

deleteTeamMembership :: Session -> SharedFolderId -> TeamNodeId -> AffRESTError Int
deleteTeamMembership session sharedFolderId teamNodeId = do
  let token = getToken session
  client <- liftEffect $ getClient session
  { delete_team_membership } <- mutation
    client
    "delete_team_membership"
    { delete_team_membership: onlyArgs { token: token
                                       , shared_folder_id: sharedFolderId
                                       , team_node_id: teamNodeId } }
  pure $ case A.head delete_team_membership of
    Nothing -> Left (CustomError $ "Failed  to delete team membership. team node id=" <> show teamNodeId <> " shared folder id=" <> show sharedFolderId)
Karen Konou's avatar
Karen Konou committed
98
    Just _ -> Right sharedFolderId
99 100
  where
    getToken (Session { token }) = token
101 102 103

getNodeContext :: Session -> Int -> Int -> AffRESTError GQLCTX.NodeContext
getNodeContext session context_id node_id = do
104 105
  let query = GQLCTX.nodeContextQuery `withVars` { context_id, node_id }
  { contexts } <- queryGql session "get node context" query
106
  --liftEffect $ here.log2 "[getNodeContext] node context" contexts
107 108 109
  case A.head contexts of
    Nothing -> pure $ Left $ CustomError "no node context found"
    Just context -> pure $ Right context -- TODO: error handling
110

111 112 113 114 115 116 117 118 119
type ContextsForNgramsGQL = { contexts_for_ngrams :: Array GQLCTX.Context }
getContextsForNgrams :: Session -> Int -> Array String -> AffRESTError (Array GQLCTX.Context)
getContextsForNgrams session corpus_id ngrams_terms = do
  let query = GQLCTX.contextsForNgramsQuery `withVars` { corpus_id
                                                       , ngrams_terms: GQLCTX.NgramsTerms ngrams_terms }
  { contexts_for_ngrams } <- queryGql session "get contexts for ngrams" query

  pure $ Right contexts_for_ngrams
  --pure $ Right contexts_for_ngrams
120

121 122 123 124 125 126 127 128 129 130 131 132
updateNodeContextCategory :: Session -> Int -> Int -> Int -> AffRESTError Int
updateNodeContextCategory session context_id node_id category = do
  client <- liftEffect $ getClient session
  { update_node_context_category } <- mutation
    client
    "update_node_context_category"
    { update_node_context_category: onlyArgs { context_id
                                             , node_id
                                             , category } }
  pure $ case A.head update_node_context_category of
    Nothing -> Left (CustomError $ "Failed to update node category")
    Just _ -> Right context_id