Skip to content
This repository has been archived by the owner on Feb 6, 2022. It is now read-only.

Commit

Permalink
Allow skipping all refetching when performing mutation
Browse files Browse the repository at this point in the history
Fixes #18

There are cases when it's not important enough for a mutation
to refetch queries. For example, a like counter component
will most likely keep track of likes in a local state to ensure
instant response on click to increment a counter. In that case,
it's only important to increment like counter on backend and
not spend bandwidth on unnecessarily refreshing other parts of the page.

This change allows `refetchQueries` to be set to `false` to disable
any kind of refetching
  • Loading branch information
Vadim Demedes committed Dec 4, 2019
1 parent 1de0c15 commit fbd463c
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 3 deletions.
11 changes: 10 additions & 1 deletion docs/src/pages/docs/mutations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ const Blog = () => {
};
```

If you wish to disable refetching of any queries, set `refetchQueries` parameter to `false`:

```js
useMutation(CREATE_POST_MUTATION, {
refetchQueries: false
});
```

## API

### useMutation(query, options?)
Expand Down Expand Up @@ -223,10 +231,11 @@ Type: `object`

##### options.refetchQueries

Type: `DocumentNode[]`<br/>
Type: `boolean | DocumentNode[]`<br/>
Default: `[]`

List of queries to refetch after the mutation succeeds.
Set to `false` to disable any refetching.

##### options.waitForRefetchQueries

Expand Down
7 changes: 6 additions & 1 deletion src/Draqula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface QueryOptions {
}

interface MutateOptions {
readonly refetchQueries: DocumentNode[];
readonly refetchQueries: boolean | DocumentNode[];
readonly waitForRefetchQueries?: boolean;
}

Expand Down Expand Up @@ -250,6 +250,11 @@ export default class Draqula {
retry: false
});

// If refetching is disabled, we don't need to do any of the following
if (options.refetchQueries === false) {
return data;
}

const typenames = getTypenames(data);
const refetchQueries: string[] = [];

Expand Down
2 changes: 1 addition & 1 deletion src/useMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import NetworkError from './lib/network-error';
import GraphQLError from './lib/graphql-error';

interface MutationOptions {
readonly refetchQueries: DocumentNode[];
readonly refetchQueries: boolean | DocumentNode[];
readonly waitForRefetchQueries: boolean;
}

Expand Down
143 changes: 143 additions & 0 deletions test/useMutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,3 +803,146 @@ test('delete cache for refetched queries', async t => {
error: null
});
});

test('avoid refetching queries if refetchQueries === false', async t => {
const client = new Draqula('http://graph.ql');

const todos = {
todos: [
{
id: 'a',
title: 'A',
__typename: 'Todo'
}
],
__typename: 'RootQuery'
};

nock('http://graph.ql')
.post('/', /todos/)
.reply(200, {
data: todos
});

const posts = {
posts: [
{
id: 'x',
title: 'X',
__typename: 'Post'
}
],
__typename: 'RootQuery'
};

nock('http://graph.ql')
.post('/', /posts/)
.delay(100)
.reply(200, {
data: posts
});

nock('http://graph.ql')
.post('/', {
query: /.+/,
variables: {
title: 'B'
}
})
.reply(200, {
data: {
createTodo: {
id: 'b',
title: 'B',
__typename: 'Todo'
},
__typename: 'RootMutation'
}
});

const wrapper = createWrapper(client);
const mutation = renderHook(
() => {
return useMutation(CREATE_TODO_MUTATION, {
refetchQueries: false
});
},
{wrapper}
);
const todosQuery = renderHook(() => useQuery(TODOS_QUERY), {wrapper});
const postsQuery = renderHook(() => useQuery(POSTS_QUERY), {wrapper});

assertMutation(t, mutation.result, {
data: null,
isLoading: false,
error: null
});

assertQuery(t, todosQuery.result, {
data: null,
isLoading: true,
error: null
});

assertQuery(t, postsQuery.result, {
data: null,
isLoading: true,
error: null
});

await todosQuery.waitForNextUpdate();
await postsQuery.waitForNextUpdate();

assertQuery(t, todosQuery.result, {
data: todos,
isLoading: false,
error: null
});

assertQuery(t, postsQuery.result, {
data: posts,
isLoading: false,
error: null
});

act(() => {
mutation.result.current.mutate({
title: 'B'
});
});

assertMutation(t, mutation.result, {
data: null,
isLoading: true,
error: null
});

await mutation.waitForNextUpdate();
await t.throwsAsync(todosQuery.waitForNextUpdate({timeout: 1000}));
t.true(nock.isDone());

assertMutation(t, mutation.result, {
data: {
createTodo: {
id: 'b',
title: 'B',
__typename: 'Todo'
},
__typename: 'RootMutation'
},
isLoading: false,
error: null
});

assertQuery(t, todosQuery.result, {
data: todos,
isLoading: false,
error: null
});

assertQuery(t, postsQuery.result, {
data: posts,
isLoading: false,
error: null
});
});

0 comments on commit fbd463c

Please sign in to comment.