From 4e6394355d67f038d72b48be2fb633c06e8c5e20 Mon Sep 17 00:00:00 2001 From: Tom Harding Date: Fri, 26 Jul 2024 18:17:28 +0200 Subject: [PATCH] Return correct type when an update_many has no arguments PR-URL: https://github.com/hasura/graphql-engine-mono/pull/10954 GitOrigin-RevId: 214163f4f4764ea66194bf26bfe202a807771e90 --- .../src/Test/Mutations/Insert/MultipleSpec.hs | 4 ++++ .../MultiplePerRequest/UpdateManySpec.hs | 23 +++++++++++++++++++ .../Backends/Postgres/Instances/Execute.hs | 6 +++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/server/lib/api-tests/src/Test/Mutations/Insert/MultipleSpec.hs b/server/lib/api-tests/src/Test/Mutations/Insert/MultipleSpec.hs index 0973086e81a..d6d04afb406 100644 --- a/server/lib/api-tests/src/Test/Mutations/Insert/MultipleSpec.hs +++ b/server/lib/api-tests/src/Test/Mutations/Insert/MultipleSpec.hs @@ -121,6 +121,7 @@ tests = do } ] ) { + affected_rows returning { id title @@ -134,6 +135,7 @@ tests = do [interpolateYaml| data: insert_#{schemaName}_article: + affected_rows: 2 returning: - id: 1 title: "Article 1" @@ -156,6 +158,7 @@ tests = do insert_#{schemaName}_article( objects: [] ) { + affected_rows returning { id title @@ -169,6 +172,7 @@ tests = do [interpolateYaml| data: insert_#{schemaName}_article: + affected_rows: 0 returning: [] |] diff --git a/server/lib/api-tests/src/Test/Mutations/MultiplePerRequest/UpdateManySpec.hs b/server/lib/api-tests/src/Test/Mutations/MultiplePerRequest/UpdateManySpec.hs index 966f8df7239..4c0828d3ef9 100644 --- a/server/lib/api-tests/src/Test/Mutations/MultiplePerRequest/UpdateManySpec.hs +++ b/server/lib/api-tests/src/Test/Mutations/MultiplePerRequest/UpdateManySpec.hs @@ -69,6 +69,29 @@ schema = tests :: SpecWith TestEnvironment tests = do describe "Update many objects" do + it "When no updates are provided" \testEnvironment -> do + let expected :: Value + expected = + [yaml| + data: + update_hasura_author_many: [] + |] + + actual :: IO Value + actual = + postGraphql + testEnvironment + [graphql| + mutation { + update_hasura_author_many( + updates: [] + ) { + affected_rows + } + } + |] + + shouldReturnYaml testEnvironment actual expected it "When many equals zero" \testEnvironment -> do let expected :: Value expected = diff --git a/server/src-lib/Hasura/Backends/Postgres/Instances/Execute.hs b/server/src-lib/Hasura/Backends/Postgres/Instances/Execute.hs index da5b786790b..c0debc6880c 100644 --- a/server/src-lib/Hasura/Backends/Postgres/Instances/Execute.hs +++ b/server/src-lib/Hasura/Backends/Postgres/Instances/Execute.hs @@ -45,7 +45,7 @@ import Hasura.Backends.Postgres.Translate.Select qualified as DS import Hasura.Backends.Postgres.Types.Function qualified as Postgres import Hasura.Backends.Postgres.Types.Update qualified as Postgres import Hasura.Base.Error (QErr) -import Hasura.EncJSON (EncJSON, encJFromJValue) +import Hasura.EncJSON (EncJSON, encJFromJValue, encJFromList) import Hasura.Function.Cache import Hasura.GraphQL.Execute.Backend ( BackendExecute (..), @@ -307,7 +307,9 @@ convertUpdate sourceName modelSourceType env manager logger userInfo updateOpera pure $ concat whereModelsList let modelNames = [ModelNameInfo (modelName, modelType, sourceName, modelSourceType)] <> (argModelNames) <> preUpdatePermissionModelNames <> postUpdateCheckModelNames <> (returnModels) if Postgres.updateVariantIsEmpty $ IR._auUpdateVariant updateOperation - then pure $ (OnBaseMonad $ pure $ IR.buildEmptyMutResp $ IR._auOutput preparedUpdate, modelNames) + then case mutationUpdateVariant of + Postgres.SingleBatch _ -> pure $ (OnBaseMonad $ pure $ IR.buildEmptyMutResp (IR._auOutput preparedUpdate), modelNames) + Postgres.MultipleBatches _ -> pure (OnBaseMonad $ pure (encJFromList []), modelNames) else pure $ ( OnBaseMonad