Skip to content

Commit

Permalink
Remove DocumentTransformer and revert cache to original implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed May 12, 2023
1 parent 6af9d94 commit fc93384
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 303 deletions.
10 changes: 0 additions & 10 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@
src/*
!src/react/
src/react/*
!src/cache/
src/cache/*

# Allowed cache files
!src/cache/inmemory
src/cache/inmemory/*
!src/cache/inmemory/documentTransformer.ts
!src/cache/inmemory/__tests__/
src/cache/inmemory/__tests__/*
!src/cache/inmemory/__tests__/documentTransforms.ts

# Allow src/react/cache
!src/react/cache/
Expand Down
227 changes: 0 additions & 227 deletions src/cache/inmemory/__tests__/documentTransforms.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/cache/inmemory/documentTransformer.ts

This file was deleted.

49 changes: 13 additions & 36 deletions src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { StoreWriter } from './writeToStore';
import { EntityStore, supportsResultCaching } from './entityStore';
import { makeVar, forgetCache, recallCache } from './reactiveVars';
import { Policies } from './policies';
import { DocumentTransformer } from './documentTransformer';
import { hasOwn, normalizeConfig, shouldCanonizeResults } from './helpers';
import { canonicalStringify } from './object-canon';
import { OperationVariables } from '../../core';
Expand Down Expand Up @@ -62,27 +61,13 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
// cache.policies.addPossibletypes.
public readonly policies: Policies;

// Dynamically imported code can augment existing documentTransforms or
// documentTransformsForLink by calling cache.documentTransforms.add or
// cache.documentTransformsForLink.add
public readonly documentTransforms: DocumentTransformer;
public readonly documentTransformsForLink: DocumentTransformer;

public readonly makeVar = makeVar;

constructor(config: InMemoryCacheConfig = {}) {
super();
this.config = normalizeConfig(config);
this.addTypename = !!this.config.addTypename;

this.documentTransforms = new DocumentTransformer({
transforms: [this.addTypenameToDocument.bind(this)]
}).add(...(this.config.documentTransforms ?? []));

this.documentTransformsForLink = new DocumentTransformer({
transforms: [this.addFragmentsToDocument.bind(this)]
}).add(...(this.config.documentTransformsForLink ?? []));

this.policies = new Policies({
cache: this,
dataIdFromObject: this.config.dataIdFromObject,
Expand Down Expand Up @@ -525,27 +510,6 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
}

public transformDocument(document: DocumentNode): DocumentNode {
return this.documentTransforms.transform(document);
}

public transformForLink(document: DocumentNode): DocumentNode {
return this.documentTransformsForLink.transform(document);
}

protected broadcastWatches(options?: BroadcastOptions) {
if (!this.txCount) {
this.watches.forEach(c => this.maybeBroadcastWatch(c, options));
}
}

private addFragmentsToDocument(document: DocumentNode): DocumentNode {
const { fragments } = this.config;
return fragments
? fragments.transform(document)
: document;
}

private addTypenameToDocument(document: DocumentNode): DocumentNode {
if (this.addTypename) {
let result = this.typenameDocumentCache.get(document);
if (!result) {
Expand All @@ -561,6 +525,19 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
return document;
}

public transformForLink(document: DocumentNode): DocumentNode {
const { fragments } = this.config;
return fragments
? fragments.transform(document)
: document;
}

protected broadcastWatches(options?: BroadcastOptions) {
if (!this.txCount) {
this.watches.forEach(c => this.maybeBroadcastWatch(c, options));
}
}

// This method is wrapped by maybeBroadcastWatch, which is called by
// broadcastWatches, so that we compute and broadcast results only when
// the data that would be broadcast might have changed. It would be
Expand Down
4 changes: 0 additions & 4 deletions src/cache/inmemory/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ export interface InMemoryCacheConfig extends ApolloReducerConfig {
resultCacheMaxSize?: number;
canonizeResults?: boolean;
fragments?: FragmentRegistryAPI;
documentTransforms?: DocumentTransform[]
documentTransformsForLink?: DocumentTransform[]
}

export interface MergeInfo {
Expand All @@ -154,5 +152,3 @@ export interface ReadMergeModifyContext {
// A JSON.stringify-serialized version of context.variables.
varString?: string;
}

export type DocumentTransform = (document: DocumentNode) => DocumentNode

0 comments on commit fc93384

Please sign in to comment.