2019-08-09 12:19:17 +03:00
|
|
|
module Hasura.GraphQL.Schema.Function
|
|
|
|
( procFuncArgs
|
|
|
|
, mkFuncArgsInp
|
|
|
|
, mkFuncQueryFld
|
|
|
|
, mkFuncAggQueryFld
|
2019-10-18 11:29:47 +03:00
|
|
|
, mkFuncArgsTy
|
2019-08-09 12:19:17 +03:00
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.Sequence as Seq
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
|
|
|
|
import Hasura.GraphQL.Schema.Common
|
|
|
|
import Hasura.GraphQL.Schema.Select
|
|
|
|
import Hasura.GraphQL.Validate.Types
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.SQL.Types
|
|
|
|
|
|
|
|
{-
|
|
|
|
input function_args {
|
|
|
|
arg1: arg-type1!
|
|
|
|
. .
|
|
|
|
. .
|
|
|
|
argn: arg-typen!
|
|
|
|
}
|
|
|
|
-}
|
|
|
|
|
2019-11-20 09:47:06 +03:00
|
|
|
procFuncArgs :: Seq.Seq a -> (a -> Maybe FunctionArgName) -> (a -> Text -> b) -> [b]
|
|
|
|
procFuncArgs argSeq nameFn resultFn =
|
2019-08-09 12:19:17 +03:00
|
|
|
fst $ foldl mkItem ([], 1::Int) argSeq
|
|
|
|
where
|
2019-08-28 22:27:15 +03:00
|
|
|
mkItem (items, argNo) fa =
|
2019-11-20 09:47:06 +03:00
|
|
|
case nameFn fa of
|
2019-08-09 12:19:17 +03:00
|
|
|
Just argName ->
|
|
|
|
let argT = getFuncArgNameTxt argName
|
2019-11-20 09:47:06 +03:00
|
|
|
in (items <> pure (resultFn fa argT), argNo)
|
2019-08-09 12:19:17 +03:00
|
|
|
Nothing ->
|
|
|
|
let argT = "arg_" <> T.pack (show argNo)
|
2019-11-20 09:47:06 +03:00
|
|
|
in (items <> pure (resultFn fa argT), argNo + 1)
|
2019-08-09 12:19:17 +03:00
|
|
|
|
2019-10-18 11:29:47 +03:00
|
|
|
mkFuncArgsInp :: QualifiedFunction -> Seq.Seq FunctionArg -> Maybe InpObjTyInfo
|
|
|
|
mkFuncArgsInp funcName funcArgs =
|
2019-08-09 12:19:17 +03:00
|
|
|
bool (Just inpObj) Nothing $ null funcArgs
|
|
|
|
where
|
|
|
|
funcArgsTy = mkFuncArgsTy funcName
|
|
|
|
|
|
|
|
inpObj = mkHsraInpTyInfo Nothing funcArgsTy $
|
|
|
|
fromInpValL argInps
|
|
|
|
|
2019-11-20 09:47:06 +03:00
|
|
|
argInps = procFuncArgs funcArgs faName mkInpVal
|
2019-08-09 12:19:17 +03:00
|
|
|
|
2019-08-28 22:27:15 +03:00
|
|
|
mkInpVal fa t =
|
2019-08-09 12:19:17 +03:00
|
|
|
InpValInfo Nothing (G.Name t) Nothing $
|
2019-10-18 11:29:47 +03:00
|
|
|
G.toGT $ mkScalarTy $ _qptName $ faType fa
|
2019-08-09 12:19:17 +03:00
|
|
|
|
|
|
|
{-
|
|
|
|
|
|
|
|
function(
|
|
|
|
args: function_args
|
|
|
|
where: table_bool_exp
|
|
|
|
limit: Int
|
|
|
|
offset: Int
|
|
|
|
): [table!]!
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
mkFuncArgs :: FunctionInfo -> ParamMap
|
|
|
|
mkFuncArgs funInfo =
|
|
|
|
fromInpValL $ funcInpArgs <> mkSelArgs retTable
|
|
|
|
where
|
|
|
|
funcName = fiName funInfo
|
2019-11-20 09:47:06 +03:00
|
|
|
funcArgs = getInputArgs funInfo
|
2019-08-09 12:19:17 +03:00
|
|
|
retTable = fiReturnType funInfo
|
|
|
|
|
|
|
|
funcArgDesc = G.Description $ "input parameters for function " <>> funcName
|
|
|
|
funcInpArg = InpValInfo (Just funcArgDesc) "args" Nothing $ G.toGT $ G.toNT $
|
|
|
|
mkFuncArgsTy funcName
|
|
|
|
funcInpArgs = bool [funcInpArg] [] $ null funcArgs
|
|
|
|
|
|
|
|
mkFuncQueryFld
|
2019-09-17 04:51:11 +03:00
|
|
|
:: FunctionInfo -> Maybe PGDescription -> ObjFldInfo
|
|
|
|
mkFuncQueryFld funInfo descM =
|
2019-08-09 12:19:17 +03:00
|
|
|
mkHsraObjFldInfo (Just desc) fldName (mkFuncArgs funInfo) ty
|
|
|
|
where
|
|
|
|
retTable = fiReturnType funInfo
|
|
|
|
funcName = fiName funInfo
|
|
|
|
|
2019-09-17 04:51:11 +03:00
|
|
|
desc = mkDescriptionWith descM $ "execute function " <> funcName
|
2019-08-09 12:19:17 +03:00
|
|
|
<<> " which returns " <>> retTable
|
|
|
|
fldName = qualObjectToName funcName
|
|
|
|
|
|
|
|
ty = G.toGT $ G.toNT $ G.toLT $ G.toNT $ mkTableTy retTable
|
|
|
|
|
|
|
|
{-
|
|
|
|
|
|
|
|
function_aggregate(
|
|
|
|
args: function_args
|
|
|
|
where: table_bool_exp
|
|
|
|
limit: Int
|
|
|
|
offset: Int
|
|
|
|
): table_aggregate!
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
mkFuncAggQueryFld
|
2019-09-17 04:51:11 +03:00
|
|
|
:: FunctionInfo -> Maybe PGDescription -> ObjFldInfo
|
|
|
|
mkFuncAggQueryFld funInfo descM =
|
2019-08-09 12:19:17 +03:00
|
|
|
mkHsraObjFldInfo (Just desc) fldName (mkFuncArgs funInfo) ty
|
|
|
|
where
|
|
|
|
funcName = fiName funInfo
|
|
|
|
retTable = fiReturnType funInfo
|
|
|
|
|
2019-09-17 04:51:11 +03:00
|
|
|
desc = mkDescriptionWith descM $ "execute function " <> funcName
|
2019-08-09 12:19:17 +03:00
|
|
|
<<> " and query aggregates on result of table type "
|
|
|
|
<>> retTable
|
|
|
|
|
|
|
|
fldName = qualObjectToName funcName <> "_aggregate"
|
|
|
|
|
|
|
|
ty = G.toGT $ G.toNT $ mkTableAggTy retTable
|