@pothos/selection-mapper 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/README.md +16 -6
  3. package/dts/deep-equal.d.ts +4 -0
  4. package/dts/deep-equal.d.ts.map +1 -1
  5. package/dts/errors.d.ts +7 -0
  6. package/dts/errors.d.ts.map +1 -0
  7. package/dts/index.d.ts +3 -2
  8. package/dts/index.d.ts.map +1 -1
  9. package/dts/loader-map.d.ts +37 -16
  10. package/dts/loader-map.d.ts.map +1 -1
  11. package/dts/matches.d.ts +5 -0
  12. package/dts/matches.d.ts.map +1 -1
  13. package/dts/parent-types.d.ts +9 -0
  14. package/dts/parent-types.d.ts.map +1 -0
  15. package/dts/plan.d.ts +36 -20
  16. package/dts/plan.d.ts.map +1 -1
  17. package/dts/shape.d.ts +8 -0
  18. package/dts/shape.d.ts.map +1 -0
  19. package/dts/types.d.ts +21 -1
  20. package/dts/types.d.ts.map +1 -1
  21. package/dts/walk.d.ts +20 -2
  22. package/dts/walk.d.ts.map +1 -1
  23. package/esm/deep-equal.d.ts +4 -0
  24. package/esm/deep-equal.d.ts.map +1 -1
  25. package/esm/deep-equal.js +60 -28
  26. package/esm/deep-equal.js.map +1 -1
  27. package/esm/errors.d.ts +7 -0
  28. package/esm/errors.d.ts.map +1 -0
  29. package/esm/errors.js +11 -0
  30. package/esm/errors.js.map +1 -0
  31. package/esm/index.d.ts +3 -2
  32. package/esm/index.d.ts.map +1 -1
  33. package/esm/index.js +2 -1
  34. package/esm/index.js.map +1 -1
  35. package/esm/loader-map.d.ts +37 -16
  36. package/esm/loader-map.d.ts.map +1 -1
  37. package/esm/loader-map.js +165 -99
  38. package/esm/loader-map.js.map +1 -1
  39. package/esm/matches.d.ts +5 -0
  40. package/esm/matches.d.ts.map +1 -1
  41. package/esm/matches.js +11 -3
  42. package/esm/matches.js.map +1 -1
  43. package/esm/parent-types.d.ts +9 -0
  44. package/esm/parent-types.d.ts.map +1 -0
  45. package/esm/parent-types.js +24 -0
  46. package/esm/parent-types.js.map +1 -0
  47. package/esm/plan.d.ts +36 -20
  48. package/esm/plan.d.ts.map +1 -1
  49. package/esm/plan.js +107 -71
  50. package/esm/plan.js.map +1 -1
  51. package/esm/shape.d.ts +8 -0
  52. package/esm/shape.d.ts.map +1 -0
  53. package/esm/shape.js +94 -0
  54. package/esm/shape.js.map +1 -0
  55. package/esm/types.d.ts +21 -1
  56. package/esm/types.d.ts.map +1 -1
  57. package/esm/types.js.map +1 -1
  58. package/esm/walk.d.ts +20 -2
  59. package/esm/walk.d.ts.map +1 -1
  60. package/esm/walk.js +199 -71
  61. package/esm/walk.js.map +1 -1
  62. package/lib/deep-equal.js +60 -28
  63. package/lib/deep-equal.js.map +1 -1
  64. package/lib/errors.js +18 -0
  65. package/lib/errors.js.map +1 -0
  66. package/lib/index.js +13 -3
  67. package/lib/index.js.map +1 -1
  68. package/lib/loader-map.js +145 -90
  69. package/lib/loader-map.js.map +1 -1
  70. package/lib/matches.js +14 -3
  71. package/lib/matches.js.map +1 -1
  72. package/lib/parent-types.js +29 -0
  73. package/lib/parent-types.js.map +1 -0
  74. package/lib/plan.js +107 -71
  75. package/lib/plan.js.map +1 -1
  76. package/lib/shape.js +109 -0
  77. package/lib/shape.js.map +1 -0
  78. package/lib/walk.js +186 -68
  79. package/lib/walk.js.map +1 -1
  80. package/package.json +4 -3
  81. package/src/deep-equal.ts +77 -34
  82. package/src/errors.ts +12 -0
  83. package/src/index.ts +6 -1
  84. package/src/loader-map.ts +235 -125
  85. package/src/matches.ts +15 -3
  86. package/src/parent-types.ts +33 -0
  87. package/src/plan.ts +145 -78
  88. package/src/shape.ts +126 -0
  89. package/src/types.ts +25 -1
  90. package/src/walk.ts +250 -83
package/src/index.ts CHANGED
@@ -7,9 +7,12 @@ export { Adapter } from './adapter.js';
7
7
  export { deepEqual } from './deep-equal.js';
8
8
  export {
9
9
  cacheKey,
10
+ fieldKey,
11
+ forExecution,
12
+ forOperation,
10
13
  getLoaderMapping,
14
+ type Mapping,
11
15
  type Mappings,
12
- setFieldMapping,
13
16
  setLoaderMappings,
14
17
  setRowFieldMapping,
15
18
  setRowMappings,
@@ -21,10 +24,12 @@ export {
21
24
  selectedFieldNames,
22
25
  } from './matches.js';
23
26
  export { type Node, NodeAdapter, type QueryVisitor } from './node.js';
27
+ export { mappingParentTypes } from './parent-types.js';
24
28
  export { Plan, type PlayedPlan } from './plan.js';
25
29
  export type {
26
30
  MergeOptions,
27
31
  NestedSelection,
32
+ ParentRowOptions,
28
33
  Position,
29
34
  SelectedFieldNode,
30
35
  SelectFn,
package/src/loader-map.ts CHANGED
@@ -23,46 +23,185 @@ export type Mappings = Record<string, Mapping>;
23
23
  /** The mapping of a static selection: nothing can ever be recorded beneath one. */
24
24
  export const EMPTY_MAPPING: Mapping = Object.freeze({ nested: Object.freeze({}) as Mappings });
25
25
 
26
+ /** Unions already made. A mapping is not changed once recorded, so one pair has one answer. */
27
+ const unions = new WeakMap<Mapping, WeakMap<Mapping, Mapping>>();
28
+
26
29
  /**
27
30
  * Adopts the first mapping recorded for a key; a later one deep-unions into a copy, so a recorded
28
31
  * mapping is never changed after the fact (two plays may accept a different subset of the fields).
32
+ *
33
+ * Invocations of one field share what is beneath them (see `Plan.nested`), so a mapping is a DAG
34
+ * whose keys can all lead to one mapping. A pair is unioned once however many keys lead to it, or
35
+ * the union would take as long as the DAG written out as a tree.
29
36
  */
30
37
  export function unionMappings(into: Mapping | undefined, from: Mapping): Mapping {
31
38
  if (!into || into === EMPTY_MAPPING) {
32
39
  return from;
33
40
  }
34
41
 
42
+ if (into === from || from === EMPTY_MAPPING) {
43
+ return into;
44
+ }
45
+
46
+ let byFrom = unions.get(into);
47
+
48
+ if (!byFrom) {
49
+ byFrom = new WeakMap();
50
+ unions.set(into, byFrom);
51
+ }
52
+
53
+ const known = byFrom.get(from);
54
+
55
+ if (known) {
56
+ return known;
57
+ }
58
+
35
59
  const nested: Mappings = { ...into.nested };
36
60
 
37
61
  for (const key of Object.keys(from.nested)) {
38
62
  nested[key] = unionMappings(nested[key], from.nested[key]);
39
63
  }
40
64
 
41
- return into.position === undefined ? { nested } : { nested, position: into.position };
65
+ const union = into.position === undefined ? { nested } : { nested, position: into.position };
66
+
67
+ byFrom.set(from, union);
68
+
69
+ return union;
42
70
  }
43
71
 
44
72
  /**
45
- * Per request context: the mappings recorded so far, and the keys they were recorded under, by
46
- * the path prefix that rehomed them. `responsePath` drops list indices, so every row of a list
47
- * rebuilds the same key strings; a prefix builds them once.
73
+ * Per execution: the mappings recorded so far.
48
74
  *
49
- * `mappings` holds what a plan recorded for a whole field: every row of that field was loaded by
50
- * it, so one mapping per path answers for all of them. `rows` holds a mapping that answers for
51
- * one row only — because two plans disagreed about a key, or because the plan loaded that row
52
- * alone. A mapping carries the position a connection pages with, so a row answering from another
53
- * row's mapping would page with arguments its own data was never fetched with. `rowScoped` stays
54
- * false while nothing has been recorded there, so a request whose rows were all loaded the same
55
- * way pays nothing for the row tier.
75
+ * `scopes` holds what a plan recorded beneath a field execution: everything resolved beneath it was
76
+ * loaded by that plan, so it answers for all of it, and for nothing else. `rows` holds a mapping
77
+ * from a plan that loaded one row alone, and answers for that row only. A mapping carries the
78
+ * position a connection pages with, so a row answering from another row's mapping would page with
79
+ * arguments its own data was never fetched with. `rowScoped` stays false while nothing has been
80
+ * recorded there, so a request whose rows were all loaded the same way pays nothing for the row
81
+ * tier.
82
+ *
83
+ * `scopes` and `rows` are keyed by a field execution's place (see `placeOf`), which tells sibling
84
+ * rows apart, in weak maps: a `Map` holding every path link of a request tripled the time spent
85
+ * collecting garbage.
86
+ */
87
+ interface Cache {
88
+ places: Map<string, object>;
89
+ rows: WeakMap<object, WeakMap<object, Map<string, Mapping>>>;
90
+ rowScoped: boolean;
91
+ scopes: WeakMap<object, Mappings>;
92
+ }
93
+
94
+ /** Per context: the cache of each execution, by `executionOf`. */
95
+ const contextCaches = createContextCache(() => new WeakMap<object, Cache>());
96
+
97
+ /**
98
+ * An object the executor builds once per execution, and once per event of a subscription, and
99
+ * hands to every resolver in it, @defer and @stream payloads included:
100
+ *
101
+ * - graphql-js 17: the `getAbortSignal` of the execution.
102
+ * - graphql-js 16 and @graphql-tools/executor (Yoga): the root link of the path. They build one
103
+ * path chain per execution or event, linking each field to its parent's, so every resolver
104
+ * beneath a root field shares its link, and the `subscribe` resolver has its own. Their fields'
105
+ * links carry a `typename`. A root field doesn't share its link with the others, so each root
106
+ * field has a cache of its own, which is all it reads.
107
+ * - graphql-jit: the coerced `variableValues`. It builds a new path, without `typename`, for every
108
+ * resolver, but coerces the variables afresh for every execution and event.
109
+ */
110
+ function executionOf(info: GraphQLResolveInfo): object {
111
+ const signal = (info as { getAbortSignal?: object }).getAbortSignal;
112
+
113
+ if (signal) {
114
+ return signal;
115
+ }
116
+
117
+ let { path } = info;
118
+
119
+ if (path.typename === undefined) {
120
+ return info.variableValues;
121
+ }
122
+
123
+ while (path.prev) {
124
+ path = path.prev;
125
+ }
126
+
127
+ return path;
128
+ }
129
+
130
+ function cache(ctx: object, info: GraphQLResolveInfo): Cache {
131
+ const executions = contextCaches(ctx);
132
+ const execution = executionOf(info);
133
+ let cached = executions.get(execution);
134
+
135
+ if (!cached) {
136
+ cached = {
137
+ places: new Map(),
138
+ rows: new WeakMap(),
139
+ rowScoped: false,
140
+ scopes: new WeakMap(),
141
+ };
142
+ executions.set(execution, cached);
143
+ }
144
+
145
+ return cached;
146
+ }
147
+
148
+ /**
149
+ * Where `path`, a link of `info.path`, sits in its execution: the link itself, where the executor
150
+ * builds one per field execution and shares it with the resolvers beneath, which a field's link
151
+ * carrying a `typename` tells. Where it builds a new path for every resolver (graphql-jit), an
152
+ * object standing for the path's keys, list indices included.
56
153
  */
57
- const cache = createContextCache(() => ({
58
- mappings: new Map<string, Mapping>(),
59
- rehomed: new Map<string, Map<string, string>>(),
60
- rows: new WeakMap<object, WeakMap<object, Map<string, Mapping>>>(),
61
- rowScoped: false,
62
- scopes: new WeakMap<object, Mappings>(),
63
- }));
154
+ function placeOf(cached: Cache, info: GraphQLResolveInfo, path: GraphQLResolveInfo['path']) {
155
+ if (info.path.typename !== undefined) {
156
+ return path;
157
+ }
64
158
 
65
- type Cache = ReturnType<typeof cache>;
159
+ const at = executionPath(path);
160
+ let place = cached.places.get(at);
161
+
162
+ if (!place) {
163
+ place = {};
164
+ cached.places.set(at, place);
165
+ }
166
+
167
+ return place;
168
+ }
169
+
170
+ /**
171
+ * The entries of `cache` for the operation `info` is in. Keyed on its `variableValues`, which
172
+ * every executor coerces afresh for each operation, so operations sharing a context never share
173
+ * an entry. The events of a subscription may share one, so a plan cached here runs its `select`
174
+ * and `query` callbacks once for all of them. Use `forExecution` if the callbacks read per-event
175
+ * state.
176
+ */
177
+ export function forOperation<T>(
178
+ cache: WeakMap<object, Map<string, T>>,
179
+ info: GraphQLResolveInfo,
180
+ ): Map<string, T> {
181
+ return entriesFor(cache, info.variableValues);
182
+ }
183
+
184
+ /**
185
+ * The entries of `cache` for the execution `info` is in. Each event of a subscription gets its own
186
+ * entries, and `@defer` and `@stream` payloads share the entries of their execution.
187
+ */
188
+ export function forExecution<T>(
189
+ cache: WeakMap<object, Map<string, T>>,
190
+ info: GraphQLResolveInfo,
191
+ ): Map<string, T> {
192
+ return entriesFor(cache, executionOf(info));
193
+ }
194
+
195
+ function entriesFor<T>(cache: WeakMap<object, Map<string, T>>, key: object): Map<string, T> {
196
+ let entries = cache.get(key);
197
+
198
+ if (!entries) {
199
+ entries = new Map();
200
+ cache.set(key, entries);
201
+ }
202
+
203
+ return entries;
204
+ }
66
205
 
67
206
  /**
68
207
  * Memoised per path link, which is created once per field per row and never mutated. graphql-js
@@ -90,38 +229,55 @@ export function responsePath(path: GraphQLResolveInfo['path'] | undefined): stri
90
229
  return key;
91
230
  }
92
231
 
93
- export function cacheKey(type: string, path: GraphQLResolveInfo['path']) {
94
- return `${type}@${responsePath(path)}`;
95
- }
96
-
97
232
  /**
98
- * The keys `mappings` is recorded under when it is rehomed beneath `prefix`. Built once per
99
- * prefix and shared by both tiers: the strings depend on the path and the plan's own keys, never
100
- * on which row is being recorded for.
233
+ * Memoised per path link, as `pathKeys` is. Built only where paths are rebuilt for every resolver,
234
+ * so it saves the walk over a resolver's ancestors from building each prefix again.
101
235
  */
102
- function rehomedKeys(cached: Cache, prefix: string) {
103
- let keys = cached.rehomed.get(prefix);
236
+ const executionPaths = new WeakMap<object, string>();
237
+
238
+ /** Every key of a response path, list indices included, joined by `.`. */
239
+ function executionPath(path: GraphQLResolveInfo['path']): string {
240
+ let key = executionPaths.get(path);
104
241
 
105
- if (!keys) {
106
- keys = new Map();
107
- cached.rehomed.set(prefix, keys);
242
+ if (key === undefined) {
243
+ key = path.prev ? `${executionPath(path.prev)}.${path.key}` : String(path.key);
244
+ executionPaths.set(path, key);
108
245
  }
109
246
 
110
- return keys;
247
+ return key;
248
+ }
249
+
250
+ export function cacheKey(type: string, path: GraphQLResolveInfo['path']) {
251
+ return `${type}@${responsePath(path)}`;
111
252
  }
112
253
 
113
- /** `key`, a plan's own `Type@relative.path`, moved beneath `prefix`. Built once per prefix. */
114
- function rehome(keys: Map<string, string>, key: string, prefix: string) {
115
- let under = keys.get(key);
254
+ /** A number for each field node seen, by identity. Weak, so the nodes go with their document. */
255
+ const fieldNodeIds = new WeakMap<object, number>();
256
+ let nextFieldNodeId = 0;
116
257
 
117
- if (under === undefined) {
118
- const at = key.indexOf('@');
258
+ /**
259
+ * The key to cache a field's plan under: `cacheKey(type, info.path)` plus an id for each node in
260
+ * `info.fieldNodes`. The path leaves out list indices, so the items of a list share a plan. The
261
+ * root fields of a mutation run in turn, and a callback may read what an earlier field changed.
262
+ * Their paths differ, so each gets its own plan. The nodes are part of the key because fragments on
263
+ * different types can merge different nodes into one response path, and a plan keyed on the path
264
+ * alone would be reused for a field that selects something else.
265
+ */
266
+ export function fieldKey(type: string, info: GraphQLResolveInfo): string {
267
+ let key = cacheKey(type, info.path);
268
+
269
+ for (const node of info.fieldNodes) {
270
+ let id = fieldNodeIds.get(node);
119
271
 
120
- under = `${key.slice(0, at)}@${prefix}.${key.slice(at + 1)}`;
121
- keys.set(key, under);
272
+ if (id === undefined) {
273
+ id = nextFieldNodeId++;
274
+ fieldNodeIds.set(node, id);
275
+ }
276
+
277
+ key += `:${id}`;
122
278
  }
123
279
 
124
- return under;
280
+ return key;
125
281
  }
126
282
 
127
283
  /** `row` if a mapping can be hung off it. A root field resolves with no parent value at all. */
@@ -130,105 +286,50 @@ function ownerOf(row: unknown): object | null {
130
286
  }
131
287
 
132
288
  /**
133
- * Records `mapping` at `key` for `row`. An unclaimed key takes it, and a key already holding this
134
- * very mapping needs no write, so the rows after the first write nothing. Only a key two plans
135
- * disagree about gives the row a mapping of its own — or, with no row to hang it off, nothing, so
136
- * the resolvers beneath fall back and load their own data.
289
+ * Records `mapping` for this field execution against `row` alone.
137
290
  */
138
- function claim(
139
- cached: Cache,
140
- key: string,
141
- mapping: Mapping,
142
- row: object | null,
143
- path: GraphQLResolveInfo['path'],
144
- ) {
145
- const held = cached.mappings.get(key);
146
-
147
- if (held === undefined) {
148
- cached.mappings.set(key, mapping);
149
- } else if (held !== mapping && row) {
150
- claimForRow(cached, key.slice(0, key.indexOf('@')), mapping, row, path);
151
- }
152
- }
153
-
154
- /**
155
- * Records `mapping` for this field execution against `row` alone, never in the shared tier. The caller loaded
156
- * `row` by itself, so an unclaimed key is not the caller's to take: the shared tier answers for
157
- * every row of a field, and a sibling that was never loaded would read the entry as proof that
158
- * it had been. With no row to hang it off there is nothing to record, and the resolvers beneath
159
- * fall back and load their own data.
160
- */
161
- function claimForRow(
162
- cached: Cache,
163
- type: string,
164
- mapping: Mapping,
165
- row: object,
166
- path: GraphQLResolveInfo['path'],
167
- ) {
168
- let byPath = cached.rows.get(row);
169
- if (!byPath) {
170
- byPath = new WeakMap();
171
- cached.rows.set(row, byPath);
291
+ function claimForRow(cached: Cache, type: string, mapping: Mapping, row: object, place: object) {
292
+ let byPlace = cached.rows.get(row);
293
+ if (!byPlace) {
294
+ byPlace = new WeakMap();
295
+ cached.rows.set(row, byPlace);
172
296
  cached.rowScoped = true;
173
297
  }
174
- let own = byPath.get(path);
298
+ let own = byPlace.get(place);
175
299
  if (!own) {
176
300
  own = new Map();
177
- byPath.set(path, own);
301
+ byPlace.set(place, own);
178
302
  }
179
303
  own.set(type, mapping);
180
304
  }
181
305
 
182
306
  /**
183
307
  * Records the mappings of a plan rooted at the field `info` resolves, under that field's path.
184
- * The plan loaded the whole field, so its mappings answer for every row of it: they go to the
185
- * shared tier. Multiple plans can contribute to one field (Relay nodes of different model
308
+ * The plan loaded the whole field, so its mappings are used for everything resolved beneath this
309
+ * execution of it. Multiple plans can contribute to one field (Relay nodes of different model
186
310
  * types share the list's path), so later contributions replace matching keys and retain others.
187
311
  */
188
312
  export function setLoaderMappings(ctx: object, info: GraphQLResolveInfo, mappings: Mappings) {
189
- const cached = cache(ctx);
190
- const prefix = responsePath(info.path);
191
- const keys = rehomedKeys(cached, prefix);
192
- const previous = cached.scopes.get(info.path);
193
- cached.scopes.set(info.path, previous ? { ...previous, ...mappings } : mappings);
194
-
195
- for (const key of Object.keys(mappings)) {
196
- cached.mappings.set(rehome(keys, key, prefix), mappings[key]);
197
- }
313
+ const cached = cache(ctx, info);
314
+ const place = placeOf(cached, info, info.path);
315
+ const previous = cached.scopes.get(place);
316
+ cached.scopes.set(place, previous ? { ...previous, ...mappings } : mappings);
198
317
  }
199
318
 
200
319
  /**
201
320
  * Records a row's mappings and scopes its descendants to this concrete field execution. A
202
321
  * descendant's parent is a different object, so row identity alone cannot carry its plan through
203
- * a relation, connection wrapper, or list. The execution path retains indices to isolate siblings.
322
+ * a relation, connection wrapper, or list. The place keeps list indices to isolate siblings.
204
323
  */
205
324
  export function setRowMappings(ctx: object, info: GraphQLResolveInfo, mappings: Mappings) {
206
- cache(ctx).scopes.set(info.path, mappings);
207
- }
325
+ const cached = cache(ctx, info);
208
326
 
209
- /**
210
- * Records the mapping of the field `info` resolves (under its own parent type) along with the
211
- * mappings beneath it, so both the field's resolver and the resolvers below it can look
212
- * themselves up. `row` is the value the field is about to resolve with: the parent row when it
213
- * was loaded by the planned query, and the reloaded row when the fallback loader fetched it.
214
- */
215
- export function setFieldMapping(
216
- ctx: object,
217
- info: GraphQLResolveInfo,
218
- mapping: Mapping,
219
- row: unknown,
220
- ) {
221
- const cached = cache(ctx);
222
- const owner = ownerOf(row);
223
-
224
- claim(cached, cacheKey(info.parentType.name, info.path), mapping, owner, info.path);
225
- cached.scopes.set(info.path, mapping.nested);
327
+ cached.scopes.set(placeOf(cached, info, info.path), mappings);
226
328
  }
227
329
 
228
330
  /**
229
- * `setFieldMapping` for a plan that loaded `row` alone: the field's own mapping is recorded
230
- * against `row` rather than claimed for the whole field, so a sibling row the plan never loaded
231
- * keeps falling back instead of reading the entry and resolving against data it does not carry.
331
+ * Records a mapping for `row` only, for the field `info` resolves. Use it when the plan loaded
332
+ * `row` alone. The field's other rows don't use this mapping.
232
333
  *
233
334
  * Descendants inherit the mapping through this field's execution scope, even when their parent
234
335
  * is a child object or a connection wrapper rather than the row that was reloaded.
@@ -239,32 +340,41 @@ export function setRowFieldMapping(
239
340
  mapping: Mapping,
240
341
  row: unknown,
241
342
  ) {
242
- const cached = cache(ctx);
343
+ const cached = cache(ctx, info);
243
344
  const owner = ownerOf(row);
345
+ const place = placeOf(cached, info, info.path);
244
346
 
245
347
  if (owner) {
246
- claimForRow(cached, info.parentType.name, mapping, owner, info.path);
348
+ claimForRow(cached, info.parentType.name, mapping, owner, place);
247
349
  }
248
350
 
249
- cached.scopes.set(info.path, mapping.nested);
351
+ cached.scopes.set(place, mapping.nested);
250
352
  }
251
353
 
252
354
  /**
253
- * The mapping recorded for the field at `path` on `row`, preferring one recorded for that row
254
- * over the plan's. `row` is the value whose data the mapping describes — the resolver's parent.
355
+ * The mapping recorded for the field `info` resolves on `row`, preferring one recorded for that
356
+ * row over the plan's. `row` is the value whose data the mapping describes — the resolver's
357
+ * parent. The plan's mapping comes from the nearest field execution above this one that recorded
358
+ * mappings. Returns `null` if there is none or it has no mapping for this field, even if a mapping
359
+ * was recorded for another row at the same path.
255
360
  */
256
361
  export function getLoaderMapping(
257
362
  ctx: object,
258
- path: GraphQLResolveInfo['path'],
363
+ info: GraphQLResolveInfo,
259
364
  type: string,
260
365
  row?: unknown,
261
366
  ): Mapping | null {
262
- const cached = cache(ctx);
263
- const key = cacheKey(type, path);
367
+ const { path } = info;
368
+ const cached = cache(ctx, info);
264
369
 
265
370
  if (cached.rowScoped) {
266
371
  const owner = ownerOf(row);
267
- const own = owner && cached.rows.get(owner)?.get(path)?.get(type);
372
+ const own =
373
+ owner &&
374
+ cached.rows
375
+ .get(owner)
376
+ ?.get(placeOf(cached, info, path))
377
+ ?.get(type);
268
378
 
269
379
  if (own) {
270
380
  return own;
@@ -272,7 +382,7 @@ export function getLoaderMapping(
272
382
  }
273
383
 
274
384
  for (let ancestor = path.prev; ancestor; ancestor = ancestor.prev) {
275
- const scope = cached.scopes.get(ancestor);
385
+ const scope = cached.scopes.get(placeOf(cached, info, ancestor));
276
386
 
277
387
  if (scope) {
278
388
  // A different plan may have omitted this field. Do not treat a sibling's shared mapping
@@ -283,5 +393,5 @@ export function getLoaderMapping(
283
393
  }
284
394
  }
285
395
 
286
- return cached.mappings.get(key) ?? null;
396
+ return null;
287
397
  }
package/src/matches.ts CHANGED
@@ -36,6 +36,18 @@ export interface IndirectInclude {
36
36
  paths?: IndirectPathSegment[][];
37
37
  }
38
38
 
39
+ /** The paths an include follows: `paths` when it has any, else `path`, else none. */
40
+ export function includePaths<Segment>(include: {
41
+ path?: Segment[];
42
+ paths?: Segment[][];
43
+ }): Segment[][] | undefined {
44
+ if (include.paths?.length) {
45
+ return include.paths;
46
+ }
47
+
48
+ return include.path?.length ? [include.path] : undefined;
49
+ }
50
+
39
51
  export interface Match {
40
52
  /** The GraphQL type of the matched field's return type */
41
53
  type: GraphQLNamedType;
@@ -148,10 +160,10 @@ export type FieldVisitor = (field: FieldNode, level: SelectionLevel) => boolean
148
160
  * `segment` is the indirect-include path segment the caller is looking for, when it has one; only
149
161
  * its `type` is read, to pin the type a fragment is expected on.
150
162
  *
151
- * `memo` records the named fragments already expanded, keyed on the fragment, the two types, the
163
+ * `memo` records the named fragments already visited, keyed on the fragment, the two types, the
152
164
  * deferred flag, and `memoKey` — whatever else the caller varies as it descends (`matchPath`
153
165
  * passes its remaining path length and its alias path). A fragment spread more than once at one
154
- * point of the traversal is expanded once: a valid fragment DAG can spread the same fragment at
166
+ * point of the traversal is visited once: a valid fragment DAG can spread the same fragment at
155
167
  * every level, which makes expanding every spread exponential in its depth. A caller that varies
156
168
  * nothing passes no `memoKey`, leaving its memo finer than it needs to be by the deferred flag —
157
169
  * a little time, never correctness, for a caller that accumulates.
@@ -338,7 +350,7 @@ function matchPath(
338
350
  * Follows each of `paths`, prefixed by any type-level `prefix`, from `selection` (which is expected
339
351
  * to be a selection on `type`), reporting the fields at their ends to `onMatch` in path order then
340
352
  * document order, and stopping when `onMatch` says to. Paths are followed through fragments; see
341
- * `resolveFragmentTypes` for the rules. Each path gets its own memo: what one path expanded says
353
+ * `resolveFragmentTypes` for the rules. Each path gets its own memo: what one path visited says
342
354
  * nothing about the next.
343
355
  */
344
356
  function eachMatch(
@@ -0,0 +1,33 @@
1
+ import type { BuildCache, PothosOutputFieldConfig, SchemaTypes } from '@pothos/core';
2
+
3
+ /**
4
+ * The types whose loader mappings answer for a field: its parent type, and each interface of it
5
+ * that has a model and declares the same field. Pothos builds an inherited field from the
6
+ * interface's options, so an implementation that redefines the field (other relation arguments,
7
+ * say) does not read what the interface's field loaded.
8
+ */
9
+ export function mappingParentTypes<Types extends SchemaTypes>(
10
+ buildCache: BuildCache<Types>,
11
+ fieldConfig: PothosOutputFieldConfig<Types>,
12
+ modelExtension: string,
13
+ ): Set<string> {
14
+ const parentTypes = new Set([fieldConfig.parentType]);
15
+ const parentConfig = buildCache.getTypeConfig(fieldConfig.parentType);
16
+
17
+ if (parentConfig.kind !== 'Interface' && parentConfig.kind !== 'Object') {
18
+ return parentTypes;
19
+ }
20
+
21
+ for (const iface of parentConfig.interfaces) {
22
+ const { name, extensions } = buildCache.getTypeConfig(iface, 'Interface');
23
+ const ifaceField = buildCache.builder.configStore
24
+ .getFields(name, 'Interface')
25
+ .get(fieldConfig.name);
26
+
27
+ if (extensions?.[modelExtension] && ifaceField?.pothosOptions === fieldConfig.pothosOptions) {
28
+ parentTypes.add(name);
29
+ }
30
+ }
31
+
32
+ return parentTypes;
33
+ }