这是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
14 changes: 1 addition & 13 deletions server/src-lib/Hasura/GraphQL/Resolve/Insert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Hasura.GraphQL.Resolve.Insert
(convertInsert)
where

import Control.Arrow (second)
import Data.Has
import Hasura.Prelude
import Hasura.Server.Utils
Expand Down Expand Up @@ -445,17 +444,6 @@ insertMultipleObjects role tn multiObjIns addCols mutFlds errP =
getRet (t, r@(RR.MRet _)) = Just (t, r)
getRet _ = Nothing

-- | build mutation response for empty objects
withEmptyObjs :: RR.MutFlds -> Convert RespTx
withEmptyObjs = return . mkTx
where
mkTx = return . J.encode . OMap.fromList . map (second convMutFld)
-- generate empty mutation response
convMutFld = \case
RR.MCount -> J.toJSON (0 :: Int)
RR.MExp e -> J.toJSON e
RR.MRet _ -> J.toJSON ([] :: [J.Value])

prefixErrPath :: (MonadError QErr m) => Field -> m a -> m a
prefixErrPath fld =
withPathK "selectionSet" . fieldAsPath fld . withPathK "args"
Expand All @@ -470,7 +458,7 @@ convertInsert role tn fld = prefixErrPath fld $ do
annVals <- withArg arguments "objects" asArray
-- if insert input objects is empty array then
-- do not perform insert and return mutation response
bool (withNonEmptyObjs annVals mutFlds) (withEmptyObjs mutFlds) $ null annVals
bool (withNonEmptyObjs annVals mutFlds) (buildEmptyMutResp mutFlds) $ null annVals
where
withNonEmptyObjs annVals mutFlds = do
InsCtx vn tableCols defValMap relInfoMap updPerm <- getInsCtx tn
Expand Down
24 changes: 21 additions & 3 deletions server/src-lib/Hasura/GraphQL/Resolve/Mutation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ module Hasura.GraphQL.Resolve.Mutation
( convertUpdate
, convertDelete
, convertMutResp
, buildEmptyMutResp
) where

import Control.Arrow (second)
import Hasura.Prelude

import qualified Data.Aeson as J
import qualified Data.HashMap.Strict.InsOrd as OMap
import qualified Language.GraphQL.Draft.Syntax as G

Expand Down Expand Up @@ -117,13 +120,17 @@ convertUpdate tn filterExp fld = do
let updExpsM = [ setExpM, incExpM, appendExpM, prependExpM
, deleteKeyExpM, deleteElemExpM, deleteAtPathExpM
]
updExp = concat $ catMaybes updExpsM
setItems = concat $ catMaybes updExpsM
-- atleast one of update operators is expected
unless (any isJust updExpsM) $ throwVE $
"atleast any one of _set, _inc, _append, _prepend, _delete_key, _delete_elem and "
<> " _delete_at_path operator is expected"
let p1 = RU.UpdateQueryP1 tn updExp (filterExp, whereExp) mutFlds
return $ RU.updateQueryToTx (p1, prepArgs)
let p1 = RU.UpdateQueryP1 tn setItems (filterExp, whereExp) mutFlds
whenNonEmptyItems = return $ RU.updateQueryToTx (p1, prepArgs)
whenEmptyItems = buildEmptyMutResp mutFlds
-- if there are not set items then do not perform
-- update and return empty mutation response
bool whenNonEmptyItems whenEmptyItems $ null setItems
where
args = _fArguments fld

Expand All @@ -138,3 +145,14 @@ convertDelete tn filterExp fld = do
args <- get
let p1 = RD.DeleteQueryP1 tn (filterExp, whereExp) mutFlds
return $ RD.deleteQueryToTx (p1, args)

-- | build mutation response for empty objects
buildEmptyMutResp :: Monad m => RR.MutFlds -> m RespTx
buildEmptyMutResp = return . mkTx
where
mkTx = return . J.encode . OMap.fromList . map (second convMutFld)
-- generate empty mutation response
convMutFld = \case
RR.MCount -> J.toJSON (0 :: Int)
RR.MExp e -> J.toJSON e
RR.MRet _ -> J.toJSON ([] :: [J.Value])
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
description: Update mutation on author
url: /v1alpha1/graphql
status: 200
response:
data:
update_author:
affected_rows: 0
returning: []
query:
variables:
set: {}
query: |
mutation update_author ($set: author_set_input!) {
update_author(
where: {id: {_eq: 1}},
_set: $set
) {
affected_rows
returning{
id
name
articles{
id
title
content
is_published
}
}
}
}
4 changes: 4 additions & 0 deletions server/tests-py/test_graphql_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ class TestGraphqlUpdateBasic(DefaultTestQueries):
def test_set_author_name(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + "/author_set_name.yaml")

def test_empty_set_author(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + "/author_empty_set.yaml")
hge_ctx.may_skip_test_teardown = True

def test_set_person_details(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + "/person_set_details.yaml")

Expand Down