这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion server/src-exec/Migrate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import qualified Data.Yaml.TH as Y
import qualified Database.PG.Query as Q

curCatalogVer :: T.Text
curCatalogVer = "21"
curCatalogVer = "22"

migrateMetadata
:: ( MonadTx m
Expand Down Expand Up @@ -354,6 +354,12 @@ from20To21 :: (MonadTx m) => m ()
from20To21 = liftTx $ Q.catchE defaultTxErrorHandler $ do
Q.unitQ "CREATE INDEX ON hdb_catalog.event_log (locked)" () False

from21To22 :: (MonadTx m) => m ()
from21To22 = do
Q.Discard () <- liftTx $ Q.multiQE defaultTxErrorHandler
$(Q.sqlFromFile "src-rsr/migrate_from_21_to_22.sql")
pure ()

migrateCatalog
:: ( MonadTx m
, CacheRWM m
Expand Down Expand Up @@ -396,6 +402,7 @@ migrateCatalog migrationTime = migrateFrom =<< getCatalogVersion
, ("18", from18To19)
, ("19", from19To20)
, ("20", from20To21)
, ("21", from21To22)
]

postMigrate = do
Expand Down
1 change: 0 additions & 1 deletion server/src-lib/Hasura/GraphQL/Resolve/Mutation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Hasura.GraphQL.Resolve.Mutation
, buildEmptyMutResp
) where

import Control.Arrow (second)
import Data.Has
import Hasura.Prelude

Expand Down
30 changes: 22 additions & 8 deletions server/src-lib/Hasura/GraphQL/Resolve/Select.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module Hasura.GraphQL.Resolve.Select
, toPGQuery
) where

import Control.Arrow (first)
import Data.Has
import Data.Parser.JSONPath
import Hasura.Prelude
Expand Down Expand Up @@ -413,13 +412,28 @@ convertAggSelect opCtx fld =

parseFunctionArgs
::( MonadError QErr m)
=> FuncArgSeq -> AnnInpVal -> m [UnresolvedVal]
parseFunctionArgs argSeq val = fmap catMaybes $
flip withObject val $ \_ obj ->
fmap toList $ forM argSeq $ \(FuncArgItem argName) ->
forM (OMap.lookup argName obj) $ fmap (maybe nullSQL UVPG) . asPGColumnValueM
=> FuncArgSeq
-> AnnInpVal
-> m (RS.FunctionArgsExpG UnresolvedVal)
parseFunctionArgs argSeq val = flip withObject val $ \_ obj -> do
(positionalArgs, argsLeft) <- spanMaybeM (parsePositionalArg obj) argSeq
namedArgs <- Map.fromList . catMaybes <$> traverse (parseNamedArg obj) argsLeft
pure $ RS.FunctionArgsExp positionalArgs namedArgs
where
nullSQL = UVSQL $ S.SEUnsafe "NULL"
parsePositionalArg obj (FuncArgItem gqlName _ _) =
maybe (pure Nothing) (fmap Just . parseArg) $ OMap.lookup gqlName obj

parseArg = fmap (maybe (UVSQL S.SENull) UVPG) . asPGColumnValueM

parseNamedArg obj (FuncArgItem gqlName maybeSqlName hasDefault) =
case OMap.lookup gqlName obj of
Just argInpVal -> case maybeSqlName of
Just sqlName -> Just . (getFuncArgNameTxt sqlName,) <$> parseArg argInpVal
Nothing -> throw400 NotSupported
"Only last set of positional arguments can be omitted"
Nothing -> if not hasDefault then
throw400 NotSupported "Non default arguments cannot be omitted"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for plumbing this information through.

else pure Nothing

fromFuncQueryField
:: (MonadError QErr m)
Expand All @@ -429,7 +443,7 @@ fromFuncQueryField
-> m (RS.AnnFnSelG s UnresolvedVal)
fromFuncQueryField fn qf argSeq fld = fieldAsPath fld $ do
funcArgsM <- withArgM (_fArguments fld) "args" $ parseFunctionArgs argSeq
let funcArgs = fromMaybe [] funcArgsM
let funcArgs = fromMaybe RS.emptyFunctionArgsExp funcArgsM
RS.AnnFnSel qf funcArgs <$> fn fld

convertFuncQuerySimple
Expand Down
9 changes: 6 additions & 3 deletions server/src-lib/Hasura/GraphQL/Resolve/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,12 @@ type OrdByItemMap = Map.HashMap G.Name OrdByItem

type OrdByCtx = Map.HashMap G.NamedType OrdByItemMap

newtype FuncArgItem
= FuncArgItem {getArgName :: G.Name}
deriving (Show, Eq)
data FuncArgItem
= FuncArgItem
{ _faiInputArgName :: !G.Name
, _faiSqlArgName :: !(Maybe FunctionArgName)
, _faiHasDefault :: !Bool
} deriving (Show, Eq)

type FuncArgSeq = Seq.Seq FuncArgItem

Expand Down
10 changes: 5 additions & 5 deletions server/src-lib/Hasura/GraphQL/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Hasura.GraphQL.Schema
, checkSchemaConflicts
) where

import Control.Lens.Extended hiding (op)
import Control.Lens.Extended hiding (op)

import qualified Data.HashMap.Strict as Map
import qualified Data.HashSet as Set
Expand All @@ -37,13 +37,13 @@ import Hasura.SQL.Types
import Hasura.GraphQL.Schema.BoolExp
import Hasura.GraphQL.Schema.Common
import Hasura.GraphQL.Schema.Function
import Hasura.GraphQL.Schema.Merge
import Hasura.GraphQL.Schema.Mutation.Common
import Hasura.GraphQL.Schema.Mutation.Delete
import Hasura.GraphQL.Schema.Mutation.Insert
import Hasura.GraphQL.Schema.Mutation.Update
import Hasura.GraphQL.Schema.OrderBy
import Hasura.GraphQL.Schema.Select
import Hasura.GraphQL.Schema.Merge

getInsPerm :: TableInfo PGColumnInfo -> RoleName -> Maybe InsPermInfo
getInsPerm tabInfo role
Expand Down Expand Up @@ -375,8 +375,8 @@ getRootFldsRole' tn primCols constraints fields funcs insM selM updM delM viM =
, g fi
)

mkFuncArgItemSeq fi = Seq.fromList $
procFuncArgs (fiInputArgs fi) $ \_ t -> FuncArgItem $ G.Name t
mkFuncArgItemSeq fi = Seq.fromList $ procFuncArgs (fiInputArgs fi)
$ \fa t -> FuncArgItem (G.Name t) (faName fa) (faHasDefault fa)


getSelPermission :: TableInfo PGColumnInfo -> RoleName -> Maybe SelPermInfo
Expand Down Expand Up @@ -636,7 +636,7 @@ instance Monoid TyAgg where
-- | A role-specific mapping from root field names to allowed operations.
data RootFields
= RootFields
{ rootQueryFields :: !(Map.HashMap G.Name (QueryCtx, ObjFldInfo))
{ rootQueryFields :: !(Map.HashMap G.Name (QueryCtx, ObjFldInfo))
, rootMutationFields :: !(Map.HashMap G.Name (MutationCtx, ObjFldInfo))
} deriving (Show, Eq)

Expand Down
14 changes: 7 additions & 7 deletions server/src-lib/Hasura/GraphQL/Schema/Function.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ input function_args {

procFuncArgs
:: Seq.Seq FunctionArg
-> (PGScalarType -> Text -> a) -> [a]
-> (FunctionArg -> Text -> a) -> [a]
procFuncArgs argSeq f =
fst $ foldl mkItem ([], 1::Int) argSeq
where
mkItem (items, argNo) (FunctionArg nameM ty) =
case nameM of
mkItem (items, argNo) fa =
case faName fa of
Just argName ->
let argT = getFuncArgNameTxt argName
in (items <> pure (f ty argT), argNo)
in (items <> pure (f fa argT), argNo)
Nothing ->
let argT = "arg_" <> T.pack (show argNo)
in (items <> pure (f ty argT), argNo + 1)
in (items <> pure (f fa argT), argNo + 1)

mkFuncArgsInp :: FunctionInfo -> Maybe InpObjTyInfo
mkFuncArgsInp funcInfo =
Expand All @@ -62,9 +62,9 @@ mkFuncArgsInp funcInfo =

argInps = procFuncArgs funcArgs mkInpVal

mkInpVal ty t =
mkInpVal fa t =
InpValInfo Nothing (G.Name t) Nothing $
G.toGT $ mkScalarTy ty
G.toGT $ mkScalarTy $ faType fa

{-

Expand Down
17 changes: 15 additions & 2 deletions server/src-lib/Hasura/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ module Hasura.Prelude
, choice
, bsToTxt
, txtToBs
, spanMaybeM
) where

import Control.Applicative as M (Alternative (..))
import Control.Arrow as M (first, second, (&&&), (***))
import Control.Monad as M (void, when)
import Control.Monad.Base as M
import Control.Monad.Except as M
Expand All @@ -20,7 +22,7 @@ import Data.Bool as M (bool)
import Data.Data as M (Data (..))
import Data.Either as M (lefts, partitionEithers,
rights)
import Data.Foldable as M (foldrM, for_, toList,
import Data.Foldable as M (asum, foldrM, for_, toList,
traverse_)
import Data.Function as M (on, (&))
import Data.Functor as M (($>), (<&>))
Expand Down Expand Up @@ -56,10 +58,21 @@ onLeft :: (Monad m) => Either e a -> (e -> m a) -> m a
onLeft e f = either f return e

choice :: (Alternative f) => [f a] -> f a
choice = foldr (<|>) empty
choice = asum

bsToTxt :: B.ByteString -> Text
bsToTxt = TE.decodeUtf8With TE.lenientDecode

txtToBs :: Text -> B.ByteString
txtToBs = TE.encodeUtf8

-- Like 'span', but monadic and with a function that produces 'Maybe' instead of 'Bool'
spanMaybeM
:: (Foldable f, Monad m)
=> (a -> m (Maybe b)) -> f a -> m ([b], [a])
spanMaybeM f = go . toList
where
go [] = pure ([], [])
go l@(x:xs) = f x >>= \case
Just y -> first (y:) <$> go xs
Nothing -> pure ([], l)
1 change: 0 additions & 1 deletion server/src-lib/Hasura/RQL/DDL/Permission.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import Hasura.SQL.Types
import qualified Database.PG.Query as Q
import qualified Hasura.SQL.DML as S

import Control.Arrow (first)
import Data.Aeson
import Data.Aeson.Casing
import Data.Aeson.TH
Expand Down
1 change: 0 additions & 1 deletion server/src-lib/Hasura/RQL/DDL/Schema/Diff.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import Hasura.SQL.Types

import qualified Database.PG.Query as Q

import Control.Arrow ((***))
import Data.Aeson.Casing
import Data.Aeson.TH

Expand Down
24 changes: 16 additions & 8 deletions server/src-lib/Hasura/RQL/DDL/Schema/Function.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,27 @@ data RawFuncInfo
, rfiReturnsSet :: !Bool
, rfiInputArgTypes :: ![PGScalarType]
, rfiInputArgNames :: ![T.Text]
, rfiDefaultArgs :: !Int
, rfiReturnsTable :: !Bool
} deriving (Show, Eq)
$(deriveJSON (aesonDrop 3 snakeCase) ''RawFuncInfo)

mkFunctionArgs :: [PGScalarType] -> [T.Text] -> [FunctionArg]
mkFunctionArgs tys argNames =
mkFunctionArgs :: Int -> [PGScalarType] -> [T.Text] -> [FunctionArg]
mkFunctionArgs defArgsNo tys argNames =
bool withNames withNoNames $ null argNames
where
withNoNames = flip map tys $ \ty -> FunctionArg Nothing ty
withNames = zipWith mkArg argNames tys
hasDefaultBoolSeq = replicate (length argNames - defArgsNo) False
-- only last arguments can have default expression
<> replicate defArgsNo True

mkArg "" ty = FunctionArg Nothing ty
mkArg n ty = flip FunctionArg ty $ Just $ FunctionArgName n
tysWithHasDefault = zip tys hasDefaultBoolSeq

withNoNames = flip map tysWithHasDefault $
\(ty, hasDef) -> FunctionArg Nothing ty hasDef
withNames = zipWith mkArg argNames tysWithHasDefault

mkArg "" (ty, hasDef) = FunctionArg Nothing ty hasDef
mkArg n (ty, hasDef) = FunctionArg (Just $ FunctionArgName n) ty hasDef

validateFuncArgs :: MonadError QErr m => [FunctionArg] -> m ()
validateFuncArgs args =
Expand All @@ -81,7 +89,7 @@ mkFunctionInfo qf rawFuncInfo = do
-- throw error if function type is VOLATILE
when (funTy == FTVOLATILE) $ throw400 NotSupported "function of type \"VOLATILE\" is not supported now"

let funcArgs = mkFunctionArgs inpArgTyps inpArgNames
let funcArgs = mkFunctionArgs defArgsNo inpArgTyps inpArgNames
validateFuncArgs funcArgs

let funcArgsSeq = Seq.fromList funcArgs
Expand All @@ -90,7 +98,7 @@ mkFunctionInfo qf rawFuncInfo = do
return $ FunctionInfo qf False funTy funcArgsSeq retTable [dep]
where
RawFuncInfo hasVariadic funTy retSn retN retTyTyp
retSet inpArgTyps inpArgNames returnsTab
retSet inpArgTyps inpArgNames defArgsNo returnsTab
= rawFuncInfo

saveFunctionToCatalog :: QualifiedFunction -> Bool -> Q.TxE QErr ()
Expand Down
1 change: 0 additions & 1 deletion server/src-lib/Hasura/RQL/DDL/Schema/Rename.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module Hasura.RQL.DDL.Schema.Rename
)
where

import Control.Arrow ((***))
import Hasura.Prelude
import qualified Hasura.RQL.DDL.EventTrigger as DS
import Hasura.RQL.DDL.Permission
Expand Down
6 changes: 4 additions & 2 deletions server/src-lib/Hasura/RQL/DML/Select/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Hasura.RQL.DML.Select.Internal
)
where

import Control.Arrow ((&&&))
import Data.List (delete, sort)
import Instances.TH.Lift ()

Expand Down Expand Up @@ -718,7 +717,10 @@ mkFuncSelectWith f annFn =
funcSel = S.mkSelect { S.selFrom = Just $ S.FromExp [frmItem]
, S.selExtr = [S.Extractor S.SEStar Nothing]
}
frmItem = S.mkFuncFromItem qf fnArgs
frmItem = S.mkFuncFromItem qf $ mkSQLFunctionArgs fnArgs

mkSQLFunctionArgs (FunctionArgsExp positional named) =
S.FunctionArgs positional named

newTabFrom = (_asnFrom annSel) {_tfIden = Just $ toIden funcAls}

Expand Down
13 changes: 12 additions & 1 deletion server/src-lib/Hasura/RQL/DML/Select/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,21 @@ type AnnSimpleSel = AnnSimpleSelG S.SQLExp
type AnnAggSelG v = AnnSelG (TableAggFldsG v) v
type AnnAggSel = AnnAggSelG S.SQLExp

data FunctionArgsExpG a
= FunctionArgsExp
{ _faePositional :: ![a]
, _faeNamed :: !(HM.HashMap Text a)
} deriving (Show, Eq, Functor, Foldable, Traversable)

emptyFunctionArgsExp :: FunctionArgsExpG a
emptyFunctionArgsExp = FunctionArgsExp [] HM.empty

type FunctionArgExp = FunctionArgsExpG S.SQLExp

data AnnFnSelG s v
= AnnFnSel
{ _afFn :: !QualifiedFunction
, _afFnArgs :: ![v]
, _afFnArgs :: !(FunctionArgsExpG v)
, _afSelect :: !s
} deriving (Show, Eq)

Expand Down
6 changes: 6 additions & 0 deletions server/src-lib/Hasura/RQL/Types/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module Hasura.RQL.Types.Common
, unNonEmptyText
, adminText
, rootText

, FunctionArgName(..)
) where

import Hasura.Prelude
Expand Down Expand Up @@ -174,3 +176,7 @@ data ForeignKey
$(deriveJSON (aesonDrop 3 snakeCase) ''ForeignKey)

instance Hashable ForeignKey

newtype FunctionArgName =
FunctionArgName { getFuncArgNameTxt :: T.Text}
deriving (Show, Eq, ToJSON)
9 changes: 3 additions & 6 deletions server/src-lib/Hasura/RQL/Types/SchemaCache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,11 @@ funcTypToTxt FTSTABLE = "STABLE"
instance Show FunctionType where
show = T.unpack . funcTypToTxt

newtype FunctionArgName =
FunctionArgName { getFuncArgNameTxt :: T.Text}
deriving (Show, Eq, ToJSON)

data FunctionArg
= FunctionArg
{ faName :: !(Maybe FunctionArgName)
, faType :: !PGScalarType
{ faName :: !(Maybe FunctionArgName)
, faType :: !PGScalarType
, faHasDefault :: !Bool
} deriving (Show, Eq)

$(deriveToJSON (aesonDrop 2 snakeCase) ''FunctionArg)
Expand Down
Loading