Version: 0.15.0

Client Configuration

offix-client

ApolloOfflineClient

ApolloOfflineClient extends the ApolloClient class. Check the ApolloClient constructor docs to see the options that can be passed.

There are some additional options specific to ApolloOfflineClient.

networkStatus

NetworkStatus Interface for detecting changes in network status. (Uses browser networking APIs by default)

offlineStorage

The PersistentStore you want your client to use for persisting offline operations in the offline queue (Uses IndexedDB by default).

cacheStorage

The PersistentStore you want your client to use for persisting the Apollo Cache (Uses IndexedDB by default).

cachePersistor

The CachePersistor instance used by the client to persist the Apollo Cache across application restarts. Pass your own instance to override the one that is created by default.

Example:

import { ApolloOfflineClient, createDefaultCacheStorage } from "offix-client";
import { HttpLink } from "apollo-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";
import { CachePersistor } from "apollo-cache-persist";
const link = new HttpLink({ uri: "http://example.com/graphql" });
const cache = new InMemoryCache()
const cachePersistor = new CachePersistor({
cache,
storage: createDefaultCacheStorage()
})
const client = new ApolloOfflineClient({
cache,
cachePersistor,
link
});

Note: if using TypeScript, you may need to declare the cachePersistor as follows const cachePersistor = new CachePersistor<object>(...options) or you may experience compiler errors.

This example uses createDefaultCacheStorage to create the default IndexedDB based storage driver. The storage can be swapped depending on the platform. For example window.localstorage in older browsers or AsyncStorage in React Native.

offlineQueueListener

ApolloOfflineQueueListener User provided listener that contains a set of methods that are called when certain events occur in the queue.

conflictProvider

ObjectState Interface that defines how object state is progressed. This interface needs to match state provider supplied on server.

conflictStrategy

ConflictResolutionStrategyinterface used on the client to resolve conflicts. The default strategy merges client changes onto the server changes.

mutationCacheUpdates

CacheUpdates Cache updates functions for your mutations. Argument allows to restore optimistic responses on application restarts.

retryOptions

The options to configure how failed offline mutations are retried. See apollo-link-retry.

inputMapper

If your mutation variables are not passed directly, for example if you use input types, an inputMapper is a set of functions that tells Offix how to read the mutation variables.

For example, if your mutations use Input types:

const CREATE_TASK = gql`
mutation createTask($input: TaskInput!) {
createTask(input: $input) {
id
title
description
}
}`
client.offlineMutate({
mutation: CREATE_TASK,
variables: {
input: {
title: 'new task title',
description: 'new task description'
}
},
returnType: 'Task'
})

ApolloOfflineClient will need an additional inputMapper object with the following functions:

  • deserialize - to know how to convert the variables object into a flat object that can be used to generate optimistic responses and cache update functions.
  • serialize - to know how to convert the serialized object back into the correct variables object after performing conflict resolution.
import { ApolloOfflineClient, createDefaultCacheStorage } from "offix-client";
const client = new ApolloOfflineClient({
cache: new InMemoryCache(),
link: new HttpLink({ uri: "http://example.com/graphql" }),
inputMapper: {
deserialize: (variables) => { return variables.input },
serialize: (variables) => { return { input: variables } }
}
});

offix-client-boost

createClient

createClient accepts all of the ApolloOfflineClient options described above as well as the ones listed below.

  • httpUrl (required) - The URL of the GraphQL server
  • wsUrl (required) - The URL of the websocket endpoint for subscriptions
  • cache - The Apollo InMemoryCache that will be used. (creates one by default).
  • authContextProvider - An object or an async function that returns an AuthContext object with authentication headers that will be passed in GraphQL requests and in the connectionParams of websocket connections.
  • fileUpload - If set to true, GraphGL file uploads will be enabled and supported. (default is false)
  • websocketClientOptions - Options for the websocket client used for subscriptions. See subscriptions-transport-ws