offix-cache package helps developers to manage some of their client's state such as cache, optimistic responses and subscriptions.
Offix Cache capabilities are available automatically when using
In Apollo Client, mutation results are not applied to the UI until responses are received from the server. To provide a better user experience, an application may want to update the UI immediately. Optimistic Responses are an easy to way to achieve this goal. However, creating individual optimistic responses for each mutation in your application can introduce boilerplate code. offix-cache can automatically create optimistic responses for you to reduce this boilerplate.
createOptimisticResponse function returns an object which can be passed directly to Apollo Client's mutate function.
createOptimisticResponse will help to build expected response object from input arguments.
if your mutation returns different values you will still need to build it manually.
Mutation Cache Helpers
offix-cache provides a mechanism to automatically update the client cache based on the result returned by a mutation. The
createMutationOptions function returns a
MutationOptions object compatible with Apollo Client's mutate.
We can also provide more than one query to update in the cache by providing an array to the
mutationOptions is either of the two objects shown above, we can then pass this object to our mutate function.
Pagination and relationships
Offix by default assumes that the object returned by mutations contains just your data. In situations when you need to wrap your data into some container to provide pagination and other information, Offix will require an additional parameter. For example when the API returns TaskPage etc. the actual data might be returned over
When this query is cached, it may be necessary to update a field within the query and not the entire query, for example with relationships. In order to do this, it is necessary to provide the name of the field being updated with the
offix-cache provides a subscription cache update method helper which can generate the necessary options to be used with Apollo Client's
To use this helper, we first need to create some options. These options should take the folowing form:
This options object will be used to inform the subscription helper that for every data object received because of the
GET_TASKS query should also be kept up to date in the cache.
We can then create the required cache update functions in the following way:
To use this helper we then pass this
subscriptionOptions variable to the
subscribeToMore function of our
The cache will now be kept up to date with automatic data deduplication being performed.
Pagination and relationships
Similarly to the mutation cache update helpers, it is necessary to provide the
returnField parameter to specify the name of the field to update within the query.
offix-cache also provides the ability to automatically call
subscribeToMore on your
ObservableQuery. This can be useful in a situation where you may have multiple subscriptions which can affect one single query. For example, if you have a
TaskDeleted and a
TaskUpdated subscription you would need three separate
subscribeToMore function calls. This can become tedious as your number of subscriptions grow. To combat this, we can use the
subscribeToMoreHelper function from offix-cache to automatically handle this for us by passing it an array of subscriptions and their corresponding queries which need to be updated.