2023-01-30 19:04:56 +03:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
-- | Define and handle v1/metadata API operations to track, untrack, and get logical models.
|
2023-02-22 12:22:22 +03:00
|
|
|
module Hasura.LogicalModel.API
|
2023-02-21 16:45:12 +03:00
|
|
|
( GetLogicalModel (..),
|
|
|
|
TrackLogicalModel (..),
|
|
|
|
UntrackLogicalModel (..),
|
|
|
|
runGetLogicalModel,
|
|
|
|
runTrackLogicalModel,
|
|
|
|
runUntrackLogicalModel,
|
|
|
|
dropLogicalModelInMetadata,
|
2023-02-22 12:22:22 +03:00
|
|
|
module Hasura.LogicalModel.Types,
|
2023-01-30 19:04:56 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2023-02-15 19:26:16 +03:00
|
|
|
import Autodocodec (HasCodec)
|
|
|
|
import Autodocodec qualified as AC
|
2023-02-03 19:27:44 +03:00
|
|
|
import Control.Lens (preview, (^?))
|
2023-01-30 19:04:56 +03:00
|
|
|
import Data.Aeson
|
2023-02-14 15:14:33 +03:00
|
|
|
import Data.Environment qualified as Env
|
2023-02-20 14:46:55 +03:00
|
|
|
import Data.HashMap.Strict.InsOrd.Extended qualified as OMap
|
2023-01-30 19:04:56 +03:00
|
|
|
import Hasura.Base.Error
|
2023-02-15 20:55:06 +03:00
|
|
|
import Hasura.CustomReturnType (CustomReturnType)
|
2023-01-30 19:04:56 +03:00
|
|
|
import Hasura.EncJSON
|
2023-03-02 19:02:27 +03:00
|
|
|
import Hasura.LogicalModel.Metadata (LogicalModelArgumentName, LogicalModelMetadata (..), parseInterpolatedQuery)
|
2023-02-22 12:22:22 +03:00
|
|
|
import Hasura.LogicalModel.Types
|
2023-02-15 19:26:16 +03:00
|
|
|
import Hasura.Metadata.DTO.Utils (codecNamePrefix)
|
2023-01-30 19:04:56 +03:00
|
|
|
import Hasura.Prelude
|
2023-02-15 20:55:06 +03:00
|
|
|
import Hasura.RQL.Types.Backend (Backend, ScalarType, SourceConnConfiguration)
|
2023-02-03 19:27:44 +03:00
|
|
|
import Hasura.RQL.Types.Common (SourceName, sourceNameToText, successMsg)
|
2023-01-30 19:04:56 +03:00
|
|
|
import Hasura.RQL.Types.Metadata
|
|
|
|
import Hasura.RQL.Types.Metadata.Backend
|
|
|
|
import Hasura.RQL.Types.Metadata.Object
|
|
|
|
import Hasura.RQL.Types.SchemaCache.Build
|
|
|
|
import Hasura.SQL.AnyBackend qualified as AB
|
|
|
|
import Hasura.SQL.Backend
|
|
|
|
import Hasura.Server.Init.FeatureFlag as FF
|
|
|
|
import Hasura.Server.Types (HasServerConfigCtx (..), ServerConfigCtx (..))
|
2023-02-14 17:38:38 +03:00
|
|
|
import Language.GraphQL.Draft.Syntax (unName)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
-- | Default implementation of the 'track_logical_model' request payload.
|
|
|
|
data TrackLogicalModel (b :: BackendType) = TrackLogicalModel
|
2023-02-22 16:45:27 +03:00
|
|
|
{ tlmSource :: SourceName,
|
|
|
|
tlmRootFieldName :: LogicalModelName,
|
|
|
|
tlmCode :: Text,
|
|
|
|
tlmArguments :: HashMap LogicalModelArgumentName (ScalarType b),
|
|
|
|
tlmDescription :: Maybe Text,
|
|
|
|
tlmReturns :: CustomReturnType b
|
2023-02-15 19:26:16 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
instance (Backend b) => HasCodec (TrackLogicalModel b) where
|
2023-02-15 19:26:16 +03:00
|
|
|
codec =
|
|
|
|
AC.CommentCodec
|
2023-02-21 16:45:12 +03:00
|
|
|
("A request to track a logical model")
|
|
|
|
$ AC.object (codecNamePrefix @b <> "TrackLogicalModel")
|
|
|
|
$ TrackLogicalModel
|
2023-02-15 19:26:16 +03:00
|
|
|
<$> AC.requiredField "source" sourceDoc
|
2023-02-22 16:45:27 +03:00
|
|
|
AC..= tlmSource
|
2023-02-15 19:26:16 +03:00
|
|
|
<*> AC.requiredField "root_field_name" rootFieldDoc
|
2023-02-22 16:45:27 +03:00
|
|
|
AC..= tlmRootFieldName
|
2023-02-15 19:26:16 +03:00
|
|
|
<*> AC.requiredField "code" codeDoc
|
2023-02-22 16:45:27 +03:00
|
|
|
AC..= tlmCode
|
2023-02-15 19:26:16 +03:00
|
|
|
<*> AC.optionalFieldWithDefault "arguments" mempty argumentsDoc
|
2023-02-22 16:45:27 +03:00
|
|
|
AC..= tlmArguments
|
2023-02-15 19:26:16 +03:00
|
|
|
<*> AC.optionalField "description" descriptionDoc
|
2023-02-22 16:45:27 +03:00
|
|
|
AC..= tlmDescription
|
2023-02-15 19:26:16 +03:00
|
|
|
<*> AC.requiredField "returns" returnsDoc
|
2023-02-22 16:45:27 +03:00
|
|
|
AC..= tlmReturns
|
2023-02-15 19:26:16 +03:00
|
|
|
where
|
2023-02-21 16:45:12 +03:00
|
|
|
sourceDoc = "The source in which this logical model should be tracked"
|
|
|
|
rootFieldDoc = "Root field name for the logical model"
|
2023-02-15 19:26:16 +03:00
|
|
|
codeDoc = "Native code expression (SQL) to run"
|
|
|
|
argumentsDoc = "Free variables in the expression and their types"
|
|
|
|
returnsDoc = "Return type (table) of the expression"
|
|
|
|
descriptionDoc = "A description of the query which appears in the graphql schema"
|
|
|
|
|
|
|
|
deriving via
|
2023-02-21 16:45:12 +03:00
|
|
|
(AC.Autodocodec (TrackLogicalModel b))
|
2023-02-15 19:26:16 +03:00
|
|
|
instance
|
2023-02-21 16:45:12 +03:00
|
|
|
(Backend b) => FromJSON (TrackLogicalModel b)
|
2023-02-15 19:26:16 +03:00
|
|
|
|
|
|
|
deriving via
|
2023-02-21 16:45:12 +03:00
|
|
|
(AC.Autodocodec (TrackLogicalModel b))
|
2023-02-15 19:26:16 +03:00
|
|
|
instance
|
2023-02-21 16:45:12 +03:00
|
|
|
(Backend b) => ToJSON (TrackLogicalModel b)
|
2023-02-15 19:26:16 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
-- | Validate a logical model and extract the logical model info from the request.
|
2023-03-02 19:02:27 +03:00
|
|
|
logicalModelTrackToMetadata ::
|
2023-02-15 19:26:16 +03:00
|
|
|
forall b m.
|
|
|
|
( BackendMetadata b,
|
|
|
|
MonadIO m,
|
|
|
|
MonadError QErr m
|
|
|
|
) =>
|
|
|
|
Env.Environment ->
|
|
|
|
SourceConnConfiguration b ->
|
2023-02-21 16:45:12 +03:00
|
|
|
TrackLogicalModel b ->
|
2023-03-02 19:02:27 +03:00
|
|
|
m (LogicalModelMetadata b)
|
|
|
|
logicalModelTrackToMetadata env sourceConnConfig TrackLogicalModel {..} = do
|
|
|
|
code <- parseInterpolatedQuery tlmCode `onLeft` \e -> throw400 ParseFailed e
|
2023-02-15 19:26:16 +03:00
|
|
|
|
2023-03-02 19:02:27 +03:00
|
|
|
let logicalModelMetadata =
|
|
|
|
LogicalModelMetadata
|
|
|
|
{ _lmmRootFieldName = tlmRootFieldName,
|
|
|
|
_lmmCode = code,
|
|
|
|
_lmmReturns = tlmReturns,
|
|
|
|
_lmmArguments = tlmArguments,
|
|
|
|
_lmmDescription = tlmDescription
|
|
|
|
}
|
2023-02-15 19:26:16 +03:00
|
|
|
|
2023-03-02 19:02:27 +03:00
|
|
|
validateLogicalModel @b env sourceConnConfig logicalModelMetadata
|
|
|
|
|
|
|
|
pure logicalModelMetadata
|
2023-02-15 19:26:16 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
-- | API payload for the 'get_logical_model' endpoint.
|
|
|
|
data GetLogicalModel (b :: BackendType) = GetLogicalModel
|
2023-02-22 16:45:27 +03:00
|
|
|
{ glmSource :: SourceName
|
2023-01-30 19:04:56 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
deriving instance Backend b => Show (GetLogicalModel b)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
deriving instance Backend b => Eq (GetLogicalModel b)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
instance Backend b => FromJSON (GetLogicalModel b) where
|
|
|
|
parseJSON = withObject "GetLogicalModel" $ \o -> do
|
2023-02-22 16:45:27 +03:00
|
|
|
glmSource <- o .: "source"
|
2023-02-21 16:45:12 +03:00
|
|
|
pure GetLogicalModel {..}
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
instance Backend b => ToJSON (GetLogicalModel b) where
|
|
|
|
toJSON GetLogicalModel {..} =
|
2023-01-30 19:04:56 +03:00
|
|
|
object
|
2023-02-22 16:45:27 +03:00
|
|
|
[ "source" .= glmSource
|
2023-01-30 19:04:56 +03:00
|
|
|
]
|
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
-- | Handler for the 'get_logical_model' endpoint.
|
|
|
|
runGetLogicalModel ::
|
2023-01-30 19:04:56 +03:00
|
|
|
forall b m.
|
|
|
|
( BackendMetadata b,
|
|
|
|
MetadataM m,
|
|
|
|
HasServerConfigCtx m,
|
|
|
|
MonadIO m,
|
|
|
|
MonadError QErr m
|
|
|
|
) =>
|
2023-02-21 16:45:12 +03:00
|
|
|
GetLogicalModel b ->
|
2023-01-30 19:04:56 +03:00
|
|
|
m EncJSON
|
2023-02-21 16:45:12 +03:00
|
|
|
runGetLogicalModel q = do
|
2023-01-30 19:04:56 +03:00
|
|
|
throwIfFeatureDisabled
|
|
|
|
|
|
|
|
metadata <- getMetadata
|
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
let logicalModel :: Maybe (LogicalModels b)
|
|
|
|
logicalModel = metadata ^? metaSources . ix (glmSource q) . toSourceMetadata . smLogicalModels @b
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
pure (encJFromJValue (OMap.elems <$> logicalModel))
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
-- | Handler for the 'track_logical_model' endpoint. The type 'TrackLogicalModel b'
|
|
|
|
-- (appearing here in wrapped as 'BackendTrackLogicalModel b' for 'AnyBackend'
|
|
|
|
-- compatibility) is defined in 'class LogicalModelMetadata'.
|
|
|
|
runTrackLogicalModel ::
|
2023-01-30 19:04:56 +03:00
|
|
|
forall b m.
|
|
|
|
( BackendMetadata b,
|
|
|
|
CacheRWM m,
|
|
|
|
MetadataM m,
|
|
|
|
MonadError QErr m,
|
|
|
|
HasServerConfigCtx m,
|
|
|
|
MonadIO m
|
|
|
|
) =>
|
2023-02-14 15:14:33 +03:00
|
|
|
Env.Environment ->
|
2023-02-21 16:45:12 +03:00
|
|
|
TrackLogicalModel b ->
|
2023-01-30 19:04:56 +03:00
|
|
|
m EncJSON
|
2023-02-21 16:45:12 +03:00
|
|
|
runTrackLogicalModel env trackLogicalModelRequest = do
|
2023-01-30 19:04:56 +03:00
|
|
|
throwIfFeatureDisabled
|
|
|
|
|
2023-02-03 19:27:44 +03:00
|
|
|
sourceConnConfig <-
|
|
|
|
maybe (throw400 NotFound $ "Source " <> sourceNameToText source <> " not found.") pure
|
|
|
|
. preview (metaSources . ix source . toSourceMetadata @b . smConfiguration)
|
|
|
|
=<< getMetadata
|
|
|
|
|
2023-03-02 19:02:27 +03:00
|
|
|
(metadata :: LogicalModelMetadata b) <- do
|
|
|
|
liftIO (runExceptT (logicalModelTrackToMetadata @b env sourceConnConfig trackLogicalModelRequest))
|
2023-02-15 19:26:16 +03:00
|
|
|
`onLeftM` throwError
|
2023-02-01 11:44:50 +03:00
|
|
|
|
2023-03-02 19:02:27 +03:00
|
|
|
let fieldName = _lmmRootFieldName metadata
|
2023-02-01 11:44:50 +03:00
|
|
|
metadataObj =
|
2023-01-30 19:04:56 +03:00
|
|
|
MOSourceObjId source $
|
|
|
|
AB.mkAnyBackend $
|
2023-02-21 16:45:12 +03:00
|
|
|
SMOLogicalModel @b fieldName
|
2023-01-30 19:04:56 +03:00
|
|
|
|
|
|
|
buildSchemaCacheFor metadataObj $
|
|
|
|
MetadataModifier $
|
2023-02-22 16:45:27 +03:00
|
|
|
(metaSources . ix source . toSourceMetadata @b . smLogicalModels)
|
2023-02-20 14:46:55 +03:00
|
|
|
%~ OMap.insert fieldName metadata
|
2023-01-30 19:04:56 +03:00
|
|
|
|
|
|
|
pure successMsg
|
|
|
|
where
|
2023-02-22 16:45:27 +03:00
|
|
|
source = tlmSource trackLogicalModelRequest
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
-- | API payload for the 'untrack_logical_model' endpoint.
|
|
|
|
data UntrackLogicalModel (b :: BackendType) = UntrackLogicalModel
|
2023-02-22 16:45:27 +03:00
|
|
|
{ utlmSource :: SourceName,
|
|
|
|
utlmRootFieldName :: LogicalModelName
|
2023-01-30 19:04:56 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
deriving instance Show (UntrackLogicalModel b)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
deriving instance Eq (UntrackLogicalModel b)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
instance FromJSON (UntrackLogicalModel b) where
|
|
|
|
parseJSON = withObject "UntrackLogicalModel" $ \o -> do
|
2023-02-22 16:45:27 +03:00
|
|
|
utlmSource <- o .: "source"
|
|
|
|
utlmRootFieldName <- o .: "root_field_name"
|
2023-02-21 16:45:12 +03:00
|
|
|
pure UntrackLogicalModel {..}
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
instance ToJSON (UntrackLogicalModel b) where
|
|
|
|
toJSON UntrackLogicalModel {..} =
|
2023-01-30 19:04:56 +03:00
|
|
|
object
|
2023-02-22 16:45:27 +03:00
|
|
|
[ "source" .= utlmSource,
|
|
|
|
"root_field_name" .= utlmRootFieldName
|
2023-01-30 19:04:56 +03:00
|
|
|
]
|
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
-- | Handler for the 'untrack_logical_model' endpoint.
|
|
|
|
runUntrackLogicalModel ::
|
2023-01-30 19:04:56 +03:00
|
|
|
forall b m.
|
|
|
|
( BackendMetadata b,
|
|
|
|
MonadError QErr m,
|
|
|
|
CacheRWM m,
|
|
|
|
MetadataM m,
|
|
|
|
HasServerConfigCtx m,
|
|
|
|
MonadIO m
|
|
|
|
) =>
|
2023-02-21 16:45:12 +03:00
|
|
|
UntrackLogicalModel b ->
|
2023-01-30 19:04:56 +03:00
|
|
|
m EncJSON
|
2023-02-21 16:45:12 +03:00
|
|
|
runUntrackLogicalModel q = do
|
2023-01-30 19:04:56 +03:00
|
|
|
throwIfFeatureDisabled
|
|
|
|
|
2023-02-14 17:38:38 +03:00
|
|
|
-- Check source exists
|
|
|
|
sourceMetadata <-
|
|
|
|
maybe (throw400 NotFound $ "Source " <> sourceNameToText source <> " not found.") pure
|
|
|
|
. preview (metaSources . ix source . toSourceMetadata @b)
|
|
|
|
=<< getMetadata
|
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
-- Check the logical model exists
|
2023-03-02 19:02:27 +03:00
|
|
|
unless (any ((== fieldName) . _lmmRootFieldName) $ _smLogicalModels sourceMetadata) do
|
2023-02-21 16:45:12 +03:00
|
|
|
throw400 NotFound $ "Logical model '" <> unName (getLogicalModelName fieldName) <> "' not found in source '" <> sourceNameToText source <> "'."
|
2023-02-14 17:38:38 +03:00
|
|
|
|
2023-01-30 19:04:56 +03:00
|
|
|
let metadataObj =
|
|
|
|
MOSourceObjId source $
|
|
|
|
AB.mkAnyBackend $
|
2023-02-21 16:45:12 +03:00
|
|
|
SMOLogicalModel @b fieldName
|
2023-01-30 19:04:56 +03:00
|
|
|
|
|
|
|
buildSchemaCacheFor metadataObj $
|
2023-02-21 16:45:12 +03:00
|
|
|
dropLogicalModelInMetadata @b source fieldName
|
2023-01-30 19:04:56 +03:00
|
|
|
|
|
|
|
pure successMsg
|
|
|
|
where
|
2023-02-22 16:45:27 +03:00
|
|
|
source = utlmSource q
|
|
|
|
fieldName = utlmRootFieldName q
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
dropLogicalModelInMetadata :: forall b. BackendMetadata b => SourceName -> LogicalModelName -> MetadataModifier
|
|
|
|
dropLogicalModelInMetadata source rootFieldName =
|
2023-01-30 19:04:56 +03:00
|
|
|
MetadataModifier $
|
2023-02-22 16:45:27 +03:00
|
|
|
metaSources . ix source . toSourceMetadata @b . smLogicalModels
|
2023-02-20 14:46:55 +03:00
|
|
|
%~ OMap.delete rootFieldName
|
2023-01-30 19:04:56 +03:00
|
|
|
|
|
|
|
-- | check feature flag is enabled before carrying out any actions
|
|
|
|
throwIfFeatureDisabled :: (HasServerConfigCtx m, MonadIO m, MonadError QErr m) => m ()
|
|
|
|
throwIfFeatureDisabled = do
|
|
|
|
configCtx <- askServerConfigCtx
|
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
enableLogicalModels <- liftIO (_sccCheckFeatureFlag configCtx FF.logicalModelInterface)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-21 16:45:12 +03:00
|
|
|
unless enableLogicalModels (throw500 "LogicalModels is disabled!")
|