Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
}
}
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];
}
});
}
}
}
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];
}
});
}
}
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
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;
}
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;
}
}
public hasClientId() {
return isClientGeneratedId(this.variables[this.idField as string]);
}
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;
}
}
}