2022-02-08 12:24:34 +03:00
|
|
|
-- | Postgres DDL Function
|
|
|
|
--
|
|
|
|
-- This module describes building information about Postgres functions by
|
|
|
|
-- validating the passed raw information.
|
|
|
|
--
|
|
|
|
-- See 'Hasura.RQL.Types.Metadata.Backend'.
|
2021-02-14 09:07:52 +03:00
|
|
|
module Hasura.Backends.Postgres.DDL.Function
|
2021-09-24 01:56:37 +03:00
|
|
|
( buildFunctionInfo,
|
|
|
|
mkFunctionArgs,
|
2021-02-14 09:07:52 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Control.Lens hiding (from, index, op, (.=))
|
|
|
|
import Control.Monad.Validate qualified as MV
|
|
|
|
import Data.Sequence qualified as Seq
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Extended
|
2022-03-08 16:02:13 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.Types hiding (FunctionName)
|
2022-05-25 13:24:41 +03:00
|
|
|
import Hasura.Backends.Postgres.Types.Function
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Base.Error
|
2023-04-03 13:18:54 +03:00
|
|
|
import Hasura.Function.Cache
|
2022-07-12 17:00:15 +03:00
|
|
|
import Hasura.GraphQL.Schema.NamingCase
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types.Backend
|
2023-04-24 21:35:48 +03:00
|
|
|
import Hasura.RQL.Types.BackendType
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.SchemaCache
|
|
|
|
import Hasura.RQL.Types.SchemaCacheTypes
|
2022-07-12 17:00:15 +03:00
|
|
|
import Hasura.RQL.Types.SourceCustomization (applyFieldNameCaseCust)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.SQL.AnyBackend qualified as AB
|
|
|
|
import Hasura.Server.Utils
|
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
2021-02-14 09:07:52 +03:00
|
|
|
|
2022-05-25 13:24:41 +03:00
|
|
|
mkFunctionArgs :: Int -> [QualifiedPGType] -> [FunctionArgName] -> [FunctionArg]
|
2021-02-14 09:07:52 +03:00
|
|
|
mkFunctionArgs defArgsNo tys argNames =
|
|
|
|
bool withNames withNoNames $ null argNames
|
|
|
|
where
|
2021-09-24 01:56:37 +03:00
|
|
|
hasDefaultBoolSeq =
|
|
|
|
replicate (length tys - defArgsNo) (HasDefault False)
|
|
|
|
-- only last arguments can have default expression
|
|
|
|
<> replicate defArgsNo (HasDefault True)
|
2021-02-14 09:07:52 +03:00
|
|
|
|
|
|
|
tysWithHasDefault = zip tys hasDefaultBoolSeq
|
|
|
|
|
|
|
|
withNoNames = flip map tysWithHasDefault $ uncurry $ FunctionArg Nothing
|
|
|
|
withNames = zipWith mkArg argNames tysWithHasDefault
|
|
|
|
|
|
|
|
mkArg "" (ty, hasDef) = FunctionArg Nothing ty hasDef
|
2021-09-24 01:56:37 +03:00
|
|
|
mkArg n (ty, hasDef) = FunctionArg (Just n) ty hasDef
|
2021-02-14 09:07:52 +03:00
|
|
|
|
|
|
|
data FunctionIntegrityError
|
|
|
|
= FunctionNameNotGQLCompliant
|
|
|
|
| FunctionVariadic
|
|
|
|
| FunctionReturnNotCompositeType
|
|
|
|
| FunctionReturnNotTable
|
|
|
|
| NonVolatileFunctionAsMutation
|
2022-07-29 17:05:03 +03:00
|
|
|
| FunctionSessionArgumentNotJSON FunctionArgName
|
|
|
|
| FunctionInvalidSessionArgument FunctionArgName
|
2021-02-14 09:07:52 +03:00
|
|
|
| FunctionInvalidArgumentNames [FunctionArgName]
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
buildFunctionInfo ::
|
|
|
|
forall pgKind m.
|
|
|
|
(Backend ('Postgres pgKind), QErrM m) =>
|
|
|
|
SourceName ->
|
|
|
|
QualifiedFunction ->
|
|
|
|
SystemDefined ->
|
|
|
|
FunctionConfig ->
|
|
|
|
FunctionPermissionsMap ->
|
|
|
|
RawFunctionInfo ('Postgres pgKind) ->
|
2021-09-24 12:18:40 +03:00
|
|
|
Maybe Text ->
|
2022-05-26 14:54:30 +03:00
|
|
|
NamingCase ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m (FunctionInfo ('Postgres pgKind), SchemaDependency)
|
2022-05-26 14:54:30 +03:00
|
|
|
buildFunctionInfo source qf systemDefined fc@FunctionConfig {..} permissions rawFuncInfo comment tCase =
|
2021-02-14 09:07:52 +03:00
|
|
|
either (throw400 NotSupported . showErrors) pure
|
|
|
|
=<< MV.runValidateT validateFunction
|
|
|
|
where
|
|
|
|
functionArgs = mkFunctionArgs defArgsNo inpArgTyps inpArgNames
|
2021-09-24 01:56:37 +03:00
|
|
|
PGRawFunctionInfo
|
|
|
|
_
|
|
|
|
hasVariadic
|
|
|
|
funVol
|
|
|
|
retSn
|
|
|
|
retN
|
|
|
|
retTyTyp
|
|
|
|
retSet
|
|
|
|
inpArgTyps
|
|
|
|
inpArgNames
|
|
|
|
defArgsNo
|
|
|
|
returnsTab
|
|
|
|
descM =
|
|
|
|
rawFuncInfo
|
2021-02-14 09:07:52 +03:00
|
|
|
returnType = QualifiedPGType retSn retN retTyTyp
|
|
|
|
|
|
|
|
throwValidateError = MV.dispute . pure
|
|
|
|
|
|
|
|
validateFunction = do
|
|
|
|
unless (has _Right $ qualifiedObjectToName qf) $
|
|
|
|
throwValidateError FunctionNameNotGQLCompliant
|
|
|
|
when hasVariadic $ throwValidateError FunctionVariadic
|
|
|
|
when (retTyTyp /= PGKindComposite) $ throwValidateError FunctionReturnNotCompositeType
|
|
|
|
unless returnsTab $ throwValidateError FunctionReturnNotTable
|
|
|
|
-- We mostly take the user at their word here and will, e.g. expose a
|
|
|
|
-- function as a query if it is marked VOLATILE (since perhaps the user
|
|
|
|
-- is using the function to do some logging, say). But this is also a
|
|
|
|
-- footgun we'll need to try to document (since `VOLATILE` is default
|
|
|
|
-- when volatility is omitted). See the original approach here:
|
|
|
|
-- https://github.com/hasura/graphql-engine/pull/5858
|
|
|
|
--
|
|
|
|
-- This is the one exception where we do some validation. We're not
|
|
|
|
-- commited to this check, and it would be backwards compatible to remove
|
|
|
|
-- it, but this seemed like an obvious case:
|
|
|
|
when (funVol /= FTVOLATILE && _fcExposedAs == Just FEAMutation) $
|
2021-03-15 16:02:58 +03:00
|
|
|
throwValidateError NonVolatileFunctionAsMutation
|
2021-02-14 09:07:52 +03:00
|
|
|
-- If 'exposed_as' is omitted we'll infer it from the volatility:
|
|
|
|
let exposeAs = flip fromMaybe _fcExposedAs $ case funVol of
|
2021-09-24 01:56:37 +03:00
|
|
|
FTVOLATILE -> FEAMutation
|
|
|
|
_ -> FEAQuery
|
2021-02-14 09:07:52 +03:00
|
|
|
|
|
|
|
-- validate function argument names
|
|
|
|
validateFunctionArgNames
|
|
|
|
|
|
|
|
inputArguments <- makeInputArguments
|
|
|
|
|
2021-10-07 16:02:19 +03:00
|
|
|
funcGivenName <- functionGraphQLName @('Postgres pgKind) qf `onLeft` throwError
|
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
let retTable = typeToTable returnType
|
|
|
|
retJsonAggSelect = bool JASSingleObject JASMultipleRows retSet
|
2022-05-26 14:54:30 +03:00
|
|
|
setNamingCase = applyFieldNameCaseCust tCase
|
2021-10-07 16:02:19 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
functionInfo =
|
2021-09-24 01:56:37 +03:00
|
|
|
FunctionInfo
|
|
|
|
qf
|
2022-05-26 14:54:30 +03:00
|
|
|
(getFunctionGQLName funcGivenName fc setNamingCase)
|
|
|
|
(getFunctionArgsGQLName funcGivenName fc setNamingCase)
|
|
|
|
(getFunctionAggregateGQLName funcGivenName fc setNamingCase)
|
2021-09-24 01:56:37 +03:00
|
|
|
systemDefined
|
|
|
|
funVol
|
|
|
|
exposeAs
|
|
|
|
inputArguments
|
|
|
|
retTable
|
|
|
|
(getPGDescription <$> descM)
|
|
|
|
permissions
|
|
|
|
retJsonAggSelect
|
2021-09-24 12:18:40 +03:00
|
|
|
comment
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|
|
|
pure
|
|
|
|
( functionInfo,
|
|
|
|
SchemaDependency
|
|
|
|
( SOSourceObj source $
|
|
|
|
AB.mkAnyBackend $
|
|
|
|
SOITable @('Postgres pgKind) retTable
|
|
|
|
)
|
|
|
|
DRTable
|
|
|
|
)
|
2021-02-14 09:07:52 +03:00
|
|
|
|
|
|
|
validateFunctionArgNames = do
|
|
|
|
let argNames = mapMaybe faName functionArgs
|
|
|
|
invalidArgs = filter (isNothing . G.mkName . getFuncArgNameTxt) argNames
|
|
|
|
unless (null invalidArgs) $
|
2022-11-02 23:53:23 +03:00
|
|
|
throwValidateError $
|
|
|
|
FunctionInvalidArgumentNames invalidArgs
|
2021-02-14 09:07:52 +03:00
|
|
|
|
|
|
|
makeInputArguments =
|
|
|
|
case _fcSessionArgument of
|
|
|
|
Nothing -> pure $ Seq.fromList $ map IAUserProvided functionArgs
|
|
|
|
Just sessionArgName -> do
|
|
|
|
unless (any (\arg -> Just sessionArgName == faName arg) functionArgs) $
|
2022-11-02 23:53:23 +03:00
|
|
|
throwValidateError $
|
|
|
|
FunctionInvalidSessionArgument sessionArgName
|
2021-09-24 01:56:37 +03:00
|
|
|
fmap Seq.fromList $
|
|
|
|
forM functionArgs $ \arg ->
|
|
|
|
if Just sessionArgName == faName arg
|
|
|
|
then do
|
|
|
|
let argTy = _qptName $ faType arg
|
|
|
|
if argTy == PGJSON
|
|
|
|
then pure $ IASessionVariables sessionArgName
|
|
|
|
else MV.refute $ pure $ FunctionSessionArgumentNotJSON sessionArgName
|
|
|
|
else pure $ IAUserProvided arg
|
2021-02-14 09:07:52 +03:00
|
|
|
|
|
|
|
showErrors allErrors =
|
2022-11-02 23:53:23 +03:00
|
|
|
"the function "
|
|
|
|
<> qf <<> " cannot be tracked "
|
2021-09-24 01:56:37 +03:00
|
|
|
<> makeReasonMessage allErrors showOneError
|
2021-02-14 09:07:52 +03:00
|
|
|
|
|
|
|
showOneError = \case
|
|
|
|
FunctionNameNotGQLCompliant -> "function name is not a legal GraphQL identifier"
|
|
|
|
FunctionVariadic -> "function with \"VARIADIC\" parameters are not supported"
|
|
|
|
FunctionReturnNotCompositeType -> "the function does not return a \"COMPOSITE\" type"
|
|
|
|
FunctionReturnNotTable -> "the function does not return a table"
|
|
|
|
NonVolatileFunctionAsMutation ->
|
2021-09-24 01:56:37 +03:00
|
|
|
"the function was requested to be exposed as a mutation, but is not marked VOLATILE. "
|
|
|
|
<> "Maybe the function was given the wrong volatility when it was defined?"
|
2021-02-14 09:07:52 +03:00
|
|
|
FunctionSessionArgumentNotJSON argName ->
|
|
|
|
"given session argument " <> argName <<> " is not of type json"
|
|
|
|
FunctionInvalidSessionArgument argName ->
|
|
|
|
"given session argument " <> argName <<> " not the input argument of the function"
|
|
|
|
FunctionInvalidArgumentNames args ->
|
|
|
|
let argsText = T.intercalate "," $ map getFuncArgNameTxt args
|
2021-09-24 01:56:37 +03:00
|
|
|
in "the function arguments " <> argsText <> " are not in compliance with GraphQL spec"
|