Skip to content
Merged
Prev Previous commit
Next Next commit
remove fetchMore in favour of declarative pagination
refetch can be used to achieve the same
  • Loading branch information
jackdclark committed Feb 22, 2019
commit 46317a45105f24c18616237d76acf999dad60cfc
11 changes: 6 additions & 5 deletions src/useQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ module.exports = function useQuery(query, opts = {}) {

return {
...state,
refetch: () => queryReq({ skipCache: true }),
fetchMore: ({ variables, ...rest } = {}) =>
// merge variables so they don't all have to be passed back in,
// just the ones that are changing
queryReq({ ...rest, variables: { ...allOpts.variables, ...variables } })
refetch: (options = {}) =>
queryReq({
skipCache: true,
updateResult: (_, result) => result,
...options
})
};
};
40 changes: 25 additions & 15 deletions test/unit/useQuery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,51 +59,61 @@ describe('useQuery', () => {
expect(state).toEqual({
loading: true,
cacheHit: false,
refetch: expect.any(Function),
fetchMore: expect.any(Function)
refetch: expect.any(Function)
});
});

it('bypasses cache when refetch is called', () => {
let refetch;
testHook(() => ({ refetch } = useQuery(TEST_QUERY)), { wrapper: Wrapper });
refetch();
expect(mockQueryReq).toHaveBeenCalledWith({ skipCache: true });
expect(mockQueryReq).toHaveBeenCalledWith({
skipCache: true,
updateResult: expect.any(Function)
});
});

it('sends a query with original options when fetchMore is called', () => {
let fetchMore;
it('merges options when refetch is called', () => {
let refetch;
testHook(
() =>
({ fetchMore } = useQuery(TEST_QUERY, {
({ refetch } = useQuery(TEST_QUERY, {
variables: { skip: 0, first: 10 }
})),
{
wrapper: Wrapper
}
);
fetchMore();
const updateResult = () => {};
refetch({
extra: 'option',
variables: { skip: 10, first: 10, extra: 'variable' },
updateResult
});
expect(mockQueryReq).toHaveBeenCalledWith({
variables: { skip: 0, first: 10 }
skipCache: true,
extra: 'option',
variables: { skip: 10, first: 10, extra: 'variable' },
updateResult
});
});

it('passes options & merges variables when fetchMore is called', () => {
let fetchMore;
it('gets updateResult to replace the result by default', () => {
let refetch;
testHook(
() =>
({ fetchMore } = useQuery(TEST_QUERY, {
({ refetch } = useQuery(TEST_QUERY, {
variables: { skip: 0, first: 10 }
})),
{
wrapper: Wrapper
}
);
fetchMore({ extra: 'option', variables: { skip: 10, extra: 'variable' } });
expect(mockQueryReq).toHaveBeenCalledWith({
extra: 'option',
variables: { skip: 10, first: 10, extra: 'variable' }
mockQueryReq.mockImplementationOnce(({ updateResult }) => {
return updateResult('previousResult', 'result');
});
refetch();
expect(mockQueryReq).toHaveReturnedWith('result');
});

it('sends the query on mount if no data & no error', () => {
Expand Down