@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/lib/walk.js
ADDED
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The traversal: types, variants, fields and fragments read into a plan's merge list. The entry
|
|
3
|
+
* points that drive it are the statics of `Plan` in plan.ts, which imports this module; the
|
|
4
|
+
* `Plan` import below is a type import, so nothing here runs at import time and the pair is not a
|
|
5
|
+
* runtime cycle.
|
|
6
|
+
*/ "use strict";
|
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
|
8
|
+
value: true
|
|
9
|
+
});
|
|
10
|
+
function _export(target, all) {
|
|
11
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
_export(exports, {
|
|
17
|
+
get walkBranches () {
|
|
18
|
+
return walkBranches;
|
|
19
|
+
},
|
|
20
|
+
get walkField () {
|
|
21
|
+
return walkField;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
const _core = require("@pothos/core");
|
|
25
|
+
const _graphql = require("graphql");
|
|
26
|
+
const _loadermap = require("./loader-map.js");
|
|
27
|
+
const _matches = require("./matches.js");
|
|
28
|
+
/** Collects a type's type-level selection, once, before its fields are walked. */ function enter(plan, type) {
|
|
29
|
+
const selection = plan.adapter.typeSelection(type);
|
|
30
|
+
if (selection) {
|
|
31
|
+
plan.collect({
|
|
32
|
+
kind: 'type',
|
|
33
|
+
query: selection
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Collects the type-level selection of `variant` when a fragment moves the plan from `type` to
|
|
39
|
+
* another type of the same model, so the variant's resolvers find what its selection promises.
|
|
40
|
+
* Whether the two selections can live in one node is decided when the plan is played.
|
|
41
|
+
*/ function enterVariant(plan, type, variant) {
|
|
42
|
+
const selection = plan.adapter.typeSelection(variant);
|
|
43
|
+
if (selection) {
|
|
44
|
+
plan.collect({
|
|
45
|
+
kind: 'variant',
|
|
46
|
+
type,
|
|
47
|
+
variant,
|
|
48
|
+
query: selection
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function walkBranches(plan, branches) {
|
|
53
|
+
const resolved = branches.flatMap((branch)=>resolveBranch(plan, branch));
|
|
54
|
+
const enteredTypes = new Set();
|
|
55
|
+
let firstType;
|
|
56
|
+
for (const { type, selectionSets } of resolved){
|
|
57
|
+
// The plan is entered as the first type a match landed on, and every other one as a
|
|
58
|
+
// same-model variant of it, so two type-level selections that conflict are reported here
|
|
59
|
+
// exactly as they are when a fragment moves the plan between the same two types. A type two
|
|
60
|
+
// matches share is entered once.
|
|
61
|
+
if (!enteredTypes.has(type)) {
|
|
62
|
+
enteredTypes.add(type);
|
|
63
|
+
if (firstType) {
|
|
64
|
+
enterVariant(plan, firstType, type);
|
|
65
|
+
} else {
|
|
66
|
+
firstType = type;
|
|
67
|
+
enter(plan, type);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const entered = new Set();
|
|
71
|
+
for (const selections of selectionSets){
|
|
72
|
+
enterVariants(plan, type, selections, entered);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
for (const { type, selectionSets, indirectPath } of resolved){
|
|
76
|
+
const walked = new Set();
|
|
77
|
+
for (const selections of selectionSets){
|
|
78
|
+
walkSelections(plan, type, selections, indirectPath, true, walked);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The selections `branch` stands for once its type's indirect include is followed, in the order
|
|
84
|
+
* they are walked. A type-level path yields one per match beneath the wrapper; a plain include
|
|
85
|
+
* re-types the plan; anything but an object or interface type yields nothing.
|
|
86
|
+
*/ function resolveBranch(plan, { type, fieldNodes, indirectPath, deferred }) {
|
|
87
|
+
// Every node selects the same field.
|
|
88
|
+
if (fieldNodes.length === 0 || fieldNodes[0].name.value.startsWith('__')) {
|
|
89
|
+
return [];
|
|
90
|
+
}
|
|
91
|
+
const { info, adapter } = plan;
|
|
92
|
+
const include = (0, _matches.includeOf)(type);
|
|
93
|
+
const beneath = [];
|
|
94
|
+
if (include?.paths?.length || include?.path?.length) {
|
|
95
|
+
for (const fieldNode of fieldNodes){
|
|
96
|
+
const matches = (0, _matches.findMatches)(info, type, fieldNode, include.paths ?? [
|
|
97
|
+
include.path
|
|
98
|
+
], {
|
|
99
|
+
path: indirectPath,
|
|
100
|
+
deferred
|
|
101
|
+
});
|
|
102
|
+
for (const match of matches){
|
|
103
|
+
beneath.push(...resolveBranch(plan, {
|
|
104
|
+
type: match.type,
|
|
105
|
+
fieldNodes: [
|
|
106
|
+
match.field
|
|
107
|
+
],
|
|
108
|
+
indirectPath: match.path,
|
|
109
|
+
deferred: match.deferred
|
|
110
|
+
}));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// The wrapper's own selection is planned only when the wrapper itself is backed by the model
|
|
114
|
+
// being queried (a variant that also points at a nested field). A plain wrapper, or one backed
|
|
115
|
+
// by another model, has nothing of its own to add to this query.
|
|
116
|
+
if (adapter.modelFor(type) !== plan.model) {
|
|
117
|
+
return beneath;
|
|
118
|
+
}
|
|
119
|
+
} else if (include) {
|
|
120
|
+
return resolveBranch(plan, {
|
|
121
|
+
type: info.schema.getType(include.getType()),
|
|
122
|
+
fieldNodes,
|
|
123
|
+
indirectPath,
|
|
124
|
+
deferred
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (!((0, _graphql.isObjectType)(type) || (0, _graphql.isInterfaceType)(type))) {
|
|
128
|
+
return beneath;
|
|
129
|
+
}
|
|
130
|
+
// A deferred selection is entered but, when deferred fragments are skipped, not walked.
|
|
131
|
+
const selectionSets = deferred && plan.skipDeferredFragments ? [] : fieldNodes.flatMap((fieldNode)=>fieldNode.selectionSet ? [
|
|
132
|
+
fieldNode.selectionSet.selections
|
|
133
|
+
] : []);
|
|
134
|
+
return [
|
|
135
|
+
...beneath,
|
|
136
|
+
{
|
|
137
|
+
type,
|
|
138
|
+
selectionSets,
|
|
139
|
+
indirectPath
|
|
140
|
+
}
|
|
141
|
+
];
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Whether `fragment` was already expanded under `key` in the pass `visited` belongs to, recording
|
|
145
|
+
* it if not. A valid fragment DAG can spread the same fragment at every level, so expanding every
|
|
146
|
+
* spread is exponential in its depth. Inline fragments cannot repeat.
|
|
147
|
+
*/ function expandedBefore(visited, key, fragment) {
|
|
148
|
+
if (fragment.kind !== _graphql.Kind.FRAGMENT_DEFINITION) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
const id = `${key}:${fragment.name.value}`;
|
|
152
|
+
if (visited.has(id)) {
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
visited.add(id);
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* First pass: enters every same-model variant a fragment under `selections` moves the plan to,
|
|
160
|
+
* before any field is merged, so a conflict between two type-level selections is reported
|
|
161
|
+
* whichever order the fragments appear in and never depends on a field-level select.
|
|
162
|
+
*/ function enterVariants(plan, type, selections, visited) {
|
|
163
|
+
for (const selection of selections){
|
|
164
|
+
if (selection.kind === _graphql.Kind.FIELD) {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const fragment = applicableFragment(plan, selection);
|
|
168
|
+
if (!fragment || expandedBefore(visited, type.name, fragment)) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
const as = fragmentTypeOf(plan, type, fragment);
|
|
172
|
+
if (as && as !== type) {
|
|
173
|
+
enterVariant(plan, type, as);
|
|
174
|
+
}
|
|
175
|
+
enterVariants(plan, as ?? type, fragment.selectionSet.selections, visited);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Second pass: walks `selections` in document order, a fragment's fields where the fragment
|
|
180
|
+
* appears. Fields apply unless the enclosing fragment cannot apply to `type`; nested fragments
|
|
181
|
+
* are always classified against `type`, so one may narrow back to it.
|
|
182
|
+
*/ function walkSelections(plan, type, selections, indirectPath, fieldsApply, visited) {
|
|
183
|
+
for (const selection of selections){
|
|
184
|
+
if (selection.kind === _graphql.Kind.FIELD) {
|
|
185
|
+
if (fieldsApply) {
|
|
186
|
+
walkField(plan, type, selection, indirectPath);
|
|
187
|
+
}
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const fragment = applicableFragment(plan, selection);
|
|
191
|
+
if (!fragment || expandedBefore(visited, `${type.name}:${fieldsApply}`, fragment)) {
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
const as = fragmentTypeOf(plan, type, fragment);
|
|
195
|
+
walkSelections(plan, as ?? type, fragment.selectionSet.selections, indirectPath, // An untyped fragment inherits; a typed one applies iff it can apply to `type`.
|
|
196
|
+
fragment.typeCondition ? as !== undefined : fieldsApply, visited);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* The fragment a non-field selection stands for, or undefined when it does not apply: skipped by
|
|
201
|
+
* a directive, or deferred while the plan skips deferred fragments.
|
|
202
|
+
*/ function applicableFragment({ info, skipDeferredFragments }, selection) {
|
|
203
|
+
if ((0, _matches.isSkipped)(info, selection) || skipDeferredFragments && (0, _matches.isDeferred)(info, selection)) {
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
return selection.kind === _graphql.Kind.FRAGMENT_SPREAD ? info.fragments[selection.name.value] : selection;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* The type to walk `fragment` as while walking `type`, or undefined when the fragment cannot
|
|
210
|
+
* apply to `type` (its fields are suppressed; nested fragments are still classified against
|
|
211
|
+
* `type`). An untyped fragment inherits `type`. An object type accepts a fragment on itself or on
|
|
212
|
+
* an interface it implements, walked as itself: a plan on an object type is a plan on rows of that
|
|
213
|
+
* type, so a fragment on any other object type cannot apply to them. An interface accepts a
|
|
214
|
+
* fragment on another type of the same model, walked as that type, so that type's own selection
|
|
215
|
+
* is planned for the rows that resolve to it.
|
|
216
|
+
*/ function fragmentTypeOf(plan, type, fragment) {
|
|
217
|
+
if (!fragment.typeCondition) {
|
|
218
|
+
return type;
|
|
219
|
+
}
|
|
220
|
+
const condition = plan.info.schema.getType(fragment.typeCondition.name.value);
|
|
221
|
+
if (condition === type) {
|
|
222
|
+
return type;
|
|
223
|
+
}
|
|
224
|
+
if ((0, _graphql.isInterfaceType)(condition) && type.getInterfaces().includes(condition)) {
|
|
225
|
+
return type;
|
|
226
|
+
}
|
|
227
|
+
if ((0, _graphql.isInterfaceType)(type) && ((0, _graphql.isObjectType)(condition) || (0, _graphql.isInterfaceType)(condition)) && plan.adapter.modelFor(condition) === plan.adapter.modelFor(type)) {
|
|
228
|
+
return condition;
|
|
229
|
+
}
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
function walkField(plan, type, fieldNode, indirectPath) {
|
|
233
|
+
const { info, context, adapter } = plan;
|
|
234
|
+
const name = fieldNode.name.value;
|
|
235
|
+
if (name.startsWith('__') || (0, _matches.isSkipped)(info, fieldNode)) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const field = type.getFields()[name];
|
|
239
|
+
if (!field) {
|
|
240
|
+
throw new _core.PothosValidationError(`Unknown field ${name} on ${type.name}`);
|
|
241
|
+
}
|
|
242
|
+
const selection = adapter.fieldSelection(field, type);
|
|
243
|
+
if (!selection) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
const alias = fieldNode.alias?.value ?? name;
|
|
247
|
+
const key = `${type.name}@${indirectPath.length > 0 ? `${indirectPath.join('.')}.` : ''}${alias}`;
|
|
248
|
+
if (typeof selection !== 'function') {
|
|
249
|
+
collectField(plan, key, alias, selection, _loadermap.EMPTY_MAPPING);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
// Where this field is, linked to where the plan it was found in hangs. One link, built once per
|
|
253
|
+
// select invocation; nothing walks it unless the adapter's callback asks.
|
|
254
|
+
const position = {
|
|
255
|
+
parent: plan.position,
|
|
256
|
+
type,
|
|
257
|
+
field,
|
|
258
|
+
node: fieldNode
|
|
259
|
+
};
|
|
260
|
+
// This invocation's mapping; every nested plan it makes records into `mapping.nested`, which
|
|
261
|
+
// stays invisible to the plan until the invocation's query is accepted.
|
|
262
|
+
const mapping = {
|
|
263
|
+
nested: {},
|
|
264
|
+
position
|
|
265
|
+
};
|
|
266
|
+
const args = (0, _core.getMappedArgumentValues)(field, fieldNode, context, info);
|
|
267
|
+
const select = selection;
|
|
268
|
+
// The select runs as soon as its own arguments are known; only its merge waits.
|
|
269
|
+
const query = (0, _core.isThenable)(args) ? args.then((mapped)=>runSelect(plan, select, mapped, position, mapping)) : runSelect(plan, select, args, position, mapping);
|
|
270
|
+
if ((0, _core.isThenable)(query)) {
|
|
271
|
+
plan.chain(query, (resolved)=>collectField(plan, key, alias, resolved, mapping));
|
|
272
|
+
} else {
|
|
273
|
+
collectField(plan, key, alias, query, mapping);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function runSelect(plan, select, args, position, mapping) {
|
|
277
|
+
return select(args, plan.context, nestedSelectionFor(plan, args, position, mapping), selectedFieldNodeFor(plan, position), position);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* A field the way the document names it, for an error message. The mapping key cannot say this:
|
|
281
|
+
* it holds the alias in place of the name, under the alias path of any indirect include above it.
|
|
282
|
+
*/ function fieldName({ type, node }) {
|
|
283
|
+
const name = `${type.name}.${node.name.value}`;
|
|
284
|
+
return node.alias ? `${name} (selected as "${node.alias.value}")` : name;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Collects a field's query with the mapping to record if a play takes it. A falsy query is
|
|
288
|
+
* collected as nothing, so the resolver loads its own data; whether the query fits, and so
|
|
289
|
+
* whether the mapping is ever recorded, is `play`'s decision.
|
|
290
|
+
*
|
|
291
|
+
* An invocation whose nested selection is still pending returned without awaiting it, so its
|
|
292
|
+
* query cannot hold what the nested plan will select and recording its mapping would claim data
|
|
293
|
+
* the query never loads. It is refused instead, and the pending plans (already handled, see
|
|
294
|
+
* `awaitNested`) are left to settle unobserved.
|
|
295
|
+
*/ function collectField(plan, key, alias, query, mapping) {
|
|
296
|
+
if (!query) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
if (mapping.pending) {
|
|
300
|
+
// Only a select invocation can be pending, and every one of those has a position.
|
|
301
|
+
throw new _core.PothosValidationError(`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.`);
|
|
302
|
+
}
|
|
303
|
+
plan.collect({
|
|
304
|
+
kind: 'field',
|
|
305
|
+
key,
|
|
306
|
+
alias,
|
|
307
|
+
query,
|
|
308
|
+
mapping
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
/** The nested selection callback of one select invocation. */ function nestedSelectionFor(plan, args, position, mapping) {
|
|
312
|
+
const { adapter, context, info } = plan;
|
|
313
|
+
const { node: fieldNode } = position;
|
|
314
|
+
return (rawQuery, pathOrInclude, typeName)=>{
|
|
315
|
+
// A caller may already have started this promise before path/type validation runs.
|
|
316
|
+
let query = typeof rawQuery === 'function' || rawQuery === true ? undefined : rawQuery;
|
|
317
|
+
try {
|
|
318
|
+
const returnType = (0, _graphql.getNamedType)(position.field.type);
|
|
319
|
+
const include = Array.isArray(pathOrInclude) ? (0, _matches.normalizeInclude)(pathOrInclude, (0, _matches.resolveType)(info.schema, returnType), typeName ? info.schema.getType(typeName) : undefined, info.schema) : pathOrInclude;
|
|
320
|
+
const target = include ? info.schema.getType(include.getType()) : returnType;
|
|
321
|
+
// `true` is the public "no query"; it never reaches an adapter.
|
|
322
|
+
query = rawQuery === true ? undefined : typeof rawQuery === 'function' ? rawQuery(args, context, position) : rawQuery;
|
|
323
|
+
if (!(0, _matches.modelOf)(adapter, info.schema, target)) {
|
|
324
|
+
// A model-less field (a scalar, a type without a model) has nothing beneath it to plan:
|
|
325
|
+
// the nested selection is the query alone, and nothing is recorded for it.
|
|
326
|
+
return query ?? {};
|
|
327
|
+
}
|
|
328
|
+
const child = plan.nested(target, position);
|
|
329
|
+
try {
|
|
330
|
+
if ((0, _core.isThenable)(query)) {
|
|
331
|
+
child.chain(query, (resolved)=>mergeQuery(child, resolved));
|
|
332
|
+
} else {
|
|
333
|
+
mergeQuery(child, query);
|
|
334
|
+
}
|
|
335
|
+
const paths = include?.paths?.length ? include.paths : include?.path?.length ? [
|
|
336
|
+
include.path
|
|
337
|
+
] : undefined;
|
|
338
|
+
if (paths) {
|
|
339
|
+
// Each match is walked as its own type; the wrapper's selection set is not walked.
|
|
340
|
+
const matches = (0, _matches.matchesForModel)(adapter, info.schema, (0, _matches.findMatches)(info, returnType, fieldNode, paths, {
|
|
341
|
+
prefix: (0, _matches.includeOf)(returnType)?.path
|
|
342
|
+
}), target);
|
|
343
|
+
walkBranches(child, matches.map((match)=>({
|
|
344
|
+
// As `Plan.fromInfo` does for its own paths: a matched type with a model is walked
|
|
345
|
+
// with it, and one without (an interface, a wrapper) as the type asked for, so its
|
|
346
|
+
// fields are found and a fragment narrowing to it applies.
|
|
347
|
+
type: (0, _matches.modelOf)(adapter, info.schema, match.type) ? match.type : target,
|
|
348
|
+
fieldNodes: [
|
|
349
|
+
match.field
|
|
350
|
+
],
|
|
351
|
+
indirectPath: match.path,
|
|
352
|
+
deferred: match.deferred
|
|
353
|
+
})));
|
|
354
|
+
} else {
|
|
355
|
+
const asType = (type)=>({
|
|
356
|
+
type,
|
|
357
|
+
fieldNodes: [
|
|
358
|
+
fieldNode
|
|
359
|
+
],
|
|
360
|
+
indirectPath: [],
|
|
361
|
+
deferred: false
|
|
362
|
+
});
|
|
363
|
+
walkBranches(child, [
|
|
364
|
+
...target === returnType ? [] : [
|
|
365
|
+
asType(target)
|
|
366
|
+
],
|
|
367
|
+
asType(returnType)
|
|
368
|
+
]);
|
|
369
|
+
}
|
|
370
|
+
} catch (error) {
|
|
371
|
+
child.abandon();
|
|
372
|
+
throw error;
|
|
373
|
+
}
|
|
374
|
+
// A promise behind the declared synchronous type, as `finish` returns one.
|
|
375
|
+
return child.pending ? awaitNested(child, mapping) : queryNested(child, mapping);
|
|
376
|
+
} catch (error) {
|
|
377
|
+
if ((0, _core.isThenable)(query)) {
|
|
378
|
+
query.then(()=>{}, ()=>{});
|
|
379
|
+
}
|
|
380
|
+
throw error;
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* The promise of a nested selection whose plan is async, counted against its invocation until it
|
|
386
|
+
* settles. Both outcomes clear the count, so an invocation can catch a failure and retry.
|
|
387
|
+
* Rejections are handled here so a discarded selection does not create an unhandled rejection.
|
|
388
|
+
* A failed child publishes no mappings; fields it did not load can fall back independently.
|
|
389
|
+
*/ function awaitNested(child, mapping) {
|
|
390
|
+
mapping.pending = (mapping.pending ?? 0) + 1;
|
|
391
|
+
const result = child.pending.then(()=>queryNested(child, mapping));
|
|
392
|
+
const settled = ()=>{
|
|
393
|
+
if (mapping.pending === 1) {
|
|
394
|
+
delete mapping.pending;
|
|
395
|
+
} else {
|
|
396
|
+
mapping.pending -= 1;
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
result.then(settled, settled);
|
|
400
|
+
return result;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* The nested plan played where the callback needs it serialized, with the mappings it recorded
|
|
404
|
+
* folded into the invocation that started it. Every nested plan of one invocation records into
|
|
405
|
+
* the same `mapping.nested`, so two that map the same key union.
|
|
406
|
+
*/ function queryNested(child, mapping) {
|
|
407
|
+
const played = child.play();
|
|
408
|
+
// Serialization can throw. Publish mappings only once the caller can use the query.
|
|
409
|
+
const query = child.adapter.toQuery(played.root);
|
|
410
|
+
for (const key of Object.keys(played.mappings)){
|
|
411
|
+
mapping.nested[key] = (0, _loadermap.unionMappings)(mapping.nested[key], played.mappings[key]);
|
|
412
|
+
}
|
|
413
|
+
return query;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* The relation query of a nested selection, merged before the walk beneath it whether the
|
|
417
|
+
* callback answered at once or resolved after that walk had already collected its fields — see
|
|
418
|
+
* `collectQuery`. A field of the document that conflicts with the query therefore loses to it
|
|
419
|
+
* either way, and is never mapped to a relation the query loaded with other arguments.
|
|
420
|
+
*/ function mergeQuery(child, query) {
|
|
421
|
+
if (query) {
|
|
422
|
+
child.collectQuery(query);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* The first field node selected at `path` beneath the field, seen through any wrapper on the
|
|
427
|
+
* field's return type (an errors plugin result, for instance), whose own path leads to the type
|
|
428
|
+
* the caller's path starts from. An empty path yields the field node itself, or the wrapper's
|
|
429
|
+
* inner node.
|
|
430
|
+
*
|
|
431
|
+
* A node selected only under a `@defer` is returned like any other, whatever the plan's deferred
|
|
432
|
+
* setting says — an adapter gating a computed value on this over-reports rather than under-;
|
|
433
|
+
* `eachSelectedField` in matches.ts says why that direction is the safe one.
|
|
434
|
+
*/ function selectedFieldNodeFor(plan, { field, node: fieldNode }) {
|
|
435
|
+
const { info } = plan;
|
|
436
|
+
let names;
|
|
437
|
+
function getSelection(path) {
|
|
438
|
+
if (path === undefined) {
|
|
439
|
+
names ??= (0, _matches.collectSelectedFieldNames)(info, (0, _graphql.getNamedType)(field.type), [
|
|
440
|
+
fieldNode
|
|
441
|
+
]);
|
|
442
|
+
return names;
|
|
443
|
+
}
|
|
444
|
+
const returnType = (0, _graphql.getNamedType)(field.type);
|
|
445
|
+
const match = (0, _matches.firstMatch)(info, returnType, fieldNode, path.map((name)=>({
|
|
446
|
+
name
|
|
447
|
+
})), {
|
|
448
|
+
prefix: (0, _matches.includeOf)(returnType)?.path
|
|
449
|
+
});
|
|
450
|
+
return match?.field ?? null;
|
|
451
|
+
}
|
|
452
|
+
return getSelection;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
//# sourceMappingURL=walk.js.map
|
package/lib/walk.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/walk.ts"],"names":["walkBranches","walkField","enter","plan","type","selection","adapter","typeSelection","collect","kind","query","enterVariant","variant","branches","resolved","flatMap","branch","resolveBranch","enteredTypes","Set","firstType","selectionSets","has","add","entered","selections","enterVariants","indirectPath","walked","walkSelections","fieldNodes","deferred","length","name","value","startsWith","info","include","includeOf","beneath","paths","path","fieldNode","matches","findMatches","match","push","field","modelFor","model","schema","getType","isObjectType","isInterfaceType","skipDeferredFragments","selectionSet","expandedBefore","visited","key","fragment","Kind","FRAGMENT_DEFINITION","id","FIELD","applicableFragment","as","fragmentTypeOf","fieldsApply","typeCondition","undefined","isSkipped","isDeferred","FRAGMENT_SPREAD","fragments","condition","getInterfaces","includes","context","getFields","PothosValidationError","fieldSelection","alias","join","collectField","EMPTY_MAPPING","position","parent","node","mapping","nested","args","getMappedArgumentValues","select","isThenable","then","mapped","runSelect","chain","nestedSelectionFor","selectedFieldNodeFor","fieldName","pending","rawQuery","pathOrInclude","typeName","returnType","getNamedType","Array","isArray","normalizeInclude","resolveType","target","modelOf","child","mergeQuery","matchesForModel","prefix","map","asType","error","abandon","awaitNested","queryNested","result","settled","played","play","toQuery","root","Object","keys","mappings","unionMappings","collectQuery","names","getSelection","collectSelectedFieldNames","firstMatch"],"mappings":"AAAA;;;;;CAKC;;;;;;;;;;;QAqGeA;eAAAA;;QAmQAC;eAAAA;;;sBAlWT;yBAYA;2BAEoD;yBAYpD;AAaP,gFAAgF,GAChF,SAASC,MACPC,IAAkC,EAClCC,IAAsB;IAEtB,MAAMC,YAAYF,KAAKG,OAAO,CAACC,aAAa,CAACH;IAE7C,IAAIC,WAAW;QACbF,KAAKK,OAAO,CAAC;YAAEC,MAAM;YAAQC,OAAOL;QAAU;IAChD;AACF;AAEA;;;;CAIC,GACD,SAASM,aACPR,IAAkC,EAClCC,IAAgB,EAChBQ,OAAmB;IAEnB,MAAMP,YAAYF,KAAKG,OAAO,CAACC,aAAa,CAACK;IAE7C,IAAIP,WAAW;QACbF,KAAKK,OAAO,CAAC;YAAEC,MAAM;YAAWL;YAAMQ;YAASF,OAAOL;QAAU;IAClE;AACF;AA6BO,SAASL,aACdG,IAAkC,EAClCU,QAAkB;IAElB,MAAMC,WAAWD,SAASE,OAAO,CAAC,CAACC,SAAWC,cAAcd,MAAMa;IAClE,MAAME,eAAe,IAAIC;IACzB,IAAIC;IAEJ,KAAK,MAAM,EAAEhB,IAAI,EAAEiB,aAAa,EAAE,IAAIP,SAAU;QAC9C,oFAAoF;QACpF,yFAAyF;QACzF,4FAA4F;QAC5F,iCAAiC;QACjC,IAAI,CAACI,aAAaI,GAAG,CAAClB,OAAO;YAC3Bc,a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AG;QAC9C3C,QAAQC,MAAM,CAAC/B,IAAI,GAAG0E,IAAAA,wBAAa,EAAC5C,QAAQC,MAAM,CAAC/B,IAAI,EAAEmE,OAAOM,QAAQ,CAACzE,IAAI;IAC/E;IAEA,OAAOhD;AACT;AAEA;;;;;CAKC,GACD,SAASwG,WACPD,KAAmC,EACnCvG,KAA+B;IAE/B,IAAIA,OAAO;QACTuG,MAAMoB,YAAY,CAAC3H;IACrB;AACF;AAEA;;;;;;;;;CASC,GACD,SAASyF,qBACPhG,IAAkC,EAClC,EAAE4C,KAAK,EAAEwC,MAAM7C,SAAS,EAAY;IAEpC,MAAM,EAAEN,IAAI,EAAE,GAAGjC;IACjB,IAAImI;IAIJ,SAASC,aAAa9F,IAAe;QACnC,IAAIA,SAAS4B,WAAW;YACtBiE,UAAUE,IAAAA,kCAAyB,EAACpG,MAAMsE,IAAAA,qBAAY,EAAC3D,MAAM3C,IAAI,GAAG;gBAACsC;aAAU;YAC/E,OAAO4F;QACT;QACA,MAAM7B,aAAaC,IAAAA,qBAAY,EAAC3D,MAAM3C,IAAI;QAC1C,MAAMyC,QAAQ4F,IAAAA,mBAAU,EACtBrG,MACAqE,YACA/D,WACAD,KAAK4E,GAAG,CAAC,CAACpF,OAAU,CAAA;gBAAEA;YAAK,CAAA,IAC3B;YAAEmF,QAAQ9E,IAAAA,kBAAS,EAACmE,aAAahE;QAAK;QAGxC,OAAOI,OAAOE,SAAS;IACzB;IAEA,OAAOwF;AACT"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pothos/selection-mapper",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "shared selection-to-query walker for Pothos ORM plugins",
|
|
5
|
+
"main": "./lib/index.js",
|
|
6
|
+
"types": "./dts/index.d.ts",
|
|
7
|
+
"module": "./esm/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
"import": {
|
|
10
|
+
"default": "./esm/index.js"
|
|
11
|
+
},
|
|
12
|
+
"require": {
|
|
13
|
+
"types": "./dts/index.d.ts",
|
|
14
|
+
"default": "./lib/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/hayes/pothos.git",
|
|
20
|
+
"directory": "packages/selection-mapper"
|
|
21
|
+
},
|
|
22
|
+
"author": "Michael Hayes",
|
|
23
|
+
"license": "ISC",
|
|
24
|
+
"keywords": [
|
|
25
|
+
"pothos",
|
|
26
|
+
"graphql",
|
|
27
|
+
"schema",
|
|
28
|
+
"typescript",
|
|
29
|
+
"plugin"
|
|
30
|
+
],
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public",
|
|
33
|
+
"provenance": true
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@pothos/core": "*",
|
|
37
|
+
"graphql": "^16.10.0 || ^17.0.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"graphql": "^17.0.1",
|
|
41
|
+
"graphql-tag": "^2.12.6",
|
|
42
|
+
"vitest": "^4.1.4",
|
|
43
|
+
"@pothos/core": "4.14.0",
|
|
44
|
+
"@pothos/test-utils": "2.2.1"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"type": "tsc --project tsconfig.type.json",
|
|
48
|
+
"build": "pnpm build:clean && pnpm build:cjs && pnpm build:dts && pnpm build:esm",
|
|
49
|
+
"build:clean": "git clean -dfX esm lib",
|
|
50
|
+
"build:cjs": "swc src -d lib --config-file .swcrc -C module.type=commonjs --strip-leading-paths",
|
|
51
|
+
"build:esm": "cp -r dts/* esm/ && swc src -d esm --config-file .swcrc -C module.type=es6 --strip-leading-paths",
|
|
52
|
+
"build:dts": "tsc",
|
|
53
|
+
"test": "pnpm vitest run"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/adapter.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ORM boundary: what an adapter tells the traversal, and where the selections it produces
|
|
3
|
+
* accumulate. One class, so an adapter's state and helpers have a home and the members it has
|
|
4
|
+
* nothing to say about are inherited rather than checked for.
|
|
5
|
+
*/
|
|
6
|
+
import type { GraphQLField, GraphQLNamedType } from 'graphql';
|
|
7
|
+
import type { MergeOptions, SelectFn, TypeLevelConflict, WalkedType } from './types.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* What the traversal requires of a node: the model it loads. `Model` is the adapter's model
|
|
11
|
+
* description: whatever it needs to look a relation's target up by name. One object per model,
|
|
12
|
+
* so model identity is the same as model equality.
|
|
13
|
+
*/
|
|
14
|
+
export interface NodeBase<Model> {
|
|
15
|
+
model: Model;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The ORM boundary, translation only. `Model` is the model description a node carries, `Query`
|
|
20
|
+
* the ORM's own selection format (prisma `{ select, include, ...args }`, drizzle
|
|
21
|
+
* `DBQueryConfig`), opaque to the traversal, and `NodeType` what selections accumulate into, of
|
|
22
|
+
* which the traversal reads only `model`.
|
|
23
|
+
*
|
|
24
|
+
* Six members must be answered: three translate a schema into queries (`modelFor`,
|
|
25
|
+
* `typeSelection`, `fieldSelection`) and three accumulate them (`createNode`, `mergeQuery`,
|
|
26
|
+
* `toQuery`). The four below those are the merge rules, inherited with the answer an adapter with
|
|
27
|
+
* no conflicts to report wants: nothing ever conflicts, and node-to-node work round-trips through
|
|
28
|
+
* `toQuery`. An adapter that gives every consumer of a relation its own slot inherits all four.
|
|
29
|
+
*
|
|
30
|
+
* `NodeAdapter` in node.ts overrides all four over a concrete query tree, and is what the prisma
|
|
31
|
+
* and drizzle adapters extend; an adapter whose query is not such a tree extends this directly.
|
|
32
|
+
*/
|
|
33
|
+
export abstract class Adapter<Model, Query, NodeType extends NodeBase<Model>> {
|
|
34
|
+
/**
|
|
35
|
+
* Whether a fragment under `@defer` is walked. `EntryOptions.skipDeferredFragments` overrides
|
|
36
|
+
* it for one entry point.
|
|
37
|
+
*/
|
|
38
|
+
skipDeferredFragments = true;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The model a type carries, or undefined. Does not follow indirect includes (the walker's own
|
|
42
|
+
* `modelOf` does). One object per model: identity is model identity.
|
|
43
|
+
*/
|
|
44
|
+
abstract modelFor(type: GraphQLNamedType): Model | undefined;
|
|
45
|
+
|
|
46
|
+
/** What the type always needs, merged whenever the type is entered, or undefined. */
|
|
47
|
+
abstract typeSelection(type: GraphQLNamedType): Query | undefined;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* What the field selects: a static query, a select function, or nothing. `type` is the type the
|
|
51
|
+
* field is being walked on (its parent type, or a same-model type the plan moved to), for an
|
|
52
|
+
* adapter that classifies a selection's keys against the parent model.
|
|
53
|
+
*/
|
|
54
|
+
abstract fieldSelection(
|
|
55
|
+
field: GraphQLField<unknown, unknown>,
|
|
56
|
+
type: WalkedType,
|
|
57
|
+
): Query | SelectFn<Query> | undefined;
|
|
58
|
+
|
|
59
|
+
/** A fresh, empty node for `model`. */
|
|
60
|
+
abstract createNode(model: Model): NodeType;
|
|
61
|
+
|
|
62
|
+
/** Folds `query` into `node`, in place. Never mutates `query`. */
|
|
63
|
+
abstract mergeQuery(node: NodeType, query: Query, options?: MergeOptions): void;
|
|
64
|
+
|
|
65
|
+
/** The node as a query of this ORM's format. */
|
|
66
|
+
abstract toQuery(node: NodeType): Query;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Whether merging `query` into `node` would leave everything already selected as it is. A yes
|
|
70
|
+
* or no; `firstConflict` is the version that names a key. Inherited: nothing ever conflicts.
|
|
71
|
+
*/
|
|
72
|
+
canMergeQuery(_node: NodeType, _query: Query, _options?: MergeOptions): boolean {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The first key of a type-level `query` that conflicts with what `node` holds, for the error
|
|
78
|
+
* message that names it. Inherited: none.
|
|
79
|
+
*/
|
|
80
|
+
firstConflict(_node: NodeType, _query: Query): TypeLevelConflict | undefined {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* `mergeQuery` from another node: everything `from` holds, merged into `node`. Inherited: the
|
|
86
|
+
* round trip through `toQuery`.
|
|
87
|
+
*/
|
|
88
|
+
mergeNode(node: NodeType, from: NodeType): void {
|
|
89
|
+
this.mergeQuery(node, this.toQuery(from));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** `canMergeQuery` node to node. Inherited: the round trip through `toQuery`. */
|
|
93
|
+
canMergeNode(node: NodeType, from: NodeType): boolean {
|
|
94
|
+
return this.canMergeQuery(node, this.toQuery(from));
|
|
95
|
+
}
|
|
96
|
+
}
|