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 AddBookMutation
is executed.
library/states/book.state.ts
import { state } from '@apollo-orbit/angular';
import shortid from 'shortid';
import { AddBookMutation } from '../../graphql/types';
export const bookState = state(descriptor => descriptor
.optimisticResponse(AddBookMutation, ({ book }) => ({
__typename: 'Mutation' as const,
addBook: {
__typename: 'Book' as const,
id: shortid.generate(),
genre: book.genre ?? null,
name: book.name,
authorId: book.authorId
}
}))
);