Skip to main content

refetchQueries

Overview

refetchQueries is analogous to refetchQueries option passed to useMutation.
It enables refetching queries after a mutation has executed.

Usage

Let's modify bookState to refetch the full list of books from the server instead of modifying the cache on the client when AddBook mutation is executed.

library/states/book.state.ts
import { state } from '@apollo-orbit/react';
import { AddBookDocument, BooksDocument } from '../../graphql';

export const bookState = state(descriptor => descriptor
.refetchQueries(AddBookDocument, info => [BooksDocument])
.mutationUpdate(AddBookDocument, (cache, info) => {
const addBook = info.data?.addBook;
if (!addBook) return;
cache.updateQuery(BooksDocument, data => data ? { books: [...data.books, addBook] } : data);
})
);

Now, whenever AddBook mutation is executed, Books query will be refetched from the server and any components watching Books query will be updated with the new data.

API

type RefetchQueryDescriptor = Array<string | DocumentNode | PureQueryOptions | QueryOptions> | 'all' | 'active';