@pothos/selection-mapper 0.1.0 → 0.2.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.
- package/CHANGELOG.md +24 -0
- package/README.md +17 -6
- package/dts/deep-equal.d.ts +4 -0
- package/dts/deep-equal.d.ts.map +1 -1
- package/dts/errors.d.ts +7 -0
- package/dts/errors.d.ts.map +1 -0
- package/dts/index.d.ts +3 -2
- package/dts/index.d.ts.map +1 -1
- package/dts/loader-map.d.ts +36 -8
- package/dts/loader-map.d.ts.map +1 -1
- package/dts/matches.d.ts +5 -0
- package/dts/matches.d.ts.map +1 -1
- package/dts/parent-types.d.ts +9 -0
- package/dts/parent-types.d.ts.map +1 -0
- package/dts/plan.d.ts +36 -20
- package/dts/plan.d.ts.map +1 -1
- package/dts/shape.d.ts +8 -0
- package/dts/shape.d.ts.map +1 -0
- package/dts/types.d.ts +21 -1
- package/dts/types.d.ts.map +1 -1
- package/dts/walk.d.ts +20 -2
- package/dts/walk.d.ts.map +1 -1
- package/esm/deep-equal.d.ts +4 -0
- package/esm/deep-equal.d.ts.map +1 -1
- package/esm/deep-equal.js +60 -28
- package/esm/deep-equal.js.map +1 -1
- package/esm/errors.d.ts +7 -0
- package/esm/errors.d.ts.map +1 -0
- package/esm/errors.js +11 -0
- package/esm/errors.js.map +1 -0
- package/esm/index.d.ts +3 -2
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +2 -1
- package/esm/index.js.map +1 -1
- package/esm/loader-map.d.ts +36 -8
- package/esm/loader-map.d.ts.map +1 -1
- package/esm/loader-map.js +177 -60
- package/esm/loader-map.js.map +1 -1
- package/esm/matches.d.ts +5 -0
- package/esm/matches.d.ts.map +1 -1
- package/esm/matches.js +11 -3
- package/esm/matches.js.map +1 -1
- package/esm/parent-types.d.ts +9 -0
- package/esm/parent-types.d.ts.map +1 -0
- package/esm/parent-types.js +24 -0
- package/esm/parent-types.js.map +1 -0
- package/esm/plan.d.ts +36 -20
- package/esm/plan.d.ts.map +1 -1
- package/esm/plan.js +107 -71
- package/esm/plan.js.map +1 -1
- package/esm/shape.d.ts +8 -0
- package/esm/shape.d.ts.map +1 -0
- package/esm/shape.js +94 -0
- package/esm/shape.js.map +1 -0
- package/esm/types.d.ts +21 -1
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js.map +1 -1
- package/esm/walk.d.ts +20 -2
- package/esm/walk.d.ts.map +1 -1
- package/esm/walk.js +199 -71
- package/esm/walk.js.map +1 -1
- package/lib/deep-equal.js +60 -28
- package/lib/deep-equal.js.map +1 -1
- package/lib/errors.js +18 -0
- package/lib/errors.js.map +1 -0
- package/lib/index.js +13 -0
- package/lib/index.js.map +1 -1
- package/lib/loader-map.js +157 -53
- package/lib/loader-map.js.map +1 -1
- package/lib/matches.js +14 -3
- package/lib/matches.js.map +1 -1
- package/lib/parent-types.js +29 -0
- package/lib/parent-types.js.map +1 -0
- package/lib/plan.js +107 -71
- package/lib/plan.js.map +1 -1
- package/lib/shape.js +109 -0
- package/lib/shape.js.map +1 -0
- package/lib/walk.js +186 -68
- package/lib/walk.js.map +1 -1
- package/package.json +4 -3
- package/src/deep-equal.ts +77 -34
- package/src/errors.ts +12 -0
- package/src/index.ts +6 -0
- package/src/loader-map.ts +261 -71
- package/src/matches.ts +15 -3
- package/src/parent-types.ts +33 -0
- package/src/plan.ts +145 -78
- package/src/shape.ts +126 -0
- package/src/types.ts +25 -1
- package/src/walk.ts +250 -83
package/src/index.ts
CHANGED
|
@@ -7,7 +7,11 @@ 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
16
|
setFieldMapping,
|
|
13
17
|
setLoaderMappings,
|
|
@@ -21,10 +25,12 @@ export {
|
|
|
21
25
|
selectedFieldNames,
|
|
22
26
|
} from './matches.js';
|
|
23
27
|
export { type Node, NodeAdapter, type QueryVisitor } from './node.js';
|
|
28
|
+
export { mappingParentTypes } from './parent-types.js';
|
|
24
29
|
export { Plan, type PlayedPlan } from './plan.js';
|
|
25
30
|
export type {
|
|
26
31
|
MergeOptions,
|
|
27
32
|
NestedSelection,
|
|
33
|
+
ParentRowOptions,
|
|
28
34
|
Position,
|
|
29
35
|
SelectedFieldNode,
|
|
30
36
|
SelectFn,
|
package/src/loader-map.ts
CHANGED
|
@@ -23,46 +23,195 @@ 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
|
-
|
|
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
|
|
73
|
+
* Per execution: the mappings recorded so far, and the keys they were recorded under, by
|
|
46
74
|
* the path prefix that rehomed them. `responsePath` drops list indices, so every row of a list
|
|
47
75
|
* rebuilds the same key strings; a prefix builds them once.
|
|
48
76
|
*
|
|
49
|
-
* `
|
|
50
|
-
*
|
|
51
|
-
* one row only — because two plans disagreed about a key, or because the
|
|
52
|
-
* alone. A mapping carries the position a connection pages with, so a row
|
|
53
|
-
* row's mapping would page with arguments its own data was never fetched
|
|
54
|
-
* false while nothing has been recorded there, so a request whose rows
|
|
55
|
-
* way pays nothing for the row tier.
|
|
77
|
+
* `scopes` holds what a plan recorded beneath a field execution: everything resolved beneath it
|
|
78
|
+
* was loaded by that plan, so it answers for all of it, and for nothing else. `rows` holds a
|
|
79
|
+
* mapping that answers for one row only — because two plans disagreed about a key, or because the
|
|
80
|
+
* plan loaded that row alone. A mapping carries the position a connection pages with, so a row
|
|
81
|
+
* answering from another row's mapping would page with arguments its own data was never fetched
|
|
82
|
+
* with. `rowScoped` stays false while nothing has been recorded there, so a request whose rows
|
|
83
|
+
* were all loaded the same way pays nothing for the row tier.
|
|
84
|
+
*
|
|
85
|
+
* `mappings` holds a mapping recorded for each path, list indices dropped. It tells whether
|
|
86
|
+
* the plans of a path's rows agree (see `claim`), and never answers a lookup: a row at the same
|
|
87
|
+
* path that no plan loaded would read it as proof that one had.
|
|
88
|
+
*
|
|
89
|
+
* `scopes` and `rows` are keyed by a field execution's place (see `placeOf`), which tells sibling
|
|
90
|
+
* rows apart, in weak maps: a `Map` holding every path link of a request tripled the time spent
|
|
91
|
+
* collecting garbage.
|
|
92
|
+
*/
|
|
93
|
+
interface Cache {
|
|
94
|
+
mappings: Map<string, Mapping>;
|
|
95
|
+
places: Map<string, object>;
|
|
96
|
+
rehomed: Map<string, Map<string, string>>;
|
|
97
|
+
rows: WeakMap<object, WeakMap<object, Map<string, Mapping>>>;
|
|
98
|
+
rowScoped: boolean;
|
|
99
|
+
scopes: WeakMap<object, Mappings>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Per context: the cache of each execution, by `executionOf`. */
|
|
103
|
+
const contextCaches = createContextCache(() => new WeakMap<object, Cache>());
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* An object the executor builds once per execution, and once per event of a subscription, and
|
|
107
|
+
* hands to every resolver in it, @defer and @stream payloads included:
|
|
108
|
+
*
|
|
109
|
+
* - graphql-js 17: the `getAbortSignal` of the execution.
|
|
110
|
+
* - graphql-js 16 and @graphql-tools/executor (Yoga): the root link of the path. They build one
|
|
111
|
+
* path chain per execution or event, linking each field to its parent's, so every resolver
|
|
112
|
+
* beneath a root field shares its link, and the `subscribe` resolver has its own. Their fields'
|
|
113
|
+
* links carry a `typename`. A root field doesn't share its link with the others, so each root
|
|
114
|
+
* field has a cache of its own, which is all it reads.
|
|
115
|
+
* - graphql-jit: the coerced `variableValues`. It builds a new path, without `typename`, for every
|
|
116
|
+
* resolver, but coerces the variables afresh for every execution and event.
|
|
117
|
+
*/
|
|
118
|
+
function executionOf(info: GraphQLResolveInfo): object {
|
|
119
|
+
const signal = (info as { getAbortSignal?: object }).getAbortSignal;
|
|
120
|
+
|
|
121
|
+
if (signal) {
|
|
122
|
+
return signal;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let { path } = info;
|
|
126
|
+
|
|
127
|
+
if (path.typename === undefined) {
|
|
128
|
+
return info.variableValues;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
while (path.prev) {
|
|
132
|
+
path = path.prev;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return path;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function cache(ctx: object, info: GraphQLResolveInfo): Cache {
|
|
139
|
+
const executions = contextCaches(ctx);
|
|
140
|
+
const execution = executionOf(info);
|
|
141
|
+
let cached = executions.get(execution);
|
|
142
|
+
|
|
143
|
+
if (!cached) {
|
|
144
|
+
cached = {
|
|
145
|
+
mappings: new Map(),
|
|
146
|
+
places: new Map(),
|
|
147
|
+
rehomed: new Map(),
|
|
148
|
+
rows: new WeakMap(),
|
|
149
|
+
rowScoped: false,
|
|
150
|
+
scopes: new WeakMap(),
|
|
151
|
+
};
|
|
152
|
+
executions.set(execution, cached);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return cached;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Where `path`, a link of `info.path`, sits in its execution: the link itself, where the executor
|
|
160
|
+
* builds one per field execution and shares it with the resolvers beneath, which a field's link
|
|
161
|
+
* carrying a `typename` tells. Where it builds a new path for every resolver (graphql-jit), an
|
|
162
|
+
* object standing for the path's keys, list indices included.
|
|
163
|
+
*/
|
|
164
|
+
function placeOf(cached: Cache, info: GraphQLResolveInfo, path: GraphQLResolveInfo['path']) {
|
|
165
|
+
if (info.path.typename !== undefined) {
|
|
166
|
+
return path;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const at = executionPath(path);
|
|
170
|
+
let place = cached.places.get(at);
|
|
171
|
+
|
|
172
|
+
if (!place) {
|
|
173
|
+
place = {};
|
|
174
|
+
cached.places.set(at, place);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return place;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The entries of `cache` for the operation `info` is in. Keyed on its `variableValues`, which
|
|
182
|
+
* every executor coerces afresh for each operation, so operations sharing a context never share
|
|
183
|
+
* an entry. The events of a subscription may share them, so a plan cached here runs its `select`
|
|
184
|
+
* and `query` callbacks once for all of them; use `forExecution` where they read per-event state.
|
|
56
185
|
*/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
186
|
+
export function forOperation<T>(
|
|
187
|
+
cache: WeakMap<object, Map<string, T>>,
|
|
188
|
+
info: GraphQLResolveInfo,
|
|
189
|
+
): Map<string, T> {
|
|
190
|
+
return entriesFor(cache, info.variableValues);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The entries of `cache` for the execution `info` is in, by `executionOf`, as mappings are kept:
|
|
195
|
+
* each event of a subscription gets its own, and @defer and @stream payloads share their
|
|
196
|
+
* execution's.
|
|
197
|
+
*/
|
|
198
|
+
export function forExecution<T>(
|
|
199
|
+
cache: WeakMap<object, Map<string, T>>,
|
|
200
|
+
info: GraphQLResolveInfo,
|
|
201
|
+
): Map<string, T> {
|
|
202
|
+
return entriesFor(cache, executionOf(info));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function entriesFor<T>(cache: WeakMap<object, Map<string, T>>, key: object): Map<string, T> {
|
|
206
|
+
let entries = cache.get(key);
|
|
207
|
+
|
|
208
|
+
if (!entries) {
|
|
209
|
+
entries = new Map();
|
|
210
|
+
cache.set(key, entries);
|
|
211
|
+
}
|
|
64
212
|
|
|
65
|
-
|
|
213
|
+
return entries;
|
|
214
|
+
}
|
|
66
215
|
|
|
67
216
|
/**
|
|
68
217
|
* Memoised per path link, which is created once per field per row and never mutated. graphql-js
|
|
@@ -90,14 +239,61 @@ export function responsePath(path: GraphQLResolveInfo['path'] | undefined): stri
|
|
|
90
239
|
return key;
|
|
91
240
|
}
|
|
92
241
|
|
|
242
|
+
/**
|
|
243
|
+
* Memoised per path link, as `pathKeys` is. Built only where paths are rebuilt for every resolver,
|
|
244
|
+
* so it saves the walk over a resolver's ancestors from building each prefix again.
|
|
245
|
+
*/
|
|
246
|
+
const executionPaths = new WeakMap<object, string>();
|
|
247
|
+
|
|
248
|
+
/** Every key of a response path, list indices included, joined by `.`. */
|
|
249
|
+
function executionPath(path: GraphQLResolveInfo['path']): string {
|
|
250
|
+
let key = executionPaths.get(path);
|
|
251
|
+
|
|
252
|
+
if (key === undefined) {
|
|
253
|
+
key = path.prev ? `${executionPath(path.prev)}.${path.key}` : String(path.key);
|
|
254
|
+
executionPaths.set(path, key);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return key;
|
|
258
|
+
}
|
|
259
|
+
|
|
93
260
|
export function cacheKey(type: string, path: GraphQLResolveInfo['path']) {
|
|
94
261
|
return `${type}@${responsePath(path)}`;
|
|
95
262
|
}
|
|
96
263
|
|
|
264
|
+
/** A number for each field node seen, by identity. Weak, so the nodes go with their document. */
|
|
265
|
+
const fieldNodeIds = new WeakMap<object, number>();
|
|
266
|
+
let nextFieldNodeId = 0;
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* `cacheKey(type, info.path)` and the identity of every node in `info.fieldNodes`. The path keeps
|
|
270
|
+
* each place in the response apart, list indices aside, so the root fields of a mutation, which run
|
|
271
|
+
* in turn and whose callbacks may read what an earlier one changed, never share a plan. The nodes
|
|
272
|
+
* decide what a plan loads: fragments on different types can merge different nodes into one
|
|
273
|
+
* response path, so a plan keyed on the path alone would be reused for a field asking for
|
|
274
|
+
* something else.
|
|
275
|
+
*/
|
|
276
|
+
export function fieldKey(type: string, info: GraphQLResolveInfo): string {
|
|
277
|
+
let key = cacheKey(type, info.path);
|
|
278
|
+
|
|
279
|
+
for (const node of info.fieldNodes) {
|
|
280
|
+
let id = fieldNodeIds.get(node);
|
|
281
|
+
|
|
282
|
+
if (id === undefined) {
|
|
283
|
+
id = nextFieldNodeId++;
|
|
284
|
+
fieldNodeIds.set(node, id);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
key += `:${id}`;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return key;
|
|
291
|
+
}
|
|
292
|
+
|
|
97
293
|
/**
|
|
98
294
|
* The keys `mappings` is recorded under when it is rehomed beneath `prefix`. Built once per
|
|
99
|
-
* prefix
|
|
100
|
-
*
|
|
295
|
+
* prefix: the strings depend on the path and the plan's own keys, never on which row is being
|
|
296
|
+
* recorded for.
|
|
101
297
|
*/
|
|
102
298
|
function rehomedKeys(cached: Cache, prefix: string) {
|
|
103
299
|
let keys = cached.rehomed.get(prefix);
|
|
@@ -132,65 +328,49 @@ function ownerOf(row: unknown): object | null {
|
|
|
132
328
|
/**
|
|
133
329
|
* Records `mapping` at `key` for `row`. An unclaimed key takes it, and a key already holding this
|
|
134
330
|
* 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
|
|
136
|
-
* the resolvers beneath fall back and load their own data.
|
|
331
|
+
* disagree about gives the row a mapping of its own — or, with no row to hang it off, nothing.
|
|
137
332
|
*/
|
|
138
|
-
function claim(
|
|
139
|
-
cached: Cache,
|
|
140
|
-
key: string,
|
|
141
|
-
mapping: Mapping,
|
|
142
|
-
row: object | null,
|
|
143
|
-
path: GraphQLResolveInfo['path'],
|
|
144
|
-
) {
|
|
333
|
+
function claim(cached: Cache, key: string, mapping: Mapping, row: object | null, place: object) {
|
|
145
334
|
const held = cached.mappings.get(key);
|
|
146
335
|
|
|
147
336
|
if (held === undefined) {
|
|
148
337
|
cached.mappings.set(key, mapping);
|
|
149
338
|
} else if (held !== mapping && row) {
|
|
150
|
-
claimForRow(cached, key.slice(0, key.indexOf('@')), mapping, row,
|
|
339
|
+
claimForRow(cached, key.slice(0, key.indexOf('@')), mapping, row, place);
|
|
151
340
|
}
|
|
152
341
|
}
|
|
153
342
|
|
|
154
343
|
/**
|
|
155
|
-
* Records `mapping` for this field execution against `row` alone
|
|
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.
|
|
344
|
+
* Records `mapping` for this field execution against `row` alone.
|
|
160
345
|
*/
|
|
161
|
-
function claimForRow(
|
|
162
|
-
cached
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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);
|
|
346
|
+
function claimForRow(cached: Cache, type: string, mapping: Mapping, row: object, place: object) {
|
|
347
|
+
let byPlace = cached.rows.get(row);
|
|
348
|
+
if (!byPlace) {
|
|
349
|
+
byPlace = new WeakMap();
|
|
350
|
+
cached.rows.set(row, byPlace);
|
|
172
351
|
cached.rowScoped = true;
|
|
173
352
|
}
|
|
174
|
-
let own =
|
|
353
|
+
let own = byPlace.get(place);
|
|
175
354
|
if (!own) {
|
|
176
355
|
own = new Map();
|
|
177
|
-
|
|
356
|
+
byPlace.set(place, own);
|
|
178
357
|
}
|
|
179
358
|
own.set(type, mapping);
|
|
180
359
|
}
|
|
181
360
|
|
|
182
361
|
/**
|
|
183
362
|
* 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
|
|
185
|
-
*
|
|
363
|
+
* The plan loaded the whole field, so its mappings answer for everything resolved beneath this
|
|
364
|
+
* execution of it. Multiple plans can contribute to one field (Relay nodes of different model
|
|
186
365
|
* types share the list's path), so later contributions replace matching keys and retain others.
|
|
187
366
|
*/
|
|
188
367
|
export function setLoaderMappings(ctx: object, info: GraphQLResolveInfo, mappings: Mappings) {
|
|
189
|
-
const cached = cache(ctx);
|
|
368
|
+
const cached = cache(ctx, info);
|
|
190
369
|
const prefix = responsePath(info.path);
|
|
191
370
|
const keys = rehomedKeys(cached, prefix);
|
|
192
|
-
const
|
|
193
|
-
cached.scopes.
|
|
371
|
+
const place = placeOf(cached, info, info.path);
|
|
372
|
+
const previous = cached.scopes.get(place);
|
|
373
|
+
cached.scopes.set(place, previous ? { ...previous, ...mappings } : mappings);
|
|
194
374
|
|
|
195
375
|
for (const key of Object.keys(mappings)) {
|
|
196
376
|
cached.mappings.set(rehome(keys, key, prefix), mappings[key]);
|
|
@@ -200,10 +380,12 @@ export function setLoaderMappings(ctx: object, info: GraphQLResolveInfo, mapping
|
|
|
200
380
|
/**
|
|
201
381
|
* Records a row's mappings and scopes its descendants to this concrete field execution. A
|
|
202
382
|
* descendant's parent is a different object, so row identity alone cannot carry its plan through
|
|
203
|
-
* a relation, connection wrapper, or list. The
|
|
383
|
+
* a relation, connection wrapper, or list. The place keeps list indices to isolate siblings.
|
|
204
384
|
*/
|
|
205
385
|
export function setRowMappings(ctx: object, info: GraphQLResolveInfo, mappings: Mappings) {
|
|
206
|
-
cache(ctx
|
|
386
|
+
const cached = cache(ctx, info);
|
|
387
|
+
|
|
388
|
+
cached.scopes.set(placeOf(cached, info, info.path), mappings);
|
|
207
389
|
}
|
|
208
390
|
|
|
209
391
|
/**
|
|
@@ -218,17 +400,17 @@ export function setFieldMapping(
|
|
|
218
400
|
mapping: Mapping,
|
|
219
401
|
row: unknown,
|
|
220
402
|
) {
|
|
221
|
-
const cached = cache(ctx);
|
|
222
|
-
const
|
|
403
|
+
const cached = cache(ctx, info);
|
|
404
|
+
const place = placeOf(cached, info, info.path);
|
|
223
405
|
|
|
224
|
-
claim(cached, cacheKey(info.parentType.name, info.path), mapping,
|
|
225
|
-
cached.scopes.set(
|
|
406
|
+
claim(cached, cacheKey(info.parentType.name, info.path), mapping, ownerOf(row), place);
|
|
407
|
+
cached.scopes.set(place, mapping.nested);
|
|
226
408
|
}
|
|
227
409
|
|
|
228
410
|
/**
|
|
229
411
|
* `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
|
|
231
|
-
*
|
|
412
|
+
* against `row` rather than claimed for the whole field, so the field's lookup on `row` answers
|
|
413
|
+
* from it whatever the field's other rows were loaded with.
|
|
232
414
|
*
|
|
233
415
|
* Descendants inherit the mapping through this field's execution scope, even when their parent
|
|
234
416
|
* is a child object or a connection wrapper rather than the row that was reloaded.
|
|
@@ -239,32 +421,40 @@ export function setRowFieldMapping(
|
|
|
239
421
|
mapping: Mapping,
|
|
240
422
|
row: unknown,
|
|
241
423
|
) {
|
|
242
|
-
const cached = cache(ctx);
|
|
424
|
+
const cached = cache(ctx, info);
|
|
243
425
|
const owner = ownerOf(row);
|
|
426
|
+
const place = placeOf(cached, info, info.path);
|
|
244
427
|
|
|
245
428
|
if (owner) {
|
|
246
|
-
claimForRow(cached, info.parentType.name, mapping, owner,
|
|
429
|
+
claimForRow(cached, info.parentType.name, mapping, owner, place);
|
|
247
430
|
}
|
|
248
431
|
|
|
249
|
-
cached.scopes.set(
|
|
432
|
+
cached.scopes.set(place, mapping.nested);
|
|
250
433
|
}
|
|
251
434
|
|
|
252
435
|
/**
|
|
253
|
-
* The mapping recorded for the field
|
|
254
|
-
* over the plan's. `row` is the value whose data the mapping describes — the resolver's
|
|
436
|
+
* The mapping recorded for the field `info` resolves on `row`, preferring one recorded for that
|
|
437
|
+
* row over the plan's. `row` is the value whose data the mapping describes — the resolver's
|
|
438
|
+
* parent. The plan's comes from the nearest field execution above that recorded one: with none
|
|
439
|
+
* above, no plan loaded `row`, whatever was recorded for another row at the same path.
|
|
255
440
|
*/
|
|
256
441
|
export function getLoaderMapping(
|
|
257
442
|
ctx: object,
|
|
258
|
-
|
|
443
|
+
info: GraphQLResolveInfo,
|
|
259
444
|
type: string,
|
|
260
445
|
row?: unknown,
|
|
261
446
|
): Mapping | null {
|
|
262
|
-
const
|
|
263
|
-
const
|
|
447
|
+
const { path } = info;
|
|
448
|
+
const cached = cache(ctx, info);
|
|
264
449
|
|
|
265
450
|
if (cached.rowScoped) {
|
|
266
451
|
const owner = ownerOf(row);
|
|
267
|
-
const own =
|
|
452
|
+
const own =
|
|
453
|
+
owner &&
|
|
454
|
+
cached.rows
|
|
455
|
+
.get(owner)
|
|
456
|
+
?.get(placeOf(cached, info, path))
|
|
457
|
+
?.get(type);
|
|
268
458
|
|
|
269
459
|
if (own) {
|
|
270
460
|
return own;
|
|
@@ -272,7 +462,7 @@ export function getLoaderMapping(
|
|
|
272
462
|
}
|
|
273
463
|
|
|
274
464
|
for (let ancestor = path.prev; ancestor; ancestor = ancestor.prev) {
|
|
275
|
-
const scope = cached.scopes.get(ancestor);
|
|
465
|
+
const scope = cached.scopes.get(placeOf(cached, info, ancestor));
|
|
276
466
|
|
|
277
467
|
if (scope) {
|
|
278
468
|
// A different plan may have omitted this field. Do not treat a sibling's shared mapping
|
|
@@ -283,5 +473,5 @@ export function getLoaderMapping(
|
|
|
283
473
|
}
|
|
284
474
|
}
|
|
285
475
|
|
|
286
|
-
return
|
|
476
|
+
return null;
|
|
287
477
|
}
|
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
|
|
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
|
|
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
|
|
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
|
+
}
|