diff --git a/package.json b/package.json index 7ddb9b3b1..607022eff 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "@gadget-client/bulk-actions-test": "^1.113.0", "@gadget-client/full-auth": "^1.9.0", "@gadget-client/js-clients-test": "1.499.0-development.2005", - "@gadget-client/kitchen-sink": "1.5.0-development.200", + "@gadget-client/kitchen-sink": "1.9.0-development.206", "@gadget-client/related-products-example": "^1.865.0", "@gadget-client/zxcv-deeply-nested": "^1.212.0", "@gadget-client/zxcv-manythrough-example": "1.3.0-nick-dev.3", @@ -37,6 +37,7 @@ "@gadgetinc/eslint-config": "^0.6.1", "@gadgetinc/prettier-config": "^0.4.0", "@gadgetinc/react": "workspace:*", + "tiny-graphql-query-compiler": "workspace:*", "@swc/core": "^1.3.90", "@swc/jest": "^0.2.36", "@types/jest": "^29.5.12", diff --git a/packages/api-client-core/package.json b/packages/api-client-core/package.json index 103f60d7d..b76f2157a 100644 --- a/packages/api-client-core/package.json +++ b/packages/api-client-core/package.json @@ -1,6 +1,6 @@ { "name": "@gadgetinc/api-client-core", - "version": "0.15.35", + "version": "0.15.36", "files": [ "Readme.md", "dist/**/*" diff --git a/packages/api-client-core/src/operationRunners.ts b/packages/api-client-core/src/operationRunners.ts index 60aac27fe..bb1be7bae 100644 --- a/packages/api-client-core/src/operationRunners.ts +++ b/packages/api-client-core/src/operationRunners.ts @@ -341,7 +341,7 @@ const processBulkActionResponse = ( } }; -const processActionResponse = ( +export const processActionResponse = ( defaultSelection: FieldSelection | null, response: any, record: any, diff --git a/packages/react/package.json b/packages/react/package.json index 733eee649..093042057 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -48,7 +48,7 @@ }, "dependencies": { "@0no-co/graphql.web": "^1.0.4", - "@gadgetinc/api-client-core": "^0.15.35", + "@gadgetinc/api-client-core": "^0.15.36", "@hookform/resolvers": "^3.3.1", "filesize": "^10.1.2", "pluralize": "^8.0.0", diff --git a/packages/react/spec/useAction.spec.tsx b/packages/react/spec/useAction.spec.tsx index 26f8b59a2..04f43227e 100644 --- a/packages/react/spec/useAction.spec.tsx +++ b/packages/react/spec/useAction.spec.tsx @@ -841,4 +841,48 @@ describe("useAction", () => { }, }); }); + + test("handles upsert data response", async () => { + const { result } = renderHook(() => useAction(kitchenSinkApi.widget.upsert), { wrapper: MockClientWrapper(relatedProductsApi) }); + + let mutationPromise: any; + act(() => { + mutationPromise = result.current[1]({ id: "123", widget: { name: "Something new", inventoryCount: 100 } }); + }); + + expect(result.current[0].data).toBeFalsy(); + expect(result.current[0].fetching).toBe(true); + expect(result.current[0].error).toBeFalsy(); + + expect(mockUrqlClient.executeMutation).toBeCalledTimes(1); + + mockUrqlClient.executeMutation.pushResponse("upsertWidget", { + data: { + upsertWidget: { + success: true, + widget: { + id: "123", + name: "Something new", + inventoryCount: 101, + }, + }, + }, + stale: false, + hasNext: false, + }); + + await act(async () => { + const promiseResult = await mutationPromise; + + expect(promiseResult.data!.id).toEqual("123"); + expect(promiseResult.data!.name).toEqual("Something new"); + expect(promiseResult.fetching).toBe(false); + expect(promiseResult.error).toBeFalsy(); + }); + + expect(result.current[0].data?.id).toEqual("123"); + expect(result.current[0].data?.name).toEqual("Something new"); + expect(result.current[0].fetching).toBe(false); + expect(result.current[0].error).toBeFalsy(); + }); }); diff --git a/packages/react/src/useAction.ts b/packages/react/src/useAction.ts index eea7f6256..3f37c84cb 100644 --- a/packages/react/src/useAction.ts +++ b/packages/react/src/useAction.ts @@ -11,8 +11,8 @@ import { capitalizeIdentifier, disambiguateActionVariables, get, - hydrateRecord, namespaceDataPath, + processActionResponse, } from "@gadgetinc/api-client-core"; import { useCallback, useContext, useEffect, useMemo } from "react"; import type { AnyVariables, OperationContext, UseMutationState } from "urql"; @@ -142,14 +142,13 @@ const processResult = ( let data = null; if (result.data) { const dataPath = namespaceDataPath([action.operationName], action.namespace); - const mutationData = get(result.data, dataPath); if (mutationData) { const errors = mutationData["errors"]; if (errors && errors[0]) { error = ErrorWrapper.forErrorsResponse(errors, error?.response); } else { - data = action.hasReturnType ? mutationData.result : hydrateRecord(result, mutationData[action.modelSelectionField]); + data = processActionResponse(action.defaultSelection, result, mutationData, action.modelSelectionField, action.hasReturnType); } } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 897be8985..1dfbc9351 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -36,8 +36,8 @@ importers: specifier: 1.499.0-development.2005 version: 1.499.0-development.2005 '@gadget-client/kitchen-sink': - specifier: 1.5.0-development.200 - version: 1.5.0-development.200 + specifier: 1.9.0-development.206 + version: 1.9.0-development.206 '@gadget-client/related-products-example': specifier: ^1.865.0 version: 1.865.0 @@ -107,6 +107,9 @@ importers: semver: specifier: ^7.6.2 version: 7.6.2 + tiny-graphql-query-compiler: + specifier: workspace:* + version: link:packages/tiny-graphql-query-compiler tsx: specifier: ^4.9.3 version: 4.9.3 @@ -3933,10 +3936,11 @@ packages: '@gadgetinc/api-client-core': link:packages/api-client-core dev: true - /@gadget-client/kitchen-sink@1.5.0-development.200: - resolution: {integrity: sha1-CsaWHLuyQuUM5nzKuMjIoSZFQ08=, tarball: https://registry.gadget.dev/npm/_/tarball/97394/193776/8325} + /@gadget-client/kitchen-sink@1.9.0-development.206: + resolution: {integrity: sha1-nRG0XHhCS1kiOiv36iX4oO0PKBo=, tarball: https://registry.gadget.dev/npm/_/tarball/97394/193776/11255} dependencies: '@gadgetinc/api-client-core': link:packages/api-client-core + tiny-graphql-query-compiler: link:packages/tiny-graphql-query-compiler dev: true /@gadget-client/related-products-example@1.865.0: