2023-01-30 19:04:56 +03:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
|
|
|
|
-- | This module houses the function associated with the default implementation
|
|
|
|
-- of Metadata V1 commands (and their types) for handling user-specified Native
|
|
|
|
-- Query fragments.
|
|
|
|
--
|
|
|
|
-- The definitions herein ought to suffice for any instantiation of Native
|
|
|
|
-- Queries that only deviates in the contents of the 'TrackNativeQuery' payload.
|
|
|
|
-- And as such, the metadata endpoint 'Hasura.Server.API.Metadata' is hardwired
|
|
|
|
-- directly to this module without any overloading provided.
|
|
|
|
module Hasura.NativeQuery.API
|
|
|
|
( GetNativeQuery (..),
|
2023-02-15 19:26:16 +03:00
|
|
|
TrackNativeQuery (..),
|
2023-01-30 19:04:56 +03:00
|
|
|
UntrackNativeQuery (..),
|
|
|
|
runGetNativeQuery,
|
|
|
|
runTrackNativeQuery,
|
|
|
|
runUntrackNativeQuery,
|
|
|
|
dropNativeQueryInMetadata,
|
|
|
|
module Hasura.NativeQuery.Types,
|
|
|
|
)
|
|
|
|
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-02-15 19:26:16 +03:00
|
|
|
import Hasura.Metadata.DTO.Utils (codecNamePrefix)
|
|
|
|
import Hasura.NativeQuery.Metadata (NativeQueryArgumentName, NativeQueryInfo (..), parseInterpolatedQuery)
|
2023-01-30 19:04:56 +03:00
|
|
|
import Hasura.NativeQuery.Types
|
|
|
|
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-15 19:26:16 +03:00
|
|
|
-- | Default implementation of the 'track_native_query' request payload.
|
|
|
|
data TrackNativeQuery (b :: BackendType) = TrackNativeQuery
|
|
|
|
{ tnqSource :: SourceName,
|
|
|
|
tnqRootFieldName :: NativeQueryName,
|
|
|
|
tnqCode :: Text,
|
|
|
|
tnqArguments :: HashMap NativeQueryArgumentName (ScalarType b),
|
|
|
|
tnqDescription :: Maybe Text,
|
2023-02-15 20:55:06 +03:00
|
|
|
tnqReturns :: CustomReturnType b
|
2023-02-15 19:26:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
instance (Backend b) => HasCodec (TrackNativeQuery b) where
|
|
|
|
codec =
|
|
|
|
AC.CommentCodec
|
|
|
|
("A request to track a native query")
|
|
|
|
$ AC.object (codecNamePrefix @b <> "TrackNativeQuery")
|
|
|
|
$ TrackNativeQuery
|
|
|
|
<$> AC.requiredField "source" sourceDoc
|
|
|
|
AC..= tnqSource
|
|
|
|
<*> AC.requiredField "root_field_name" rootFieldDoc
|
|
|
|
AC..= tnqRootFieldName
|
|
|
|
<*> AC.requiredField "code" codeDoc
|
|
|
|
AC..= tnqCode
|
|
|
|
<*> AC.optionalFieldWithDefault "arguments" mempty argumentsDoc
|
|
|
|
AC..= tnqArguments
|
|
|
|
<*> AC.optionalField "description" descriptionDoc
|
|
|
|
AC..= tnqDescription
|
|
|
|
<*> AC.requiredField "returns" returnsDoc
|
|
|
|
AC..= tnqReturns
|
|
|
|
where
|
|
|
|
sourceDoc = "The source in whic this native query should be tracked"
|
|
|
|
rootFieldDoc = "Root field name for the native query"
|
|
|
|
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
|
|
|
|
(AC.Autodocodec (TrackNativeQuery b))
|
|
|
|
instance
|
|
|
|
(Backend b) => FromJSON (TrackNativeQuery b)
|
|
|
|
|
|
|
|
deriving via
|
|
|
|
(AC.Autodocodec (TrackNativeQuery b))
|
|
|
|
instance
|
|
|
|
(Backend b) => ToJSON (TrackNativeQuery b)
|
|
|
|
|
|
|
|
-- | Default implementation of the method 'nativeQueryTrackToInfo'.
|
|
|
|
nativeQueryTrackToInfo ::
|
|
|
|
forall b m.
|
|
|
|
( BackendMetadata b,
|
|
|
|
MonadIO m,
|
|
|
|
MonadError QErr m
|
|
|
|
) =>
|
|
|
|
Env.Environment ->
|
|
|
|
SourceConnConfiguration b ->
|
|
|
|
TrackNativeQuery b ->
|
|
|
|
m (NativeQueryInfo b)
|
|
|
|
nativeQueryTrackToInfo env sourceConnConfig TrackNativeQuery {..} = do
|
|
|
|
nqiCode <- parseInterpolatedQuery tnqCode `onLeft` \e -> throw400 ParseFailed e
|
|
|
|
let nqiRootFieldName = tnqRootFieldName
|
|
|
|
nqiReturns = tnqReturns
|
|
|
|
nqiArguments = tnqArguments
|
|
|
|
nqiDescription = tnqDescription
|
|
|
|
nqInfoImpl = NativeQueryInfo {..}
|
|
|
|
|
|
|
|
validateNativeQuery @b env sourceConnConfig nqInfoImpl
|
|
|
|
|
|
|
|
pure nqInfoImpl
|
|
|
|
|
2023-01-30 19:04:56 +03:00
|
|
|
-- | API payload for the 'get_native_query' endpoint.
|
|
|
|
data GetNativeQuery (b :: BackendType) = GetNativeQuery
|
|
|
|
{ gnqSource :: SourceName
|
|
|
|
}
|
|
|
|
|
|
|
|
deriving instance Backend b => Show (GetNativeQuery b)
|
|
|
|
|
|
|
|
deriving instance Backend b => Eq (GetNativeQuery b)
|
|
|
|
|
|
|
|
instance Backend b => FromJSON (GetNativeQuery b) where
|
|
|
|
parseJSON = withObject "GetNativeQuery" $ \o -> do
|
|
|
|
gnqSource <- o .: "source"
|
|
|
|
pure GetNativeQuery {..}
|
|
|
|
|
|
|
|
instance Backend b => ToJSON (GetNativeQuery b) where
|
|
|
|
toJSON GetNativeQuery {..} =
|
|
|
|
object
|
|
|
|
[ "source" .= gnqSource
|
|
|
|
]
|
|
|
|
|
|
|
|
-- | Handler for the 'get_native_query' endpoint.
|
|
|
|
runGetNativeQuery ::
|
|
|
|
forall b m.
|
|
|
|
( BackendMetadata b,
|
|
|
|
MetadataM m,
|
|
|
|
HasServerConfigCtx m,
|
|
|
|
MonadIO m,
|
|
|
|
MonadError QErr m
|
|
|
|
) =>
|
|
|
|
GetNativeQuery b ->
|
|
|
|
m EncJSON
|
|
|
|
runGetNativeQuery q = do
|
|
|
|
throwIfFeatureDisabled
|
|
|
|
|
|
|
|
metadata <- getMetadata
|
|
|
|
|
2023-02-02 13:53:25 +03:00
|
|
|
let nativeQuery :: Maybe (NativeQueries b)
|
|
|
|
nativeQuery = metadata ^? metaSources . ix (gnqSource q) . toSourceMetadata . smNativeQueries @b
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-20 14:46:55 +03:00
|
|
|
pure (encJFromJValue (OMap.elems <$> nativeQuery))
|
2023-01-30 19:04:56 +03:00
|
|
|
|
|
|
|
-- | Handler for the 'track_native_query' endpoint. The type 'TrackNativeQuery
|
|
|
|
-- b' (appearing here in wrapped as 'BackendTrackNativeQuery b' for 'AnyBackend'
|
|
|
|
-- compatibility) is defined in 'class NativeQueryMetadata'.
|
|
|
|
runTrackNativeQuery ::
|
|
|
|
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-15 19:26:16 +03:00
|
|
|
TrackNativeQuery b ->
|
2023-01-30 19:04:56 +03:00
|
|
|
m EncJSON
|
2023-02-15 19:26:16 +03:00
|
|
|
runTrackNativeQuery env trackNativeQueryRequest = 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
|
|
|
|
|
|
|
|
(metadata :: NativeQueryInfo b) <- do
|
2023-02-15 19:26:16 +03:00
|
|
|
liftIO (runExceptT (nativeQueryTrackToInfo @b env sourceConnConfig trackNativeQueryRequest))
|
|
|
|
`onLeftM` throwError
|
2023-02-01 11:44:50 +03:00
|
|
|
|
2023-02-15 19:26:16 +03:00
|
|
|
let fieldName = nqiRootFieldName metadata
|
2023-02-01 11:44:50 +03:00
|
|
|
metadataObj =
|
2023-01-30 19:04:56 +03:00
|
|
|
MOSourceObjId source $
|
|
|
|
AB.mkAnyBackend $
|
|
|
|
SMONativeQuery @b fieldName
|
|
|
|
|
|
|
|
buildSchemaCacheFor metadataObj $
|
|
|
|
MetadataModifier $
|
|
|
|
(metaSources . ix source . toSourceMetadata @b . smNativeQueries)
|
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-15 19:26:16 +03:00
|
|
|
source = tnqSource trackNativeQueryRequest
|
2023-01-30 19:04:56 +03:00
|
|
|
|
|
|
|
-- | API payload for the 'untrack_native_query' endpoint.
|
|
|
|
data UntrackNativeQuery (b :: BackendType) = UntrackNativeQuery
|
|
|
|
{ utnqSource :: SourceName,
|
2023-02-08 18:46:09 +03:00
|
|
|
utnqRootFieldName :: NativeQueryName
|
2023-01-30 19:04:56 +03:00
|
|
|
}
|
|
|
|
|
2023-02-08 18:46:09 +03:00
|
|
|
deriving instance Show (UntrackNativeQuery b)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-08 18:46:09 +03:00
|
|
|
deriving instance Eq (UntrackNativeQuery b)
|
2023-01-30 19:04:56 +03:00
|
|
|
|
2023-02-08 18:46:09 +03:00
|
|
|
instance FromJSON (UntrackNativeQuery b) where
|
2023-01-30 19:04:56 +03:00
|
|
|
parseJSON = withObject "UntrackNativeQuery" $ \o -> do
|
|
|
|
utnqSource <- o .: "source"
|
2023-02-02 13:53:25 +03:00
|
|
|
utnqRootFieldName <- o .: "root_field_name"
|
2023-01-30 19:04:56 +03:00
|
|
|
pure UntrackNativeQuery {..}
|
|
|
|
|
2023-02-08 18:46:09 +03:00
|
|
|
instance ToJSON (UntrackNativeQuery b) where
|
2023-01-30 19:04:56 +03:00
|
|
|
toJSON UntrackNativeQuery {..} =
|
|
|
|
object
|
|
|
|
[ "source" .= utnqSource,
|
2023-02-02 13:53:25 +03:00
|
|
|
"root_field_name" .= utnqRootFieldName
|
2023-01-30 19:04:56 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
-- | Handler for the 'untrack_native_query' endpoint.
|
|
|
|
runUntrackNativeQuery ::
|
|
|
|
forall b m.
|
|
|
|
( BackendMetadata b,
|
|
|
|
MonadError QErr m,
|
|
|
|
CacheRWM m,
|
|
|
|
MetadataM m,
|
|
|
|
HasServerConfigCtx m,
|
|
|
|
MonadIO m
|
|
|
|
) =>
|
|
|
|
UntrackNativeQuery b ->
|
|
|
|
m EncJSON
|
|
|
|
runUntrackNativeQuery q = do
|
|
|
|
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
|
|
|
|
|
|
|
|
-- Check native query exists
|
2023-02-15 19:26:16 +03:00
|
|
|
unless (any ((== fieldName) . nqiRootFieldName) $ _smNativeQueries sourceMetadata) do
|
2023-02-14 17:38:38 +03:00
|
|
|
throw400 NotFound $ "Native query '" <> unName (getNativeQueryName fieldName) <> "' not found in source '" <> sourceNameToText source <> "'."
|
|
|
|
|
2023-01-30 19:04:56 +03:00
|
|
|
let metadataObj =
|
|
|
|
MOSourceObjId source $
|
|
|
|
AB.mkAnyBackend $
|
|
|
|
SMONativeQuery @b fieldName
|
|
|
|
|
|
|
|
buildSchemaCacheFor metadataObj $
|
|
|
|
dropNativeQueryInMetadata @b source fieldName
|
|
|
|
|
|
|
|
pure successMsg
|
|
|
|
where
|
|
|
|
source = utnqSource q
|
|
|
|
fieldName = utnqRootFieldName q
|
|
|
|
|
2023-02-08 18:46:09 +03:00
|
|
|
dropNativeQueryInMetadata :: forall b. BackendMetadata b => SourceName -> NativeQueryName -> MetadataModifier
|
2023-01-30 19:04:56 +03:00
|
|
|
dropNativeQueryInMetadata source rootFieldName =
|
|
|
|
MetadataModifier $
|
2023-02-20 14:46:55 +03:00
|
|
|
metaSources . ix source . toSourceMetadata @b . smNativeQueries
|
|
|
|
%~ 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
|
|
|
|
|
|
|
|
enableNativeQuery <- liftIO (_sccCheckFeatureFlag configCtx FF.nativeQueryInterface)
|
|
|
|
|
|
|
|
unless enableNativeQuery (throw500 "NativeQuery is disabled!")
|