Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(graphcache) - fix no selectionset mutation updates in optimistic phase #654

Merged
merged 4 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tough-bats-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': patch
---

Fix case where a mutation-rootfield would cause an empty call to the cache.updates[mutationRootField].
56 changes: 56 additions & 0 deletions exchanges/graphcache/src/cacheExchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,62 @@ describe('data dependencies', () => {
expect(reexec).not.toHaveBeenCalled();
expect(result).toHaveBeenCalledTimes(2);
});

it('writes optimistic mutations to the cache', () => {
jest.useFakeTimers();

const mutation = gql`
mutation {
concealAuthor
}
`;

const mutationData = {
__typename: 'Mutation',
concealAuthor: true,
};

const client = createClient({ url: 'http://0.0.0.0' });
const { source: ops$, next } = makeSubject<Operation>();

jest.spyOn(client, 'reexecuteOperation').mockImplementation(next);

const opMutation = client.createRequestOperation('mutation', {
key: 1,
query: mutation,
});

const response = jest.fn(
(forwardOp: Operation): OperationResult => {
if (forwardOp.key === 1) {
return { operation: opMutation, data: mutationData };
}

return undefined as any;
}
);

const result = jest.fn();
const forward: ExchangeIO = ops$ => pipe(ops$, delay(1), map(response));

const updates = {
Mutation: {
concealAuthor: jest.fn(),
},
};

pipe(
cacheExchange({ updates })({ forward, client })(ops$),
tap(result),
publish
);

next(opMutation);
expect(updates.Mutation.concealAuthor).toHaveBeenCalledTimes(0);

jest.runAllTimers();
expect(updates.Mutation.concealAuthor).toHaveBeenCalledTimes(1);
});
});

describe('optimistic updates', () => {
Expand Down
2 changes: 1 addition & 1 deletion exchanges/graphcache/src/operations/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ const writeSelection = (
} else if (entityKey && !isRoot) {
// This is a leaf node, so we're setting the field's value directly
InMemoryData.writeRecord(entityKey || typename, fieldKey, fieldValue);
}
} else if (ctx.optimistic && isRoot) continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we leave a TODO here for scalar optimistic results or just address that in a subsequent PR?
I’d have time to take a look at that, because I suppose it’s expected behaviour 🤷‍♀️

Copy link
Collaborator Author

@JoviDeCroock JoviDeCroock Mar 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to defer this to a later PR, if you don't get to it I can also look into it no pressure 😄


if (isRoot) {
// We have to update the context to reflect up-to-date ResolveInfo
Expand Down