Skip to main content

optimisticResponse

Overview

optimisticResponse is analogous to optimisticResponse option passed to useMutation.
It enables optimistic UI, allowing for immediate UI updates before the server responds.

Usage

Let's extend bookState to return an optimistic response when AddBook mutation is executed.

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

export const bookState = state(descriptor => descriptor
.optimisticResponse(AddBookDocument, ({ book }) => ({
__typename: 'Mutation' as const,
addBook: {
__typename: 'Book' as const,
id: shortid.generate(),
genre: book.genre ?? null,
name: book.name,
authorId: book.authorId
}
}))
);