···11+---
22+'@urql/exchange-graphcache': minor
33+---
44+55+Add `globalIDs` configuration option to omit typenames in cache keys.
+14
exchanges/graphcache/src/store/store.test.ts
···237237 });
238238});
239239240240+describe('Store with Global IDs', () => {
241241+ it('generates keys without typenames when set to true', () => {
242242+ const store = new Store({ globalIDs: true });
243243+ expect(store.keyOfEntity({ __typename: 'Any', id: '123' })).toBe('123');
244244+ expect(store.keyOfEntity({ __typename: 'None', id: '123' })).toBe('123');
245245+ });
246246+247247+ it('generates keys without typenames when matching an input set', () => {
248248+ const store = new Store({ globalIDs: ['User'] });
249249+ expect(store.keyOfEntity({ __typename: 'Any', id: '123' })).toBe('Any:123');
250250+ expect(store.keyOfEntity({ __typename: 'User', id: '123' })).toBe('123');
251251+ });
252252+});
253253+240254describe('Store with ResolverConfig', () => {
241255 it("sets the store's resolvers field to the given argument", () => {
242256 const resolversOption = {
+21-8
exchanges/graphcache/src/store/store.ts
···5050 updates: UpdatesConfig;
5151 optimisticMutations: OptimisticMutationConfig;
5252 keys: KeyingConfig;
5353+ globalIDs: Set<string> | boolean;
5354 schema?: SchemaIntrospector;
54555556 rootFields: { query: string; mutation: string; subscription: string };
···6162 this.resolvers = opts.resolvers || {};
6263 this.optimisticMutations = opts.optimistic || {};
6364 this.keys = opts.keys || {};
6565+6666+ this.globalIDs = Array.isArray(opts.globalIDs)
6767+ ? new Set(opts.globalIDs)
6868+ : !!opts.globalIDs;
64696570 let queryName = 'Query';
6671 let mutationName = 'Mutation';
···107112 // In resolvers and updaters we may have a specific parent
108113 // object available that can be used to skip to a specific parent
109114 // key directly without looking at its incomplete properties
110110- if (contextRef && data === contextRef.parent) return contextRef.parentKey;
111111-112112- if (data == null || typeof data === 'string') return data || null;
113113- if (!data.__typename) return null;
114114- if (this.rootNames[data.__typename]) return data.__typename;
115115+ if (contextRef && data === contextRef.parent) {
116116+ return contextRef.parentKey;
117117+ } else if (data == null || typeof data === 'string') {
118118+ return data || null;
119119+ } else if (!data.__typename) {
120120+ return null;
121121+ } else if (this.rootNames[data.__typename]) {
122122+ return data.__typename;
123123+ }
115124116116- let key: string | null | void;
125125+ let key: string | null = null;
117126 if (this.keys[data.__typename]) {
118118- key = this.keys[data.__typename](data);
127127+ key = this.keys[data.__typename](data) || null;
119128 } else if (data.id != null) {
120129 key = `${data.id}`;
121130 } else if (data._id != null) {
122131 key = `${data._id}`;
123132 }
124133125125- return key ? `${data.__typename}:${key}` : null;
134134+ const typename = data.__typename;
135135+ const globalID =
136136+ this.globalIDs === true ||
137137+ (this.globalIDs && this.globalIDs.has(typename));
138138+ return globalID || !key ? key : `${typename}:${key}`;
126139 }
127140128141 resolve(entity: Entity, field: string, args?: FieldArgs): DataField {
+14
exchanges/graphcache/src/types.ts
···581581 * the full keys docs.
582582 */
583583 keys?: KeyingConfig;
584584+ /** Enables global IDs for keying GraphQL types.
585585+ *
586586+ * @remarks
587587+ * When `globalIDs` are enabled, GraphQL object type names will not contribute
588588+ * to the keys of entities and instead only their ID fields (or `keys` return
589589+ * values will be used.
590590+ *
591591+ * This is useful to overlap types of differing typenames. While this isn’t recommended
592592+ * it can be necessary to represent more complex interface relationships.
593593+ *
594594+ * If this should only be applied to a limited set of type names, a list of
595595+ * type names may be passed instead.
596596+ */
597597+ globalIDs?: string[] | boolean;
584598 /** Configures Graphcache with Schema Introspection data.
585599 *
586600 * @remarks