Skip to content

Commit

Permalink
Run Policies's merge before cache merge
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonmorixe committed Mar 7, 2022
1 parent 6ca525a commit 2b34247
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/cache/inmemory/__tests__/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2496,6 +2496,20 @@ describe('writing to the store', () => {
},
},

{
existing: {
__ref: "Account:12345",
},
incoming: {
__typename: "Account",
contact: "[email protected]",
id: 12345,
},
merged: {
__ref: "Account:12345",
},
},

{
existing: {
__typename: "Account",
Expand Down
7 changes: 5 additions & 2 deletions src/cache/inmemory/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ export interface FieldFunctionOptions<
// helper function can be used to merge objects in a way that respects any
// custom merge functions defined for their fields.
mergeObjects: MergeObjectsFunction;

context: ReadMergeModifyContext | undefined;
}

type MergeObjectsFunction = <T extends StoreObject | Reference>(
Expand Down Expand Up @@ -542,7 +544,7 @@ export class Policies {
});
}

private getTypePolicy(typename: string): Policies["typePolicies"][string] {
public getTypePolicy(typename: string): Policies["typePolicies"][string] {
if (!hasOwn.call(this.typePolicies, typename)) {
const policy: Policies["typePolicies"][string] =
this.typePolicies[typename] = Object.create(null);
Expand Down Expand Up @@ -878,7 +880,7 @@ export class Policies {
// that need to deduplicate child objects and references.
void 0,
{ typename,
fieldName: field.name.value,
fieldName: field?.name.value || "ROOT",
field,
variables: context.variables },
context,
Expand Down Expand Up @@ -917,6 +919,7 @@ function makeFieldFunctionOptions(
);
},
mergeObjects: makeMergeObjectsFunction(context.store),
context
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/cache/inmemory/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export interface InMemoryCacheConfig extends ApolloReducerConfig {
}

export interface MergeInfo {
field: FieldNode;
field: FieldNode | undefined;
typename: string | undefined;
merge: FieldMergeFunction;
};
Expand Down
23 changes: 22 additions & 1 deletion src/cache/inmemory/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class StoreWriter {
context.incomingById.forEach(({ storeObject, mergeTree, fieldNodeSet }, dataId) => {
const entityRef = makeReference(dataId);

if (mergeTree && mergeTree.map.size) {
if (mergeTree && (mergeTree.map.size || mergeTree.info)) {
const applied = this.applyMerges(mergeTree, entityRef, storeObject, context);
if (isReference(applied)) {
// Assume References returned by applyMerges have already been merged
Expand Down Expand Up @@ -421,6 +421,24 @@ export class StoreWriter {
previous.mergeTree = mergeMergeTrees(previous.mergeTree, mergeTree);
fieldNodeSet.forEach(field => previous.fieldNodeSet.add(field));
} else {

if(typename && mergeTreeIsEmpty(mergeTree)) {
const typePolicy = policies.getTypePolicy(
typename,
);

const merge = typePolicy.merge

if (merge) {
mergeTree.info = {
field: undefined as any, // !!!
typename,
merge,
};
}
}


context.incomingById.set(dataId, {
storeObject: incoming,
// Save a reference to mergeTree only if it is not empty, because
Expand Down Expand Up @@ -660,6 +678,9 @@ export class StoreWriter {
}

if (mergeTree.info) {
if(isReference(existing) && isReference(incoming)){
return incoming;
}
return this.cache.policies.runMergeFunction(
existing,
incoming,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ describe('relayStylePagination', () => {
readField: () => undefined,
canRead: () => false,
mergeObjects: (existing, _incoming) => existing,
context: undefined,
};

it('should maintain endCursor and startCursor with empty edges', () => {
Expand Down

0 comments on commit 2b34247

Please sign in to comment.