Skip to content

Commit

Permalink
feat(graphcache): track abstract-concrete type mapping (#3548)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Apr 2, 2024
1 parent 6bd4c90 commit a6f5162
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-toes-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': minor
---

Track abstract types being written so that we have a more reliable way of matching abstract fragments
30 changes: 28 additions & 2 deletions exchanges/graphcache/src/operations/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import {
} from '../ast';

import { warn, pushDebugNode, popDebugNode } from '../helpers/help';
import { hasField, currentOperation, currentOptimistic } from '../store/data';
import {
hasField,
currentOperation,
currentOptimistic,
writeConcreteType,
getConcreteTypes,
} from '../store/data';
import { keyOfField } from '../store/keys';
import type { Store } from '../store/store';

Expand Down Expand Up @@ -219,17 +225,30 @@ export function makeSelectionIterator(
!fragment.typeCondition ||
(ctx.store.schema
? isInterfaceOfType(ctx.store.schema, fragment, typename)
: isFragmentHeuristicallyMatching(
: (currentOperation === 'read' &&
isFragmentMatching(
fragment.typeCondition.name.value,
typename
)) ||
isFragmentHeuristicallyMatching(
fragment,
typename,
entityKey,
ctx.variables,
ctx.store.logger
));

if (isMatching) {
if (process.env.NODE_ENV !== 'production')
pushDebugNode(typename, fragment);
const isFragmentOptional = isOptional(select);
if (
fragment.typeCondition &&
typename !== fragment.typeCondition.name.value
) {
writeConcreteType(fragment.typeCondition.name.value, typename!);
}

child = makeSelectionIterator(
typename,
entityKey,
Expand All @@ -250,6 +269,13 @@ export function makeSelectionIterator(
};
}

const isFragmentMatching = (typeCondition: string, typename: string | void) => {
if (!typename) return false;
if (typeCondition === typename) return true;
const types = getConcreteTypes(typeCondition);
return types.size && types.has(typename);
};

export const ensureData = (x: DataField): Data | NullArray<Data> | null =>
x == null ? null : (x as Data | NullArray<Data>);

Expand Down
20 changes: 20 additions & 0 deletions exchanges/graphcache/src/store/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export interface InMemoryData {
optimisticOrder: number[];
/** This may be a persistence adapter that will receive changes in a batch */
storage: StorageAdapter | null;
/** A map of all the types we have encountered that did not map directly to a concrete type */
abstractToConcreteMap: Map<string, Set<string>>;
}

let currentOwnership: null | WeakSet<any> = null;
Expand Down Expand Up @@ -249,6 +251,7 @@ export const make = (queryRootKey: string): InMemoryData => ({
optimistic: new Map(),
base: new Map(),
},
abstractToConcreteMap: new Map(),
records: {
optimistic: new Map(),
base: new Map(),
Expand Down Expand Up @@ -482,6 +485,23 @@ export const writeType = (typename: string, entityKey: string) => {
}
};

export const getConcreteTypes = (typename: string): Set<string> =>
currentData!.abstractToConcreteMap.get(typename) || DEFAULT_EMPTY_SET;

export const writeConcreteType = (
abstractType: string,
concreteType: string
) => {
const existingTypes = currentData!.abstractToConcreteMap.get(abstractType);
if (!existingTypes) {
const typeSet = new Set<string>();
typeSet.add(concreteType);
currentData!.abstractToConcreteMap.set(abstractType, typeSet);
} else {
existingTypes.add(concreteType);
}
};

/** Writes an entity's field (a "record") to data */
export const writeRecord = (
entityKey: string,
Expand Down

0 comments on commit a6f5162

Please sign in to comment.