@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/node.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The node one root accumulates into — a model, its arguments, its columns, its relations and its
|
|
3
|
+
* computed values — and the merge, compare and conflict rules over a tree of them, shared by every
|
|
4
|
+
* adapter whose query is a tree of that shape.
|
|
5
|
+
*
|
|
6
|
+
* A subclass supplies the only format-specific parts: `visitQuery`, the key loop of the ORM's own
|
|
7
|
+
* query, and `toQuery`, the node written back. The traversal never reaches in here — it drives the
|
|
8
|
+
* `Adapter` contract, of which this implements everything but those two.
|
|
9
|
+
*/ "use strict";
|
|
10
|
+
Object.defineProperty(exports, "__esModule", {
|
|
11
|
+
value: true
|
|
12
|
+
});
|
|
13
|
+
function _export(target, all) {
|
|
14
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
_export(exports, {
|
|
20
|
+
get NodeAdapter () {
|
|
21
|
+
return NodeAdapter;
|
|
22
|
+
},
|
|
23
|
+
get createNode () {
|
|
24
|
+
return createNode;
|
|
25
|
+
},
|
|
26
|
+
get relation () {
|
|
27
|
+
return relation;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
const _core = require("@pothos/core");
|
|
31
|
+
const _adapter = require("./adapter.js");
|
|
32
|
+
const _deepequal = require("./deep-equal.js");
|
|
33
|
+
function createNode(model) {
|
|
34
|
+
return {
|
|
35
|
+
model,
|
|
36
|
+
args: {},
|
|
37
|
+
columns: new Set(),
|
|
38
|
+
relations: new Map(),
|
|
39
|
+
computed: new Map()
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function relation(node, name, model, value) {
|
|
43
|
+
if ((0, _core.isThenable)(value)) {
|
|
44
|
+
throw new _core.PothosValidationError(`Relation "${name}" was given a promise. Await nestedSelection() (or a helper built on it, such as getQuery) inside an async selection function.`);
|
|
45
|
+
}
|
|
46
|
+
let child = node.relations.get(name);
|
|
47
|
+
if (!child) {
|
|
48
|
+
child = createNode(model);
|
|
49
|
+
node.relations.set(name, child);
|
|
50
|
+
}
|
|
51
|
+
return child;
|
|
52
|
+
}
|
|
53
|
+
class NodeAdapter extends _adapter.Adapter {
|
|
54
|
+
/**
|
|
55
|
+
* One merger and one merge check per adapter, re-used down the tree: each saves and restores
|
|
56
|
+
* the node it is at rather than allocating a visitor per level, so a merge allocates only what
|
|
57
|
+
* the subclass's own `visitQuery` does. They cannot be this object — a lenient merge runs a
|
|
58
|
+
* check inside itself, so one `this` could not hold both cursors.
|
|
59
|
+
*/ merger = new Merger(this);
|
|
60
|
+
mergeCheck = new MergeCheck(this);
|
|
61
|
+
createNode(model) {
|
|
62
|
+
return createNode(model);
|
|
63
|
+
}
|
|
64
|
+
mergeQuery(node, query, options) {
|
|
65
|
+
this.merger.run(node, query, options);
|
|
66
|
+
}
|
|
67
|
+
canMergeQuery(node, query, options) {
|
|
68
|
+
return this.mergeCheck.run(node, query, options);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Whether `value` cannot join `computed` under `name`. A `deepEqual` against the value already
|
|
72
|
+
* there, which is what an adapter whose computed values are plain values wants; drizzle
|
|
73
|
+
* compares its `extras` by identity, and prisma overrides it for the `_count: true` wildcard.
|
|
74
|
+
*/ computedConflicts(computed, name, value) {
|
|
75
|
+
return computed.has(name) && !(0, _deepequal.deepEqual)(computed.get(name), value);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The first top-level key of `query` that conflicts with `node`. Top-level only, so the error
|
|
79
|
+
* names the key a user wrote rather than something nested beneath it.
|
|
80
|
+
*/ firstConflict(node, query) {
|
|
81
|
+
let found;
|
|
82
|
+
this.visitQuery(query, node.model, {
|
|
83
|
+
column () {},
|
|
84
|
+
allColumns () {},
|
|
85
|
+
args () {},
|
|
86
|
+
relation: (name, _model, nested)=>{
|
|
87
|
+
const child = node.relations.get(name);
|
|
88
|
+
if (!found && child && !new MergeCheck(this).run(child, nested)) {
|
|
89
|
+
found = {
|
|
90
|
+
kind: 'relation',
|
|
91
|
+
name
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
computed: (name, value, kind = 'extra')=>{
|
|
96
|
+
if (!found && this.computedConflicts(node.computed, name, value)) {
|
|
97
|
+
found = {
|
|
98
|
+
kind,
|
|
99
|
+
name
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
return found;
|
|
105
|
+
}
|
|
106
|
+
/** Node to node, without the serialize/merge round trip. */ mergeNode(into, from) {
|
|
107
|
+
if (from.columns === null) {
|
|
108
|
+
into.columns = null;
|
|
109
|
+
} else if (into.columns) {
|
|
110
|
+
for (const column of from.columns){
|
|
111
|
+
into.columns.add(column);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (hasKeys(from.args)) {
|
|
115
|
+
into.args = from.args;
|
|
116
|
+
}
|
|
117
|
+
for (const [name, value] of from.computed){
|
|
118
|
+
into.computed.set(name, value);
|
|
119
|
+
}
|
|
120
|
+
for (const [name, child] of from.relations){
|
|
121
|
+
this.mergeNode(relation(into, name, child.model, undefined), child);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** `canMergeQuery` node to node, likewise. */ canMergeNode(into, from) {
|
|
125
|
+
if (!(0, _deepequal.deepEqual)(into.args, from.args)) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
for (const [name, value] of from.computed){
|
|
129
|
+
if (this.computedConflicts(into.computed, name, value)) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
for (const [name, child] of from.relations){
|
|
134
|
+
const existing = into.relations.get(name);
|
|
135
|
+
if (existing && !this.canMergeNode(existing, child)) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/** One query folded into a node, in place. */ class Merger {
|
|
143
|
+
adapter;
|
|
144
|
+
node;
|
|
145
|
+
/** A relation query must add no columns; the plan beneath it adds the ones it needs. */ asQuery = false;
|
|
146
|
+
/** A conflicting relation or computed value is left out, not merged. Top level only. */ lenient = false;
|
|
147
|
+
constructor(adapter){
|
|
148
|
+
this.adapter = adapter;
|
|
149
|
+
}
|
|
150
|
+
run(node, query, options) {
|
|
151
|
+
this.node = node;
|
|
152
|
+
this.asQuery = options?.asQuery ?? false;
|
|
153
|
+
this.lenient = options?.lenient ?? false;
|
|
154
|
+
this.adapter.visitQuery(query, node.model, this);
|
|
155
|
+
}
|
|
156
|
+
column(name) {
|
|
157
|
+
this.node.columns?.add(name);
|
|
158
|
+
}
|
|
159
|
+
allColumns() {
|
|
160
|
+
if (!this.asQuery) {
|
|
161
|
+
this.node.columns = null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
relation(name, model, query) {
|
|
165
|
+
const parent = this.node;
|
|
166
|
+
if (this.lenient) {
|
|
167
|
+
const existing = parent.relations.get(name);
|
|
168
|
+
// The subtree is checked whole, so once the relation is accepted it merges whole.
|
|
169
|
+
if (existing && !new MergeCheck(this.adapter).run(existing, query)) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const child = relation(parent, name, model, query);
|
|
174
|
+
const { asQuery, lenient } = this;
|
|
175
|
+
this.node = child;
|
|
176
|
+
this.asQuery = false;
|
|
177
|
+
this.lenient = false;
|
|
178
|
+
this.adapter.visitQuery(query, model, this);
|
|
179
|
+
this.node = parent;
|
|
180
|
+
this.asQuery = asQuery;
|
|
181
|
+
this.lenient = lenient;
|
|
182
|
+
}
|
|
183
|
+
computed(name, value) {
|
|
184
|
+
if (this.lenient && this.adapter.computedConflicts(this.node.computed, name, value)) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
this.node.computed.set(name, value);
|
|
188
|
+
}
|
|
189
|
+
args(args) {
|
|
190
|
+
if (hasKeys(args)) {
|
|
191
|
+
this.node.args = args;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Whether a query merges into a node without changing what is already selected. Runs the whole
|
|
197
|
+
* query even after the first failure (a rejection is the rare path) so a `visitQuery` stays a
|
|
198
|
+
* plain loop.
|
|
199
|
+
*/ class MergeCheck {
|
|
200
|
+
adapter;
|
|
201
|
+
node;
|
|
202
|
+
ignoreArgs = false;
|
|
203
|
+
ok = true;
|
|
204
|
+
constructor(adapter){
|
|
205
|
+
this.adapter = adapter;
|
|
206
|
+
}
|
|
207
|
+
run(node, query, options) {
|
|
208
|
+
this.node = node;
|
|
209
|
+
this.ignoreArgs = options?.ignoreArgs ?? false;
|
|
210
|
+
this.ok = true;
|
|
211
|
+
this.adapter.visitQuery(query, node.model, this);
|
|
212
|
+
return this.ok;
|
|
213
|
+
}
|
|
214
|
+
column() {}
|
|
215
|
+
allColumns() {}
|
|
216
|
+
relation(name, model, query) {
|
|
217
|
+
const child = this.node.relations.get(name);
|
|
218
|
+
if (!this.ok || !child) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const parent = this.node;
|
|
222
|
+
const { ignoreArgs } = this;
|
|
223
|
+
// Below the top, a relation's own arguments are part of what is already selected.
|
|
224
|
+
this.node = child;
|
|
225
|
+
this.ignoreArgs = false;
|
|
226
|
+
this.adapter.visitQuery(query, model, this);
|
|
227
|
+
this.node = parent;
|
|
228
|
+
this.ignoreArgs = ignoreArgs;
|
|
229
|
+
}
|
|
230
|
+
computed(name, value) {
|
|
231
|
+
if (this.ok && this.adapter.computedConflicts(this.node.computed, name, value)) {
|
|
232
|
+
this.ok = false;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
args(args) {
|
|
236
|
+
if (this.ok && !this.ignoreArgs && !(0, _deepequal.deepEqual)(this.node.args, args)) {
|
|
237
|
+
this.ok = false;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/** Whether an object has any own enumerable key. */ function hasKeys(value) {
|
|
242
|
+
return Object.keys(value).length > 0;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
//# sourceMappingURL=node.js.map
|
package/lib/node.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/node.ts"],"names":["NodeAdapter","createNode","relation","model","args","columns","Set","relations","Map","computed","node","name","value","isThenable","PothosValidationError","child","get","set","Adapter","merger","Merger","mergeCheck","MergeCheck","mergeQuery","query","options","run","canMergeQuery","computedConflicts","has","deepEqual","firstConflict","found","visitQuery","column","allColumns","_model","nested","kind","mergeNode","into","from","add","hasKeys","undefined","canMergeNode","existing","asQuery","lenient","adapter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IACF;AACF;AAEA,kDAAkD,GAClD,SAAST,QAAQ/B,KAAa;IAC5B,OAAOyC,OAAOC,IAAI,CAAC1C,OAAO2C,MAAM,GAAG;AACrC"}
|
package/lib/plan.js
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
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
|
+
*/ "use strict";
|
|
10
|
+
Object.defineProperty(exports, "__esModule", {
|
|
11
|
+
value: true
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(exports, "Plan", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function() {
|
|
16
|
+
return Plan;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const _core = require("@pothos/core");
|
|
20
|
+
const _graphql = require("graphql");
|
|
21
|
+
const _loadermap = require("./loader-map.js");
|
|
22
|
+
const _matches = require("./matches.js");
|
|
23
|
+
const _walk = require("./walk.js");
|
|
24
|
+
// Shared, since neither carries anything per merge.
|
|
25
|
+
const AS_QUERY = Object.freeze({
|
|
26
|
+
asQuery: true
|
|
27
|
+
});
|
|
28
|
+
const LENIENT = Object.freeze({
|
|
29
|
+
lenient: true
|
|
30
|
+
});
|
|
31
|
+
class Plan {
|
|
32
|
+
adapter;
|
|
33
|
+
position;
|
|
34
|
+
initial;
|
|
35
|
+
context;
|
|
36
|
+
info;
|
|
37
|
+
skipDeferredFragments;
|
|
38
|
+
/** What the plan loads. A node is created for it once per play. */ model;
|
|
39
|
+
/**
|
|
40
|
+
* The merges waiting on a user callback that returned a promise, in the order they were
|
|
41
|
+
* appended. Absent until the first one: a synchronous plan never creates a promise.
|
|
42
|
+
*/ pending;
|
|
43
|
+
/** The traversal's whole output: every merge it collected, in the order it collected them. */ merges = [];
|
|
44
|
+
/**
|
|
45
|
+
* The play `settle` ran, which a later play takes whole when the seed conflicts with none of
|
|
46
|
+
* it. Never handed out: a play that reuses it copies it into a node of its own.
|
|
47
|
+
*/ played;
|
|
48
|
+
/**
|
|
49
|
+
* The plan for the field `info` resolves, settled, or undefined when paths are given and
|
|
50
|
+
* nothing is selected under them. Declared synchronous: the result is a promise only when a
|
|
51
|
+
* select function returned one, and must then be awaited. The plan is handed out rather than
|
|
52
|
+
* played, so a plugin that owes a resolver a synchronous query builder settles this first and
|
|
53
|
+
* plays it behind the resolver's own selection with `plan.query(select)`.
|
|
54
|
+
*/ static fromInfo(adapter, options) {
|
|
55
|
+
const { info, typeName, path, paths } = options;
|
|
56
|
+
const returnType = (0, _graphql.getNamedType)(info.returnType);
|
|
57
|
+
const target = typeName ? info.schema.getType(typeName) : returnType;
|
|
58
|
+
// graphql merges every occurrence of the field's response key into `info.fieldNodes`; each is
|
|
59
|
+
// planned into the one root, so the query answers whichever occurrence a resolver runs for.
|
|
60
|
+
let rootType = target;
|
|
61
|
+
let branches;
|
|
62
|
+
if (paths?.length || path?.length) {
|
|
63
|
+
const includePaths = normalizePaths(paths?.length ? paths : [
|
|
64
|
+
path
|
|
65
|
+
]);
|
|
66
|
+
const prefix = (0, _matches.includeOf)(returnType)?.path;
|
|
67
|
+
const found = info.fieldNodes.flatMap((fieldNode)=>(0, _matches.findMatches)(info, returnType, fieldNode, includePaths, {
|
|
68
|
+
prefix
|
|
69
|
+
}));
|
|
70
|
+
if (found.length === 0) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
// Matches are filtered against the model the root loads, which is the first match's own
|
|
74
|
+
// when no `typeName` names it: the return type is then a wrapper with no model, and
|
|
75
|
+
// filtering by it would keep every match whatever model it returns.
|
|
76
|
+
rootType = typeName ? target : found[0].type;
|
|
77
|
+
const matches = (0, _matches.matchesForModel)(adapter, info.schema, found, rootType);
|
|
78
|
+
// Every match is planned into the one root, entered under its own type first.
|
|
79
|
+
branches = matches.map((match)=>({
|
|
80
|
+
// A matched type with its own model (including variants of the target model) is walked
|
|
81
|
+
// with its own model. Types without a model (interfaces, wrappers) are walked as the
|
|
82
|
+
// requested type so its fields can be found.
|
|
83
|
+
type: typeName && !(0, _matches.modelOf)(adapter, info.schema, match.type) ? target : match.type,
|
|
84
|
+
fieldNodes: [
|
|
85
|
+
match.field
|
|
86
|
+
],
|
|
87
|
+
indirectPath: match.path,
|
|
88
|
+
deferred: match.deferred
|
|
89
|
+
}));
|
|
90
|
+
} else {
|
|
91
|
+
branches = [
|
|
92
|
+
{
|
|
93
|
+
type: target,
|
|
94
|
+
fieldNodes: info.fieldNodes,
|
|
95
|
+
indirectPath: [],
|
|
96
|
+
deferred: false
|
|
97
|
+
}
|
|
98
|
+
];
|
|
99
|
+
}
|
|
100
|
+
const plan = new Plan(adapter, options, rootType, positionForResolvedField(info), options.initial);
|
|
101
|
+
try {
|
|
102
|
+
(0, _walk.walkBranches)(plan, branches);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
plan.abandon();
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
return plan.finish(plan.settle);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The plan loading the field `info` resolves for its parent row, played. The loaded row
|
|
111
|
+
* replaces the parent the field resolver sees, so besides the field's own selection it
|
|
112
|
+
* carries the parent type's type-level selection. The field is what the row is loaded for, so
|
|
113
|
+
* it is merged first and a type-level relation whose arguments conflict with it is left out.
|
|
114
|
+
*/ static forParentRow(adapter, context, info, skipDeferredFragments) {
|
|
115
|
+
const type = info.parentType;
|
|
116
|
+
const plan = new Plan(adapter, {
|
|
117
|
+
context,
|
|
118
|
+
info,
|
|
119
|
+
skipDeferredFragments
|
|
120
|
+
}, type);
|
|
121
|
+
try {
|
|
122
|
+
// Every node selecting the field (one per fragment it appears under) plans into the same
|
|
123
|
+
// row, so the loaded row satisfies each of them.
|
|
124
|
+
for (const fieldNode of info.fieldNodes){
|
|
125
|
+
(0, _walk.walkField)(plan, type, fieldNode, []);
|
|
126
|
+
}
|
|
127
|
+
} catch (error) {
|
|
128
|
+
plan.abandon();
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
return plan.finish(plan.enterParentType, type);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* `source` is an entry point's options for a root plan and the parent plan for a nested one,
|
|
135
|
+
* which is why `initial` is passed beside it: it belongs to the root alone and a nested plan
|
|
136
|
+
* must never inherit one. `position` is where the field this plan hangs beneath is, undefined
|
|
137
|
+
* when there is no field above the plan.
|
|
138
|
+
*/ constructor(adapter, source, type, position, /** Merged first by a play a caller gives no seed of its own. */ initial){
|
|
139
|
+
this.adapter = adapter;
|
|
140
|
+
this.position = position;
|
|
141
|
+
this.initial = initial;
|
|
142
|
+
this.context = source.context;
|
|
143
|
+
this.info = source.info;
|
|
144
|
+
this.skipDeferredFragments = source.skipDeferredFragments ?? adapter.skipDeferredFragments;
|
|
145
|
+
const model = (0, _matches.modelOf)(adapter, source.info.schema, type);
|
|
146
|
+
if (!model) {
|
|
147
|
+
throw new _core.PothosValidationError(`Expected ${(0, _matches.resolveType)(source.info.schema, type).name} to have a model`);
|
|
148
|
+
}
|
|
149
|
+
// Not entered: a type is entered when its selection set is walked.
|
|
150
|
+
this.model = model;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The plan beneath one nested selection: this plan's adapter, context, info and deferred
|
|
154
|
+
* setting, with merges of its own. It hangs beneath the field whose select function made it.
|
|
155
|
+
*/ nested(type, position) {
|
|
156
|
+
return new Plan(this.adapter, this, type, position);
|
|
157
|
+
}
|
|
158
|
+
/** Appends one merge to the list a play folds. The traversal's only way to add to a plan. */ collect(merge) {
|
|
159
|
+
this.merges.push(merge);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* The relation query of the nested selection this plan was made for. It takes the head of the
|
|
163
|
+
* list, so it is merged before the fields walked beneath it whether the callback that produced
|
|
164
|
+
* it answered at once or resolved after the walk. A nested plan has at most one.
|
|
165
|
+
*/ collectQuery(query) {
|
|
166
|
+
this.merges.unshift({
|
|
167
|
+
kind: 'query',
|
|
168
|
+
query
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Appends the merge of `value` to the plan's pending chain. Only merges are chained, never user
|
|
173
|
+
* code, so a link can never append another and the chain needs no loop. Each link waits on the
|
|
174
|
+
* previous one, so async merges run in the order they were appended. Both promises get a
|
|
175
|
+
* handler at once, so a callback that rejects early is never an unhandled rejection.
|
|
176
|
+
*/ chain(value, merge) {
|
|
177
|
+
const prev = this.pending;
|
|
178
|
+
this.pending = prev ? Promise.all([
|
|
179
|
+
prev,
|
|
180
|
+
value
|
|
181
|
+
]).then(([, v])=>merge(v)) : Promise.resolve(value).then(merge);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* A plan that threw synchronously never reaches `finish`, so the merges it had already chained
|
|
185
|
+
* would reject unobserved once their callbacks settle. The throw is what the caller sees.
|
|
186
|
+
*/ abandon() {
|
|
187
|
+
this.pending?.catch(noop);
|
|
188
|
+
}
|
|
189
|
+
finish(done, arg) {
|
|
190
|
+
return this.pending ? this.pending.then(()=>done.call(this, arg)) : done.call(this, arg);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* A fresh node with this plan's merges folded into it, and the mappings of the merges it took.
|
|
194
|
+
* `seed` takes the place of the plan's own `initial`: it is merged before anything the traversal
|
|
195
|
+
* collected, so a relation or computed value the document plans with other arguments loses, and
|
|
196
|
+
* its field falls back to loading on its own.
|
|
197
|
+
*
|
|
198
|
+
* Synchronous, and runs no user callback, so a play of an async plan costs no more than a play
|
|
199
|
+
* of a synchronous one.
|
|
200
|
+
*/ play(seed) {
|
|
201
|
+
const { adapter } = this;
|
|
202
|
+
const root = adapter.createNode(this.model);
|
|
203
|
+
const settled = this.reusable(seed);
|
|
204
|
+
// Merged before anything else, so on a conflict it wins. A settled play already holds the
|
|
205
|
+
// plan's own `initial`, so reusing one merges the seed and nothing else: `mergeQuery` is not
|
|
206
|
+
// required to be idempotent.
|
|
207
|
+
const first = settled ? seed : seed ?? this.initial;
|
|
208
|
+
if (first) {
|
|
209
|
+
adapter.mergeQuery(root, first);
|
|
210
|
+
}
|
|
211
|
+
if (settled) {
|
|
212
|
+
// Every merge that play took still fits and every merge it left out still does not, so
|
|
213
|
+
// the node it built is taken whole.
|
|
214
|
+
adapter.mergeNode(root, settled.root);
|
|
215
|
+
return {
|
|
216
|
+
plan: this,
|
|
217
|
+
root,
|
|
218
|
+
mappings: settled.mappings
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
const mappings = {};
|
|
222
|
+
for (const merge of this.merges){
|
|
223
|
+
switch(merge.kind){
|
|
224
|
+
case 'type':
|
|
225
|
+
adapter.mergeQuery(root, merge.query);
|
|
226
|
+
break;
|
|
227
|
+
case 'query':
|
|
228
|
+
adapter.mergeQuery(root, merge.query, AS_QUERY);
|
|
229
|
+
break;
|
|
230
|
+
case 'variant':
|
|
231
|
+
this.mergeVariant(root, merge.type, merge.variant, merge.query);
|
|
232
|
+
break;
|
|
233
|
+
case 'field':
|
|
234
|
+
{
|
|
235
|
+
// One options object for the pair: `mergeQuery` reads only the alias, and
|
|
236
|
+
// `canMergeQuery` only the `ignoreArgs` beside it.
|
|
237
|
+
const options = {
|
|
238
|
+
ignoreArgs: true,
|
|
239
|
+
alias: merge.alias
|
|
240
|
+
};
|
|
241
|
+
// A field's selection is merged, and its mapping recorded, only while it fits what is
|
|
242
|
+
// already in the node; otherwise it is left out and its resolver loads its own data.
|
|
243
|
+
if (adapter.canMergeQuery(root, merge.query, options)) {
|
|
244
|
+
adapter.mergeQuery(root, merge.query, options);
|
|
245
|
+
mappings[merge.key] = (0, _loadermap.unionMappings)(mappings[merge.key], merge.mapping);
|
|
246
|
+
}
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
default:
|
|
250
|
+
{
|
|
251
|
+
const unknown = merge;
|
|
252
|
+
throw new _core.PothosValidationError(`Unknown root merge ${String(unknown.kind)}`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return {
|
|
257
|
+
plan: this,
|
|
258
|
+
root,
|
|
259
|
+
mappings
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* The plan played behind `seed`, its mappings recorded for the resolvers beneath it, and its
|
|
264
|
+
* node serialized. A cached plan accepts the current resolver info to scope each execution.
|
|
265
|
+
* Every play records its mappings on the request context; an adapter whose
|
|
266
|
+
* resolvers read a loaded row another way simply never looks them up.
|
|
267
|
+
*/ query(seed, info = this.info) {
|
|
268
|
+
// With no seed, a settled play's node is what a fresh play would build, so hand it straight
|
|
269
|
+
// over: `toQuery` does not mutate, and a later seeded play still merges from it.
|
|
270
|
+
const { root, mappings } = seed === undefined && this.played ? this.played : this.play(seed);
|
|
271
|
+
(0, _loadermap.setLoaderMappings)(this.context, info, mappings);
|
|
272
|
+
return this.adapter.toQuery(root);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* The plan played once, so a play-time error — a conflict between two type-level selections —
|
|
276
|
+
* is raised now rather than only inside a `query()` a resolver may never call. That play is
|
|
277
|
+
* kept, and a later one whose seed conflicts with none of it takes it whole, so settling costs
|
|
278
|
+
* the resolver's own play nothing.
|
|
279
|
+
*/ settle() {
|
|
280
|
+
this.played = this.play();
|
|
281
|
+
return this;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* The plan played, with `type`'s type-level selection merged in behind it, minus what conflicts
|
|
285
|
+
* with the field the row is loaded for. A parent-row plan is never replayed behind a caller's
|
|
286
|
+
* selection, so it plays once, here.
|
|
287
|
+
*/ enterParentType(type) {
|
|
288
|
+
const played = this.play();
|
|
289
|
+
const selection = this.adapter.typeSelection(type);
|
|
290
|
+
if (selection) {
|
|
291
|
+
this.adapter.mergeQuery(played.root, selection, LENIENT);
|
|
292
|
+
}
|
|
293
|
+
return played;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* A settled play that a play seeded with `seed` can take whole, or undefined. A seed conflicting
|
|
297
|
+
* with nothing the settled play holds changes no decision it made, since every rejection was
|
|
298
|
+
* against something the seed leaves in place — unless the seed stands in for the plan's own
|
|
299
|
+
* `initial`, which the settled play already merged.
|
|
300
|
+
*/ reusable(seed) {
|
|
301
|
+
if (!this.played || seed !== undefined && this.initial !== undefined) {
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
return seed === undefined || !this.adapter.firstConflict(this.played.root, seed) ? this.played : undefined;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Unlike a field-level select, a type-level selection has no per-field fallback, so a relation
|
|
308
|
+
* argument or a computed value that conflicts with what is already in the node is an error
|
|
309
|
+
* rather than a rejection.
|
|
310
|
+
*/ mergeVariant(node, type, variant, selection) {
|
|
311
|
+
const conflict = this.adapter.firstConflict(node, selection);
|
|
312
|
+
if (conflict) {
|
|
313
|
+
switch(conflict.kind){
|
|
314
|
+
case 'relation':
|
|
315
|
+
throw new _core.PothosValidationError(`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.`);
|
|
316
|
+
case 'extra':
|
|
317
|
+
throw new _core.PothosValidationError(`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.`);
|
|
318
|
+
default:
|
|
319
|
+
{
|
|
320
|
+
const unknown = conflict.kind;
|
|
321
|
+
throw new _core.PothosValidationError(`Unknown type-level conflict ${unknown}`);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
this.adapter.mergeQuery(node, selection);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Where the field being resolved is, which every position beneath it links back to, so a select
|
|
330
|
+
* function can tell where in the query its field sits. Undefined when `info` does not name the
|
|
331
|
+
* field it resolves (a caller building one by hand): the plan then starts at its own fields.
|
|
332
|
+
*/ function positionForResolvedField(info) {
|
|
333
|
+
const node = info.fieldNodes[0];
|
|
334
|
+
const field = info.parentType?.getFields()[node.name.value];
|
|
335
|
+
return field && {
|
|
336
|
+
parent: undefined,
|
|
337
|
+
type: info.parentType,
|
|
338
|
+
field,
|
|
339
|
+
node
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
function normalizePaths(paths) {
|
|
343
|
+
return paths.map((path)=>path.map((segment)=>typeof segment === 'string' ? {
|
|
344
|
+
name: segment
|
|
345
|
+
} : segment));
|
|
346
|
+
}
|
|
347
|
+
function noop() {}
|
|
348
|
+
|
|
349
|
+
//# sourceMappingURL=plan.js.map
|
package/lib/plan.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/plan.ts"],"names":["Plan","AS_QUERY","Object","freeze","asQuery","LENIENT","lenient","context","info","skipDeferredFragments","model","pending","merges","played","fromInfo","adapter","options","typeName","path","paths","returnType","getNamedType","target","schema","getType","rootType","branches","length","includePaths","normalizePaths","prefix","includeOf","found","fieldNodes","flatMap","fieldNode","findMatches","undefined","type","matches","matchesForModel","map","match","modelOf","field","indirectPath","deferred","plan","positionForResolvedField","initial","walkBranches","error","abandon","finish","settle","forParentRow","parentType","walkField","enterParentType","source","position","PothosValidationError","resolveType","name","nested","collect","merge","push","collectQuery","query","unshift","kind","chain","value","prev","Promise","all","then","v","resolve","catch","noop","done","arg","call","play","seed","root","createNode","settled","reusable","first","mergeQuery","mergeNode","mappings","mergeVariant","variant","ignoreArgs","alias","canMergeQuery","key","unionMappings","mapping","unknown","String","setLoaderMappings","toQuery","selection","typeSelection","firstConflict","node","conflict","getFields","parent","segment"],"mappings":"AAAA;;;;;;;;CAQC;;;;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AmB;oBACpF;YACF;QACF;QAEA,IAAI,CAACxF,OAAO,CAAC6E,UAAU,CAACkB,MAAMH;IAChC;AACF;AAEA;;;;CAIC,GACD,SAAS3D,yBAAyBxC,IAAwB;IACxD,MAAMsG,OAAOtG,KAAKyB,UAAU,CAAC,EAAE;IAC/B,MAAMW,QAAQpC,KAAKgD,UAAU,EAAEwD,WAAW,CAACF,KAAK/C,IAAI,CAACU,KAAK,CAAC;IAE3D,OAAO7B,SAAS;QAAEqE,QAAQ5E;QAAWC,MAAM9B,KAAKgD,UAAU;QAAEZ;QAAOkE;IAAK;AAC1E;AAEA,SAASjF,eAAeV,KAAsB;IAC5C,OAAOA,MAAMsB,GAAG,CAAC,CAACvB,OAChBA,KAAKuB,GAAG,CAAC,CAACyE,UAAa,OAAOA,YAAY,WAAW;gBAAEnD,MAAMmD;YAAQ,IAAIA;AAE7E;AAEA,SAASjC,QAAQ"}
|
package/lib/types.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
*/ "use strict";
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=types.js.map
|
package/lib/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;CAGC"}
|