refetchQueries
Overview
refetchQueries
is analogous to refetchQueries
option passed to mutate
method in Apollo
.
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 AddBookMutation
is executed.
library/states/book.state.ts
import { state } from '@apollo-orbit/angular';
import { AddBookMutation, BooksQuery } from '../../graphql/types';
export const bookState = state(descriptor => descriptor
.refetchQueries(AddBookMutation, info => [new BooksQuery()])
.mutationUpdate(AddBookMutation, (cache, info) => {
const addBook = info.data?.addBook;
if (!addBook) return;
cache.updateQuery(new BooksQuery(), data => data ? { books: [...data.books, addBook] } : data);
})
);
Now, whenever AddBookMutation
is executed, BooksQuery
will be refetched from the server and any components watching BooksQuery
will be updated with the new data.
API
type RefetchQueryDescriptor = Array<string | DocumentNode | PureQueryOptions | QueryOptions> | 'all' | 'active';