optimisticResponse
Overview
optimisticResponse is analogous to optimisticResponse option passed to mutate method in Apollo.
It enables optimistic UI, allowing for immediate UI updates before the server responds.
Usage
Let's extend bookState to return an optimistic response when ADD_BOOK_MUTATION is executed.
library/states/book.state.ts
import { state } from '@apollo-orbit/angular/state';
import shortid from 'shortid';
import { ADD_BOOK_MUTATION } from '../../graphql/types';
export const bookState = state(descriptor => descriptor
.optimisticResponse(ADD_BOOK_MUTATION, ({ book }) => ({
__typename: 'Mutation' as const,
addBook: {
__typename: 'Book' as const,
id: shortid.generate(),
genre: book.genre ?? null,
name: book.name,
authorId: book.authorId
}
}))
);