Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
feat: provide offline mutation wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenCoady authored and wtrocki committed May 15, 2019
1 parent d757b91 commit b2aff3c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/sync/src/OfflineApolloClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { NormalizedCacheObject } from "apollo-cache-inmemory";
import { ApolloClient } from "apollo-client";
import { OfflineStore, OfflineQueueListener } from "./offline";
import { MutationHelperOptions } from "./cache";
import { FetchResult } from "apollo-link";

/**
* Extension to ApolloClient providing additional capabilities.
Expand All @@ -21,4 +23,11 @@ export interface ApolloOfflineClient extends ApolloClient<NormalizedCacheObject>
* @param listener
*/
registerOfflineEventListener(listener: OfflineQueueListener): void;

/**
* Allows the client to perform an offline mutation
* @param options the mutation helper options used to build the offline mutation
*/
offlineMutation<T>(options: MutationHelperOptions): Promise<FetchResult<T>>;

}
19 changes: 19 additions & 0 deletions packages/sync/src/OfflineClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { CompositeQueueListener } from "./offline/events/CompositeQueueListener"
import { ListenerProvider } from "./offline/events/ListenerProvider";
import { ApolloOfflineClient } from "./OfflineApolloClient";
import { buildCachePersistence } from "./offline/storage/defaultStorage";
import { MutationHelperOptions, createMutationOptions } from "./cache";
import { Options } from "graphql/utilities/buildClientSchema";
import { FetchResult } from "apollo-link";

/**
* Factory for creating Apollo Offline Client
Expand Down Expand Up @@ -87,9 +90,25 @@ export class OfflineClient implements ListenerProvider {
this.queueListeners.push(listener);
}

/**
* Offline wrapper for apollo mutations. Provide Mutation Helper Options and use
* this offline friendly function to handle the optimistic UI and cache updates.
* @param options the MutationHelperOptions to create the mutation
*/
public offlineMutation<T>(options: MutationHelperOptions): Promise<FetchResult<T>> {
if (!this.apolloClient) {
throw new Error("Apollo offline client not initialised before mutation called.");
} else {
return this.apolloClient.mutate<T>(
createMutationOptions(options)
);
}
}

protected decorateApolloClient(apolloClient: any): ApolloOfflineClient {
apolloClient.offlineStore = this.offlineStore;
apolloClient.registerOfflineEventListener = this.registerOfflineEventListener.bind(this);
apolloClient.offlineMutation = this.offlineMutation.bind(this);
return apolloClient;
}

Expand Down

0 comments on commit b2aff3c

Please sign in to comment.