@pothos/selection-mapper 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/README.md +16 -6
- package/dts/deep-equal.d.ts +4 -0
- package/dts/deep-equal.d.ts.map +1 -1
- package/dts/errors.d.ts +7 -0
- package/dts/errors.d.ts.map +1 -0
- package/dts/index.d.ts +3 -2
- package/dts/index.d.ts.map +1 -1
- package/dts/loader-map.d.ts +37 -16
- package/dts/loader-map.d.ts.map +1 -1
- package/dts/matches.d.ts +5 -0
- package/dts/matches.d.ts.map +1 -1
- package/dts/parent-types.d.ts +9 -0
- package/dts/parent-types.d.ts.map +1 -0
- package/dts/plan.d.ts +36 -20
- package/dts/plan.d.ts.map +1 -1
- package/dts/shape.d.ts +8 -0
- package/dts/shape.d.ts.map +1 -0
- package/dts/types.d.ts +21 -1
- package/dts/types.d.ts.map +1 -1
- package/dts/walk.d.ts +20 -2
- package/dts/walk.d.ts.map +1 -1
- package/esm/deep-equal.d.ts +4 -0
- package/esm/deep-equal.d.ts.map +1 -1
- package/esm/deep-equal.js +60 -28
- package/esm/deep-equal.js.map +1 -1
- package/esm/errors.d.ts +7 -0
- package/esm/errors.d.ts.map +1 -0
- package/esm/errors.js +11 -0
- package/esm/errors.js.map +1 -0
- package/esm/index.d.ts +3 -2
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +2 -1
- package/esm/index.js.map +1 -1
- package/esm/loader-map.d.ts +37 -16
- package/esm/loader-map.d.ts.map +1 -1
- package/esm/loader-map.js +165 -99
- package/esm/loader-map.js.map +1 -1
- package/esm/matches.d.ts +5 -0
- package/esm/matches.d.ts.map +1 -1
- package/esm/matches.js +11 -3
- package/esm/matches.js.map +1 -1
- package/esm/parent-types.d.ts +9 -0
- package/esm/parent-types.d.ts.map +1 -0
- package/esm/parent-types.js +24 -0
- package/esm/parent-types.js.map +1 -0
- package/esm/plan.d.ts +36 -20
- package/esm/plan.d.ts.map +1 -1
- package/esm/plan.js +107 -71
- package/esm/plan.js.map +1 -1
- package/esm/shape.d.ts +8 -0
- package/esm/shape.d.ts.map +1 -0
- package/esm/shape.js +94 -0
- package/esm/shape.js.map +1 -0
- package/esm/types.d.ts +21 -1
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js.map +1 -1
- package/esm/walk.d.ts +20 -2
- package/esm/walk.d.ts.map +1 -1
- package/esm/walk.js +199 -71
- package/esm/walk.js.map +1 -1
- package/lib/deep-equal.js +60 -28
- package/lib/deep-equal.js.map +1 -1
- package/lib/errors.js +18 -0
- package/lib/errors.js.map +1 -0
- package/lib/index.js +13 -3
- package/lib/index.js.map +1 -1
- package/lib/loader-map.js +145 -90
- package/lib/loader-map.js.map +1 -1
- package/lib/matches.js +14 -3
- package/lib/matches.js.map +1 -1
- package/lib/parent-types.js +29 -0
- package/lib/parent-types.js.map +1 -0
- package/lib/plan.js +107 -71
- package/lib/plan.js.map +1 -1
- package/lib/shape.js +109 -0
- package/lib/shape.js.map +1 -0
- package/lib/walk.js +186 -68
- package/lib/walk.js.map +1 -1
- package/package.json +4 -3
- package/src/deep-equal.ts +77 -34
- package/src/errors.ts +12 -0
- package/src/index.ts +6 -1
- package/src/loader-map.ts +235 -125
- package/src/matches.ts +15 -3
- package/src/parent-types.ts +33 -0
- package/src/plan.ts +145 -78
- package/src/shape.ts +126 -0
- package/src/types.ts +25 -1
- package/src/walk.ts +250 -83
package/esm/plan.d.ts
CHANGED
|
@@ -2,11 +2,11 @@ import { type GraphQLNamedType, type GraphQLResolveInfo } from 'graphql';
|
|
|
2
2
|
import type { Adapter, NodeBase } from './adapter.js';
|
|
3
3
|
import { type Mapping, type Mappings } from './loader-map.js';
|
|
4
4
|
import type { Node } from './node.js';
|
|
5
|
-
import type { EntryOptions, Position, WalkedType } from './types.js';
|
|
5
|
+
import type { EntryOptions, ParentRowOptions, Position, WalkedType } from './types.js';
|
|
6
6
|
/**
|
|
7
7
|
* One merge a traversal collected, in the order it happened: a type's selection, a same-model
|
|
8
|
-
* variant's,
|
|
9
|
-
*
|
|
8
|
+
* variant's, or a field's. A play folds the list into a node; nothing is merged, accepted or
|
|
9
|
+
* rejected before then.
|
|
10
10
|
*
|
|
11
11
|
* A record holds its query by reference, so a select function that mutates the query it returned
|
|
12
12
|
* after returning it changes what a later play builds — a bug in the select function. Nothing
|
|
@@ -16,9 +16,6 @@ import type { EntryOptions, Position, WalkedType } from './types.js';
|
|
|
16
16
|
export type RootMerge<Query> = {
|
|
17
17
|
kind: 'type';
|
|
18
18
|
query: Query;
|
|
19
|
-
} | {
|
|
20
|
-
kind: 'query';
|
|
21
|
-
query: Query;
|
|
22
19
|
} | {
|
|
23
20
|
kind: 'variant';
|
|
24
21
|
type: WalkedType;
|
|
@@ -68,6 +65,17 @@ export declare class Plan<Model, Query, NodeType extends NodeBase<Model> = Node<
|
|
|
68
65
|
pending?: Promise<void>;
|
|
69
66
|
/** The traversal's whole output: every merge it collected, in the order it collected them. */
|
|
70
67
|
private readonly merges;
|
|
68
|
+
/** The plan whose merges a play folds: this one, or a walk shared with other invocations. */
|
|
69
|
+
private walk;
|
|
70
|
+
/** The relation query of the nested selection this plan was made for, merged ahead of `walk`. */
|
|
71
|
+
private relationQuery?;
|
|
72
|
+
/** Whether a select function in this walk read the position above its own field. */
|
|
73
|
+
positionRead: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* The walks beneath every field of the plans hanging from one root, by `nested`'s `key`. Shared
|
|
76
|
+
* with the plan this one was created from, so the root creates it and every nested plan reads it.
|
|
77
|
+
*/
|
|
78
|
+
private readonly walks;
|
|
71
79
|
/**
|
|
72
80
|
* The play `settle` ran, which a later play takes whole when the seed conflicts with none of
|
|
73
81
|
* it. Never handed out: a play that reuses it copies it into a node of its own.
|
|
@@ -86,8 +94,9 @@ export declare class Plan<Model, Query, NodeType extends NodeBase<Model> = Node<
|
|
|
86
94
|
* replaces the parent the field resolver sees, so besides the field's own selection it
|
|
87
95
|
* carries the parent type's type-level selection. The field is what the row is loaded for, so
|
|
88
96
|
* it is merged first and a type-level relation whose arguments conflict with it is left out.
|
|
97
|
+
* `initial` is merged before anything else.
|
|
89
98
|
*/
|
|
90
|
-
static forParentRow<Model, Query, NodeType extends NodeBase<Model> = Node<Model>>(adapter: Adapter<Model, Query, NodeType>,
|
|
99
|
+
static forParentRow<Model, Query, NodeType extends NodeBase<Model> = Node<Model>>(adapter: Adapter<Model, Query, NodeType>, options: ParentRowOptions<Query>): PlayedPlan<Model, Query, NodeType>;
|
|
91
100
|
/**
|
|
92
101
|
* `source` is an entry point's options for a root plan and the parent plan for a nested one,
|
|
93
102
|
* which is why `initial` is passed beside it: it belongs to the root alone and a nested plan
|
|
@@ -99,15 +108,28 @@ export declare class Plan<Model, Query, NodeType extends NodeBase<Model> = Node<
|
|
|
99
108
|
initial?: Query | undefined);
|
|
100
109
|
/**
|
|
101
110
|
* The plan beneath one nested selection: this plan's adapter, context, info and deferred
|
|
102
|
-
* setting
|
|
103
|
-
|
|
104
|
-
|
|
111
|
+
* setting. It holds the invocation's relation query and plays over a walk of the selection
|
|
112
|
+
* beneath the field, which `walk` collects the first time this plan needs it.
|
|
113
|
+
*
|
|
114
|
+
* One field node is invoked once per type it is reached on in a walk (see `walkField`). A named
|
|
115
|
+
* fragment spread at every level reaches the same selection set beneath several field nodes,
|
|
116
|
+
* which may hang beneath different plans, so a walk per invocation is exponential in the
|
|
117
|
+
* document's depth.
|
|
118
|
+
*
|
|
119
|
+
* What a walk collects depends only on `key`, which names the selection set by its shape (see
|
|
120
|
+
* shape.ts) and holds everything else the walk reads, unless a select function reads the
|
|
121
|
+
* position above it: `positionRead` says it did. Every other invocation with the same key, under
|
|
122
|
+
* any plan from the same root, shares a walk that did not. A walk that is still pending may yet
|
|
123
|
+
* read it, so it is not shared. A walk that throws is not kept, so a select function that catches
|
|
124
|
+
* the failure can retry it.
|
|
125
|
+
*/
|
|
126
|
+
nested(type: GraphQLNamedType, position: Position, key: string, walk: (plan: Plan<Model, Query, NodeType>) => void): Plan<Model, Query, NodeType>;
|
|
105
127
|
/** Appends one merge to the list a play folds. The traversal's only way to add to a plan. */
|
|
106
128
|
collect(merge: RootMerge<Query>): void;
|
|
107
129
|
/**
|
|
108
|
-
* The relation query of the nested selection this plan was made for. It
|
|
109
|
-
*
|
|
110
|
-
*
|
|
130
|
+
* The relation query of the nested selection this plan was made for. It is merged ahead of the
|
|
131
|
+
* walk, whether the callback that produced it answered at once or resolved after the walk. A
|
|
132
|
+
* nested plan has at most one.
|
|
111
133
|
*/
|
|
112
134
|
collectQuery(query: Query): void;
|
|
113
135
|
/**
|
|
@@ -142,7 +164,7 @@ export declare class Plan<Model, Query, NodeType extends NodeBase<Model> = Node<
|
|
|
142
164
|
/**
|
|
143
165
|
* The plan played behind `seed`, its mappings recorded for the resolvers beneath it, and its
|
|
144
166
|
* node serialized. A cached plan accepts the current resolver info to scope each execution.
|
|
145
|
-
* Every play records its mappings on the request context; an adapter whose
|
|
167
|
+
* Every play records its mappings for the execution on the request context; an adapter whose
|
|
146
168
|
* resolvers read a loaded row another way simply never looks them up.
|
|
147
169
|
*/
|
|
148
170
|
query(seed?: Query, info?: GraphQLResolveInfo): Query;
|
|
@@ -166,11 +188,5 @@ export declare class Plan<Model, Query, NodeType extends NodeBase<Model> = Node<
|
|
|
166
188
|
* `initial`, which the settled play already merged.
|
|
167
189
|
*/
|
|
168
190
|
private reusable;
|
|
169
|
-
/**
|
|
170
|
-
* Unlike a field-level select, a type-level selection has no per-field fallback, so a relation
|
|
171
|
-
* argument or a computed value that conflicts with what is already in the node is an error
|
|
172
|
-
* rather than a rejection.
|
|
173
|
-
*/
|
|
174
|
-
private mergeVariant;
|
|
175
191
|
}
|
|
176
192
|
//# sourceMappingURL=plan.d.ts.map
|
package/esm/plan.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plan.d.ts","sourceRoot":"","sources":["../src/plan.ts"],"names":[],"mappings":"
|
|
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E,CAAC,IAAI,EAAE,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC;IACtC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;IAOzD;;;;;;;;OAQG;IACH,IAAI,CAAC,IAAI,CAAC,EAAE,KAAK,GAAG,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,CAAC,CAsErD;IAED;;;;;OAKG;IACH,KAAK,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,qBAAY,GAAG,KAAK,CAQ3C;IAED;;;;;OAKG;IACH,MAAM,IAAI,IAAI,CAIb;IAED;;;;OAIG;IACH,eAAe,CAAC,IAAI,EAAE,gBAAgB,GAAG,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,CAAC,CAS1E;IAED;;;;;OAKG;IACH,OAAO,CAAC,QAAQ;CASjB"}
|
package/esm/plan.js
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
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
|
-
*/ import { PothosValidationError } from '@pothos/core';
|
|
10
1
|
import { getNamedType } from 'graphql';
|
|
2
|
+
import { unexpectedError } from './errors.js';
|
|
11
3
|
import { setLoaderMappings, unionMappings } from './loader-map.js';
|
|
12
|
-
import { findMatches, includeOf, matchesForModel, modelOf, resolveType } from './matches.js';
|
|
4
|
+
import { findMatches, includeOf, includePaths, matchesForModel, modelOf, resolveType } from './matches.js';
|
|
13
5
|
import { walkBranches, walkField } from './walk.js';
|
|
14
6
|
// Shared, since neither carries anything per merge.
|
|
15
7
|
const AS_QUERY = Object.freeze({
|
|
@@ -31,6 +23,13 @@ export class Plan {
|
|
|
31
23
|
* appended. Absent until the first one: a synchronous plan never creates a promise.
|
|
32
24
|
*/ pending;
|
|
33
25
|
/** The traversal's whole output: every merge it collected, in the order it collected them. */ merges = [];
|
|
26
|
+
/** The plan whose merges a play folds: this one, or a walk shared with other invocations. */ walk = this;
|
|
27
|
+
/** The relation query of the nested selection this plan was made for, merged ahead of `walk`. */ relationQuery;
|
|
28
|
+
/** Whether a select function in this walk read the position above its own field. */ positionRead = false;
|
|
29
|
+
/**
|
|
30
|
+
* The walks beneath every field of the plans hanging from one root, by `nested`'s `key`. Shared
|
|
31
|
+
* with the plan this one was created from, so the root creates it and every nested plan reads it.
|
|
32
|
+
*/ walks;
|
|
34
33
|
/**
|
|
35
34
|
* The play `settle` ran, which a later play takes whole when the seed conflicts with none of
|
|
36
35
|
* it. Never handed out: a play that reuses it copies it into a node of its own.
|
|
@@ -49,12 +48,14 @@ export class Plan {
|
|
|
49
48
|
// planned into the one root, so the query answers whichever occurrence a resolver runs for.
|
|
50
49
|
let rootType = target;
|
|
51
50
|
let branches;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
const entryPaths = includePaths({
|
|
52
|
+
path,
|
|
53
|
+
paths
|
|
54
|
+
});
|
|
55
|
+
if (entryPaths) {
|
|
56
|
+
const normalized = normalizePaths(entryPaths);
|
|
56
57
|
const prefix = includeOf(returnType)?.path;
|
|
57
|
-
const found = info.fieldNodes.flatMap((fieldNode)=>findMatches(info, returnType, fieldNode,
|
|
58
|
+
const found = info.fieldNodes.flatMap((fieldNode)=>findMatches(info, returnType, fieldNode, normalized, {
|
|
58
59
|
prefix
|
|
59
60
|
}));
|
|
60
61
|
if (found.length === 0) {
|
|
@@ -101,13 +102,11 @@ export class Plan {
|
|
|
101
102
|
* replaces the parent the field resolver sees, so besides the field's own selection it
|
|
102
103
|
* carries the parent type's type-level selection. The field is what the row is loaded for, so
|
|
103
104
|
* it is merged first and a type-level relation whose arguments conflict with it is left out.
|
|
104
|
-
|
|
105
|
+
* `initial` is merged before anything else.
|
|
106
|
+
*/ static forParentRow(adapter, options) {
|
|
107
|
+
const { info } = options;
|
|
105
108
|
const type = info.parentType;
|
|
106
|
-
const plan = new Plan(adapter,
|
|
107
|
-
context,
|
|
108
|
-
info,
|
|
109
|
-
skipDeferredFragments
|
|
110
|
-
}, type);
|
|
109
|
+
const plan = new Plan(adapter, options, type, undefined, options.initial);
|
|
111
110
|
try {
|
|
112
111
|
// Every node selecting the field (one per fragment it appears under) plans into the same
|
|
113
112
|
// row, so the loaded row satisfies each of them.
|
|
@@ -132,31 +131,61 @@ export class Plan {
|
|
|
132
131
|
this.context = source.context;
|
|
133
132
|
this.info = source.info;
|
|
134
133
|
this.skipDeferredFragments = source.skipDeferredFragments ?? adapter.skipDeferredFragments;
|
|
134
|
+
this.walks = source instanceof Plan ? source.walks : new Map();
|
|
135
135
|
const model = modelOf(adapter, source.info.schema, type);
|
|
136
136
|
if (!model) {
|
|
137
|
-
throw
|
|
137
|
+
throw unexpectedError(`Expected ${resolveType(source.info.schema, type).name} to have a model`);
|
|
138
138
|
}
|
|
139
139
|
// Not entered: a type is entered when its selection set is walked.
|
|
140
140
|
this.model = model;
|
|
141
141
|
}
|
|
142
142
|
/**
|
|
143
143
|
* The plan beneath one nested selection: this plan's adapter, context, info and deferred
|
|
144
|
-
* setting
|
|
145
|
-
|
|
146
|
-
|
|
144
|
+
* setting. It holds the invocation's relation query and plays over a walk of the selection
|
|
145
|
+
* beneath the field, which `walk` collects the first time this plan needs it.
|
|
146
|
+
*
|
|
147
|
+
* One field node is invoked once per type it is reached on in a walk (see `walkField`). A named
|
|
148
|
+
* fragment spread at every level reaches the same selection set beneath several field nodes,
|
|
149
|
+
* which may hang beneath different plans, so a walk per invocation is exponential in the
|
|
150
|
+
* document's depth.
|
|
151
|
+
*
|
|
152
|
+
* What a walk collects depends only on `key`, which names the selection set by its shape (see
|
|
153
|
+
* shape.ts) and holds everything else the walk reads, unless a select function reads the
|
|
154
|
+
* position above it: `positionRead` says it did. Every other invocation with the same key, under
|
|
155
|
+
* any plan from the same root, shares a walk that did not. A walk that is still pending may yet
|
|
156
|
+
* read it, so it is not shared. A walk that throws is not kept, so a select function that catches
|
|
157
|
+
* the failure can retry it.
|
|
158
|
+
*/ nested(type, position, key, walk) {
|
|
159
|
+
let walked = this.walks.get(key);
|
|
160
|
+
if (!walked) {
|
|
161
|
+
const created = new Plan(this.adapter, this, type, position);
|
|
162
|
+
try {
|
|
163
|
+
walk(created);
|
|
164
|
+
} catch (error) {
|
|
165
|
+
created.abandon();
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
if (!(created.positionRead || created.pending)) {
|
|
169
|
+
this.walks.set(key, created);
|
|
170
|
+
}
|
|
171
|
+
walked = created;
|
|
172
|
+
}
|
|
173
|
+
const plan = new Plan(this.adapter, this, type, position);
|
|
174
|
+
plan.walk = walked;
|
|
175
|
+
if (walked.pending) {
|
|
176
|
+
plan.chain(walked.pending, noop);
|
|
177
|
+
}
|
|
178
|
+
return plan;
|
|
147
179
|
}
|
|
148
180
|
/** Appends one merge to the list a play folds. The traversal's only way to add to a plan. */ collect(merge) {
|
|
149
181
|
this.merges.push(merge);
|
|
150
182
|
}
|
|
151
183
|
/**
|
|
152
|
-
* The relation query of the nested selection this plan was made for. It
|
|
153
|
-
*
|
|
154
|
-
*
|
|
184
|
+
* The relation query of the nested selection this plan was made for. It is merged ahead of the
|
|
185
|
+
* walk, whether the callback that produced it answered at once or resolved after the walk. A
|
|
186
|
+
* nested plan has at most one.
|
|
155
187
|
*/ collectQuery(query) {
|
|
156
|
-
this.
|
|
157
|
-
kind: 'query',
|
|
158
|
-
query
|
|
159
|
-
});
|
|
188
|
+
this.relationQuery = query;
|
|
160
189
|
}
|
|
161
190
|
/**
|
|
162
191
|
* Appends the merge of `value` to the plan's pending chain. Only merges are chained, never user
|
|
@@ -188,38 +217,47 @@ export class Plan {
|
|
|
188
217
|
* Synchronous, and runs no user callback, so a play of an async plan costs no more than a play
|
|
189
218
|
* of a synchronous one.
|
|
190
219
|
*/ play(seed) {
|
|
191
|
-
const { adapter } = this;
|
|
192
|
-
const root = adapter.createNode(this.model);
|
|
193
220
|
const settled = this.reusable(seed);
|
|
194
|
-
// Merged before anything else, so on a conflict it wins. A settled play already holds the
|
|
195
|
-
// plan's own `initial`, so reusing one merges the seed and nothing else: `mergeQuery` is not
|
|
196
|
-
// required to be idempotent.
|
|
197
|
-
const first = settled ? seed : seed ?? this.initial;
|
|
198
|
-
if (first) {
|
|
199
|
-
adapter.mergeQuery(root, first);
|
|
200
|
-
}
|
|
201
221
|
if (settled) {
|
|
202
|
-
|
|
203
|
-
// the
|
|
204
|
-
|
|
222
|
+
const root = this.adapter.createNode(this.model);
|
|
223
|
+
// A settled play already holds the plan's own `initial`, so only the seed is merged here.
|
|
224
|
+
// `mergeQuery` is not required to be idempotent. Every merge that play took still fits and
|
|
225
|
+
// every merge it left out still does not, so its node is taken whole.
|
|
226
|
+
if (seed) {
|
|
227
|
+
this.adapter.mergeQuery(root, seed);
|
|
228
|
+
}
|
|
229
|
+
this.adapter.mergeNode(root, settled.root);
|
|
205
230
|
return {
|
|
206
231
|
plan: this,
|
|
207
232
|
root,
|
|
208
233
|
mappings: settled.mappings
|
|
209
234
|
};
|
|
210
235
|
}
|
|
236
|
+
const { adapter } = this;
|
|
237
|
+
const root = adapter.createNode(this.model);
|
|
211
238
|
const mappings = {};
|
|
212
|
-
|
|
239
|
+
// Merged before anything else, so on a conflict it wins.
|
|
240
|
+
const first = seed ?? this.initial;
|
|
241
|
+
if (first) {
|
|
242
|
+
adapter.mergeQuery(root, first);
|
|
243
|
+
}
|
|
244
|
+
if (this.relationQuery) {
|
|
245
|
+
adapter.mergeQuery(root, this.relationQuery, AS_QUERY);
|
|
246
|
+
}
|
|
247
|
+
for (const merge of this.walk.merges){
|
|
213
248
|
switch(merge.kind){
|
|
214
249
|
case 'type':
|
|
215
250
|
adapter.mergeQuery(root, merge.query);
|
|
216
251
|
break;
|
|
217
|
-
case 'query':
|
|
218
|
-
adapter.mergeQuery(root, merge.query, AS_QUERY);
|
|
219
|
-
break;
|
|
220
252
|
case 'variant':
|
|
221
|
-
|
|
222
|
-
|
|
253
|
+
{
|
|
254
|
+
const conflict = adapter.firstConflict(root, merge.query);
|
|
255
|
+
if (conflict) {
|
|
256
|
+
throw variantConflict(merge.type, merge.variant, conflict);
|
|
257
|
+
}
|
|
258
|
+
adapter.mergeQuery(root, merge.query);
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
223
261
|
case 'field':
|
|
224
262
|
{
|
|
225
263
|
// One options object for the pair: `mergeQuery` reads only the alias, and
|
|
@@ -239,7 +277,7 @@ export class Plan {
|
|
|
239
277
|
default:
|
|
240
278
|
{
|
|
241
279
|
const unknown = merge;
|
|
242
|
-
throw
|
|
280
|
+
throw unexpectedError(`Unknown root merge ${String(unknown.kind)}`);
|
|
243
281
|
}
|
|
244
282
|
}
|
|
245
283
|
}
|
|
@@ -252,7 +290,7 @@ export class Plan {
|
|
|
252
290
|
/**
|
|
253
291
|
* The plan played behind `seed`, its mappings recorded for the resolvers beneath it, and its
|
|
254
292
|
* node serialized. A cached plan accepts the current resolver info to scope each execution.
|
|
255
|
-
* Every play records its mappings on the request context; an adapter whose
|
|
293
|
+
* Every play records its mappings for the execution on the request context; an adapter whose
|
|
256
294
|
* resolvers read a loaded row another way simply never looks them up.
|
|
257
295
|
*/ query(seed, info = this.info) {
|
|
258
296
|
// With no seed, a settled play's node is what a fresh play would build, so hand it straight
|
|
@@ -293,26 +331,23 @@ export class Plan {
|
|
|
293
331
|
}
|
|
294
332
|
return seed === undefined || !this.adapter.firstConflict(this.played.root, seed) ? this.played : undefined;
|
|
295
333
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Unlike a field-level select, a type-level selection has no per-field fallback, so a relation
|
|
337
|
+
* argument or a computed value that conflicts with what is already in the node is an error rather
|
|
338
|
+
* than a rejection. It surfaces while a request runs, so the client sees a generic message and the
|
|
339
|
+
* detail is its `cause`, which responses leave out.
|
|
340
|
+
*/ function variantConflict(type, variant, conflict) {
|
|
341
|
+
switch(conflict.kind){
|
|
342
|
+
case 'relation':
|
|
343
|
+
return unexpectedError(`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.`);
|
|
344
|
+
case 'extra':
|
|
345
|
+
return unexpectedError(`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.`);
|
|
346
|
+
default:
|
|
347
|
+
{
|
|
348
|
+
const unknown = conflict.kind;
|
|
349
|
+
return unexpectedError(`Unknown type-level conflict ${unknown}`);
|
|
313
350
|
}
|
|
314
|
-
}
|
|
315
|
-
this.adapter.mergeQuery(node, selection);
|
|
316
351
|
}
|
|
317
352
|
}
|
|
318
353
|
/**
|
|
@@ -324,6 +359,7 @@ export class Plan {
|
|
|
324
359
|
const field = info.parentType?.getFields()[node.name.value];
|
|
325
360
|
return field && {
|
|
326
361
|
parent: undefined,
|
|
362
|
+
walkParent: undefined,
|
|
327
363
|
type: info.parentType,
|
|
328
364
|
field,
|
|
329
365
|
node
|
package/esm/plan.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/plan.ts"],"names":["
|
|
1
|
+
{"version":3,"sources":["../src/plan.ts"],"names":["getNamedType","unexpectedError","setLoaderMappings","unionMappings","findMatches","includeOf","includePaths","matchesForModel","modelOf","resolveType","walkBranches","walkField","AS_QUERY","Object","freeze","asQuery","LENIENT","lenient","Plan","context","info","skipDeferredFragments","model","pending","merges","walk","relationQuery","positionRead","walks","played","fromInfo","adapter","options","typeName","path","paths","returnType","target","schema","getType","rootType","branches","entryPaths","normalized","normalizePaths","prefix","found","fieldNodes","flatMap","fieldNode","length","undefined","type","matches","map","match","field","indirectPath","deferred","plan","positionForResolvedField","initial","error","abandon","finish","settle","forParentRow","parentType","enterParentType","source","position","Map","name","nested","key","walked","get","created","set","chain","noop","collect","merge","push","collectQuery","query","value","prev","Promise","all","then","v","resolve","catch","done","arg","call","play","seed","settled","reusable","root","createNode","mergeQuery","mergeNode","mappings","first","kind","conflict","firstConflict","variantConflict","variant","ignoreArgs","alias","canMergeQuery","mapping","unknown","String","toQuery","selection","typeSelection","node","getFields","parent","walkParent","segment"],"mappings":"AAAA,SAAyDA,YAAY,QAAQ,UAAU;AAEvF,SAASC,eAAe,QAAQ,cAAc;AAC9C,SAAsCC,iBAAiB,EAAEC,aAAa,QAAQ,kBAAkB;AAChG,SACEC,WAAW,EAEXC,SAAS,EACTC,YAAY,EACZC,eAAe,EACfC,OAAO,EAEPC,WAAW,QACN,eAAe;AAUtB,SAAsBC,YAAY,EAAEC,SAAS,QAAQ,YAAY;AAEjE,oDAAoD;AACpD,MAAMC,WAAyBC,OAAOC,MAAM,CAAC;IAAEC,SAAS;AAAK;AAC7D,MAAMC,UAAwBH,OAAOC,MAAM,CAAC;IAAEG,SAAS;AAAK;AAuC5D,OAAO,MAAMC;;;;IACFC,QAAgB;IAChBC,KAAyB;IACzBC,sBAA+B;IACxC,iEAAiE,GACjE,AAASC,MAAa;IAEtB;;;GAGC,GACDC,QAAwB;IAExB,4FAA4F,GAC5F,AAAiBC,SAA6B,EAAE,CAAC;IAEjD,2FAA2F,GAC3F,AAAQC,OAAqC,IA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C,yBAAyB,EAAEmD,KAAKoB,IAAI,CAAC,KAAK,EAAEuC,QAAQvC,IAAI,CAAC,uBAAuB,EAAEoC,SAASpC,IAAI,CAAC,2EAA2E,CAAC;QAEjL,KAAK;YACH,OAAOvE,gBACL,CAAC,yBAAyB,EAAEmD,KAAKoB,IAAI,CAAC,KAAK,EAAEuC,QAAQvC,IAAI,CAAC,oBAAoB,EAAEoC,SAASpC,IAAI,CAAC,iHAAiH,CAAC;QAEpN;YAAS;gBACP,MAAM4C,UAAiBR,SAASD,IAAI;gBAEpC,OAAO1G,gBAAgB,CAAC,4BAA4B,EAAEmH,SAAmB;YAC3E;IACF;AACF;AAEA;;;;CAIC,GACD,SAASxD,yBAAyBxC,IAAwB;IACxD,MAAMqG,OAAOrG,KAAK2B,UAAU,CAAC,EAAE;IAC/B,MAAMS,QAAQpC,KAAK+C,UAAU,EAAEuD,WAAW,CAACD,KAAKjD,IAAI,CAACc,KAAK,CAAC;IAE3D,OAAO9B,SAAS;QAAEmE,QAAQxE;QAAWyE,YAAYzE;QAAWC,MAAMhC,KAAK+C,UAAU;QAAEX;QAAOiE;IAAK;AACjG;AAEA,SAAS7E,eAAeT,KAAsB;IAC5C,OAAOA,MAAMmB,GAAG,CAAC,CAACpB,OAChBA,KAAKoB,GAAG,CAAC,CAACuE,UAAa,OAAOA,YAAY,WAAW;gBAAErD,MAAMqD;YAAQ,IAAIA;AAE7E;AAEA,SAAS7C,QAAQ"}
|
package/esm/shape.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type FieldNode, type GraphQLResolveInfo, type SelectionSetNode } from 'graphql';
|
|
2
|
+
export declare function selectionShape(info: GraphQLResolveInfo, selectionSet: SelectionSetNode | undefined): number;
|
|
3
|
+
/**
|
|
4
|
+
* The shape of a field selection: its alias, name, arguments, directives and the shape of its
|
|
5
|
+
* selection set. Two field nodes with one shape select the same thing under the same response key.
|
|
6
|
+
*/
|
|
7
|
+
export declare function fieldShape(info: GraphQLResolveInfo, field: FieldNode): number;
|
|
8
|
+
//# sourceMappingURL=shape.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shape.d.ts","sourceRoot":"","sources":["../src/shape.ts"],"names":[],"mappings":"AAMA,OAAO,EAEL,KAAK,SAAS,EACd,KAAK,kBAAkB,EAIvB,KAAK,gBAAgB,EACtB,MAAM,SAAS,CAAC;AAgBjB,wBAAgB,cAAc,CAC5B,IAAI,EAAE,kBAAkB,EACxB,YAAY,EAAE,gBAAgB,GAAG,SAAS,GACzC,MAAM,CAER;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,kBAAkB,EAAE,KAAK,EAAE,SAAS,GAAG,MAAM,CAU7E"}
|
package/esm/shape.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shape of a selection set: a number that two selection sets of one document share exactly
|
|
3
|
+
* when they select the same thing, written the same way. A walk beneath a field reads its
|
|
4
|
+
* selection set and nothing else of the node, so walks are shared by shape rather than by node.
|
|
5
|
+
*/ import { PothosValidationError } from '@pothos/core';
|
|
6
|
+
import { Kind, print } from 'graphql';
|
|
7
|
+
/**
|
|
8
|
+
* Per operation: a fragment spread is shaped by its name, which means the same definition only
|
|
9
|
+
* within one document. An operation's node belongs to one document and, unlike `info.fragments`,
|
|
10
|
+
* is the same in every execution of a cached document, so each set is shaped once per document.
|
|
11
|
+
*/ const shapesByOperation = new WeakMap();
|
|
12
|
+
export function selectionShape(info, selectionSet) {
|
|
13
|
+
return selectionSet ? shapeOfSet(shapesOf(info), selectionSet) : 0;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The shape of a field selection: its alias, name, arguments, directives and the shape of its
|
|
17
|
+
* selection set. Two field nodes with one shape select the same thing under the same response key.
|
|
18
|
+
*/ export function fieldShape(info, field) {
|
|
19
|
+
const shapes = shapesOf(info);
|
|
20
|
+
let id = shapes.fields.get(field);
|
|
21
|
+
if (id === undefined) {
|
|
22
|
+
id = shapeOfSelection(shapes, field);
|
|
23
|
+
shapes.fields.set(field, id);
|
|
24
|
+
}
|
|
25
|
+
return id;
|
|
26
|
+
}
|
|
27
|
+
function shapesOf(info) {
|
|
28
|
+
let shapes = shapesByOperation.get(info.operation);
|
|
29
|
+
if (!shapes) {
|
|
30
|
+
shapes = {
|
|
31
|
+
ids: new Map(),
|
|
32
|
+
sets: new WeakMap(),
|
|
33
|
+
fields: new WeakMap()
|
|
34
|
+
};
|
|
35
|
+
shapesByOperation.set(info.operation, shapes);
|
|
36
|
+
}
|
|
37
|
+
return shapes;
|
|
38
|
+
}
|
|
39
|
+
function shapeOfSet(shapes, selectionSet) {
|
|
40
|
+
let id = shapes.sets.get(selectionSet);
|
|
41
|
+
if (id === undefined) {
|
|
42
|
+
id = intern(shapes, selectionSet.selections.map((selection)=>shapeOfSelection(shapes, selection)));
|
|
43
|
+
shapes.sets.set(selectionSet, id);
|
|
44
|
+
}
|
|
45
|
+
return id;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A selection's text with every selection set beneath it replaced by its id. A spread stops at its
|
|
49
|
+
* name, so nothing is shaped twice however often a fragment is spread.
|
|
50
|
+
*/ function shapeOfSelection(shapes, selection) {
|
|
51
|
+
switch(selection.kind){
|
|
52
|
+
case Kind.FIELD:
|
|
53
|
+
return intern(shapes, [
|
|
54
|
+
'field',
|
|
55
|
+
selection.alias?.value ?? '',
|
|
56
|
+
selection.name.value,
|
|
57
|
+
printAll(selection.arguments),
|
|
58
|
+
printAll(selection.directives),
|
|
59
|
+
selection.selectionSet ? shapeOfSet(shapes, selection.selectionSet) : 0
|
|
60
|
+
]);
|
|
61
|
+
case Kind.FRAGMENT_SPREAD:
|
|
62
|
+
return intern(shapes, [
|
|
63
|
+
'spread',
|
|
64
|
+
selection.name.value,
|
|
65
|
+
printAll(selection.directives)
|
|
66
|
+
]);
|
|
67
|
+
case Kind.INLINE_FRAGMENT:
|
|
68
|
+
return intern(shapes, [
|
|
69
|
+
'inline',
|
|
70
|
+
selection.typeCondition?.name.value ?? '',
|
|
71
|
+
printAll(selection.directives),
|
|
72
|
+
shapeOfSet(shapes, selection.selectionSet)
|
|
73
|
+
]);
|
|
74
|
+
default:
|
|
75
|
+
{
|
|
76
|
+
const unknown = selection;
|
|
77
|
+
throw new PothosValidationError(`Unknown selection kind ${unknown.kind}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function printAll(nodes) {
|
|
82
|
+
return nodes?.length ? nodes.map((node)=>print(node)) : [];
|
|
83
|
+
}
|
|
84
|
+
/** The id of `parts`, assigned the first time they are seen. Ids start at 1; 0 means no set. */ function intern(shapes, parts) {
|
|
85
|
+
const text = JSON.stringify(parts);
|
|
86
|
+
let id = shapes.ids.get(text);
|
|
87
|
+
if (id === undefined) {
|
|
88
|
+
id = shapes.ids.size + 1;
|
|
89
|
+
shapes.ids.set(text, id);
|
|
90
|
+
}
|
|
91
|
+
return id;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//# sourceMappingURL=shape.js.map
|
package/esm/shape.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/shape.ts"],"names":["PothosValidationError","Kind","print","shapesByOperation","WeakMap","selectionShape","info","selectionSet","shapeOfSet","shapesOf","fieldShape","field","shapes","id","fields","get","undefined","shapeOfSelection","set","operation","ids","Map","sets","intern","selections","map","selection","kind","FIELD","alias","value","name","printAll","arguments","directives","FRAGMENT_SPREAD","INLINE_FRAGMENT","typeCondition","unknown","nodes","length","node","parts","text","JSON","stringify","size"],"mappings":"AAAA;;;;CAIC,GACD,SAASA,qBAAqB,QAAQ,eAAe;AACrD,SAIEC,IAAI,EACJC,KAAK,QAGA,UAAU;AASjB;;;;CAIC,GACD,MAAMC,oBAAoB,IAAIC;AAE9B,OAAO,SAASC,eACdC,IAAwB,EACxBC,YAA0C;IAE1C,OAAOA,eAAeC,WAAWC,SAASH,OA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;gBAAC;gBAAUc,UAAUK,IAAI,CAACD,KAAK;gBAAEE,SAASN,UAAUQ,UAAU;aAAE;QACxF,KAAKjC,KAAKmC,eAAe;YACvB,OAAOb,OAAOX,QAAQ;gBACpB;gBACAc,UAAUW,aAAa,EAAEN,KAAKD,SAAS;gBACvCE,SAASN,UAAUQ,UAAU;gBAC7B1B,WAAWI,QAAQc,UAAUnB,YAAY;aAC1C;QACH;YAAS;gBACP,MAAM+B,UAAiBZ;gBAEvB,MAAM,IAAI1B,sBAAsB,CAAC,uBAAuB,EAAE,AAACsC,QAA0BX,IAAI,EAAE;YAC7F;IACF;AACF;AAEA,SAASK,SAASO,KAAqC;IACrD,OAAOA,OAAOC,SAASD,MAAMd,GAAG,CAAC,CAACgB,OAASvC,MAAMuC,SAAS,EAAE;AAC9D;AAEA,8FAA8F,GAC9F,SAASlB,OAAOX,MAAc,EAAE8B,KAAgB;IAC9C,MAAMC,OAAOC,KAAKC,SAAS,CAACH;IAC5B,IAAI7B,KAAKD,OAAOQ,GAAG,CAACL,GAAG,CAAC4B;IAExB,IAAI9B,OAAOG,WAAW;QACpBH,KAAKD,OAAOQ,GAAG,CAAC0B,IAAI,GAAG;QACvBlC,OAAOQ,GAAG,CAACF,GAAG,CAACyB,MAAM9B;IACvB;IAEA,OAAOA;AACT"}
|
package/esm/types.d.ts
CHANGED
|
@@ -16,8 +16,26 @@ export type WalkedType = GraphQLInterfaceType | GraphQLObjectType;
|
|
|
16
16
|
* about costs one link per select function and no arrays at all.
|
|
17
17
|
*/
|
|
18
18
|
export interface Position {
|
|
19
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* The field this one is selected beneath, or undefined at the field the plan started from.
|
|
21
|
+
* Reading it during planning has a cost. Invocations of a field at different places normally
|
|
22
|
+
* share the walk beneath it; a read keeps every walk it reads through from being shared, so
|
|
23
|
+
* each invocation is walked with its own path, which a document whose fragments branch at every
|
|
24
|
+
* level makes exponential in its depth. A fragment expanding to the implementations of a model
|
|
25
|
+
* interface walks its fields as each implementation, which adds an invocation, and a path, per
|
|
26
|
+
* implementation, except for a field the implementation inherits unchanged from that interface
|
|
27
|
+
* and no fragment narrows to the implementation: it is invoked once, on the interface, and its
|
|
28
|
+
* path names the interface. Read after planning, it is the first invocation's.
|
|
29
|
+
*/
|
|
20
30
|
readonly parent: Position | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* The same link as `parent`, read without keeping the walk beneath from being shared. Its chain
|
|
33
|
+
* is the one the shared walk holds, the first invocation's, and not always the true path, so it
|
|
34
|
+
* is only for a name that needs to agree with itself: one a plan writes into its query and a
|
|
35
|
+
* resolver beneath the same walk reads back through its mapping. Anything that depends on where
|
|
36
|
+
* a field really is reads `parent`.
|
|
37
|
+
*/
|
|
38
|
+
readonly walkParent: Position | undefined;
|
|
21
39
|
/** The type the field was walked on: its parent type, or a same-model type the plan moved to. */
|
|
22
40
|
readonly type: WalkedType;
|
|
23
41
|
readonly field: GraphQLField<unknown, unknown>;
|
|
@@ -61,6 +79,8 @@ export interface EntryOptions<Query> {
|
|
|
61
79
|
*/
|
|
62
80
|
skipDeferredFragments?: boolean;
|
|
63
81
|
}
|
|
82
|
+
/** The options of `Plan.forParentRow`, which plans the field `info` resolves on its own. */
|
|
83
|
+
export type ParentRowOptions<Query> = Pick<EntryOptions<Query>, 'context' | 'info' | 'initial' | 'skipDeferredFragments'>;
|
|
64
84
|
/** How one merge into an adapter's node differs from a plain one. */
|
|
65
85
|
export interface MergeOptions {
|
|
66
86
|
/**
|