@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/esm/matches.js
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
import { createContextCache, PothosValidationError } from '@pothos/core';
|
|
2
|
+
import { doTypesOverlap, GraphQLIncludeDirective, GraphQLSkipDirective, getDirectiveValues, getNamedType, isAbstractType, isInterfaceType, isObjectType, Kind } from 'graphql';
|
|
3
|
+
/** The include a type carries, if any. */ export function includeOf(type) {
|
|
4
|
+
return type.extensions?.pothosIndirectInclude;
|
|
5
|
+
}
|
|
6
|
+
/** Follows `pothosIndirectInclude.getType()` until a type without one. */ export function resolveType(schema, type) {
|
|
7
|
+
let target = type;
|
|
8
|
+
let include = includeOf(target);
|
|
9
|
+
while(include){
|
|
10
|
+
target = schema.getType(include.getType());
|
|
11
|
+
include = includeOf(target);
|
|
12
|
+
}
|
|
13
|
+
return target;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The model of a type, following indirect includes, which `Adapter.modelFor` does not. Every
|
|
17
|
+
* model question the plan asks goes through here.
|
|
18
|
+
*/ export function modelOf(adapter, schema, type) {
|
|
19
|
+
return adapter.modelFor(resolveType(schema, type));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* `matches` without the ones whose field returns a model other than `targetType`'s. Several
|
|
23
|
+
* implementations of an interface may share a field name while returning different models, and
|
|
24
|
+
* their selections must never be merged into the same query. Types without a model always match,
|
|
25
|
+
* and a target without one filters nothing.
|
|
26
|
+
*/ export function matchesForModel(adapter, schema, matches, targetType) {
|
|
27
|
+
const targetModel = modelOf(adapter, schema, targetType);
|
|
28
|
+
if (!targetModel) {
|
|
29
|
+
return matches;
|
|
30
|
+
}
|
|
31
|
+
return matches.filter((match)=>{
|
|
32
|
+
const model = modelOf(adapter, schema, match.type);
|
|
33
|
+
return !model || model === targetModel;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The one selection traversal this package owns. It reads the selections of `selection` at
|
|
38
|
+
* `level`: drops what `@skip`/`@include` removes, classifies every fragment
|
|
39
|
+
* (`resolveFragmentTypes`), notes whether a `@defer` was crossed, descends into the fragments
|
|
40
|
+
* that apply, expands a named fragment once per state (`memo`), and calls `visit` at every field
|
|
41
|
+
* that applies to `level.type`. What happens at a field is the caller's alone, which is the whole
|
|
42
|
+
* of the difference between its consumers: `matchPath` matches one path segment's name and
|
|
43
|
+
* descends with the rest of the path, `collectSelectedFieldNames` adds every name and descends no
|
|
44
|
+
* further, and `firstMatch` takes the first match and stops.
|
|
45
|
+
*
|
|
46
|
+
* `segment` is the indirect-include path segment the caller is looking for, when it has one; only
|
|
47
|
+
* its `type` is read, to pin the type a fragment is expected on.
|
|
48
|
+
*
|
|
49
|
+
* `memo` records the named fragments already expanded, keyed on the fragment, the two types, the
|
|
50
|
+
* deferred flag, and `memoKey` — whatever else the caller varies as it descends (`matchPath`
|
|
51
|
+
* passes its remaining path length and its alias path). A fragment spread more than once at one
|
|
52
|
+
* point of the traversal is expanded once: a valid fragment DAG can spread the same fragment at
|
|
53
|
+
* every level, which makes expanding every spread exponential in its depth. A caller that varies
|
|
54
|
+
* nothing passes no `memoKey`, leaving its memo finer than it needs to be by the deferred flag —
|
|
55
|
+
* a little time, never correctness, for a caller that accumulates.
|
|
56
|
+
*
|
|
57
|
+
* This traversal decides nothing about `@defer`: it reports `level.deferred` and the visitor
|
|
58
|
+
* chooses, and the consumers do not choose alike. `findMatches` reports the flag and
|
|
59
|
+
* `walkBranches` honours it, so a deferred branch is entered but its fields are not walked while
|
|
60
|
+
* `skipDeferredFragments` is set; `firstMatch` (the `selectedFieldNode` a select function is
|
|
61
|
+
* handed) and `selectedFieldNames` both discard it.
|
|
62
|
+
*
|
|
63
|
+
* The last two therefore over-report — they answer "yes, the document selects this" for a
|
|
64
|
+
* selection branch resolution will skip — and that direction is what makes the divergence safe.
|
|
65
|
+
* The plugins use both as gates, and a gate computed from an over-reporting source can only be
|
|
66
|
+
* wrongly true, which loads a column or a count nothing reads; it can never be wrongly false, so
|
|
67
|
+
* no row goes missing and anything under-fetched falls through to the model loader, which
|
|
68
|
+
* re-queries. Make either honour the flag and the gates start under-reporting, which loses data;
|
|
69
|
+
* `matches.test.ts` pins the direction.
|
|
70
|
+
*/ function eachSelectedField(info, selection, level, segment, visit, memo, memoKey = '') {
|
|
71
|
+
if (!selection.selectionSet) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
const { type, path, deferred } = level;
|
|
75
|
+
// A segment that pins a type condition pins it here too: its field must be found under a
|
|
76
|
+
// fragment on that type, never taken from a level that merely has a field of the same name.
|
|
77
|
+
const expectedType = pinnedType(info, segment) ?? level.expectedType;
|
|
78
|
+
const fieldsApply = expectedType.name === type.name && (isObjectType(type) || isInterfaceType(type));
|
|
79
|
+
for (const sel of selection.selectionSet.selections){
|
|
80
|
+
if (isSkipped(info, sel)) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
let fragment;
|
|
84
|
+
switch(sel.kind){
|
|
85
|
+
case Kind.FIELD:
|
|
86
|
+
{
|
|
87
|
+
if (fieldsApply && visit(sel, level) === true) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
case Kind.FRAGMENT_SPREAD:
|
|
93
|
+
fragment = info.fragments[sel.name.value];
|
|
94
|
+
break;
|
|
95
|
+
case Kind.INLINE_FRAGMENT:
|
|
96
|
+
fragment = sel;
|
|
97
|
+
break;
|
|
98
|
+
default:
|
|
99
|
+
{
|
|
100
|
+
const unsupported = sel;
|
|
101
|
+
throw new PothosValidationError(`Unsupported selection kind ${unsupported.kind}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const next = resolveFragmentTypes(info, fragment.typeCondition ? info.schema.getType(fragment.typeCondition.name.value) : undefined, type, expectedType);
|
|
105
|
+
const deferredHere = deferred || isDeferred(info, sel);
|
|
106
|
+
if (fragment.kind === Kind.FRAGMENT_DEFINITION) {
|
|
107
|
+
const state = `${fragment.name.value}|${next.type.name}|${next.expectedType.name}|${deferredHere}|${memoKey}`;
|
|
108
|
+
if (memo.has(state)) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
memo.add(state);
|
|
112
|
+
}
|
|
113
|
+
const stopped = eachSelectedField(info, fragment, {
|
|
114
|
+
type: next.type,
|
|
115
|
+
expectedType: next.expectedType,
|
|
116
|
+
path,
|
|
117
|
+
deferred: deferredHere
|
|
118
|
+
}, segment, visit, memo, memoKey);
|
|
119
|
+
if (stopped) {
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The field `name` of `type`, or a validation error. A document naming a field the type does not
|
|
127
|
+
* have only reaches here when graphql validation was skipped; without this it would read a
|
|
128
|
+
* property of undefined.
|
|
129
|
+
*/ function fieldOn(type, name) {
|
|
130
|
+
const field = isObjectType(type) || isInterfaceType(type) ? type.getFields()[name] : undefined;
|
|
131
|
+
if (!field) {
|
|
132
|
+
throw new PothosValidationError(`Unknown field ${name} on ${type.name}`);
|
|
133
|
+
}
|
|
134
|
+
return field;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Follows `includePath` from `level`, starting at segment `at`, to the fields selected at its end,
|
|
138
|
+
* reporting each to `onMatch` in document order and stopping when `onMatch` says to. Every step is
|
|
139
|
+
* one `eachSelectedField` whose visitor matches that segment by name and follows the rest of the
|
|
140
|
+
* path beneath it. The path is walked by index, so a step allocates nothing but the alias path it
|
|
141
|
+
* grew.
|
|
142
|
+
*/ function matchPath(info, selection, level, includePath, at, onMatch, memo) {
|
|
143
|
+
const remaining = includePath.length - at;
|
|
144
|
+
if (remaining === 0) {
|
|
145
|
+
return onMatch({
|
|
146
|
+
type: level.type,
|
|
147
|
+
field: selection,
|
|
148
|
+
path: level.path,
|
|
149
|
+
deferred: level.deferred
|
|
150
|
+
}) === true;
|
|
151
|
+
}
|
|
152
|
+
const segment = includePath[at];
|
|
153
|
+
return eachSelectedField(info, selection, level, segment, (field, on)=>{
|
|
154
|
+
if (field.name.value !== segment.name) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
const returnType = getNamedType(fieldOn(on.type, field.name.value).type);
|
|
158
|
+
return matchPath(info, field, {
|
|
159
|
+
type: returnType,
|
|
160
|
+
expectedType: returnType,
|
|
161
|
+
path: [
|
|
162
|
+
...on.path,
|
|
163
|
+
field.alias?.value ?? field.name.value
|
|
164
|
+
],
|
|
165
|
+
deferred: on.deferred
|
|
166
|
+
}, includePath, at + 1, onMatch, memo);
|
|
167
|
+
}, memo, `${remaining}|${level.path.join('.')}`);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Follows each of `paths`, prefixed by any type-level `prefix`, from `selection` (which is expected
|
|
171
|
+
* to be a selection on `type`), reporting the fields at their ends to `onMatch` in path order then
|
|
172
|
+
* document order, and stopping when `onMatch` says to. Paths are followed through fragments; see
|
|
173
|
+
* `resolveFragmentTypes` for the rules. Each path gets its own memo: what one path expanded says
|
|
174
|
+
* nothing about the next.
|
|
175
|
+
*/ function eachMatch(info, type, selection, paths, { prefix, path = [], deferred = false }, onMatch) {
|
|
176
|
+
for (const includePath of paths){
|
|
177
|
+
const stopped = matchPath(info, selection, {
|
|
178
|
+
type,
|
|
179
|
+
expectedType: type,
|
|
180
|
+
path,
|
|
181
|
+
deferred
|
|
182
|
+
}, prefix?.length ? [
|
|
183
|
+
...prefix,
|
|
184
|
+
...includePath
|
|
185
|
+
] : includePath, 0, onMatch, new Set());
|
|
186
|
+
if (stopped) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Every field selected at the end of one of `paths`, each with the `deferred` flag
|
|
193
|
+
* `eachSelectedField` computed for it.
|
|
194
|
+
*/ export function findMatches(info, type, selection, paths, options = {}) {
|
|
195
|
+
const matches = [];
|
|
196
|
+
eachMatch(info, type, selection, paths, options, (match)=>{
|
|
197
|
+
matches.push(match);
|
|
198
|
+
});
|
|
199
|
+
return matches;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* The first field selected at the end of `path`, in document order, or undefined. The traversal
|
|
203
|
+
* stops at it. The match's `deferred` flag is reported but this function's caller — the
|
|
204
|
+
* `selectedFieldNode` handed to a select function — discards it; see `eachSelectedField` for why
|
|
205
|
+
* over-reporting a deferred selection there is safe.
|
|
206
|
+
*/ export function firstMatch(info, type, selection, path, { prefix } = {}) {
|
|
207
|
+
let first;
|
|
208
|
+
eachMatch(info, type, selection, [
|
|
209
|
+
path
|
|
210
|
+
], {
|
|
211
|
+
prefix
|
|
212
|
+
}, (match)=>{
|
|
213
|
+
first = match;
|
|
214
|
+
return true;
|
|
215
|
+
});
|
|
216
|
+
return first;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Per request context, per execution (`info.variableValues` is built once per execution, and the
|
|
220
|
+
* names depend on the variables through `@skip`/`@include`), per return type, then per field node.
|
|
221
|
+
*/ const selectedFieldNamesCache = createContextCache(()=>new WeakMap());
|
|
222
|
+
/**
|
|
223
|
+
* The names of the fields the document selects directly beneath the field being resolved, seen
|
|
224
|
+
* through any wrapper on its return type. Every row of a list resolves the field with the same
|
|
225
|
+
* field nodes, so the result is memoised on them for the execution — on the nodes rather than on
|
|
226
|
+
* `info.fieldNodes`, which graphql-js 17 builds anew for every resolve, and beneath that on
|
|
227
|
+
* `info.variableValues` (one object per execution) and the return type.
|
|
228
|
+
*
|
|
229
|
+
* `@skip` and `@include` are honoured, `@defer` is not: a name selected only under a deferred
|
|
230
|
+
* fragment is reported as selected even when the plan that loads the row will not walk it. This
|
|
231
|
+
* is the over-reporting side of the divergence `eachSelectedField` describes, which is the safe
|
|
232
|
+
* side to gate a column or a count on. A caller that needs to know what the plan will actually
|
|
233
|
+
* load must ask the plan.
|
|
234
|
+
*/ export function selectedFieldNames(context, info) {
|
|
235
|
+
const byExecution = selectedFieldNamesCache(context);
|
|
236
|
+
let byType = byExecution.get(info.variableValues);
|
|
237
|
+
if (!byType) {
|
|
238
|
+
byType = new WeakMap();
|
|
239
|
+
byExecution.set(info.variableValues, byType);
|
|
240
|
+
}
|
|
241
|
+
let memo = memoIn(byType, getNamedType(info.returnType));
|
|
242
|
+
for (const node of info.fieldNodes){
|
|
243
|
+
memo = memoIn(memo.next, node);
|
|
244
|
+
}
|
|
245
|
+
memo.names ??= collectSelectedFieldNames(info);
|
|
246
|
+
return memo.names;
|
|
247
|
+
}
|
|
248
|
+
function memoIn(map, key) {
|
|
249
|
+
let memo = map.get(key);
|
|
250
|
+
if (!memo) {
|
|
251
|
+
memo = {
|
|
252
|
+
next: new WeakMap()
|
|
253
|
+
};
|
|
254
|
+
map.set(key, memo);
|
|
255
|
+
}
|
|
256
|
+
return memo;
|
|
257
|
+
}
|
|
258
|
+
export function collectSelectedFieldNames(info, returnType = getNamedType(info.returnType), fieldNodes = info.fieldNodes) {
|
|
259
|
+
const prefix = includeOf(returnType)?.path;
|
|
260
|
+
const names = new Set();
|
|
261
|
+
// Every name at one level: `eachSelectedField`'s last step for all of them at once. The level's
|
|
262
|
+
// `deferred` flag is never read, which is the asymmetry `eachSelectedField` documents.
|
|
263
|
+
const addName = (field)=>{
|
|
264
|
+
names.add(field.name.value);
|
|
265
|
+
};
|
|
266
|
+
for (const node of fieldNodes){
|
|
267
|
+
// Through a wrapper, the fields are those beneath its inner field.
|
|
268
|
+
const roots = prefix?.length ? findMatches(info, returnType, node, [
|
|
269
|
+
prefix
|
|
270
|
+
]) : [
|
|
271
|
+
{
|
|
272
|
+
type: returnType,
|
|
273
|
+
field: node,
|
|
274
|
+
deferred: false
|
|
275
|
+
}
|
|
276
|
+
];
|
|
277
|
+
for (const root of roots){
|
|
278
|
+
eachSelectedField(info, root.field, {
|
|
279
|
+
type: root.type,
|
|
280
|
+
expectedType: root.type,
|
|
281
|
+
path: [],
|
|
282
|
+
deferred: root.deferred
|
|
283
|
+
}, undefined, addName, new Set());
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return names;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* The type to walk and the type the next segment is expected on, when descending into a fragment.
|
|
290
|
+
* `expected` arrives pinned: `eachSelectedField` resolved any `type` on the segment before it
|
|
291
|
+
* classified a single selection.
|
|
292
|
+
*
|
|
293
|
+
* - A fragment on the expected type, or on an abstract type the expected type belongs to, walks as
|
|
294
|
+
* the expected type: every field selectable there also exists on it.
|
|
295
|
+
* - A fragment that narrows an abstract expected type to an implementation or member, or to an
|
|
296
|
+
* abstract type that overlaps it, advances the expected type so the segment's field can be found
|
|
297
|
+
* on the narrower type.
|
|
298
|
+
* - Fragments on unrelated types keep the expected type, so their fields are skipped until a nested
|
|
299
|
+
* fragment narrows back to it.
|
|
300
|
+
*/ function resolveFragmentTypes(info, fragmentType, type, expected) {
|
|
301
|
+
if (!fragmentType || fragmentType.name === expected.name) {
|
|
302
|
+
return {
|
|
303
|
+
type: fragmentType ?? type,
|
|
304
|
+
expectedType: expected
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
if (isAbstractType(fragmentType) && (isObjectType(expected) || isInterfaceType(expected)) && info.schema.isSubType(fragmentType, expected)) {
|
|
308
|
+
return {
|
|
309
|
+
type: expected,
|
|
310
|
+
expectedType: expected
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
if (isAbstractType(expected)) {
|
|
314
|
+
const narrows = isObjectType(fragmentType) || isInterfaceType(fragmentType) ? info.schema.isSubType(expected, fragmentType) : false;
|
|
315
|
+
const overlaps = isAbstractType(fragmentType) && doTypesOverlap(info.schema, expected, fragmentType);
|
|
316
|
+
if (narrows || overlaps) {
|
|
317
|
+
return {
|
|
318
|
+
type: fragmentType,
|
|
319
|
+
expectedType: fragmentType
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return {
|
|
324
|
+
type: fragmentType,
|
|
325
|
+
expectedType: expected
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* The type an indirect-include segment pins its field to, or undefined when it pins none. A
|
|
330
|
+
* pinned segment names the fragment type condition the field must be found under, so the pin
|
|
331
|
+
* decides both which fragments are descended into and whether a field selected directly at a
|
|
332
|
+
* level counts as the segment's.
|
|
333
|
+
*/ function pinnedType(info, segment) {
|
|
334
|
+
if (!segment?.type) {
|
|
335
|
+
return undefined;
|
|
336
|
+
}
|
|
337
|
+
const pinned = info.schema.getType(segment.type);
|
|
338
|
+
if (!pinned) {
|
|
339
|
+
throw new PothosValidationError(`Unknown type ${segment.type} in indirect include path segment ${segment.name}`);
|
|
340
|
+
}
|
|
341
|
+
return pinned;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Turns a path handed to `nestedSelection` into an include. A string segment is a field of the
|
|
345
|
+
* type the previous segment reached; the walker narrows through fragments on its own. A
|
|
346
|
+
* `{ name, type }` segment pins the fragment type condition the field must be found under, and
|
|
347
|
+
* is a field of that type.
|
|
348
|
+
*/ export function normalizeInclude(path, type, expectedType, schema) {
|
|
349
|
+
let currentType = path.length > 0 ? type : expectedType ?? type;
|
|
350
|
+
const normalized = [];
|
|
351
|
+
if (path.length === 0 && !(isObjectType(currentType) || isInterfaceType(currentType))) {
|
|
352
|
+
throw new PothosValidationError(`Expected ${currentType} to be an Object or Interface type`);
|
|
353
|
+
}
|
|
354
|
+
for (const segment of path){
|
|
355
|
+
const { name, type: pinned } = typeof segment === 'string' ? {
|
|
356
|
+
name: segment
|
|
357
|
+
} : segment;
|
|
358
|
+
if (pinned) {
|
|
359
|
+
const pinnedType = schema.getType(pinned);
|
|
360
|
+
if (!pinnedType) {
|
|
361
|
+
throw new PothosValidationError(`Unknown type ${pinned} in nested selection path segment ${name}`);
|
|
362
|
+
}
|
|
363
|
+
currentType = pinnedType;
|
|
364
|
+
}
|
|
365
|
+
if (!(isObjectType(currentType) || isInterfaceType(currentType))) {
|
|
366
|
+
throw new PothosValidationError(`Expected ${currentType} to be an Object or Interface type`);
|
|
367
|
+
}
|
|
368
|
+
const field = currentType.getFields()[name];
|
|
369
|
+
if (!field) {
|
|
370
|
+
throw new PothosValidationError(`Expected ${currentType} to have a field ${name}`);
|
|
371
|
+
}
|
|
372
|
+
currentType = getNamedType(field.type);
|
|
373
|
+
if (!(isObjectType(currentType) || isInterfaceType(currentType))) {
|
|
374
|
+
throw new PothosValidationError(`Expected ${currentType} to be an Object or Interface type`);
|
|
375
|
+
}
|
|
376
|
+
normalized.push(pinned ? {
|
|
377
|
+
name,
|
|
378
|
+
type: pinned
|
|
379
|
+
} : {
|
|
380
|
+
name
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
const targetType = currentType;
|
|
384
|
+
return {
|
|
385
|
+
getType: ()=>expectedType?.name ?? targetType.name,
|
|
386
|
+
path: normalized
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Whether a field or fragment is under `@skip(if: true)` or `@include(if: false)`. Asked of every
|
|
391
|
+
* selection the traversal reads, so it answers a selection carrying no directive at all — nearly
|
|
392
|
+
* every one — without building a directive's argument values twice.
|
|
393
|
+
*/ export function isSkipped(info, selection) {
|
|
394
|
+
if (!selection.directives?.length) {
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
const skip = getDirectiveValues(GraphQLSkipDirective, selection, info.variableValues);
|
|
398
|
+
if (skip?.if === true) {
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
const include = getDirectiveValues(GraphQLIncludeDirective, selection, info.variableValues);
|
|
402
|
+
if (include?.if === false) {
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
/** Whether a fragment is under `@defer` (when the schema declares the directive). */ export function isDeferred(info, node) {
|
|
408
|
+
if (!node.directives?.length) {
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
const deferDirective = info.schema.getDirective('defer');
|
|
412
|
+
if (!deferDirective) {
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
const defer = getDirectiveValues(deferDirective, node, info.variableValues);
|
|
416
|
+
return !!defer && defer.if !== false;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
//# sourceMappingURL=matches.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/matches.ts"],"names":["createContextCache","PothosValidationError","doTypesOverlap","GraphQLIncludeDirective","GraphQLSkipDirective","getDirectiveValues","getNamedType","isAbstractType","isInterfaceType","isObjectType","Kind","includeOf","type","extensions","pothosIndirectInclude","resolveType","schema","target","include","getType","modelOf","adapter","modelFor","matchesForModel","matches","targetType","targetModel","filter","match","model","eachSelectedField","info","selection","level","segment","visit","memo","memoKey","selectionSet","path","deferred","expectedType","pinnedType","fieldsApply","name","sel","selections","isSkipped","fragment","kind","FIELD","FRAGMENT_SPREAD","fragments","value","INLINE_FRAGMENT","unsupported","next","resolveFragmentTypes","typeCondition","undefined","deferredHere","isDeferred","FRAGMENT_DEFINITION","state","has","add","stopped","fieldOn","field","getFields","matchPath","includePath","at","onMatch","remaining","length","on","returnType","alias","join","eachMatch","paths","prefix","Set","findMatches","options","push","firstMatch","first","selectedFieldNamesCache","WeakMap","selectedFieldNames","context","byExecution","byType","get","variableValues","set","memoIn","node","fieldNodes","names","collectSelectedFieldNames","map","key","addName","roots","root","fragmentType","expected","isSubType","narrows","overlaps","pinned","normalizeInclude","currentType","normalized","directives","skip","if","deferDirective","getDirective","defer"],"mappings":"AAAA,SAASA,kBAAkB,EAAEC,qBAAqB,QAAQ,eAAe;AACzE,SACEC,cAAc,EAKdC,uBAAuB,EAIvBC,oBAAoB,EACpBC,kBAAkB,EAClBC,YAAY,EAEZC,cAAc,EACdC,eAAe,EACfC,YAAY,EACZC,IAAI,QACC,UAAU;AAyCjB,wCAAwC,GACxC,OAAO,SAASC,UAAUC,IAAsB;IAC9C,OAAOA,KAAKC,UAAU,EAAEC;AAC1B;AAEA,wEAAwE,GACxE,OAAO,SAASC,YAAYC,MAAqB,EAAEJ,IAAsB;IACvE,IAAIK,SAASL;IACb,IAAIM,UAAUP,UAAUM;IAExB,MAAOC,QAAS;QACdD,SAASD,OAAOG,OAAO,CAACD,QAAQC,OAAO;QACvCD,UAAUP,UAAUM;IACtB;IAEA,OAAOA;AACT;AAEA;;;CAGC,GACD,OAAO,SAASG,QACdC,OAA2B,EAC3BL,MAAqB,EACrBJ,IAAsB;IAEtB,OAAOS,QAAQC,QAAQ,CAACP,YAAYC,QAAQJ;AAC9C;AAEA;;;;;CAKC,GACD,OAAO,SAASW,gBACdF,OAA2B,EAC3BL,MAAqB,EACrBQ,OAAgB,EAChBC,UAA4B;IAE5B,MAAMC,cAAcN,QAAQC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OAAO;AACT;AAEA,mFAAmF,GACnF,OAAO,SAAS1D,WACd9B,IAAwB,EACxBoE,IAA6C;IAE7C,IAAI,CAACA,KAAKkB,UAAU,EAAE1C,QAAQ;QAC5B,OAAO;IACT;IAEA,MAAM6C,iBAAiBzF,KAAKf,MAAM,CAACyG,YAAY,CAAC;IAChD,IAAI,CAACD,gBAAgB;QACnB,OAAO;IACT;IAEA,MAAME,QAAQrH,mBAAmBmH,gBAAgBrB,MAAMpE,KAAKiE,cAAc;IAC1E,OAAO,CAAC,CAAC0B,SAASA,MAAMH,EAAE,KAAK;AACjC"}
|
package/esm/node.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Adapter, type NodeBase } from './adapter.js';
|
|
2
|
+
import type { MergeOptions, TypeLevelConflict } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* One level of the query being built, used by the prisma and drizzle adapters: the model, its
|
|
5
|
+
* arguments, the columns it selects (`null` = every column, which is final: a node never goes
|
|
6
|
+
* back to named columns), its relations and its computed values (prisma `_count` keys, drizzle
|
|
7
|
+
* `extras`). An adapter whose query is not a tree of this shape extends `Adapter` directly.
|
|
8
|
+
*/
|
|
9
|
+
export interface Node<Model> extends NodeBase<Model> {
|
|
10
|
+
args: object;
|
|
11
|
+
columns: Set<string> | null;
|
|
12
|
+
relations: Map<string, Node<Model>>;
|
|
13
|
+
computed: Map<string, unknown>;
|
|
14
|
+
}
|
|
15
|
+
export declare function createNode<Model>(model: Model): Node<Model>;
|
|
16
|
+
/**
|
|
17
|
+
* The only way to obtain a child node: get the relation's node or create it. `value` is what the
|
|
18
|
+
* map holds for the relation, checked here because a thenable is an un-awaited
|
|
19
|
+
* `nestedSelection()` that would otherwise be spread as an empty object.
|
|
20
|
+
*/
|
|
21
|
+
export declare function relation<Model>(node: Node<Model>, name: string, model: Model, value: unknown): Node<Model>;
|
|
22
|
+
/**
|
|
23
|
+
* What an adapter reports for one key of its query. Called by `NodeAdapter.visitQuery`; the
|
|
24
|
+
* visitor decides what to do with the key, so merging, checking and conflict-finding share one
|
|
25
|
+
* key loop per adapter instead of one each.
|
|
26
|
+
*
|
|
27
|
+
* A visitor is re-entrant: `relation` reads the nested query with the same visitor, so a
|
|
28
|
+
* `visitQuery` must hold no state of its own across a callback.
|
|
29
|
+
*/
|
|
30
|
+
export interface QueryVisitor<Model, Query> {
|
|
31
|
+
/** A named column of the model. */
|
|
32
|
+
column(name: string): void;
|
|
33
|
+
/** Every column. Final — a node never goes back to named columns. */
|
|
34
|
+
allColumns(): void;
|
|
35
|
+
/** A relation, with the model it targets and the query selected beneath it. */
|
|
36
|
+
relation(name: string, model: Model, query: Query): void;
|
|
37
|
+
/**
|
|
38
|
+
* A value the ORM computes per row, neither a column nor a relation (prisma `_count` keys,
|
|
39
|
+
* drizzle `extras`). `kind` is what a conflict on it is called to a user of that ORM, whose own
|
|
40
|
+
* word for these is not the shared one (prisma's counts read as a relation).
|
|
41
|
+
*/
|
|
42
|
+
computed(name: string, value: unknown, kind?: TypeLevelConflict['kind']): void;
|
|
43
|
+
/**
|
|
44
|
+
* The query arguments, normalized. A non-empty set replaces whatever the node holds. Reported
|
|
45
|
+
* on every pass, empty or not, since a merge check compares a query's arguments to the node's
|
|
46
|
+
* whether or not the query has any.
|
|
47
|
+
*/
|
|
48
|
+
args(args: object): void;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The adapter the prisma and drizzle adapters extend: the node tree above with every merge,
|
|
52
|
+
* compare and conflict rule this package owns. A subclass answers `visitQuery` and `toQuery`, plus
|
|
53
|
+
* the three translation members of `Adapter`.
|
|
54
|
+
*/
|
|
55
|
+
export declare abstract class NodeAdapter<Model, Query> extends Adapter<Model, Query, Node<Model>> {
|
|
56
|
+
/**
|
|
57
|
+
* One merger and one merge check per adapter, re-used down the tree: each saves and restores
|
|
58
|
+
* the node it is at rather than allocating a visitor per level, so a merge allocates only what
|
|
59
|
+
* the subclass's own `visitQuery` does. They cannot be this object — a lenient merge runs a
|
|
60
|
+
* check inside itself, so one `this` could not hold both cursors.
|
|
61
|
+
*/
|
|
62
|
+
private readonly merger;
|
|
63
|
+
private readonly mergeCheck;
|
|
64
|
+
/** Every key of `query`, read against `model` and reported to `visit`. The ORM's key loop. */
|
|
65
|
+
abstract visitQuery(query: Query, model: Model, visit: QueryVisitor<Model, Query>): void;
|
|
66
|
+
createNode(model: Model): Node<Model>;
|
|
67
|
+
mergeQuery(node: Node<Model>, query: Query, options?: MergeOptions): void;
|
|
68
|
+
canMergeQuery(node: Node<Model>, query: Query, options?: MergeOptions): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Whether `value` cannot join `computed` under `name`. A `deepEqual` against the value already
|
|
71
|
+
* there, which is what an adapter whose computed values are plain values wants; drizzle
|
|
72
|
+
* compares its `extras` by identity, and prisma overrides it for the `_count: true` wildcard.
|
|
73
|
+
*/
|
|
74
|
+
computedConflicts(computed: ReadonlyMap<string, unknown>, name: string, value: unknown): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* The first top-level key of `query` that conflicts with `node`. Top-level only, so the error
|
|
77
|
+
* names the key a user wrote rather than something nested beneath it.
|
|
78
|
+
*/
|
|
79
|
+
firstConflict(node: Node<Model>, query: Query): TypeLevelConflict | undefined;
|
|
80
|
+
/** Node to node, without the serialize/merge round trip. */
|
|
81
|
+
mergeNode(into: Node<Model>, from: Node<Model>): void;
|
|
82
|
+
/** `canMergeQuery` node to node, likewise. */
|
|
83
|
+
canMergeNode(into: Node<Model>, from: Node<Model>): boolean;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=node.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAEtD,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAElE;;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