@pothos/selection-mapper 0.1.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/.swcrc +10 -0
- package/LICENSE +6 -0
- package/README.md +92 -0
- package/dts/adapter.d.ts +74 -0
- package/dts/adapter.d.ts.map +1 -0
- package/dts/deep-equal.d.ts +7 -0
- package/dts/deep-equal.d.ts.map +1 -0
- package/dts/index.d.ts +13 -0
- package/dts/index.d.ts.map +1 -0
- package/dts/loader-map.d.ts +63 -0
- package/dts/loader-map.d.ts.map +1 -0
- package/dts/matches.d.ts +113 -0
- package/dts/matches.d.ts.map +1 -0
- package/dts/node.d.ts +85 -0
- package/dts/node.d.ts.map +1 -0
- package/dts/plan.d.ts +176 -0
- package/dts/plan.d.ts.map +1 -0
- package/dts/types.d.ts +97 -0
- package/dts/types.d.ts.map +1 -0
- package/dts/walk.d.ts +24 -0
- package/dts/walk.d.ts.map +1 -0
- package/esm/adapter.d.ts +74 -0
- package/esm/adapter.d.ts.map +1 -0
- package/esm/adapter.js +47 -0
- package/esm/adapter.js.map +1 -0
- package/esm/deep-equal.d.ts +7 -0
- package/esm/deep-equal.d.ts.map +1 -0
- package/esm/deep-equal.js +50 -0
- package/esm/deep-equal.js.map +1 -0
- package/esm/index.d.ts +13 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +12 -0
- package/esm/index.js.map +1 -0
- package/esm/loader-map.d.ts +63 -0
- package/esm/loader-map.d.ts.map +1 -0
- package/esm/loader-map.js +197 -0
- package/esm/loader-map.js.map +1 -0
- package/esm/matches.d.ts +113 -0
- package/esm/matches.d.ts.map +1 -0
- package/esm/matches.js +419 -0
- package/esm/matches.js.map +1 -0
- package/esm/node.d.ts +85 -0
- package/esm/node.d.ts.map +1 -0
- package/esm/node.js +232 -0
- package/esm/node.js.map +1 -0
- package/esm/package.json +3 -0
- package/esm/plan.d.ts +176 -0
- package/esm/plan.d.ts.map +1 -0
- package/esm/plan.js +339 -0
- package/esm/plan.js.map +1 -0
- package/esm/types.d.ts +97 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/types.js +10 -0
- package/esm/types.js.map +1 -0
- package/esm/walk.d.ts +24 -0
- package/esm/walk.d.ts.map +1 -0
- package/esm/walk.js +442 -0
- package/esm/walk.js.map +1 -0
- package/lib/adapter.js +43 -0
- package/lib/adapter.js.map +1 -0
- package/lib/deep-equal.js +60 -0
- package/lib/deep-equal.js.map +1 -0
- package/lib/index.js +57 -0
- package/lib/index.js.map +1 -0
- package/lib/loader-map.js +209 -0
- package/lib/loader-map.js.map +1 -0
- package/lib/matches.js +427 -0
- package/lib/matches.js.map +1 -0
- package/lib/node.js +245 -0
- package/lib/node.js.map +1 -0
- package/lib/plan.js +349 -0
- package/lib/plan.js.map +1 -0
- package/lib/types.js +9 -0
- package/lib/types.js.map +1 -0
- package/lib/walk.js +455 -0
- package/lib/walk.js.map +1 -0
- package/package.json +55 -0
- package/src/adapter.ts +96 -0
- package/src/deep-equal.ts +67 -0
- package/src/index.ts +32 -0
- package/src/loader-map.ts +287 -0
- package/src/matches.ts +681 -0
- package/src/node.ts +327 -0
- package/src/plan.ts +454 -0
- package/src/types.ts +123 -0
- package/src/walk.ts +696 -0
package/src/plan.ts
ADDED
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One root being planned: the two entry points that build one from a resolver's `info`, the
|
|
3
|
+
* merges a traversal collected for it, and the fold that turns those merges into a node.
|
|
4
|
+
*
|
|
5
|
+
* A plan holds no node — playing it builds one — so the same plan can be played more than once,
|
|
6
|
+
* behind a different seed each time. Playing is where a merge is accepted or rejected and the
|
|
7
|
+
* only place a mapping is recorded; the traversal decides nothing, so a field that lost to
|
|
8
|
+
* another occurrence of itself is still on the list and a play whose seed makes it fit takes it.
|
|
9
|
+
*/
|
|
10
|
+
import { PothosValidationError } from '@pothos/core';
|
|
11
|
+
import { type GraphQLNamedType, type GraphQLResolveInfo, getNamedType } from 'graphql';
|
|
12
|
+
import type { Adapter, NodeBase } from './adapter.js';
|
|
13
|
+
import { type Mapping, type Mappings, setLoaderMappings, unionMappings } from './loader-map.js';
|
|
14
|
+
import {
|
|
15
|
+
findMatches,
|
|
16
|
+
type IndirectPathSegment,
|
|
17
|
+
includeOf,
|
|
18
|
+
matchesForModel,
|
|
19
|
+
modelOf,
|
|
20
|
+
type PathSegment,
|
|
21
|
+
resolveType,
|
|
22
|
+
} from './matches.js';
|
|
23
|
+
import type { Node } from './node.js';
|
|
24
|
+
import type { EntryOptions, MergeOptions, Position, WalkedType } from './types.js';
|
|
25
|
+
import { type Branch, walkBranches, walkField } from './walk.js';
|
|
26
|
+
|
|
27
|
+
// Shared, since neither carries anything per merge.
|
|
28
|
+
const AS_QUERY: MergeOptions = Object.freeze({ asQuery: true });
|
|
29
|
+
const LENIENT: MergeOptions = Object.freeze({ lenient: true });
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* One merge a traversal collected, in the order it happened: a type's selection, a same-model
|
|
33
|
+
* variant's, a nested selection's relation query, or a field's. A play folds the list into a
|
|
34
|
+
* node; nothing is merged, accepted or rejected before then.
|
|
35
|
+
*
|
|
36
|
+
* A record holds its query by reference, so a select function that mutates the query it returned
|
|
37
|
+
* after returning it changes what a later play builds — a bug in the select function. Nothing
|
|
38
|
+
* copies it: a query is opaque to this package, which could only copy one by round-tripping it
|
|
39
|
+
* through the adapter on every field.
|
|
40
|
+
*/
|
|
41
|
+
export type RootMerge<Query> =
|
|
42
|
+
| { kind: 'type'; query: Query }
|
|
43
|
+
| { kind: 'query'; query: Query }
|
|
44
|
+
| { kind: 'variant'; type: WalkedType; variant: WalkedType; query: Query }
|
|
45
|
+
| { kind: 'field'; key: string; alias: string; query: Query; mapping: Mapping };
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A plan played: the node its merges built and the mappings the accepted merges recorded. A play
|
|
49
|
+
* owns its node, so a caller may merge into one without disturbing the plan or another play.
|
|
50
|
+
*/
|
|
51
|
+
export interface PlayedPlan<Model, Query, NodeType extends NodeBase<Model> = Node<Model>> {
|
|
52
|
+
plan: Plan<Model, Query, NodeType>;
|
|
53
|
+
root: NodeType;
|
|
54
|
+
mappings: Mappings;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* What a plan takes from whatever created it: an entry point's options, or the plan it hangs
|
|
59
|
+
* beneath. A parent plan satisfies this, so a nested plan is created from its parent directly
|
|
60
|
+
* with nothing allocated to carry the values across.
|
|
61
|
+
*/
|
|
62
|
+
export interface PlanSource {
|
|
63
|
+
context: object;
|
|
64
|
+
info: GraphQLResolveInfo;
|
|
65
|
+
/** Whether a fragment under `@defer` is walked; defaults to the adapter's own setting. */
|
|
66
|
+
skipDeferredFragments?: boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export class Plan<Model, Query, NodeType extends NodeBase<Model> = Node<Model>> {
|
|
70
|
+
readonly context: object;
|
|
71
|
+
readonly info: GraphQLResolveInfo;
|
|
72
|
+
readonly skipDeferredFragments: boolean;
|
|
73
|
+
/** What the plan loads. A node is created for it once per play. */
|
|
74
|
+
readonly model: Model;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The merges waiting on a user callback that returned a promise, in the order they were
|
|
78
|
+
* appended. Absent until the first one: a synchronous plan never creates a promise.
|
|
79
|
+
*/
|
|
80
|
+
pending?: Promise<void>;
|
|
81
|
+
|
|
82
|
+
/** The traversal's whole output: every merge it collected, in the order it collected them. */
|
|
83
|
+
private readonly merges: RootMerge<Query>[] = [];
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The play `settle` ran, which a later play takes whole when the seed conflicts with none of
|
|
87
|
+
* it. Never handed out: a play that reuses it copies it into a node of its own.
|
|
88
|
+
*/
|
|
89
|
+
private played?: PlayedPlan<Model, Query, NodeType>;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The plan for the field `info` resolves, settled, or undefined when paths are given and
|
|
93
|
+
* nothing is selected under them. Declared synchronous: the result is a promise only when a
|
|
94
|
+
* select function returned one, and must then be awaited. The plan is handed out rather than
|
|
95
|
+
* played, so a plugin that owes a resolver a synchronous query builder settles this first and
|
|
96
|
+
* plays it behind the resolver's own selection with `plan.query(select)`.
|
|
97
|
+
*/
|
|
98
|
+
static fromInfo<Model, Query, NodeType extends NodeBase<Model> = Node<Model>>(
|
|
99
|
+
adapter: Adapter<Model, Query, NodeType>,
|
|
100
|
+
options: EntryOptions<Query>,
|
|
101
|
+
): Plan<Model, Query, NodeType> | undefined {
|
|
102
|
+
const { info, typeName, path, paths } = options;
|
|
103
|
+
const returnType = getNamedType(info.returnType);
|
|
104
|
+
const target = typeName ? info.schema.getType(typeName)! : returnType;
|
|
105
|
+
|
|
106
|
+
// graphql merges every occurrence of the field's response key into `info.fieldNodes`; each is
|
|
107
|
+
// planned into the one root, so the query answers whichever occurrence a resolver runs for.
|
|
108
|
+
let rootType = target;
|
|
109
|
+
let branches: Branch[];
|
|
110
|
+
|
|
111
|
+
if (paths?.length || path?.length) {
|
|
112
|
+
const includePaths = normalizePaths(paths?.length ? paths : [path!]);
|
|
113
|
+
const prefix = includeOf(returnType)?.path;
|
|
114
|
+
const found = info.fieldNodes.flatMap((fieldNode) =>
|
|
115
|
+
findMatches(info, returnType, fieldNode, includePaths, { prefix }),
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
if (found.length === 0) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Matches are filtered against the model the root loads, which is the first match's own
|
|
123
|
+
// when no `typeName` names it: the return type is then a wrapper with no model, and
|
|
124
|
+
// filtering by it would keep every match whatever model it returns.
|
|
125
|
+
rootType = typeName ? target : found[0].type;
|
|
126
|
+
|
|
127
|
+
const matches = matchesForModel(adapter, info.schema, found, rootType);
|
|
128
|
+
// Every match is planned into the one root, entered under its own type first.
|
|
129
|
+
branches = matches.map((match) => ({
|
|
130
|
+
// A matched type with its own model (including variants of the target model) is walked
|
|
131
|
+
// with its own model. Types without a model (interfaces, wrappers) are walked as the
|
|
132
|
+
// requested type so its fields can be found.
|
|
133
|
+
type: typeName && !modelOf(adapter, info.schema, match.type) ? target : match.type,
|
|
134
|
+
fieldNodes: [match.field],
|
|
135
|
+
indirectPath: match.path,
|
|
136
|
+
deferred: match.deferred,
|
|
137
|
+
}));
|
|
138
|
+
} else {
|
|
139
|
+
branches = [{ type: target, fieldNodes: info.fieldNodes, indirectPath: [], deferred: false }];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const plan = new Plan(
|
|
143
|
+
adapter,
|
|
144
|
+
options,
|
|
145
|
+
rootType,
|
|
146
|
+
positionForResolvedField(info),
|
|
147
|
+
options.initial,
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
walkBranches(plan, branches);
|
|
152
|
+
} catch (error) {
|
|
153
|
+
plan.abandon();
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return plan.finish(plan.settle);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The plan loading the field `info` resolves for its parent row, played. The loaded row
|
|
162
|
+
* replaces the parent the field resolver sees, so besides the field's own selection it
|
|
163
|
+
* carries the parent type's type-level selection. The field is what the row is loaded for, so
|
|
164
|
+
* it is merged first and a type-level relation whose arguments conflict with it is left out.
|
|
165
|
+
*/
|
|
166
|
+
static forParentRow<Model, Query, NodeType extends NodeBase<Model> = Node<Model>>(
|
|
167
|
+
adapter: Adapter<Model, Query, NodeType>,
|
|
168
|
+
context: object,
|
|
169
|
+
info: GraphQLResolveInfo,
|
|
170
|
+
skipDeferredFragments?: boolean,
|
|
171
|
+
): PlayedPlan<Model, Query, NodeType> {
|
|
172
|
+
const type = info.parentType;
|
|
173
|
+
const plan = new Plan(adapter, { context, info, skipDeferredFragments }, type);
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
// Every node selecting the field (one per fragment it appears under) plans into the same
|
|
177
|
+
// row, so the loaded row satisfies each of them.
|
|
178
|
+
for (const fieldNode of info.fieldNodes) {
|
|
179
|
+
walkField(plan, type, fieldNode, []);
|
|
180
|
+
}
|
|
181
|
+
} catch (error) {
|
|
182
|
+
plan.abandon();
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return plan.finish(plan.enterParentType, type);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* `source` is an entry point's options for a root plan and the parent plan for a nested one,
|
|
191
|
+
* which is why `initial` is passed beside it: it belongs to the root alone and a nested plan
|
|
192
|
+
* must never inherit one. `position` is where the field this plan hangs beneath is, undefined
|
|
193
|
+
* when there is no field above the plan.
|
|
194
|
+
*/
|
|
195
|
+
constructor(
|
|
196
|
+
readonly adapter: Adapter<Model, Query, NodeType>,
|
|
197
|
+
source: PlanSource,
|
|
198
|
+
type: GraphQLNamedType,
|
|
199
|
+
readonly position?: Position,
|
|
200
|
+
/** Merged first by a play a caller gives no seed of its own. */
|
|
201
|
+
readonly initial?: Query,
|
|
202
|
+
) {
|
|
203
|
+
this.context = source.context;
|
|
204
|
+
this.info = source.info;
|
|
205
|
+
this.skipDeferredFragments = source.skipDeferredFragments ?? adapter.skipDeferredFragments;
|
|
206
|
+
|
|
207
|
+
const model = modelOf(adapter, source.info.schema, type);
|
|
208
|
+
|
|
209
|
+
if (!model) {
|
|
210
|
+
throw new PothosValidationError(
|
|
211
|
+
`Expected ${resolveType(source.info.schema, type).name} to have a model`,
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Not entered: a type is entered when its selection set is walked.
|
|
216
|
+
this.model = model;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* The plan beneath one nested selection: this plan's adapter, context, info and deferred
|
|
221
|
+
* setting, with merges of its own. It hangs beneath the field whose select function made it.
|
|
222
|
+
*/
|
|
223
|
+
nested(type: GraphQLNamedType, position: Position): Plan<Model, Query, NodeType> {
|
|
224
|
+
return new Plan(this.adapter, this, type, position);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Appends one merge to the list a play folds. The traversal's only way to add to a plan. */
|
|
228
|
+
collect(merge: RootMerge<Query>) {
|
|
229
|
+
this.merges.push(merge);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* The relation query of the nested selection this plan was made for. It takes the head of the
|
|
234
|
+
* list, so it is merged before the fields walked beneath it whether the callback that produced
|
|
235
|
+
* it answered at once or resolved after the walk. A nested plan has at most one.
|
|
236
|
+
*/
|
|
237
|
+
collectQuery(query: Query) {
|
|
238
|
+
this.merges.unshift({ kind: 'query', query });
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Appends the merge of `value` to the plan's pending chain. Only merges are chained, never user
|
|
243
|
+
* code, so a link can never append another and the chain needs no loop. Each link waits on the
|
|
244
|
+
* previous one, so async merges run in the order they were appended. Both promises get a
|
|
245
|
+
* handler at once, so a callback that rejects early is never an unhandled rejection.
|
|
246
|
+
*/
|
|
247
|
+
chain<T>(value: PromiseLike<T>, merge: (v: T) => void) {
|
|
248
|
+
const prev = this.pending;
|
|
249
|
+
|
|
250
|
+
this.pending = prev
|
|
251
|
+
? Promise.all([prev, value]).then(([, v]) => merge(v))
|
|
252
|
+
: Promise.resolve(value).then(merge);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* A plan that threw synchronously never reaches `finish`, so the merges it had already chained
|
|
257
|
+
* would reject unobserved once their callbacks settle. The throw is what the caller sees.
|
|
258
|
+
*/
|
|
259
|
+
abandon() {
|
|
260
|
+
this.pending?.catch(noop);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* The single exit of every entry point: `done`, a method of this plan, runs now when nothing is
|
|
265
|
+
* pending, else after every pending merge, in which case the result is a promise behind the
|
|
266
|
+
* declared synchronous type. Fixed arity, so the synchronous call allocates nothing.
|
|
267
|
+
*/
|
|
268
|
+
finish<R>(done: (this: this) => R): R;
|
|
269
|
+
finish<A, R>(done: (this: this, arg: A) => R, arg: A): R;
|
|
270
|
+
finish<A, R>(done: (this: this, arg?: A) => R, arg?: A): R {
|
|
271
|
+
return this.pending
|
|
272
|
+
? (this.pending.then(() => done.call(this, arg)) as R)
|
|
273
|
+
: done.call(this, arg);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* A fresh node with this plan's merges folded into it, and the mappings of the merges it took.
|
|
278
|
+
* `seed` takes the place of the plan's own `initial`: it is merged before anything the traversal
|
|
279
|
+
* collected, so a relation or computed value the document plans with other arguments loses, and
|
|
280
|
+
* its field falls back to loading on its own.
|
|
281
|
+
*
|
|
282
|
+
* Synchronous, and runs no user callback, so a play of an async plan costs no more than a play
|
|
283
|
+
* of a synchronous one.
|
|
284
|
+
*/
|
|
285
|
+
play(seed?: Query): PlayedPlan<Model, Query, NodeType> {
|
|
286
|
+
const { adapter } = this;
|
|
287
|
+
const root = adapter.createNode(this.model);
|
|
288
|
+
const settled = this.reusable(seed);
|
|
289
|
+
// Merged before anything else, so on a conflict it wins. A settled play already holds the
|
|
290
|
+
// plan's own `initial`, so reusing one merges the seed and nothing else: `mergeQuery` is not
|
|
291
|
+
// required to be idempotent.
|
|
292
|
+
const first = settled ? seed : (seed ?? this.initial);
|
|
293
|
+
|
|
294
|
+
if (first) {
|
|
295
|
+
adapter.mergeQuery(root, first);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (settled) {
|
|
299
|
+
// Every merge that play took still fits and every merge it left out still does not, so
|
|
300
|
+
// the node it built is taken whole.
|
|
301
|
+
adapter.mergeNode(root, settled.root);
|
|
302
|
+
|
|
303
|
+
return { plan: this, root, mappings: settled.mappings };
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const mappings: Mappings = {};
|
|
307
|
+
|
|
308
|
+
for (const merge of this.merges) {
|
|
309
|
+
switch (merge.kind) {
|
|
310
|
+
case 'type':
|
|
311
|
+
adapter.mergeQuery(root, merge.query);
|
|
312
|
+
break;
|
|
313
|
+
case 'query':
|
|
314
|
+
adapter.mergeQuery(root, merge.query, AS_QUERY);
|
|
315
|
+
break;
|
|
316
|
+
case 'variant':
|
|
317
|
+
this.mergeVariant(root, merge.type, merge.variant, merge.query);
|
|
318
|
+
break;
|
|
319
|
+
case 'field': {
|
|
320
|
+
// One options object for the pair: `mergeQuery` reads only the alias, and
|
|
321
|
+
// `canMergeQuery` only the `ignoreArgs` beside it.
|
|
322
|
+
const options: MergeOptions = { ignoreArgs: true, alias: merge.alias };
|
|
323
|
+
|
|
324
|
+
// A field's selection is merged, and its mapping recorded, only while it fits what is
|
|
325
|
+
// already in the node; otherwise it is left out and its resolver loads its own data.
|
|
326
|
+
if (adapter.canMergeQuery(root, merge.query, options)) {
|
|
327
|
+
adapter.mergeQuery(root, merge.query, options);
|
|
328
|
+
mappings[merge.key] = unionMappings(mappings[merge.key], merge.mapping);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
break;
|
|
332
|
+
}
|
|
333
|
+
default: {
|
|
334
|
+
const unknown: never = merge;
|
|
335
|
+
|
|
336
|
+
throw new PothosValidationError(
|
|
337
|
+
`Unknown root merge ${String((unknown as RootMerge<Query>).kind)}`,
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
return { plan: this, root, mappings };
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* The plan played behind `seed`, its mappings recorded for the resolvers beneath it, and its
|
|
348
|
+
* node serialized. A cached plan accepts the current resolver info to scope each execution.
|
|
349
|
+
* Every play records its mappings on the request context; an adapter whose
|
|
350
|
+
* resolvers read a loaded row another way simply never looks them up.
|
|
351
|
+
*/
|
|
352
|
+
query(seed?: Query, info = this.info): Query {
|
|
353
|
+
// With no seed, a settled play's node is what a fresh play would build, so hand it straight
|
|
354
|
+
// over: `toQuery` does not mutate, and a later seeded play still merges from it.
|
|
355
|
+
const { root, mappings } = seed === undefined && this.played ? this.played : this.play(seed);
|
|
356
|
+
|
|
357
|
+
setLoaderMappings(this.context, info, mappings);
|
|
358
|
+
|
|
359
|
+
return this.adapter.toQuery(root);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* The plan played once, so a play-time error — a conflict between two type-level selections —
|
|
364
|
+
* is raised now rather than only inside a `query()` a resolver may never call. That play is
|
|
365
|
+
* kept, and a later one whose seed conflicts with none of it takes it whole, so settling costs
|
|
366
|
+
* the resolver's own play nothing.
|
|
367
|
+
*/
|
|
368
|
+
settle(): this {
|
|
369
|
+
this.played = this.play();
|
|
370
|
+
|
|
371
|
+
return this;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* The plan played, with `type`'s type-level selection merged in behind it, minus what conflicts
|
|
376
|
+
* with the field the row is loaded for. A parent-row plan is never replayed behind a caller's
|
|
377
|
+
* selection, so it plays once, here.
|
|
378
|
+
*/
|
|
379
|
+
enterParentType(type: GraphQLNamedType): PlayedPlan<Model, Query, NodeType> {
|
|
380
|
+
const played = this.play();
|
|
381
|
+
const selection = this.adapter.typeSelection(type);
|
|
382
|
+
|
|
383
|
+
if (selection) {
|
|
384
|
+
this.adapter.mergeQuery(played.root, selection, LENIENT);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return played;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* A settled play that a play seeded with `seed` can take whole, or undefined. A seed conflicting
|
|
392
|
+
* with nothing the settled play holds changes no decision it made, since every rejection was
|
|
393
|
+
* against something the seed leaves in place — unless the seed stands in for the plan's own
|
|
394
|
+
* `initial`, which the settled play already merged.
|
|
395
|
+
*/
|
|
396
|
+
private reusable(seed: Query | undefined) {
|
|
397
|
+
if (!this.played || (seed !== undefined && this.initial !== undefined)) {
|
|
398
|
+
return undefined;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
return seed === undefined || !this.adapter.firstConflict(this.played.root, seed)
|
|
402
|
+
? this.played
|
|
403
|
+
: undefined;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Unlike a field-level select, a type-level selection has no per-field fallback, so a relation
|
|
408
|
+
* argument or a computed value that conflicts with what is already in the node is an error
|
|
409
|
+
* rather than a rejection.
|
|
410
|
+
*/
|
|
411
|
+
private mergeVariant(node: NodeType, type: WalkedType, variant: WalkedType, selection: Query) {
|
|
412
|
+
const conflict = this.adapter.firstConflict(node, selection);
|
|
413
|
+
|
|
414
|
+
if (conflict) {
|
|
415
|
+
switch (conflict.kind) {
|
|
416
|
+
case 'relation':
|
|
417
|
+
throw new PothosValidationError(
|
|
418
|
+
`Type-level selections of ${type.name} and ${variant.name} conflict on relation "${conflict.name}". Move the relation arguments to a field-level select on one of the types.`,
|
|
419
|
+
);
|
|
420
|
+
case 'extra':
|
|
421
|
+
throw new PothosValidationError(
|
|
422
|
+
`Type-level selections of ${type.name} and ${variant.name} conflict on extra "${conflict.name}". Define the extra with the same function on both types, or move it to a field-level select on one of the types.`,
|
|
423
|
+
);
|
|
424
|
+
default: {
|
|
425
|
+
const unknown: never = conflict.kind;
|
|
426
|
+
|
|
427
|
+
throw new PothosValidationError(`Unknown type-level conflict ${unknown as string}`);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
this.adapter.mergeQuery(node, selection);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Where the field being resolved is, which every position beneath it links back to, so a select
|
|
438
|
+
* function can tell where in the query its field sits. Undefined when `info` does not name the
|
|
439
|
+
* field it resolves (a caller building one by hand): the plan then starts at its own fields.
|
|
440
|
+
*/
|
|
441
|
+
function positionForResolvedField(info: GraphQLResolveInfo): Position | undefined {
|
|
442
|
+
const node = info.fieldNodes[0];
|
|
443
|
+
const field = info.parentType?.getFields()[node.name.value];
|
|
444
|
+
|
|
445
|
+
return field && { parent: undefined, type: info.parentType, field, node };
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function normalizePaths(paths: PathSegment[][]): IndirectPathSegment[][] {
|
|
449
|
+
return paths.map((path) =>
|
|
450
|
+
path.map((segment) => (typeof segment === 'string' ? { name: segment } : segment)),
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function noop() {}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The types the adapter contract and the entry points mention: where a field is, what a select
|
|
3
|
+
* function is handed, what an entry point takes, and how one merge differs from a plain one.
|
|
4
|
+
*/
|
|
5
|
+
import type { MaybePromise } from '@pothos/core';
|
|
6
|
+
import type {
|
|
7
|
+
FieldNode,
|
|
8
|
+
GraphQLField,
|
|
9
|
+
GraphQLInterfaceType,
|
|
10
|
+
GraphQLObjectType,
|
|
11
|
+
GraphQLResolveInfo,
|
|
12
|
+
} from 'graphql';
|
|
13
|
+
import type { IndirectInclude, PathSegment } from './matches.js';
|
|
14
|
+
|
|
15
|
+
export type WalkedType = GraphQLInterfaceType | GraphQLObjectType;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Where a field is. One link of a chain running from the field an entry point was called for down
|
|
19
|
+
* to the field being planned: the field's node in the document, the type it was found on, and the
|
|
20
|
+
* position of the field the plan it was found in hangs beneath.
|
|
21
|
+
*
|
|
22
|
+
* The walker builds one per select invocation and reads none of it, so a caller that wants a path
|
|
23
|
+
* or a name walks `parent` itself and materializes what it needs. A plan whose fields nothing asks
|
|
24
|
+
* about costs one link per select function and no arrays at all.
|
|
25
|
+
*/
|
|
26
|
+
export interface Position {
|
|
27
|
+
/** The field this one is selected beneath, or undefined at the field the plan started from. */
|
|
28
|
+
readonly parent: Position | undefined;
|
|
29
|
+
/** The type the field was walked on: its parent type, or a same-model type the plan moved to. */
|
|
30
|
+
readonly type: WalkedType;
|
|
31
|
+
readonly field: GraphQLField<unknown, unknown>;
|
|
32
|
+
/** The field's node in the document, which carries its alias and arguments. */
|
|
33
|
+
readonly node: FieldNode;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A relation query: a query, or a callback building one from the field's arguments. */
|
|
37
|
+
export type NestedQuery<Query> =
|
|
38
|
+
| MaybePromise<Query | null | undefined>
|
|
39
|
+
| ((args: object, ctx: object, position: Position) => MaybePromise<Query | null | undefined>);
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The callback handed to a field's select function to plan the selection beneath the field:
|
|
43
|
+
* `query` is merged first (a function is called with the field's args, `true` means no query),
|
|
44
|
+
* then the selection found under `path` (an explicit include, or a string path from the field's
|
|
45
|
+
* return type) is walked as `type` (or the field's return type). Declared synchronous: the result
|
|
46
|
+
* is a promise only when a callback beneath it returned one, and must then be awaited.
|
|
47
|
+
*/
|
|
48
|
+
export type NestedSelection<Query> = (
|
|
49
|
+
query?: NestedQuery<Query> | true,
|
|
50
|
+
path?: PathSegment[] | IndirectInclude,
|
|
51
|
+
type?: string,
|
|
52
|
+
) => Query;
|
|
53
|
+
|
|
54
|
+
/** Inspect fields beneath the current selection, through fragments and wrappers. */
|
|
55
|
+
export interface SelectedFieldNode {
|
|
56
|
+
/** Lazily collect selected schema field names; aliases do not change the names. */
|
|
57
|
+
(): ReadonlySet<string>;
|
|
58
|
+
(path: string[]): FieldNode | null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A function field selection. A falsy result selects nothing. */
|
|
62
|
+
export type SelectFn<Query> = (
|
|
63
|
+
args: object,
|
|
64
|
+
ctx: object,
|
|
65
|
+
nested: NestedSelection<Query>,
|
|
66
|
+
selectedFieldNode: SelectedFieldNode,
|
|
67
|
+
position: Position,
|
|
68
|
+
) => MaybePromise<Query | false | null | undefined>;
|
|
69
|
+
|
|
70
|
+
export interface EntryOptions<Query> {
|
|
71
|
+
context: object;
|
|
72
|
+
info: GraphQLResolveInfo;
|
|
73
|
+
typeName?: string;
|
|
74
|
+
path?: PathSegment[];
|
|
75
|
+
paths?: PathSegment[][];
|
|
76
|
+
/**
|
|
77
|
+
* Merged into the root before anything is walked, so on a conflict it wins. `Plan.fromInfo`
|
|
78
|
+
* answers undefined when paths are given and nothing is selected under them; what a caller
|
|
79
|
+
* hands its resolver then is the caller's own rule, not this package's.
|
|
80
|
+
*/
|
|
81
|
+
initial?: Query;
|
|
82
|
+
/**
|
|
83
|
+
* Overrides `Adapter.skipDeferredFragments` for this plan. The setting belongs to a builder
|
|
84
|
+
* while prisma's adapter is a module singleton, so that plugin passes it per entry point
|
|
85
|
+
* instead of putting it on the adapter.
|
|
86
|
+
*/
|
|
87
|
+
skipDeferredFragments?: boolean;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** How one merge into an adapter's node differs from a plain one. */
|
|
91
|
+
export interface MergeOptions {
|
|
92
|
+
/**
|
|
93
|
+
* The node's own top-level arguments are not compared. A field's selection is merged into a
|
|
94
|
+
* node whose arguments came from somewhere else, so only what is nested below is checked.
|
|
95
|
+
*/
|
|
96
|
+
ignoreArgs?: boolean;
|
|
97
|
+
/**
|
|
98
|
+
* The query is a relation query, not a selection. A query without a column selection must add
|
|
99
|
+
* no columns: the plan beneath it adds the ones it needs.
|
|
100
|
+
*/
|
|
101
|
+
asQuery?: boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Keys that conflict with what the node holds are left out, one at a time, instead of the merge
|
|
104
|
+
* being refused or throwing.
|
|
105
|
+
*/
|
|
106
|
+
lenient?: boolean;
|
|
107
|
+
/**
|
|
108
|
+
* The response key of the field the query came from, so an adapter that keeps one slot per
|
|
109
|
+
* selected field can name it. Absent for a type-level selection, an initial selection and a
|
|
110
|
+
* staged query.
|
|
111
|
+
*/
|
|
112
|
+
alias?: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* A key of a type-level selection that cannot be merged with what a node already holds. `kind`
|
|
117
|
+
* chooses the error message, so it is the ORM's own word for the key rather than this package's:
|
|
118
|
+
* prisma reports a conflicting `_count` as a `relation`, since that is what a user wrote.
|
|
119
|
+
*/
|
|
120
|
+
export interface TypeLevelConflict {
|
|
121
|
+
kind: 'extra' | 'relation';
|
|
122
|
+
name: string;
|
|
123
|
+
}
|