2018-06-27 16:11:32 +03:00
|
|
|
module Hasura.GraphQL.Resolve.Mutation
|
|
|
|
( convertUpdate
|
|
|
|
, convertDelete
|
2018-10-05 18:13:51 +03:00
|
|
|
, convertMutResp
|
2019-01-28 10:24:24 +03:00
|
|
|
, buildEmptyMutResp
|
2018-06-27 16:11:32 +03:00
|
|
|
) where
|
|
|
|
|
2019-03-25 21:25:25 +03:00
|
|
|
import Data.Has
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
2019-01-28 10:24:24 +03:00
|
|
|
import qualified Data.Aeson as J
|
2019-02-11 15:45:30 +03:00
|
|
|
import qualified Data.HashMap.Strict as Map
|
2018-10-12 13:36:47 +03:00
|
|
|
import qualified Data.HashMap.Strict.InsOrd as OMap
|
2018-06-27 16:11:32 +03:00
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
|
|
|
|
import qualified Hasura.RQL.DML.Delete as RD
|
|
|
|
import qualified Hasura.RQL.DML.Returning as RR
|
|
|
|
import qualified Hasura.RQL.DML.Update as RU
|
|
|
|
|
2019-04-17 12:48:41 +03:00
|
|
|
import qualified Hasura.RQL.DML.Select as RS
|
2019-07-22 15:47:13 +03:00
|
|
|
import qualified Hasura.SQL.DML as S
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-03-22 10:08:42 +03:00
|
|
|
import Hasura.EncJSON
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.GraphQL.Resolve.BoolExp
|
|
|
|
import Hasura.GraphQL.Resolve.Context
|
|
|
|
import Hasura.GraphQL.Resolve.InputValue
|
2019-04-17 12:48:41 +03:00
|
|
|
import Hasura.GraphQL.Resolve.Select (fromSelSet)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.GraphQL.Validate.Field
|
|
|
|
import Hasura.GraphQL.Validate.Types
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.SQL.Types
|
2018-07-20 13:51:20 +03:00
|
|
|
import Hasura.SQL.Value
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
convertMutResp
|
2019-09-14 09:01:06 +03:00
|
|
|
:: ( MonadResolve m, MonadReader r m, Has FieldMap r
|
2019-03-25 21:25:25 +03:00
|
|
|
, Has OrdByCtx r, Has SQLGenCtx r
|
|
|
|
)
|
2019-04-17 12:48:41 +03:00
|
|
|
=> G.NamedType -> SelSet -> m (RR.MutFldsG UnresolvedVal)
|
2018-11-02 17:01:01 +03:00
|
|
|
convertMutResp ty selSet =
|
2018-10-26 12:02:44 +03:00
|
|
|
withSelSet selSet $ \fld -> case _fName fld of
|
|
|
|
"__typename" -> return $ RR.MExp $ G.unName $ G.unNamedType ty
|
|
|
|
"affected_rows" -> return RR.MCount
|
2019-04-17 12:48:41 +03:00
|
|
|
"returning" -> do
|
|
|
|
annFlds <- fromSelSet (_fType fld) $ _fSelSet fld
|
|
|
|
annFldsResolved <- traverse
|
|
|
|
(traverse (RS.traverseAnnFld convertUnresolvedVal)) annFlds
|
|
|
|
return $ RR.MRet annFldsResolved
|
2018-10-26 12:02:44 +03:00
|
|
|
G.Name t -> throw500 $ "unexpected field in mutation resp : " <> t
|
2019-04-17 12:48:41 +03:00
|
|
|
where
|
|
|
|
convertUnresolvedVal = \case
|
|
|
|
UVPG annPGVal -> UVSQL <$> txtConverter annPGVal
|
|
|
|
UVSessVar colTy sessVar -> pure $ UVSessVar colTy sessVar
|
|
|
|
UVSQL sqlExp -> pure $ UVSQL sqlExp
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
convertRowObj
|
2019-09-14 09:01:06 +03:00
|
|
|
:: (MonadResolve m)
|
2019-09-19 07:47:36 +03:00
|
|
|
=> PGColGNameMap
|
|
|
|
-> AnnInpVal
|
2019-04-17 12:48:41 +03:00
|
|
|
-> m [(PGCol, UnresolvedVal)]
|
2019-09-19 07:47:36 +03:00
|
|
|
convertRowObj colGNameMap val =
|
2018-11-02 18:08:38 +03:00
|
|
|
flip withObject val $ \_ obj ->
|
|
|
|
forM (OMap.toList obj) $ \(k, v) -> do
|
2019-09-14 09:01:06 +03:00
|
|
|
prepExpM <- fmap mkParameterizablePGValue <$> asPGColumnValueM v
|
2019-09-19 07:47:36 +03:00
|
|
|
pgCol <- pgiColumn <$> resolvePGCol colGNameMap k
|
2019-08-29 16:07:05 +03:00
|
|
|
let prepExp = fromMaybe (UVSQL S.SENull) prepExpM
|
2019-09-19 07:47:36 +03:00
|
|
|
return (pgCol, prepExp)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-07-20 13:51:20 +03:00
|
|
|
type ApplySQLOp = (PGCol, S.SQLExp) -> S.SQLExp
|
|
|
|
|
2019-07-10 13:19:58 +03:00
|
|
|
rhsExpOp :: S.SQLOp -> S.TypeAnn -> ApplySQLOp
|
2018-07-20 13:51:20 +03:00
|
|
|
rhsExpOp op annTy (col, e) =
|
|
|
|
S.mkSQLOpExp op (S.SEIden $ toIden col) annExp
|
|
|
|
where
|
|
|
|
annExp = S.SETyAnn e annTy
|
|
|
|
|
2019-07-10 13:19:58 +03:00
|
|
|
lhsExpOp :: S.SQLOp -> S.TypeAnn -> ApplySQLOp
|
2018-07-20 13:51:20 +03:00
|
|
|
lhsExpOp op annTy (col, e) =
|
|
|
|
S.mkSQLOpExp op annExp $ S.SEIden $ toIden col
|
|
|
|
where
|
|
|
|
annExp = S.SETyAnn e annTy
|
|
|
|
|
|
|
|
convObjWithOp
|
2019-09-14 09:01:06 +03:00
|
|
|
:: (MonadResolve m)
|
2019-09-19 07:47:36 +03:00
|
|
|
=> PGColGNameMap -> ApplySQLOp -> AnnInpVal -> m [(PGCol, UnresolvedVal)]
|
|
|
|
convObjWithOp colGNameMap opFn val =
|
2018-10-12 13:36:47 +03:00
|
|
|
flip withObject val $ \_ obj -> forM (OMap.toList obj) $ \(k, v) -> do
|
2019-09-14 09:01:06 +03:00
|
|
|
colVal <- openOpaqueValue =<< asPGColumnValue v
|
2019-09-19 07:47:36 +03:00
|
|
|
pgCol <- pgiColumn <$> resolvePGCol colGNameMap k
|
|
|
|
-- TODO: why are we using txtEncoder here?
|
|
|
|
let encVal = txtEncoder $ pstValue $ _apvValue colVal
|
2018-07-20 13:51:20 +03:00
|
|
|
sqlExp = opFn (pgCol, encVal)
|
2019-04-17 12:48:41 +03:00
|
|
|
return (pgCol, UVSQL sqlExp)
|
2018-07-20 13:51:20 +03:00
|
|
|
|
|
|
|
convDeleteAtPathObj
|
2019-09-14 09:01:06 +03:00
|
|
|
:: (MonadResolve m)
|
2019-09-19 07:47:36 +03:00
|
|
|
=> PGColGNameMap -> AnnInpVal -> m [(PGCol, UnresolvedVal)]
|
|
|
|
convDeleteAtPathObj colGNameMap val =
|
2018-10-12 13:36:47 +03:00
|
|
|
flip withObject val $ \_ obj -> forM (OMap.toList obj) $ \(k, v) -> do
|
2019-09-14 09:01:06 +03:00
|
|
|
vals <- traverse (openOpaqueValue <=< asPGColumnValue) =<< asArray v
|
2019-09-19 07:47:36 +03:00
|
|
|
pgCol <- pgiColumn <$> resolvePGCol colGNameMap k
|
2019-07-22 15:47:13 +03:00
|
|
|
let valExps = map (txtEncoder . pstValue . _apvValue) vals
|
2019-07-10 13:19:58 +03:00
|
|
|
annEncVal = S.SETyAnn (S.SEArray valExps) S.textArrTypeAnn
|
2018-07-20 13:51:20 +03:00
|
|
|
sqlExp = S.SEOpApp S.jsonbDeleteAtPathOp
|
|
|
|
[S.SEIden $ toIden pgCol, annEncVal]
|
2019-04-17 12:48:41 +03:00
|
|
|
return (pgCol, UVSQL sqlExp)
|
2018-07-20 13:51:20 +03:00
|
|
|
|
2019-03-25 21:25:25 +03:00
|
|
|
convertUpdateP1
|
2019-09-14 09:01:06 +03:00
|
|
|
:: ( MonadResolve m
|
2019-03-25 21:25:25 +03:00
|
|
|
, MonadReader r m, Has FieldMap r
|
|
|
|
, Has OrdByCtx r, Has SQLGenCtx r
|
|
|
|
)
|
|
|
|
=> UpdOpCtx -- the update context
|
2018-06-27 16:11:32 +03:00
|
|
|
-> Field -- the mutation field
|
2019-04-17 12:48:41 +03:00
|
|
|
-> m (RU.AnnUpdG UnresolvedVal)
|
2019-03-25 21:25:25 +03:00
|
|
|
convertUpdateP1 opCtx fld = do
|
2018-06-27 16:11:32 +03:00
|
|
|
-- a set expression is same as a row object
|
2019-09-19 07:47:36 +03:00
|
|
|
setExpM <- withArgM args "_set" $ convertRowObj colGNameMap
|
2018-07-20 13:51:20 +03:00
|
|
|
-- where bool expression to filter column
|
2019-04-17 12:48:41 +03:00
|
|
|
whereExp <- withArg args "where" parseBoolExp
|
2018-07-20 13:51:20 +03:00
|
|
|
-- increment operator on integer columns
|
|
|
|
incExpM <- withArgM args "_inc" $
|
2019-09-19 07:47:36 +03:00
|
|
|
convObjWithOp' $ rhsExpOp S.incOp S.intTypeAnn
|
2018-07-20 13:51:20 +03:00
|
|
|
-- append jsonb value
|
|
|
|
appendExpM <- withArgM args "_append" $
|
2019-09-19 07:47:36 +03:00
|
|
|
convObjWithOp' $ rhsExpOp S.jsonbConcatOp S.jsonbTypeAnn
|
2018-07-20 13:51:20 +03:00
|
|
|
-- prepend jsonb value
|
|
|
|
prependExpM <- withArgM args "_prepend" $
|
2019-09-19 07:47:36 +03:00
|
|
|
convObjWithOp' $ lhsExpOp S.jsonbConcatOp S.jsonbTypeAnn
|
2018-07-20 13:51:20 +03:00
|
|
|
-- delete a key in jsonb object
|
|
|
|
deleteKeyExpM <- withArgM args "_delete_key" $
|
2019-09-19 07:47:36 +03:00
|
|
|
convObjWithOp' $ rhsExpOp S.jsonbDeleteOp S.textTypeAnn
|
2018-07-20 13:51:20 +03:00
|
|
|
-- delete an element in jsonb array
|
|
|
|
deleteElemExpM <- withArgM args "_delete_elem" $
|
2019-09-19 07:47:36 +03:00
|
|
|
convObjWithOp' $ rhsExpOp S.jsonbDeleteOp S.intTypeAnn
|
2018-07-20 13:51:20 +03:00
|
|
|
-- delete at path in jsonb value
|
2019-09-19 07:47:36 +03:00
|
|
|
deleteAtPathExpM <- withArgM args "_delete_at_path" $ convDeleteAtPathObj colGNameMap
|
2019-04-17 12:48:41 +03:00
|
|
|
mutFlds <- convertMutResp (_fType fld) $ _fSelSet fld
|
|
|
|
|
|
|
|
let resolvedPreSetItems =
|
|
|
|
Map.toList $ fmap partialSQLExpToUnresolvedVal preSetCols
|
|
|
|
|
2018-07-20 13:51:20 +03:00
|
|
|
let updExpsM = [ setExpM, incExpM, appendExpM, prependExpM
|
|
|
|
, deleteKeyExpM, deleteElemExpM, deleteAtPathExpM
|
|
|
|
]
|
2019-04-17 12:48:41 +03:00
|
|
|
setItems = resolvedPreSetItems ++ concat (catMaybes updExpsM)
|
2019-03-25 21:25:25 +03:00
|
|
|
|
2018-07-20 13:51:20 +03:00
|
|
|
-- atleast one of update operators is expected
|
2019-02-11 15:45:30 +03:00
|
|
|
-- or preSetItems shouldn't be empty
|
2019-03-25 21:25:25 +03:00
|
|
|
-- this is not equivalent to (null setItems)
|
2019-04-17 12:48:41 +03:00
|
|
|
unless (any isJust updExpsM || not (null resolvedPreSetItems)) $ throwVE $
|
2019-03-25 21:25:25 +03:00
|
|
|
"atleast any one of _set, _inc, _append, _prepend, "
|
|
|
|
<> "_delete_key, _delete_elem and "
|
|
|
|
<> "_delete_at_path operator is expected"
|
|
|
|
|
2019-04-17 12:48:41 +03:00
|
|
|
let unresolvedPermFltr = fmapAnnBoolExp partialSQLExpToUnresolvedVal filterExp
|
|
|
|
|
|
|
|
return $ RU.AnnUpd tn setItems
|
|
|
|
(unresolvedPermFltr, whereExp) mutFlds allCols
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2019-09-19 07:47:36 +03:00
|
|
|
convObjWithOp' = convObjWithOp colGNameMap
|
|
|
|
allCols = Map.elems colGNameMap
|
|
|
|
UpdOpCtx tn _ colGNameMap filterExp preSetCols = opCtx
|
2018-06-27 16:11:32 +03:00
|
|
|
args = _fArguments fld
|
|
|
|
|
2019-03-25 21:25:25 +03:00
|
|
|
convertUpdate
|
2019-09-14 09:01:06 +03:00
|
|
|
:: ( MonadResolve m
|
2019-04-17 12:48:41 +03:00
|
|
|
, MonadReader r m, Has FieldMap r
|
|
|
|
, Has OrdByCtx r, Has SQLGenCtx r
|
|
|
|
)
|
|
|
|
=> UpdOpCtx -- the update context
|
2019-03-25 21:25:25 +03:00
|
|
|
-> Field -- the mutation field
|
2019-04-17 12:48:41 +03:00
|
|
|
-> m RespTx
|
2019-03-25 21:25:25 +03:00
|
|
|
convertUpdate opCtx fld = do
|
2019-04-17 12:48:41 +03:00
|
|
|
annUpdUnresolved <- convertUpdateP1 opCtx fld
|
|
|
|
(annUpdResolved, prepArgs) <- withPrepArgs $ RU.traverseAnnUpd
|
|
|
|
resolveValPrep annUpdUnresolved
|
2019-03-25 21:25:25 +03:00
|
|
|
strfyNum <- stringifyNum <$> asks getter
|
2019-04-17 12:48:41 +03:00
|
|
|
let whenNonEmptyItems = return $ RU.updateQueryToTx strfyNum
|
|
|
|
(annUpdResolved, prepArgs)
|
2019-03-25 21:25:25 +03:00
|
|
|
whenEmptyItems = return $ return $
|
2019-04-17 12:48:41 +03:00
|
|
|
buildEmptyMutResp $ RU.uqp1MutFlds annUpdResolved
|
2019-03-25 21:25:25 +03:00
|
|
|
-- if there are not set items then do not perform
|
|
|
|
-- update and return empty mutation response
|
2019-04-17 12:48:41 +03:00
|
|
|
bool whenNonEmptyItems whenEmptyItems $ null $ RU.uqp1SetExps annUpdResolved
|
2019-03-25 21:25:25 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
convertDelete
|
2019-09-14 09:01:06 +03:00
|
|
|
:: ( MonadResolve m
|
2019-04-17 12:48:41 +03:00
|
|
|
, MonadReader r m, Has FieldMap r
|
|
|
|
, Has OrdByCtx r, Has SQLGenCtx r
|
|
|
|
)
|
|
|
|
=> DelOpCtx -- the delete context
|
2018-06-27 16:11:32 +03:00
|
|
|
-> Field -- the mutation field
|
2019-04-17 12:48:41 +03:00
|
|
|
-> m RespTx
|
2019-02-22 13:27:38 +03:00
|
|
|
convertDelete opCtx fld = do
|
2019-04-17 12:48:41 +03:00
|
|
|
whereExp <- withArg (_fArguments fld) "where" parseBoolExp
|
|
|
|
mutFlds <- convertMutResp (_fType fld) $ _fSelSet fld
|
|
|
|
let unresolvedPermFltr =
|
|
|
|
fmapAnnBoolExp partialSQLExpToUnresolvedVal filterExp
|
|
|
|
annDelUnresolved = RD.AnnDel tn (unresolvedPermFltr, whereExp)
|
|
|
|
mutFlds allCols
|
|
|
|
(annDelResolved, prepArgs) <- withPrepArgs $ RD.traverseAnnDel
|
|
|
|
resolveValPrep annDelUnresolved
|
2019-03-01 14:45:04 +03:00
|
|
|
strfyNum <- stringifyNum <$> asks getter
|
2019-04-17 12:48:41 +03:00
|
|
|
return $ RD.deleteQueryToTx strfyNum (annDelResolved, prepArgs)
|
2019-02-22 13:27:38 +03:00
|
|
|
where
|
2019-03-22 10:08:42 +03:00
|
|
|
DelOpCtx tn _ filterExp allCols = opCtx
|
2019-01-28 10:24:24 +03:00
|
|
|
|
|
|
|
-- | build mutation response for empty objects
|
2019-03-18 19:22:21 +03:00
|
|
|
buildEmptyMutResp :: RR.MutFlds -> EncJSON
|
|
|
|
buildEmptyMutResp = mkTx
|
2019-01-28 10:24:24 +03:00
|
|
|
where
|
2019-03-18 19:22:21 +03:00
|
|
|
mkTx = encJFromJValue . OMap.fromList . map (second convMutFld)
|
2019-01-28 10:24:24 +03:00
|
|
|
-- generate empty mutation response
|
|
|
|
convMutFld = \case
|
|
|
|
RR.MCount -> J.toJSON (0 :: Int)
|
|
|
|
RR.MExp e -> J.toJSON e
|
|
|
|
RR.MRet _ -> J.toJSON ([] :: [J.Value])
|