How to use offix-cache - 8 common examples

To help you get started, we’ve selected a few offix-cache examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aerogear / offix / packages / offix-scheduler / src / queue / OfflineQueue.ts View on Github external
public async enqueueOperation(op: T): Promise {

    const entry: QueueEntry = {
      operation: {
        qid: generateClientId(),
        op
      }
    };

    // enqueue and persist
    this.queue.push(entry);
    // notify listeners
    this.onOperationEnqueued(entry.operation);

    if (this.store) {
      try {
        await this.store.saveEntry(entry.operation);
      } catch (err) {
        console.error(err);
      }
    }
github aerogear / offix / packages / offix-offline / src / offline / processors / IDProcessor.ts View on Github external
const op = entry.operation.op;
    const operationName = op.context.operationName as string;
    const optimisticResponse = op.optimisticResponse as {[key: string]: any};
    const idField = op.context.idField || "id";

    if (!result || !optimisticResponse || !optimisticResponse[operationName]) {
      return;
    }

    let clientId = optimisticResponse[operationName][idField];
    if (!clientId) {
      return;
    }
    // Ensure we dealing with string
    clientId = clientId.toString();
    if (isClientGeneratedId(optimisticResponse[operationName][idField])) {
      queue.forEach(({ operation }) => {
        if (operation.op.variables && operation.op.variables[idField] === clientId) {
         operation.op.variables[idField] = result.data && result.data[operationName][idField];
        }
      });
    }
  }
}
github aerogear / offix / packages / offix-client / src / apollo / optimisticResponseHelpers.ts View on Github external
const op = operation.op;
  const operationName = op.context.operationName as string;
  const optimisticResponse = op.optimisticResponse as {[key: string]: any};
  const idField = op.context.idField || "id";

  if (!result || !optimisticResponse || !optimisticResponse[operationName]) {
    return;
  }

  let clientId = optimisticResponse[operationName][idField];
  if (!clientId) {
    return;
  }
  // Ensure we dealing with string
  clientId = clientId.toString();
  if (isClientGeneratedId(optimisticResponse[operationName][idField])) {
    queue.forEach((entry) => {
      if (entry.operation.op.variables && entry.operation.op.variables[idField] === clientId) {
       entry.operation.op.variables[idField] = result.data && result.data[operationName][idField];
      }
    });
  }
}
github aerogear / offix / packages / offix-offline / src / offline / OfflineMutationsHandler.ts View on Github external
public mutateOfflineElement(item: OfflineItem) {
    const optimisticResponse = item.optimisticResponse;
    const mutationName = getMutationName(item.query);
    let context;
    let updateFunction;
    const previousContext: any = {};

    context = { ...previousContext, ...this.getOfflineContext(item) };

    if (this.mutationCacheUpdates && mutationName) {
      updateFunction = this.mutationCacheUpdates[mutationName];
    }

    const mutationOptions: MutationOptions = {
      variables: item.variables,
      mutation: item.query,
      // Restore optimistic response from operation in order to see it
      optimisticResponse,
      // Pass client update functions
github aerogear / offix / packages / offix-client / src / OfflineClient.ts View on Github external
protected createOfflineMutationOptions(
    options: MutationHelperOptions): MutationOptions {
    const offlineMutationOptions = createMutationOptions(options);

    offlineMutationOptions.context.conflictBase = getBaseStateFromCache(
      this.cache as unknown as ApolloCacheWithData,
      this.config.conflictProvider,
      offlineMutationOptions as unknown as MutationOptions
    );

    if (!offlineMutationOptions.update && this.config.mutationCacheUpdates) {
      offlineMutationOptions.update = this.config.mutationCacheUpdates[offlineMutationOptions.context.operationName];
    }
    return offlineMutationOptions;
  }
github aerogear / offix / packages / offix-client / src / ApolloOfflineClient.ts View on Github external
protected createOfflineMutationOptions(
    options: MutationHelperOptions): MutationOptions {
    const offlineMutationOptions = createMutationOptions(options);

    offlineMutationOptions.context.conflictBase = getBaseStateFromCache(
      this.cache as unknown as ApolloCacheWithData,
      this.conflictProvider,
      offlineMutationOptions as unknown as MutationOptions
    );

    if (!offlineMutationOptions.update && this.mutationCacheUpdates) {
      offlineMutationOptions.update = this.mutationCacheUpdates[offlineMutationOptions.context.operationName];
    }
    return offlineMutationOptions;
  }
}
github aerogear / offix / packages / offix-offline / src / offline / OperationQueueEntry.ts View on Github external
public hasClientId() {
    return isClientGeneratedId(this.variables[this.idField as string]);
  }
github aerogear / offix / packages / offix-offline / src / offline / OperationQueueEntry.ts View on Github external
constructor(operation: Operation, offlineId?: number) {
    this.query = operation.query;
    this.variables = operation.variables;
    this.operationName = operation.operationName;
    if (offlineId) {
      this.id = offlineId.toString();
    } else {
      this.id = generateClientId();
    }
    if (typeof operation.getContext === "function") {
      const context = operation.getContext();
      this.conflictBase = context.conflictBase;
      this.returnType = context.returnType;
      this.idField = context.idField;
      this.optimisticResponse = context.optimisticResponse;
      if (context.conflictStrategy) {
        this.conflictStrategy = context.conflictStrategy.id;
      }
    }
  }

offix-cache

GraphQL Mutation and Subscription Helpers

Apache-2.0
Latest version published 4 years ago

Package Health Score

51 / 100
Full package analysis

Similar packages