@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.
- package/CHANGELOG.md +30 -0
- package/README.md +16 -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 +37 -16
- 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 +37 -16
- package/esm/loader-map.d.ts.map +1 -1
- package/esm/loader-map.js +165 -99
- 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 -3
- package/lib/index.js.map +1 -1
- package/lib/loader-map.js +145 -90
- 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 -1
- package/src/loader-map.ts +235 -125
- 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/walk.ts
CHANGED
|
@@ -4,31 +4,30 @@
|
|
|
4
4
|
* `Plan` import below is a type import, so nothing here runs at import time and the pair is not a
|
|
5
5
|
* runtime cycle.
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
8
|
-
getMappedArgumentValues,
|
|
9
|
-
isThenable,
|
|
10
|
-
type MaybePromise,
|
|
11
|
-
PothosValidationError,
|
|
12
|
-
} from '@pothos/core';
|
|
7
|
+
import { getMappedArgumentValues, isThenable, type MaybePromise } from '@pothos/core';
|
|
13
8
|
import {
|
|
14
9
|
type FieldNode,
|
|
15
10
|
type FragmentDefinitionNode,
|
|
16
11
|
type FragmentSpreadNode,
|
|
17
12
|
type GraphQLNamedType,
|
|
13
|
+
type GraphQLResolveInfo,
|
|
18
14
|
getNamedType,
|
|
19
15
|
type InlineFragmentNode,
|
|
16
|
+
isAbstractType,
|
|
20
17
|
isInterfaceType,
|
|
21
18
|
isObjectType,
|
|
22
19
|
Kind,
|
|
23
20
|
type SelectionNode,
|
|
24
21
|
} from 'graphql';
|
|
25
22
|
import type { NodeBase } from './adapter.js';
|
|
23
|
+
import { unexpectedError } from './errors.js';
|
|
26
24
|
import { EMPTY_MAPPING, type Mapping, unionMappings } from './loader-map.js';
|
|
27
25
|
import {
|
|
28
26
|
collectSelectedFieldNames,
|
|
29
27
|
findMatches,
|
|
30
28
|
firstMatch,
|
|
31
29
|
includeOf,
|
|
30
|
+
includePaths,
|
|
32
31
|
isDeferred,
|
|
33
32
|
isSkipped,
|
|
34
33
|
matchesForModel,
|
|
@@ -37,6 +36,7 @@ import {
|
|
|
37
36
|
resolveType,
|
|
38
37
|
} from './matches.js';
|
|
39
38
|
import type { Plan } from './plan.js';
|
|
39
|
+
import { fieldShape, selectionShape } from './shape.js';
|
|
40
40
|
import type { NestedSelection, Position, SelectFn, WalkedType } from './types.js';
|
|
41
41
|
|
|
42
42
|
/**
|
|
@@ -48,6 +48,55 @@ interface Invocation extends Mapping {
|
|
|
48
48
|
pending?: number;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
/**
|
|
52
|
+
* The select invocations of one walk, by the shape of the field node (see `fieldShape`) and the
|
|
53
|
+
* mapping key: two nodes of one shape at one place select the same thing under the same response
|
|
54
|
+
* key, so the first invocation answers for both, and the second is neither invoked nor collected.
|
|
55
|
+
* Local to a walk, so what it holds goes with it.
|
|
56
|
+
*/
|
|
57
|
+
type Invocations = Set<string>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* What defines a field, to tell an implementation's copy of an interface field from an override.
|
|
61
|
+
* Pothos builds every implementation's copy of an inherited field from the interface's own field
|
|
62
|
+
* options, so those agree exactly when the field was not redefined, the way plugin-prisma-next
|
|
63
|
+
* names slots and every plugin's resolver accepts an interface's mapping. A field built elsewhere
|
|
64
|
+
* has no options, and only an identical select function says the same.
|
|
65
|
+
*
|
|
66
|
+
* `Adapter.fieldSelection` is handed the walked type and may vary a selection by it. Only a select
|
|
67
|
+
* function is ever moved to the interface, and the adapters vary only a static selection by type
|
|
68
|
+
* (prisma-next's empty selection for an interface field without one), so a select function is
|
|
69
|
+
* taken to depend on the field's definition alone.
|
|
70
|
+
*/
|
|
71
|
+
function definitionOf(field: Position['field'], selection: unknown): unknown {
|
|
72
|
+
return field.extensions?.pothosOptions ?? selection;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The position of one select invocation. Its own link is exact for that invocation, and reading
|
|
77
|
+
* further up is what ties a walk to the invocations above it, so `parent` tells the plan.
|
|
78
|
+
*/
|
|
79
|
+
function positionIn(
|
|
80
|
+
plan: { readonly position?: Position; positionRead: boolean },
|
|
81
|
+
type: WalkedType,
|
|
82
|
+
field: Position['field'],
|
|
83
|
+
node: FieldNode,
|
|
84
|
+
): Position {
|
|
85
|
+
return {
|
|
86
|
+
get parent() {
|
|
87
|
+
plan.positionRead = true;
|
|
88
|
+
|
|
89
|
+
return plan.position;
|
|
90
|
+
},
|
|
91
|
+
get walkParent() {
|
|
92
|
+
return plan.position;
|
|
93
|
+
},
|
|
94
|
+
type,
|
|
95
|
+
field,
|
|
96
|
+
node,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
51
100
|
/** Collects a type's type-level selection, once, before its fields are walked. */
|
|
52
101
|
function enter<Model, Query, NodeType extends NodeBase<Model>>(
|
|
53
102
|
plan: Plan<Model, Query, NodeType>,
|
|
@@ -131,15 +180,17 @@ export function walkBranches<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
131
180
|
const entered = new Set<string>();
|
|
132
181
|
|
|
133
182
|
for (const selections of selectionSets) {
|
|
134
|
-
enterVariants(plan, type, selections, entered);
|
|
183
|
+
enterVariants(plan, type, type, selections, entered);
|
|
135
184
|
}
|
|
136
185
|
}
|
|
137
186
|
|
|
187
|
+
const invocations: Invocations = new Set();
|
|
188
|
+
|
|
138
189
|
for (const { type, selectionSets, indirectPath } of resolved) {
|
|
139
190
|
const walked = new Set<string>();
|
|
140
191
|
|
|
141
192
|
for (const selections of selectionSets) {
|
|
142
|
-
walkSelections(plan, type, selections, indirectPath, true, walked);
|
|
193
|
+
walkSelections(plan, type, selections, indirectPath, true, walked, invocations);
|
|
143
194
|
}
|
|
144
195
|
}
|
|
145
196
|
}
|
|
@@ -160,11 +211,12 @@ function resolveBranch<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
160
211
|
|
|
161
212
|
const { info, adapter } = plan;
|
|
162
213
|
const include = includeOf(type);
|
|
214
|
+
const paths = include && includePaths(include);
|
|
163
215
|
const beneath: ResolvedBranch[] = [];
|
|
164
216
|
|
|
165
|
-
if (
|
|
217
|
+
if (paths) {
|
|
166
218
|
for (const fieldNode of fieldNodes) {
|
|
167
|
-
const matches = findMatches(info, type, fieldNode,
|
|
219
|
+
const matches = findMatches(info, type, fieldNode, paths, {
|
|
168
220
|
path: indirectPath,
|
|
169
221
|
deferred,
|
|
170
222
|
});
|
|
@@ -214,11 +266,11 @@ function resolveBranch<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
214
266
|
type Fragment = FragmentDefinitionNode | InlineFragmentNode;
|
|
215
267
|
|
|
216
268
|
/**
|
|
217
|
-
* Whether `fragment` was already
|
|
218
|
-
* it if not. A valid fragment DAG can spread the same fragment at every level, so
|
|
269
|
+
* Whether `fragment` was already visited under `key` in the pass `visited` belongs to, recording
|
|
270
|
+
* it if not. A valid fragment DAG can spread the same fragment at every level, so visiting every
|
|
219
271
|
* spread is exponential in its depth. Inline fragments cannot repeat.
|
|
220
272
|
*/
|
|
221
|
-
function
|
|
273
|
+
function visitedBefore(visited: Set<string>, key: string, fragment: Fragment): boolean {
|
|
222
274
|
if (fragment.kind !== Kind.FRAGMENT_DEFINITION) {
|
|
223
275
|
return false;
|
|
224
276
|
}
|
|
@@ -238,9 +290,16 @@ function expandedBefore(visited: Set<string>, key: string, fragment: Fragment):
|
|
|
238
290
|
* First pass: enters every same-model variant a fragment under `selections` moves the plan to,
|
|
239
291
|
* before any field is merged, so a conflict between two type-level selections is reported
|
|
240
292
|
* whichever order the fragments appear in and never depends on a field-level select.
|
|
293
|
+
*
|
|
294
|
+
* `selections` are walked as `type`, and variants are entered relative to `origin`, the type the
|
|
295
|
+
* plan was last entered as. They differ only under a fragment that did not move the plan, such as
|
|
296
|
+
* an expansion that selects no fields of its own: it narrows the walk to the implementations it
|
|
297
|
+
* covers without entering them, so a nested fragment that selects fields enters the variant
|
|
298
|
+
* instead.
|
|
241
299
|
*/
|
|
242
300
|
function enterVariants<Model, Query, NodeType extends NodeBase<Model>>(
|
|
243
301
|
plan: Plan<Model, Query, NodeType>,
|
|
302
|
+
origin: WalkedType,
|
|
244
303
|
type: WalkedType,
|
|
245
304
|
selections: readonly SelectionNode[],
|
|
246
305
|
visited: Set<string>,
|
|
@@ -252,17 +311,26 @@ function enterVariants<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
252
311
|
|
|
253
312
|
const fragment = applicableFragment(plan, selection);
|
|
254
313
|
|
|
255
|
-
if (!fragment ||
|
|
314
|
+
if (!fragment || visitedBefore(visited, `${origin.name}>${type.name}`, fragment)) {
|
|
256
315
|
continue;
|
|
257
316
|
}
|
|
258
317
|
|
|
259
|
-
const
|
|
318
|
+
const { types, isExpansion } = fragmentTypesOf(plan, type, fragment);
|
|
319
|
+
// A typed fragment directly under `origin` moves the plan to its type. An expansion, an
|
|
320
|
+
// untyped fragment, or one nested under a fragment that did not move the plan, moves it only
|
|
321
|
+
// when it selects fields of its own.
|
|
322
|
+
const enters =
|
|
323
|
+
types.length > 0 &&
|
|
324
|
+
((fragment.typeCondition && !isExpansion && type === origin) ||
|
|
325
|
+
selectsFields(plan.info, fragment.selectionSet.selections));
|
|
326
|
+
|
|
327
|
+
for (const as of types.length > 0 ? types : [type]) {
|
|
328
|
+
if (enters && as !== origin) {
|
|
329
|
+
enterVariant(plan, origin, as);
|
|
330
|
+
}
|
|
260
331
|
|
|
261
|
-
|
|
262
|
-
enterVariant(plan, type, as);
|
|
332
|
+
enterVariants(plan, enters ? as : origin, as, fragment.selectionSet.selections, visited);
|
|
263
333
|
}
|
|
264
|
-
|
|
265
|
-
enterVariants(plan, as ?? type, fragment.selectionSet.selections, visited);
|
|
266
334
|
}
|
|
267
335
|
}
|
|
268
336
|
|
|
@@ -278,11 +346,13 @@ function walkSelections<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
278
346
|
indirectPath: string[],
|
|
279
347
|
fieldsApply: boolean,
|
|
280
348
|
visited: Set<string>,
|
|
349
|
+
invocations: Invocations,
|
|
350
|
+
expandedFrom?: WalkedType,
|
|
281
351
|
) {
|
|
282
352
|
for (const selection of selections) {
|
|
283
353
|
if (selection.kind === Kind.FIELD) {
|
|
284
354
|
if (fieldsApply) {
|
|
285
|
-
walkField(plan, type, selection, indirectPath);
|
|
355
|
+
walkField(plan, type, selection, indirectPath, invocations, expandedFrom);
|
|
286
356
|
}
|
|
287
357
|
|
|
288
358
|
continue;
|
|
@@ -290,24 +360,46 @@ function walkSelections<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
290
360
|
|
|
291
361
|
const fragment = applicableFragment(plan, selection);
|
|
292
362
|
|
|
293
|
-
if (
|
|
363
|
+
if (
|
|
364
|
+
!fragment ||
|
|
365
|
+
visitedBefore(visited, `${type.name}:${fieldsApply}:${expandedFrom?.name}`, fragment)
|
|
366
|
+
) {
|
|
294
367
|
continue;
|
|
295
368
|
}
|
|
296
369
|
|
|
297
|
-
const
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
370
|
+
const { types, isExpansion } = fragmentTypesOf(plan, type, fragment);
|
|
371
|
+
|
|
372
|
+
for (const as of types.length > 0 ? types : [type]) {
|
|
373
|
+
walkSelections(
|
|
374
|
+
plan,
|
|
375
|
+
as,
|
|
376
|
+
fragment.selectionSet.selections,
|
|
377
|
+
indirectPath,
|
|
378
|
+
// An untyped fragment inherits; a typed one applies iff it can apply to `type`.
|
|
379
|
+
fragment.typeCondition ? types.length > 0 : fieldsApply,
|
|
380
|
+
visited,
|
|
381
|
+
invocations,
|
|
382
|
+
isExpansion ? type : expandedFrom && keptBy(plan, expandedFrom, fragment),
|
|
383
|
+
);
|
|
384
|
+
}
|
|
308
385
|
}
|
|
309
386
|
}
|
|
310
387
|
|
|
388
|
+
/**
|
|
389
|
+
* `expandedFrom` while fields under `fragment` still name it: the fragment is untyped, on
|
|
390
|
+
* `expandedFrom` or an interface it implements, or expands it again. A fragment narrowing to one
|
|
391
|
+
* type names its fields on that type.
|
|
392
|
+
*/
|
|
393
|
+
function keptBy<Model, Query, NodeType extends NodeBase<Model>>(
|
|
394
|
+
plan: Plan<Model, Query, NodeType>,
|
|
395
|
+
expandedFrom: WalkedType,
|
|
396
|
+
fragment: Fragment,
|
|
397
|
+
): WalkedType | undefined {
|
|
398
|
+
const { types, isExpansion } = fragmentTypesOf(plan, expandedFrom, fragment);
|
|
399
|
+
|
|
400
|
+
return isExpansion || types[0] === expandedFrom ? expandedFrom : undefined;
|
|
401
|
+
}
|
|
402
|
+
|
|
311
403
|
/**
|
|
312
404
|
* The fragment a non-field selection stands for, or undefined when it does not apply: skipped by
|
|
313
405
|
* a directive, or deferred while the plan skips deferred fragments.
|
|
@@ -324,50 +416,100 @@ function applicableFragment<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
324
416
|
}
|
|
325
417
|
|
|
326
418
|
/**
|
|
327
|
-
* The
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
419
|
+
* The types to walk `fragment` as while walking `type`, or none when the fragment cannot apply to
|
|
420
|
+
* `type` (its fields are suppressed; nested fragments are still classified against `type`). An
|
|
421
|
+
* untyped fragment inherits `type`. A type accepts a fragment on itself or on an interface it
|
|
422
|
+
* implements, walked as itself: a plan on an object type is a plan on rows of that type, so a
|
|
423
|
+
* fragment on any other object type cannot apply to them. An interface also accepts a fragment
|
|
424
|
+
* on another type of the same model, walked as that type, so that type's own selection is planned
|
|
425
|
+
* for the rows that resolve to it. A fragment on an abstract type of no model (`... on Node`
|
|
426
|
+
* beneath a model interface), or of another model, is an expansion. It is walked as each
|
|
427
|
+
* implementation of `type` it covers, so the variants' type-level selections are planned for their
|
|
428
|
+
* rows and the fields it selects load with the parent query. When those selections conflict, the
|
|
429
|
+
* play throws, as it does for two same-model fragments.
|
|
334
430
|
*/
|
|
335
|
-
function
|
|
431
|
+
function fragmentTypesOf<Model, Query, NodeType extends NodeBase<Model>>(
|
|
336
432
|
plan: Plan<Model, Query, NodeType>,
|
|
337
433
|
type: WalkedType,
|
|
338
434
|
fragment: Fragment,
|
|
339
|
-
): WalkedType
|
|
435
|
+
): { types: WalkedType[]; isExpansion: boolean } {
|
|
340
436
|
if (!fragment.typeCondition) {
|
|
341
|
-
return type;
|
|
437
|
+
return { types: [type], isExpansion: false };
|
|
342
438
|
}
|
|
343
439
|
|
|
344
|
-
const
|
|
440
|
+
const { schema } = plan.info;
|
|
441
|
+
const condition = schema.getType(fragment.typeCondition.name.value)!;
|
|
345
442
|
|
|
346
|
-
if (condition
|
|
347
|
-
return type;
|
|
443
|
+
if (isOfType(type, condition)) {
|
|
444
|
+
return { types: [type], isExpansion: false };
|
|
348
445
|
}
|
|
349
446
|
|
|
350
|
-
if (isInterfaceType(
|
|
351
|
-
return
|
|
447
|
+
if (!isInterfaceType(type)) {
|
|
448
|
+
return { types: [], isExpansion: false };
|
|
352
449
|
}
|
|
353
450
|
|
|
451
|
+
const model = plan.adapter.modelFor(type);
|
|
452
|
+
|
|
354
453
|
if (
|
|
355
|
-
isInterfaceType(type) &&
|
|
356
454
|
(isObjectType(condition) || isInterfaceType(condition)) &&
|
|
357
|
-
plan.adapter.modelFor(condition) ===
|
|
455
|
+
plan.adapter.modelFor(condition) === model
|
|
358
456
|
) {
|
|
359
|
-
return condition;
|
|
457
|
+
return { types: [condition], isExpansion: false };
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
if (isAbstractType(condition)) {
|
|
461
|
+
return {
|
|
462
|
+
types: schema
|
|
463
|
+
.getPossibleTypes(type)
|
|
464
|
+
.filter(
|
|
465
|
+
(possible) =>
|
|
466
|
+
schema.isSubType(condition, possible) && plan.adapter.modelFor(possible) === model,
|
|
467
|
+
),
|
|
468
|
+
isExpansion: plan.adapter.modelFor(condition) !== model,
|
|
469
|
+
};
|
|
360
470
|
}
|
|
361
471
|
|
|
362
|
-
return
|
|
472
|
+
return { types: [], isExpansion: false };
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** Whether `type` is `condition` itself or implements it. */
|
|
476
|
+
function isOfType(type: WalkedType, condition: GraphQLNamedType) {
|
|
477
|
+
return (
|
|
478
|
+
condition === type || (isInterfaceType(condition) && type.getInterfaces().includes(condition))
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Whether `selections` select a field directly; `__typename` reads nothing from the row, and a
|
|
484
|
+
* skipped field never runs.
|
|
485
|
+
*/
|
|
486
|
+
function selectsFields(info: GraphQLResolveInfo, selections: readonly SelectionNode[]): boolean {
|
|
487
|
+
return selections.some(
|
|
488
|
+
(selection) =>
|
|
489
|
+
selection.kind === Kind.FIELD &&
|
|
490
|
+
!selection.name.value.startsWith('__') &&
|
|
491
|
+
!isSkipped(info, selection),
|
|
492
|
+
);
|
|
363
493
|
}
|
|
364
494
|
|
|
365
|
-
/**
|
|
495
|
+
/**
|
|
496
|
+
* Collects what `fieldNode` (a field of `type`) selects. A select function is invoked once per
|
|
497
|
+
* place in a walk; `invocations` holds the walk's, and without it every call invokes.
|
|
498
|
+
*
|
|
499
|
+
* Beneath an expansion of the model interface `expandedFrom`, a field an implementation inherits
|
|
500
|
+
* unchanged (see `definitionOf`) is planned on the interface: invoked once rather than once per
|
|
501
|
+
* implementation, so a select function reading `Position.parent` sees one path per level. A
|
|
502
|
+
* resolver on an implementation accepts the interface's mapping for the same definition. A
|
|
503
|
+
* redefined field, or one under a fragment narrowing to the implementation, is planned as the
|
|
504
|
+
* implementation.
|
|
505
|
+
*/
|
|
366
506
|
export function walkField<Model, Query, NodeType extends NodeBase<Model>>(
|
|
367
507
|
plan: Plan<Model, Query, NodeType>,
|
|
368
|
-
|
|
508
|
+
walkedType: WalkedType,
|
|
369
509
|
fieldNode: FieldNode,
|
|
370
510
|
indirectPath: string[],
|
|
511
|
+
invocations?: Invocations,
|
|
512
|
+
expandedFrom?: WalkedType,
|
|
371
513
|
) {
|
|
372
514
|
const { info, context, adapter } = plan;
|
|
373
515
|
const name = fieldNode.name.value;
|
|
@@ -376,30 +518,48 @@ export function walkField<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
376
518
|
return;
|
|
377
519
|
}
|
|
378
520
|
|
|
379
|
-
const
|
|
521
|
+
const walkedField = walkedType.getFields()[name];
|
|
380
522
|
|
|
381
|
-
if (!
|
|
382
|
-
throw
|
|
523
|
+
if (!walkedField) {
|
|
524
|
+
throw unexpectedError(`Unknown field ${name} on ${walkedType.name}`);
|
|
383
525
|
}
|
|
384
526
|
|
|
385
|
-
const selection = adapter.fieldSelection(
|
|
527
|
+
const selection = adapter.fieldSelection(walkedField, walkedType);
|
|
386
528
|
|
|
387
529
|
if (!selection) {
|
|
388
530
|
return;
|
|
389
531
|
}
|
|
390
532
|
|
|
391
533
|
const alias = fieldNode.alias?.value ?? name;
|
|
392
|
-
const
|
|
534
|
+
const relative = `${indirectPath.length > 0 ? `${indirectPath.join('.')}.` : ''}${alias}`;
|
|
393
535
|
|
|
394
536
|
if (typeof selection !== 'function') {
|
|
395
|
-
collectField(plan,
|
|
537
|
+
collectField(plan, `${walkedType.name}@${relative}`, alias, selection, EMPTY_MAPPING);
|
|
538
|
+
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const inherited = expandedFrom?.getFields()[name];
|
|
543
|
+
const [type, field] =
|
|
544
|
+
expandedFrom &&
|
|
545
|
+
inherited &&
|
|
546
|
+
definitionOf(inherited, adapter.fieldSelection(inherited, expandedFrom)) ===
|
|
547
|
+
definitionOf(walkedField, selection)
|
|
548
|
+
? [expandedFrom, inherited]
|
|
549
|
+
: [walkedType, walkedField];
|
|
396
550
|
|
|
551
|
+
const key = `${type.name}@${relative}`;
|
|
552
|
+
const place = `${fieldShape(info, fieldNode)}:${key}`;
|
|
553
|
+
|
|
554
|
+
if (invocations?.has(place)) {
|
|
397
555
|
return;
|
|
398
556
|
}
|
|
399
557
|
|
|
558
|
+
invocations?.add(place);
|
|
559
|
+
|
|
400
560
|
// Where this field is, linked to where the plan it was found in hangs. One link, built once per
|
|
401
561
|
// select invocation; nothing walks it unless the adapter's callback asks.
|
|
402
|
-
const position
|
|
562
|
+
const position = positionIn(plan, type, field, fieldNode);
|
|
403
563
|
// This invocation's mapping; every nested plan it makes records into `mapping.nested`, which
|
|
404
564
|
// stays invisible to the plan until the invocation's query is accepted.
|
|
405
565
|
const mapping: Invocation = { nested: {}, position };
|
|
@@ -469,7 +629,7 @@ function collectField<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
469
629
|
|
|
470
630
|
if (mapping.pending) {
|
|
471
631
|
// Only a select invocation can be pending, and every one of those has a position.
|
|
472
|
-
throw
|
|
632
|
+
throw unexpectedError(
|
|
473
633
|
`The selection function of ${fieldName(mapping.position!)} returned while a nested selection it started was still pending. Await nestedSelection() (or a helper built on it, such as getQuery) inside an async selection function.`,
|
|
474
634
|
);
|
|
475
635
|
}
|
|
@@ -495,7 +655,7 @@ function nestedSelectionFor<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
495
655
|
try {
|
|
496
656
|
const returnType = getNamedType(position.field.type);
|
|
497
657
|
const include = Array.isArray(pathOrInclude)
|
|
498
|
-
?
|
|
658
|
+
? includeForPath(
|
|
499
659
|
pathOrInclude,
|
|
500
660
|
resolveType(info.schema, returnType),
|
|
501
661
|
typeName ? info.schema.getType(typeName) : undefined,
|
|
@@ -523,23 +683,13 @@ function nestedSelectionFor<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
523
683
|
return (query ?? ({} as Query)) as Query;
|
|
524
684
|
}
|
|
525
685
|
|
|
526
|
-
const
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
mergeQuery(child, resolved),
|
|
532
|
-
);
|
|
533
|
-
} else {
|
|
534
|
-
mergeQuery(child, query);
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
const paths = include?.paths?.length
|
|
538
|
-
? include.paths
|
|
539
|
-
: include?.path?.length
|
|
540
|
-
? [include.path]
|
|
541
|
-
: undefined;
|
|
686
|
+
const paths = include && includePaths(include);
|
|
687
|
+
// Everything a walk beneath this field node reads: the shape of its selection set, not the
|
|
688
|
+
// node, so a fragment spread beneath many fields is walked once. The paths are data: an
|
|
689
|
+
// include's segments are names and type names.
|
|
690
|
+
const walkKey = `${selectionShape(info, fieldNode.selectionSet)}:${returnType.name}:${target.name}:${paths ? JSON.stringify(paths) : ''}`;
|
|
542
691
|
|
|
692
|
+
const child = plan.nested(target, position, walkKey, (walked) => {
|
|
543
693
|
if (paths) {
|
|
544
694
|
// Each match is walked as its own type; the wrapper's selection set is not walked.
|
|
545
695
|
const matches = matchesForModel(
|
|
@@ -552,7 +702,7 @@ function nestedSelectionFor<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
552
702
|
);
|
|
553
703
|
|
|
554
704
|
walkBranches(
|
|
555
|
-
|
|
705
|
+
walked,
|
|
556
706
|
matches.map((match) => ({
|
|
557
707
|
// As `Plan.fromInfo` does for its own paths: a matched type with a model is walked
|
|
558
708
|
// with it, and one without (an interface, a wrapper) as the type asked for, so its
|
|
@@ -571,14 +721,19 @@ function nestedSelectionFor<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
571
721
|
deferred: false,
|
|
572
722
|
});
|
|
573
723
|
|
|
574
|
-
walkBranches(
|
|
724
|
+
walkBranches(walked, [
|
|
575
725
|
...(target === returnType ? [] : [asType(target)]),
|
|
576
726
|
asType(returnType),
|
|
577
727
|
]);
|
|
578
728
|
}
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
|
|
729
|
+
});
|
|
730
|
+
|
|
731
|
+
if (isThenable(query)) {
|
|
732
|
+
child.chain(query as PromiseLike<Query | null | undefined>, (resolved) =>
|
|
733
|
+
mergeQuery(child, resolved),
|
|
734
|
+
);
|
|
735
|
+
} else {
|
|
736
|
+
mergeQuery(child, query);
|
|
582
737
|
}
|
|
583
738
|
|
|
584
739
|
// A promise behind the declared synchronous type, as `finish` returns one.
|
|
@@ -595,6 +750,18 @@ function nestedSelectionFor<Model, Query, NodeType extends NodeBase<Model>>(
|
|
|
595
750
|
};
|
|
596
751
|
}
|
|
597
752
|
|
|
753
|
+
/**
|
|
754
|
+
* `normalizeInclude` for a path an app's select passed while a request is planned. A path that
|
|
755
|
+
* doesn't fit the field's type is an error in the app, which clients see as `Unexpected error.`.
|
|
756
|
+
*/
|
|
757
|
+
function includeForPath(...args: Parameters<typeof normalizeInclude>) {
|
|
758
|
+
try {
|
|
759
|
+
return normalizeInclude(...args);
|
|
760
|
+
} catch (error) {
|
|
761
|
+
throw unexpectedError((error as Error).message);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
|
|
598
765
|
/**
|
|
599
766
|
* The promise of a nested selection whose plan is async, counted against its invocation until it
|
|
600
767
|
* settles. Both outcomes clear the count, so an invocation can catch a failure and retry.
|