Skip to content
New issue

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

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

Already on GitHub? # to your account

RFC: Allow fetching to be forced to true by exchanges #444

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 8 additions & 5 deletions src/exchanges/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,19 @@ export const cacheExchange: Exchange = ({ forward, client }) => {
filter(op => !shouldSkip(op) && isOperationCached(op)),
map(operation => {
const cachedResult = resultCache.get(operation.key);
if (operation.context.requestPolicy === 'cache-and-network') {
reexecuteOperation(client, operation);
}

return {
const result: OperationResult = {
...cachedResult,
operation: addMetadata(operation, {
cacheOutcome: cachedResult ? 'hit' : 'miss',
}),
};

if (operation.context.requestPolicy === 'cache-and-network') {
result.fetching = true;
reexecuteOperation(client, operation);
}

return result;
})
);

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export const useMutation = <T = any, V = object>(
client.executeMutation(request, context || {}),
toPromise
).then(result => {
const { data, error, extensions } = result;
setState({ fetching: false, data, error, extensions });
const { fetching, data, error, extensions } = result;
setState({ fetching: fetching || false, data, error, extensions });
return result;
});
},
Expand Down
33 changes: 33 additions & 0 deletions src/hooks/useQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,37 @@ describe('useQuery', () => {
rerender({ query: mockQuery, variables: mockVariables, pause: true });
expect(client.executeQuery).toBeCalledTimes(1);
});

it('should keep fetching at true when the result instructs it to', async () => {
client.executeQuery.mockImplementationOnce(() =>
pipe(
interval(400),
map(() => ({
fetching: true,
data: { test: true },
}))
)
);

const { result, waitForNextUpdate } = renderHook(
({ query, variables }) => useQuery({ query, variables }),
{
initialProps: {
query: mockQuery,
variables: mockVariables,
},
}
);

await waitForNextUpdate();
expect(client.executeQuery).toBeCalledTimes(1);

const [state] = result.current;
expect(state).toEqual({
fetching: true,
data: { test: true },
error: undefined,
extensions: undefined,
});
});
});
4 changes: 2 additions & 2 deletions src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export const useQuery = <T = any, V = object>(
...opts,
}),
onEnd(() => setState(s => ({ ...s, fetching: false }))),
subscribe(({ data, error, extensions }) => {
setState({ fetching: false, data, error, extensions });
subscribe(({ fetching, data, error, extensions }) => {
setState({ fetching: fetching || false, data, error, extensions });
})
);
},
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export interface OperationResult<Data = any> {
error?: CombinedError;
/** Optional extensions return by the Graphql server. */
extensions?: Record<string, any>;
/** Optional flag to force fetching to remain true. */
fetching?: void | true;
}

/** Input parameters for to an Exchange factory function. */
Expand Down