Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function getBaseStateFromCache(
cache: ApolloCacheWithData,
objectState: ObjectState,
mutationOptions: MutationOptions
): ConflictResolutionData {
const context = mutationOptions.context;
if (!context.conflictBase) {
// do nothing
const conflictBase = getObjectFromCache(cache, context.returnType, mutationOptions);
if (conflictBase && Object.keys(conflictBase).length !== 0) {
if (objectState.hasConflict(mutationOptions.variables, conflictBase)) {
// 🙊 Input data is conflicted with the latest server projection
throw new LocalConflictError(conflictBase, mutationOptions.variables);
}
return conflictBase;
}
}
}
private conflictHandler(errorResponse: ErrorResponse): Observable {
const { response, operation, forward, graphQLErrors } = errorResponse;
const data = this.getConflictData(graphQLErrors);
const individualStrategy = this.strategy || UseClient;
if (data && operation.getContext().returnType) {
const base = operation.getContext().conflictBase;
const conflictHandler = new ConflictHandler({
base,
client: data.clientState,
server: data.serverState,
strategy: individualStrategy,
listener: this.listener,
objectState: this.config.conflictProvider as ObjectState,
operationName: operation.operationName
});
const resolvedConflict = conflictHandler.executeStrategy();
if (resolvedConflict) {
operation.variables = resolvedConflict;
}
}
return forward(operation);
}
constructor(options = {} as ApolloOfflineClientOptions) {
Object.assign(this, options);
this.cacheStorage = options.cacheStorage || createDefaultCacheStorage();
this.offlineStorage = options.offlineStorage || createDefaultOfflineStorage();
this.conflictStrategy = options.conflictStrategy || UseClient;
this.conflictProvider = options.conflictProvider || new VersionedState();
this.link = createDefaultLink(this);
}
}