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

Commit

Permalink
fix: id overwrite issue in optimistic responses
Browse files Browse the repository at this point in the history
  • Loading branch information
darahayes authored and wtrocki committed Apr 24, 2020
1 parent 77907cb commit 55b3545
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/offix-cache/src/createOptimisticResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const createOptimisticResponse = (options: OptimisticOptions) => {
...mappedVariables,
optimisticResponse: true
};
if (operationType === CacheOperation.ADD) {
if (operationType === CacheOperation.ADD && !optimisticResponse[operation][idField]) {
optimisticResponse[operation][idField] = generateClientId();
}

Expand Down
31 changes: 31 additions & 0 deletions packages/offix-cache/test/OptimisticResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,37 @@ test("check that createNewOptimisticResponse is properly composed with name prop
expect(result.createItem.name).toBe("test");
});

test("ids are not overwritten for add operations if one is supplied", () => {
const options: OptimisticOptions = {
mutation: CREATE_ITEM,
operationType: CacheOperation.ADD,
returnType: "Test",
variables: {
id: "123",
name: "test"
}
};
const result = createOptimisticResponse(options);
expect(result.createItem.id).toBe("123");
expect(result.createItem.name).toBe("test");
});

test("ids are not overwritten for add operations if one is supplied", () => {
const options: OptimisticOptions = {
mutation: CREATE_ITEM,
operationType: CacheOperation.ADD,
idField: "customId",
returnType: "Test",
variables: {
customId: "123",
name: "test"
}
};
const result = createOptimisticResponse(options);
expect(result.createItem.customId).toBe("123");
expect(result.createItem.name).toBe("test");
});

test("check that createNewOptimisticResponse is without id", () => {
const options: OptimisticOptions = {
mutation: CREATE_ITEM,
Expand Down

0 comments on commit 55b3545

Please sign in to comment.