Skip to content

Commit

Permalink
Update js-clients to process action responses
Browse files Browse the repository at this point in the history
  • Loading branch information
garymardell committed Dec 2, 2024
1 parent 2f7f7c5 commit 8e04716
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gadgetinc/api-client-core",
"version": "0.15.35",
"version": "0.15.36",
"files": [
"Readme.md",
"dist/**/*"
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client-core/src/operationRunners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ const processBulkActionResponse = <Shape extends RecordShape = any>(
}
};

const processActionResponse = <Shape extends RecordShape = any>(
export const processActionResponse = <Shape extends RecordShape = any>(
defaultSelection: FieldSelection | null,
response: any,
record: any,
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 44 additions & 0 deletions packages/react/spec/useAction.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
5 changes: 2 additions & 3 deletions packages/react/src/useAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -142,14 +142,13 @@ const processResult = <Data, Variables extends AnyVariables>(
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);
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8e04716

Please sign in to comment.