2018-12-13 10:26:15 +03:00
|
|
|
module Hasura.RQL.DDL.RemoteSchema
|
|
|
|
( runAddRemoteSchema
|
|
|
|
, runRemoveRemoteSchema
|
2019-04-17 19:29:39 +03:00
|
|
|
, removeRemoteSchemaFromCatalog
|
2019-07-08 08:51:41 +03:00
|
|
|
, runReloadRemoteSchema
|
|
|
|
, buildGCtxMap
|
2019-04-17 19:29:39 +03:00
|
|
|
, addRemoteSchemaP1
|
2019-07-08 08:51:41 +03:00
|
|
|
, addRemoteSchemaP2Setup
|
2018-12-13 10:26:15 +03:00
|
|
|
, addRemoteSchemaP2
|
|
|
|
) where
|
2018-11-23 16:02:46 +03:00
|
|
|
|
2019-03-18 19:22:21 +03:00
|
|
|
import Hasura.EncJSON
|
2018-11-23 16:02:46 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
|
|
import qualified Data.Aeson as J
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
|
|
|
|
import Hasura.GraphQL.RemoteServer
|
|
|
|
import Hasura.RQL.Types
|
2019-07-08 08:51:41 +03:00
|
|
|
import Hasura.SQL.Types
|
2018-11-23 16:02:46 +03:00
|
|
|
|
|
|
|
import qualified Hasura.GraphQL.Schema as GS
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runAddRemoteSchema
|
2019-04-17 19:29:39 +03:00
|
|
|
:: ( QErrM m, UserInfoM m
|
|
|
|
, CacheRWM m, MonadTx m
|
|
|
|
, MonadIO m, HasHttpManager m
|
2018-12-13 10:26:15 +03:00
|
|
|
)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> AddRemoteSchemaQuery -> m EncJSON
|
2018-12-13 10:26:15 +03:00
|
|
|
runAddRemoteSchema q = do
|
2019-07-08 08:51:41 +03:00
|
|
|
addRemoteSchemaP1 name >> addRemoteSchemaP2 q
|
|
|
|
where
|
|
|
|
name = _arsqName q
|
2019-04-17 19:29:39 +03:00
|
|
|
|
|
|
|
addRemoteSchemaP1
|
2019-07-08 08:51:41 +03:00
|
|
|
:: (QErrM m, UserInfoM m, CacheRM m)
|
|
|
|
=> RemoteSchemaName -> m ()
|
|
|
|
addRemoteSchemaP1 name = do
|
2018-12-13 10:26:15 +03:00
|
|
|
adminOnly
|
2019-07-08 08:51:41 +03:00
|
|
|
remoteSchemaMap <- scRemoteSchemas <$> askSchemaCache
|
|
|
|
onJust (Map.lookup name remoteSchemaMap) $ const $
|
|
|
|
throw400 AlreadyExists $ "remote schema with name "
|
|
|
|
<> name <<> " already exists"
|
|
|
|
|
|
|
|
addRemoteSchemaP2Setup
|
|
|
|
:: (QErrM m, CacheRWM m, MonadIO m, HasHttpManager m)
|
|
|
|
=> AddRemoteSchemaQuery -> m RemoteSchemaCtx
|
|
|
|
addRemoteSchemaP2Setup q = do
|
2019-04-17 19:29:39 +03:00
|
|
|
httpMgr <- askHttpManager
|
|
|
|
rsi <- validateRemoteSchemaDef def
|
2019-07-08 08:51:41 +03:00
|
|
|
gCtx <- fetchRemoteSchema httpMgr name rsi
|
|
|
|
let rsCtx = RemoteSchemaCtx name gCtx rsi
|
|
|
|
addRemoteSchemaToCache rsCtx
|
|
|
|
return rsCtx
|
2019-04-17 19:29:39 +03:00
|
|
|
where
|
|
|
|
AddRemoteSchemaQuery name def _ = q
|
2018-11-23 16:02:46 +03:00
|
|
|
|
|
|
|
addRemoteSchemaP2
|
|
|
|
:: ( QErrM m
|
|
|
|
, CacheRWM m
|
|
|
|
, MonadTx m
|
2019-07-08 08:51:41 +03:00
|
|
|
, MonadIO m, HasHttpManager m
|
2018-11-23 16:02:46 +03:00
|
|
|
)
|
|
|
|
=> AddRemoteSchemaQuery
|
2019-03-18 19:22:21 +03:00
|
|
|
-> m EncJSON
|
2019-07-08 08:51:41 +03:00
|
|
|
addRemoteSchemaP2 q = do
|
|
|
|
void $ addRemoteSchemaP2Setup q
|
2018-11-23 16:02:46 +03:00
|
|
|
liftTx $ addRemoteSchemaToCatalog q
|
|
|
|
return successMsg
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runRemoveRemoteSchema
|
2019-04-17 19:29:39 +03:00
|
|
|
:: (QErrM m, UserInfoM m, CacheRWM m, MonadTx m)
|
2019-07-08 08:51:41 +03:00
|
|
|
=> RemoteSchemaNameQuery -> m EncJSON
|
|
|
|
runRemoveRemoteSchema (RemoteSchemaNameQuery rsn)= do
|
2019-04-17 19:29:39 +03:00
|
|
|
removeRemoteSchemaP1 rsn
|
|
|
|
removeRemoteSchemaP2 rsn
|
2018-11-23 16:02:46 +03:00
|
|
|
|
|
|
|
removeRemoteSchemaP1
|
2019-04-17 19:29:39 +03:00
|
|
|
:: (UserInfoM m, QErrM m, CacheRM m)
|
|
|
|
=> RemoteSchemaName -> m ()
|
|
|
|
removeRemoteSchemaP1 rsn = do
|
|
|
|
adminOnly
|
|
|
|
sc <- askSchemaCache
|
2019-07-08 08:51:41 +03:00
|
|
|
let rmSchemas = scRemoteSchemas sc
|
|
|
|
void $ onNothing (Map.lookup rsn rmSchemas) $
|
|
|
|
throw400 NotExists "no such remote schema"
|
2018-11-23 16:02:46 +03:00
|
|
|
|
|
|
|
removeRemoteSchemaP2
|
2019-04-17 19:29:39 +03:00
|
|
|
:: ( CacheRWM m
|
2018-11-23 16:02:46 +03:00
|
|
|
, MonadTx m
|
|
|
|
)
|
2019-04-17 19:29:39 +03:00
|
|
|
=> RemoteSchemaName
|
2019-03-18 19:22:21 +03:00
|
|
|
-> m EncJSON
|
2019-04-17 19:29:39 +03:00
|
|
|
removeRemoteSchemaP2 rsn = do
|
2019-07-08 08:51:41 +03:00
|
|
|
delRemoteSchemaFromCache rsn
|
2019-04-17 19:29:39 +03:00
|
|
|
liftTx $ removeRemoteSchemaFromCatalog rsn
|
2018-11-23 16:02:46 +03:00
|
|
|
return successMsg
|
|
|
|
|
2019-07-08 08:51:41 +03:00
|
|
|
runReloadRemoteSchema
|
|
|
|
:: ( QErrM m, UserInfoM m , CacheRWM m
|
|
|
|
, MonadIO m, HasHttpManager m
|
2019-04-17 19:29:39 +03:00
|
|
|
)
|
2019-07-08 08:51:41 +03:00
|
|
|
=> RemoteSchemaNameQuery -> m EncJSON
|
|
|
|
runReloadRemoteSchema (RemoteSchemaNameQuery name) = do
|
|
|
|
adminOnly
|
|
|
|
rmSchemas <- scRemoteSchemas <$> askSchemaCache
|
|
|
|
rsi <- fmap rscInfo $ onNothing (Map.lookup name rmSchemas) $
|
|
|
|
throw400 NotExists $ "remote schema with name "
|
|
|
|
<> name <<> " does not exist"
|
|
|
|
httpMgr <- askHttpManager
|
|
|
|
gCtx <- fetchRemoteSchema httpMgr name rsi
|
|
|
|
delRemoteSchemaFromCache name
|
|
|
|
addRemoteSchemaToCache $ RemoteSchemaCtx name gCtx rsi
|
|
|
|
return successMsg
|
|
|
|
|
|
|
|
-- | build GraphQL schema
|
|
|
|
buildGCtxMap
|
|
|
|
:: (QErrM m, CacheRWM m) => m ()
|
|
|
|
buildGCtxMap = do
|
|
|
|
-- build GraphQL Context with Hasura schema
|
|
|
|
GS.buildGCtxMapPG
|
|
|
|
sc <- askSchemaCache
|
|
|
|
let gCtxMap = scGCtxMap sc
|
|
|
|
-- Stitch remote schemas
|
|
|
|
(mergedGCtxMap, defGCtx) <- mergeSchemas (scRemoteSchemas sc) gCtxMap
|
|
|
|
writeSchemaCache sc { scGCtxMap = mergedGCtxMap
|
|
|
|
, scDefaultRemoteGCtx = defGCtx
|
|
|
|
}
|
2018-11-23 16:02:46 +03:00
|
|
|
|
|
|
|
addRemoteSchemaToCatalog
|
|
|
|
:: AddRemoteSchemaQuery
|
|
|
|
-> Q.TxE QErr ()
|
|
|
|
addRemoteSchemaToCatalog (AddRemoteSchemaQuery name def comment) =
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
INSERT into hdb_catalog.remote_schemas
|
|
|
|
(name, definition, comment)
|
|
|
|
VALUES ($1, $2, $3)
|
|
|
|
|] (name, Q.AltJ $ J.toJSON def, comment) True
|
|
|
|
|
2019-04-17 19:29:39 +03:00
|
|
|
removeRemoteSchemaFromCatalog :: RemoteSchemaName -> Q.TxE QErr ()
|
2018-11-23 16:02:46 +03:00
|
|
|
removeRemoteSchemaFromCatalog name =
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
DELETE FROM hdb_catalog.remote_schemas
|
|
|
|
WHERE name = $1
|
|
|
|
|] (Identity name) True
|