Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit 55b3545

Browse files
darahayeswtrocki
authored andcommitted
fix: id overwrite issue in optimistic responses
1 parent 77907cb commit 55b3545

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

packages/offix-cache/src/createOptimisticResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const createOptimisticResponse = (options: OptimisticOptions) => {
6363
...mappedVariables,
6464
optimisticResponse: true
6565
};
66-
if (operationType === CacheOperation.ADD) {
66+
if (operationType === CacheOperation.ADD && !optimisticResponse[operation][idField]) {
6767
optimisticResponse[operation][idField] = generateClientId();
6868
}
6969

packages/offix-cache/test/OptimisticResponse.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,37 @@ test("check that createNewOptimisticResponse is properly composed with name prop
2828
expect(result.createItem.name).toBe("test");
2929
});
3030

31+
test("ids are not overwritten for add operations if one is supplied", () => {
32+
const options: OptimisticOptions = {
33+
mutation: CREATE_ITEM,
34+
operationType: CacheOperation.ADD,
35+
returnType: "Test",
36+
variables: {
37+
id: "123",
38+
name: "test"
39+
}
40+
};
41+
const result = createOptimisticResponse(options);
42+
expect(result.createItem.id).toBe("123");
43+
expect(result.createItem.name).toBe("test");
44+
});
45+
46+
test("ids are not overwritten for add operations if one is supplied", () => {
47+
const options: OptimisticOptions = {
48+
mutation: CREATE_ITEM,
49+
operationType: CacheOperation.ADD,
50+
idField: "customId",
51+
returnType: "Test",
52+
variables: {
53+
customId: "123",
54+
name: "test"
55+
}
56+
};
57+
const result = createOptimisticResponse(options);
58+
expect(result.createItem.customId).toBe("123");
59+
expect(result.createItem.name).toBe("test");
60+
});
61+
3162
test("check that createNewOptimisticResponse is without id", () => {
3263
const options: OptimisticOptions = {
3364
mutation: CREATE_ITEM,

0 commit comments

Comments
 (0)