@distilled.cloud/core 1.0.0-rc.16 → 1.0.0-rc.17
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/lib/_bundle/aws.js +2 -0
- package/lib/_bundle/aws.js.map +1 -0
- package/lib/_bundle/base.js +5 -0
- package/lib/_bundle/base.js.map +1 -0
- package/lib/_bundle/graphql.js +3 -0
- package/lib/_bundle/graphql.js.map +1 -0
- package/lib/_bundle/rest.js +2 -0
- package/lib/_bundle/rest.js.map +1 -0
- package/lib/_bundle/xml.js +4 -0
- package/lib/_bundle/xml.js.map +1 -0
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -796
- package/lib/category.js +1 -278
- package/lib/chunks.js +1 -11
- package/lib/config.js +1 -161
- package/lib/crypto.js +1 -130
- package/lib/debug.js +1 -8
- package/lib/error-category.js +1 -47
- package/lib/errors.js +1 -198
- package/lib/graphql.js +1 -1058
- package/lib/graphql.types.js +1 -6
- package/lib/index.js +0 -15
- package/lib/json-patch.js +1 -226
- package/lib/json-patch.js.map +1 -1
- package/lib/pagination.js +1 -271
- package/lib/plain-values.js +1 -39
- package/lib/plain-values.js.map +1 -1
- package/lib/protocol-http.js +1 -812
- package/lib/protocol-rest.js +1 -375
- package/lib/query.js +1 -27
- package/lib/response-validation-strict.js +1 -284
- package/lib/response-validation.js +1 -118
- package/lib/retry-after.js +1 -112
- package/lib/retry.js +1 -197
- package/lib/schema-lite-impl.js +1 -341
- package/lib/schema-lite-typed.js +1 -26
- package/lib/schema-lite-typed.js.map +1 -1
- package/lib/schema-lite.js +1 -31
- package/lib/schema.js +1 -220
- package/lib/testing.js +1 -41
- package/lib/testing.js.map +1 -1
- package/lib/trait.js +1 -131
- package/lib/xml.js +1 -323
- package/package.json +1 -1
- package/src/api.ts +7 -6
- package/lib/api.js.map +0 -1
- package/lib/category.js.map +0 -1
- package/lib/chunks.js.map +0 -1
- package/lib/config.js.map +0 -1
- package/lib/crypto.js.map +0 -1
- package/lib/debug.js.map +0 -1
- package/lib/error-category.js.map +0 -1
- package/lib/errors.js.map +0 -1
- package/lib/graphql.js.map +0 -1
- package/lib/graphql.types.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/pagination.js.map +0 -1
- package/lib/protocol-http.js.map +0 -1
- package/lib/protocol-rest.js.map +0 -1
- package/lib/query.js.map +0 -1
- package/lib/response-validation-strict.js.map +0 -1
- package/lib/response-validation.js.map +0 -1
- package/lib/retry-after.js.map +0 -1
- package/lib/retry.js.map +0 -1
- package/lib/schema-lite-impl.js.map +0 -1
- package/lib/schema-lite.js.map +0 -1
- package/lib/schema.js.map +0 -1
- package/lib/trait.js.map +0 -1
- package/lib/xml.js.map +0 -1
package/lib/graphql.js
CHANGED
|
@@ -1,1058 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
import * as Context from "effect/Context";
|
|
54
|
-
import * as Data from "effect/Data";
|
|
55
|
-
import * as Effect from "effect/Effect";
|
|
56
|
-
import * as Option from "effect/Option";
|
|
57
|
-
import * as Redacted from "effect/Redacted";
|
|
58
|
-
import * as Schedule from "effect/Schedule";
|
|
59
|
-
import * as S from "effect/Schema";
|
|
60
|
-
import * as Stream from "effect/Stream";
|
|
61
|
-
import { unwrapRedactedDeep } from "./plain-values.js";
|
|
62
|
-
export const QuerySymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/graphql/Query");
|
|
63
|
-
const inspect = /*@__PURE__*/ Symbol.for("nodejs.util.inspect.custom");
|
|
64
|
-
|
|
65
|
-
export class GqlError {
|
|
66
|
-
_tag = "GqlError";
|
|
67
|
-
message;
|
|
68
|
-
constructor(message) {
|
|
69
|
-
this.message = message;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
// U+2500U+2500 Errors U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500
|
|
73
|
-
//
|
|
74
|
-
// Every entry of a GraphQL `errors` array becomes one typed issue. Generated
|
|
75
|
-
// SDKs declare tagged error classes plus the matchers that recognize them,
|
|
76
|
-
// scoped to the root fields that can return them (or global). `Query.fn`
|
|
77
|
-
// classifies each entry against the roots its path points at, so the error
|
|
78
|
-
// channel is the union of the errors declared by the roots the plan reads.
|
|
79
|
-
|
|
80
|
-
export const errorFields = {
|
|
81
|
-
message: S.String,
|
|
82
|
-
code: /*@__PURE__*/ S.optional(S.String),
|
|
83
|
-
path: /*@__PURE__*/ S.optional(
|
|
84
|
-
/*@__PURE__*/ S.Array(/*@__PURE__*/ S.Union([S.String, S.Number]))),
|
|
85
|
-
locations: /*@__PURE__*/ S.optional(
|
|
86
|
-
/*@__PURE__*/ S.Array(/*@__PURE__*/ S.Struct({ line: S.Number, column: S.Number }))),
|
|
87
|
-
extensions: /*@__PURE__*/ S.optional(S.Unknown),
|
|
88
|
-
traceId: /*@__PURE__*/ S.optional(S.String),
|
|
89
|
-
status: /*@__PURE__*/ S.optional(S.Number),
|
|
90
|
-
|
|
91
|
-
retryAfter: /*@__PURE__*/ S.optional(S.Number),
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
export class UnknownGraphQLError extends /*@__PURE__*/ /*@__PURE__*/ S.TaggedError()("UnknownGraphQLError", {
|
|
95
|
-
...errorFields,
|
|
96
|
-
|
|
97
|
-
coordinate: /*@__PURE__*/ S.optional(S.String),
|
|
98
|
-
}) {
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export class GraphQLFailure extends /*@__PURE__*/ /*@__PURE__*/ Data.TaggedError("GraphQLFailure") {
|
|
102
|
-
get message() {
|
|
103
|
-
return this.errors
|
|
104
|
-
.map((e) => `${e._tag}${e.path?.length ? ` at ${e.path.join(".")}` : ""}: ${e.message}`)
|
|
105
|
-
.join("; ");
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export class GraphQLTransportError extends /*@__PURE__*/ /*@__PURE__*/ Data.TaggedError("GraphQLTransportError") {
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export class GraphQLPaginationError extends /*@__PURE__*/ /*@__PURE__*/ Data.TaggedError("GraphQLPaginationError") {
|
|
113
|
-
}
|
|
114
|
-
export const errorSpec = (make, tag, matchers, options = {}) => ({
|
|
115
|
-
tag,
|
|
116
|
-
make: (props) => new make(props),
|
|
117
|
-
matchers,
|
|
118
|
-
retryable: options.retryable ?? false,
|
|
119
|
-
global: options.global ?? false,
|
|
120
|
-
});
|
|
121
|
-
export const scalarField = (name, options = {}) => ({
|
|
122
|
-
name,
|
|
123
|
-
kind: "scalar",
|
|
124
|
-
...(options.sensitive ? { sensitive: true } : {}),
|
|
125
|
-
});
|
|
126
|
-
export const objectField = (name, objectType) => ({
|
|
127
|
-
name,
|
|
128
|
-
kind: "object",
|
|
129
|
-
of: objectType,
|
|
130
|
-
});
|
|
131
|
-
export const listField = (name, itemType, argTypes) => ({
|
|
132
|
-
name,
|
|
133
|
-
kind: "list",
|
|
134
|
-
of: itemType,
|
|
135
|
-
argTypes,
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
export const connectionField = (name, nodeType, argTypes) => ({
|
|
139
|
-
name,
|
|
140
|
-
kind: "connection",
|
|
141
|
-
of: nodeType,
|
|
142
|
-
argTypes,
|
|
143
|
-
});
|
|
144
|
-
const objectRef = (meta) => ({ tag: "object", meta });
|
|
145
|
-
const listRef = (itemType) => ({
|
|
146
|
-
tag: "list",
|
|
147
|
-
of: itemType,
|
|
148
|
-
});
|
|
149
|
-
const fieldResult = (parent, field) => {
|
|
150
|
-
const inner = () => {
|
|
151
|
-
if (field.kind === "scalar")
|
|
152
|
-
return { tag: "scalar" };
|
|
153
|
-
if (field.kind === "object")
|
|
154
|
-
return objectRef(field.of);
|
|
155
|
-
return listRef(objectRef(field.of)); // list | connection
|
|
156
|
-
};
|
|
157
|
-
return parent.tag === "list" ? listRef(fieldResult(parent.of, field)) : inner();
|
|
158
|
-
};
|
|
159
|
-
const unwrapList = (typeRef) => {
|
|
160
|
-
if (typeRef.tag !== "list")
|
|
161
|
-
throw new GqlError("expected a list Query");
|
|
162
|
-
return typeRef.of;
|
|
163
|
-
};
|
|
164
|
-
const fieldsOf = (typeRef) => {
|
|
165
|
-
if (typeRef.tag === "object")
|
|
166
|
-
return typeRef.meta.fields;
|
|
167
|
-
if (typeRef.tag === "list")
|
|
168
|
-
return fieldsOf(typeRef.of);
|
|
169
|
-
return {};
|
|
170
|
-
};
|
|
171
|
-
export class GqlTransport extends /*@__PURE__*/ /*@__PURE__*/ Context.Service()("@distilled.cloud/graphql/Transport") {
|
|
172
|
-
}
|
|
173
|
-
const log = (..._args) => { };
|
|
174
|
-
export class QueryNode {
|
|
175
|
-
[QuerySymbol] = QuerySymbol;
|
|
176
|
-
expr;
|
|
177
|
-
constructor(expr) {
|
|
178
|
-
this.expr = expr;
|
|
179
|
-
return proxy(this);
|
|
180
|
-
}
|
|
181
|
-
pipe(...steps) {
|
|
182
|
-
return steps.reduce((current, step) => step(current), this);
|
|
183
|
-
}
|
|
184
|
-
[inspect]() {
|
|
185
|
-
return printExpr(this.expr);
|
|
186
|
-
}
|
|
187
|
-
toString() {
|
|
188
|
-
return this[inspect]();
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
export const isQuery = (value) => (typeof value === "object" || typeof value === "function") &&
|
|
192
|
-
value !== null &&
|
|
193
|
-
QuerySymbol in value;
|
|
194
|
-
const make = (expr) => new QueryNode(expr);
|
|
195
|
-
const itemQuery = (list) => new QueryNode({
|
|
196
|
-
_tag: "Item",
|
|
197
|
-
list: list.expr,
|
|
198
|
-
type: unwrapList(list.expr.type),
|
|
199
|
-
});
|
|
200
|
-
function proxy(self) {
|
|
201
|
-
const callable = Object.assign(function (bag) {
|
|
202
|
-
return bag == null ? self : applyBag(self, bag);
|
|
203
|
-
}, self);
|
|
204
|
-
return new Proxy(callable, {
|
|
205
|
-
has: (_, prop) => prop === QuerySymbol ||
|
|
206
|
-
prop === inspect ||
|
|
207
|
-
prop in self ||
|
|
208
|
-
(typeof prop === "string" && prop in fieldsOf(self.expr.type)),
|
|
209
|
-
get: (_target, prop) => {
|
|
210
|
-
if (prop === QuerySymbol)
|
|
211
|
-
return QuerySymbol;
|
|
212
|
-
if (prop === inspect)
|
|
213
|
-
return self[inspect].bind(self);
|
|
214
|
-
if (prop === "pipe")
|
|
215
|
-
return self.pipe.bind(self);
|
|
216
|
-
if (typeof prop === "string" && prop in self) {
|
|
217
|
-
const member = self[prop];
|
|
218
|
-
return typeof member === "function" ? member.bind(self) : member;
|
|
219
|
-
}
|
|
220
|
-
if (typeof prop !== "string")
|
|
221
|
-
return undefined;
|
|
222
|
-
const field = fieldsOf(self.expr.type)[prop];
|
|
223
|
-
if (!field)
|
|
224
|
-
return undefined;
|
|
225
|
-
return make({
|
|
226
|
-
_tag: "Prop",
|
|
227
|
-
parent: self.expr,
|
|
228
|
-
field,
|
|
229
|
-
type: fieldResult(self.expr.type, field),
|
|
230
|
-
});
|
|
231
|
-
},
|
|
232
|
-
apply: (_target, _thisArg, args) => applyBag(self, args[0] ?? {}),
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
const applyBag = (self, bag) => {
|
|
236
|
-
const argTypes = self.expr._tag === "Prop"
|
|
237
|
-
? self.expr.field.argTypes
|
|
238
|
-
: self.expr._tag === "Root"
|
|
239
|
-
? self.expr.argTypes
|
|
240
|
-
: undefined;
|
|
241
|
-
const args = {};
|
|
242
|
-
for (const [argName, argValue] of Object.entries(bag)) {
|
|
243
|
-
if (argTypes && argName in argTypes)
|
|
244
|
-
args[argName] = argValue;
|
|
245
|
-
}
|
|
246
|
-
if (self.expr._tag === "Prop") {
|
|
247
|
-
return make({
|
|
248
|
-
...self.expr,
|
|
249
|
-
args: { ...self.expr.args, ...args },
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
if (self.expr._tag === "Root") {
|
|
253
|
-
return make({
|
|
254
|
-
...self.expr,
|
|
255
|
-
args: { ...self.expr.args, ...args },
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
return self;
|
|
259
|
-
};
|
|
260
|
-
export const root = (op, field, meta, args, argTypes, errors = []) => make({
|
|
261
|
-
_tag: "Root",
|
|
262
|
-
op,
|
|
263
|
-
field,
|
|
264
|
-
args,
|
|
265
|
-
argTypes,
|
|
266
|
-
type: objectRef(meta),
|
|
267
|
-
errors,
|
|
268
|
-
});
|
|
269
|
-
export const rootList = (op, field, meta, args, argTypes, errors = []) => make({
|
|
270
|
-
_tag: "Root",
|
|
271
|
-
op,
|
|
272
|
-
field,
|
|
273
|
-
args,
|
|
274
|
-
argTypes,
|
|
275
|
-
type: listRef(objectRef(meta)),
|
|
276
|
-
errors,
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
export const rootConnection = (op, field, meta, args, argTypes, errors = []) => make({
|
|
280
|
-
_tag: "Root",
|
|
281
|
-
op,
|
|
282
|
-
field,
|
|
283
|
-
args,
|
|
284
|
-
argTypes,
|
|
285
|
-
type: listRef(objectRef(meta)),
|
|
286
|
-
connection: true,
|
|
287
|
-
errors,
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
export const rootLeaf = (op, field, list, args, argTypes, errors = [], options = {}) => make({
|
|
291
|
-
_tag: "Root",
|
|
292
|
-
op,
|
|
293
|
-
field,
|
|
294
|
-
args,
|
|
295
|
-
argTypes,
|
|
296
|
-
type: list ? listRef({ tag: "scalar" }) : { tag: "scalar" },
|
|
297
|
-
errors,
|
|
298
|
-
...(options.sensitive ? { sensitive: true } : {}),
|
|
299
|
-
});
|
|
300
|
-
const printExpr = (expr) => {
|
|
301
|
-
switch (expr._tag) {
|
|
302
|
-
case "Root":
|
|
303
|
-
return expr.args ? `${expr.field}(${JSON.stringify(expr.args)})` : expr.field;
|
|
304
|
-
case "Prop":
|
|
305
|
-
return `${printExpr(expr.parent)}.${expr.field.name}`;
|
|
306
|
-
case "Item":
|
|
307
|
-
return "$item";
|
|
308
|
-
case "MapValue":
|
|
309
|
-
return `map(${printExpr(expr.parent)})`;
|
|
310
|
-
case "MapItems":
|
|
311
|
-
return `mapItems(${printExpr(expr.parent)})`;
|
|
312
|
-
case "Filter":
|
|
313
|
-
return `filter(${printExpr(expr.parent)})`;
|
|
314
|
-
case "FlatMap":
|
|
315
|
-
return `flatMap(${printExpr(expr.parent)})`;
|
|
316
|
-
case "Literal":
|
|
317
|
-
return `of(${String(expr.value)})`;
|
|
318
|
-
}
|
|
319
|
-
};
|
|
320
|
-
const emptySel = (field) => ({
|
|
321
|
-
field,
|
|
322
|
-
alias: undefined,
|
|
323
|
-
args: undefined,
|
|
324
|
-
children: new Map(),
|
|
325
|
-
isScalar: false,
|
|
326
|
-
isList: false,
|
|
327
|
-
});
|
|
328
|
-
const stableStringify = (value) => {
|
|
329
|
-
if (value === null || typeof value !== "object")
|
|
330
|
-
return JSON.stringify(value);
|
|
331
|
-
if (Array.isArray(value))
|
|
332
|
-
return `[${value.map(stableStringify).join(",")}]`;
|
|
333
|
-
const keys = Object.keys(value).sort();
|
|
334
|
-
return `{${keys.map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`).join(",")}}`;
|
|
335
|
-
};
|
|
336
|
-
const bindArgs = (forest, args, argTypes) => {
|
|
337
|
-
if (args === undefined)
|
|
338
|
-
return undefined;
|
|
339
|
-
const bound = {};
|
|
340
|
-
let hasArgs = false;
|
|
341
|
-
for (const [name, value] of Object.entries(args)) {
|
|
342
|
-
if (value === undefined)
|
|
343
|
-
continue;
|
|
344
|
-
forest.vars.set(`v${forest.varI}`, {
|
|
345
|
-
type: argTypes?.[name] ?? "String",
|
|
346
|
-
value,
|
|
347
|
-
});
|
|
348
|
-
bound[name] = `v${forest.varI++}`;
|
|
349
|
-
hasArgs = true;
|
|
350
|
-
}
|
|
351
|
-
return hasArgs ? bound : undefined;
|
|
352
|
-
};
|
|
353
|
-
const ensureChild = (forest, parent, field, options) => {
|
|
354
|
-
const args = unwrapRedactedDeep(options.args);
|
|
355
|
-
const key = args === undefined ? field : `${field}:${stableStringify(args)}`;
|
|
356
|
-
const existing = parent.children.get(key);
|
|
357
|
-
if (existing)
|
|
358
|
-
return existing;
|
|
359
|
-
const taken = new Set([...parent.children.values()].map((c) => c.alias ?? c.field));
|
|
360
|
-
let alias;
|
|
361
|
-
if (taken.has(field)) {
|
|
362
|
-
do {
|
|
363
|
-
alias = `${field}_${forest.aliasI++}`;
|
|
364
|
-
} while (taken.has(alias));
|
|
365
|
-
}
|
|
366
|
-
const sel = {
|
|
367
|
-
field,
|
|
368
|
-
alias,
|
|
369
|
-
args: bindArgs(forest, args, options.argTypes),
|
|
370
|
-
children: new Map(),
|
|
371
|
-
isScalar: options.isScalar ?? false,
|
|
372
|
-
isList: options.isList ?? false,
|
|
373
|
-
...(options.sensitive ? { sensitive: true } : {}),
|
|
374
|
-
};
|
|
375
|
-
parent.children.set(key, sel);
|
|
376
|
-
return sel;
|
|
377
|
-
};
|
|
378
|
-
const paintConnection = (forest, connection) => {
|
|
379
|
-
const edges = ensureChild(forest, connection, "edges", { isList: true });
|
|
380
|
-
ensureChild(forest, edges, "node", {});
|
|
381
|
-
return ["edges", "node"];
|
|
382
|
-
};
|
|
383
|
-
const paintAbs = (forest, expr) => {
|
|
384
|
-
switch (expr._tag) {
|
|
385
|
-
case "Root": {
|
|
386
|
-
if (forest.kind !== undefined && forest.kind !== expr.op) {
|
|
387
|
-
throw new GqlError(`cannot mix ${forest.kind} and ${expr.op} in one document`);
|
|
388
|
-
}
|
|
389
|
-
forest.kind = expr.op;
|
|
390
|
-
const node = ensureChild(forest, forest.roots, expr.field, {
|
|
391
|
-
args: expr.args,
|
|
392
|
-
argTypes: expr.argTypes,
|
|
393
|
-
// Leaf roots (`projectDelete: Boolean!`) take no sub-selection.
|
|
394
|
-
isScalar: expr.type.tag === "scalar" || (expr.type.tag === "list" && expr.type.of.tag === "scalar"),
|
|
395
|
-
isList: expr.type.tag === "list" && !expr.connection,
|
|
396
|
-
sensitive: expr.sensitive,
|
|
397
|
-
});
|
|
398
|
-
const key = node.alias ?? node.field;
|
|
399
|
-
forest.rootAlias.set(expr, key);
|
|
400
|
-
forest.rootErrors.set(key, { field: expr.field, errors: expr.errors });
|
|
401
|
-
return expr.connection ? [key, ...paintConnection(forest, node)] : [key];
|
|
402
|
-
}
|
|
403
|
-
case "Prop": {
|
|
404
|
-
const parentPath = paintAbs(forest, expr.parent);
|
|
405
|
-
const parentSel = selAt(forest.roots, parentPath);
|
|
406
|
-
const node = ensureChild(forest, parentSel, expr.field.name, {
|
|
407
|
-
args: expr.args,
|
|
408
|
-
argTypes: expr.field.argTypes,
|
|
409
|
-
isScalar: expr.field.kind === "scalar",
|
|
410
|
-
isList: expr.field.kind === "list",
|
|
411
|
-
sensitive: expr.field.sensitive,
|
|
412
|
-
});
|
|
413
|
-
const path = [...parentPath, node.alias ?? node.field];
|
|
414
|
-
return expr.field.kind === "connection" ? [...path, ...paintConnection(forest, node)] : path;
|
|
415
|
-
}
|
|
416
|
-
default:
|
|
417
|
-
throw new GqlError(`paintAbs: ${expr._tag}`);
|
|
418
|
-
}
|
|
419
|
-
};
|
|
420
|
-
const selAt = (roots, path) => {
|
|
421
|
-
let current = roots;
|
|
422
|
-
for (const segment of path) {
|
|
423
|
-
if (segment === "*")
|
|
424
|
-
continue;
|
|
425
|
-
const child = [...current.children.values()].find((candidate) => (candidate.alias ?? candidate.field) === segment);
|
|
426
|
-
if (!child) {
|
|
427
|
-
throw new GqlError(`internal: missing ${String(segment)}`);
|
|
428
|
-
}
|
|
429
|
-
current = child;
|
|
430
|
-
}
|
|
431
|
-
return current;
|
|
432
|
-
};
|
|
433
|
-
const connectionNodeSel = (connection) => {
|
|
434
|
-
const edges = [...connection.children.values()].find((c) => c.field === "edges");
|
|
435
|
-
const node = edges ? [...edges.children.values()].find((c) => c.field === "node") : undefined;
|
|
436
|
-
if (!node)
|
|
437
|
-
throw new GqlError("internal: connection missing edges.node");
|
|
438
|
-
return node;
|
|
439
|
-
};
|
|
440
|
-
const relSel = (expr, listSel) => {
|
|
441
|
-
switch (expr._tag) {
|
|
442
|
-
case "Item":
|
|
443
|
-
return listSel;
|
|
444
|
-
case "Prop": {
|
|
445
|
-
const parent = relSel(expr.parent, listSel);
|
|
446
|
-
const child = [...parent.children.values()].find((c) => c.field === expr.field.name);
|
|
447
|
-
if (!child) {
|
|
448
|
-
throw new GqlError(`missing ${expr.field.name} in list selection`);
|
|
449
|
-
}
|
|
450
|
-
return expr.field.kind === "connection" ? connectionNodeSel(child) : child;
|
|
451
|
-
}
|
|
452
|
-
default:
|
|
453
|
-
return listSel;
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
const paintRel = (forest, expr, listSel) => {
|
|
457
|
-
switch (expr._tag) {
|
|
458
|
-
case "Item":
|
|
459
|
-
case "Literal":
|
|
460
|
-
return;
|
|
461
|
-
case "Prop": {
|
|
462
|
-
paintRel(forest, expr.parent, listSel);
|
|
463
|
-
const child = ensureChild(forest, relSel(expr.parent, listSel), expr.field.name, {
|
|
464
|
-
args: expr.args,
|
|
465
|
-
argTypes: expr.field.argTypes,
|
|
466
|
-
isScalar: expr.field.kind === "scalar",
|
|
467
|
-
isList: expr.field.kind === "list",
|
|
468
|
-
sensitive: expr.field.sensitive,
|
|
469
|
-
});
|
|
470
|
-
if (expr.field.kind === "connection")
|
|
471
|
-
paintConnection(forest, child);
|
|
472
|
-
return;
|
|
473
|
-
}
|
|
474
|
-
case "MapValue":
|
|
475
|
-
case "FlatMap":
|
|
476
|
-
paintRel(forest, expr.parent, listSel);
|
|
477
|
-
return;
|
|
478
|
-
case "MapItems":
|
|
479
|
-
paintRel(forest, expr.parent, listSel);
|
|
480
|
-
visitRel(expr.mapped, forest, relSel(expr.parent, listSel));
|
|
481
|
-
return;
|
|
482
|
-
case "Filter":
|
|
483
|
-
paintRel(forest, expr.parent, listSel);
|
|
484
|
-
paintRel(forest, expr.predicate, relSel(expr.parent, listSel));
|
|
485
|
-
return;
|
|
486
|
-
case "Root":
|
|
487
|
-
throw new GqlError("root inside list item");
|
|
488
|
-
}
|
|
489
|
-
};
|
|
490
|
-
const visitExpr = (forest, expr) => {
|
|
491
|
-
switch (expr._tag) {
|
|
492
|
-
case "Root":
|
|
493
|
-
case "Prop":
|
|
494
|
-
paintAbs(forest, expr);
|
|
495
|
-
return;
|
|
496
|
-
case "Item":
|
|
497
|
-
return;
|
|
498
|
-
case "Literal":
|
|
499
|
-
return;
|
|
500
|
-
case "MapValue":
|
|
501
|
-
case "FlatMap":
|
|
502
|
-
visitExpr(forest, expr.parent);
|
|
503
|
-
return;
|
|
504
|
-
case "MapItems": {
|
|
505
|
-
visitExpr(forest, expr.parent);
|
|
506
|
-
const path = paintAbs(forest, graphqlBase(expr.parent));
|
|
507
|
-
visitRel(expr.mapped, forest, selAt(forest.roots, path));
|
|
508
|
-
return;
|
|
509
|
-
}
|
|
510
|
-
case "Filter": {
|
|
511
|
-
visitExpr(forest, expr.parent);
|
|
512
|
-
const path = paintAbs(forest, graphqlBase(expr.parent));
|
|
513
|
-
paintRel(forest, expr.predicate, selAt(forest.roots, path));
|
|
514
|
-
return;
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
};
|
|
518
|
-
const graphqlBase = (expr) => {
|
|
519
|
-
switch (expr._tag) {
|
|
520
|
-
case "Root":
|
|
521
|
-
case "Prop":
|
|
522
|
-
case "Item":
|
|
523
|
-
return expr;
|
|
524
|
-
case "MapValue":
|
|
525
|
-
case "MapItems":
|
|
526
|
-
case "Filter":
|
|
527
|
-
case "FlatMap":
|
|
528
|
-
return graphqlBase(expr.parent);
|
|
529
|
-
case "Literal":
|
|
530
|
-
throw new GqlError("literal has no graphql base");
|
|
531
|
-
}
|
|
532
|
-
};
|
|
533
|
-
const visitRel = (plan, forest, listSel) => {
|
|
534
|
-
if (isQuery(plan)) {
|
|
535
|
-
paintRel(forest, plan.expr, listSel);
|
|
536
|
-
return;
|
|
537
|
-
}
|
|
538
|
-
if (plan !== null && typeof plan === "object" && !Effect.isEffect(plan)) {
|
|
539
|
-
for (const nested of Object.values(plan)) {
|
|
540
|
-
visitRel(nested, forest, listSel);
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
};
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
const visitPlan = (plan, forest) => {
|
|
549
|
-
if (isQuery(plan)) {
|
|
550
|
-
log("visit", printExpr(plan.expr));
|
|
551
|
-
visitExpr(forest, plan.expr);
|
|
552
|
-
return;
|
|
553
|
-
}
|
|
554
|
-
if (Effect.isEffect(plan))
|
|
555
|
-
return;
|
|
556
|
-
if (plan !== null && typeof plan === "object" && !Array.isArray(plan)) {
|
|
557
|
-
for (const nested of Object.values(plan)) {
|
|
558
|
-
visitPlan(nested, forest);
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
};
|
|
562
|
-
const validate = (node) => {
|
|
563
|
-
for (const child of node.children.values()) {
|
|
564
|
-
if (!child.isScalar && child.children.size === 0) {
|
|
565
|
-
return `field "${child.field}" has no sub-selection`;
|
|
566
|
-
}
|
|
567
|
-
const inner = validate(child);
|
|
568
|
-
if (inner)
|
|
569
|
-
return inner;
|
|
570
|
-
}
|
|
571
|
-
return undefined;
|
|
572
|
-
};
|
|
573
|
-
const printChildren = (node, indent) => {
|
|
574
|
-
const pad = " ".repeat(indent);
|
|
575
|
-
const lines = [];
|
|
576
|
-
for (const child of node.children.values()) {
|
|
577
|
-
let head = child.alias ? `${child.alias}: ${child.field}` : child.field;
|
|
578
|
-
if (child.args && Object.keys(child.args).length > 0) {
|
|
579
|
-
const printed = Object.entries(child.args)
|
|
580
|
-
.map(([argName, varName]) => `${argName}: $${varName}`)
|
|
581
|
-
.join(", ");
|
|
582
|
-
head += `(${printed})`;
|
|
583
|
-
}
|
|
584
|
-
if (child.isScalar)
|
|
585
|
-
lines.push(`${pad}${head}`);
|
|
586
|
-
else {
|
|
587
|
-
lines.push(`${pad}${head} {`);
|
|
588
|
-
lines.push(printChildren(child, indent + 1));
|
|
589
|
-
lines.push(`${pad}}`);
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
return lines.join("\n");
|
|
593
|
-
};
|
|
594
|
-
const emptyForest = () => ({
|
|
595
|
-
kind: undefined,
|
|
596
|
-
roots: emptySel(""),
|
|
597
|
-
vars: new Map(),
|
|
598
|
-
varI: 0,
|
|
599
|
-
aliasI: 0,
|
|
600
|
-
rootAlias: new WeakMap(),
|
|
601
|
-
rootErrors: new Map(),
|
|
602
|
-
});
|
|
603
|
-
const emit = (forest) => {
|
|
604
|
-
const err = validate(forest.roots);
|
|
605
|
-
if (err)
|
|
606
|
-
throw new GqlError(err);
|
|
607
|
-
const kind = forest.kind ?? "query";
|
|
608
|
-
const varDefs = [...forest.vars.entries()]
|
|
609
|
-
.map(([name, binding]) => `$${name}: ${binding.type}`)
|
|
610
|
-
.join(", ");
|
|
611
|
-
const header = varDefs.length > 0 ? `${kind} Gql(${varDefs})` : `${kind} Gql`;
|
|
612
|
-
const variables = {};
|
|
613
|
-
for (const [name, binding] of forest.vars)
|
|
614
|
-
variables[name] = binding.value;
|
|
615
|
-
return {
|
|
616
|
-
document: `${header} {\n${printChildren(forest.roots, 1)}\n}`,
|
|
617
|
-
operationName: "Gql",
|
|
618
|
-
variables,
|
|
619
|
-
kind,
|
|
620
|
-
tree: forest.roots,
|
|
621
|
-
rootAlias: forest.rootAlias,
|
|
622
|
-
rootErrors: forest.rootErrors,
|
|
623
|
-
};
|
|
624
|
-
};
|
|
625
|
-
const nodesFromConnection = (value) => {
|
|
626
|
-
if (value == null)
|
|
627
|
-
return value;
|
|
628
|
-
if (Array.isArray(value))
|
|
629
|
-
return value.map(nodesFromConnection);
|
|
630
|
-
const edges = value.edges;
|
|
631
|
-
if (!Array.isArray(edges))
|
|
632
|
-
return [];
|
|
633
|
-
return edges.map((edge) => (edge == null ? edge : edge.node));
|
|
634
|
-
};
|
|
635
|
-
const readField = (parent, name) => {
|
|
636
|
-
if (parent == null)
|
|
637
|
-
return parent;
|
|
638
|
-
if (Array.isArray(parent)) {
|
|
639
|
-
return parent.map((item) => (item == null ? item : item[name]));
|
|
640
|
-
}
|
|
641
|
-
return parent[name];
|
|
642
|
-
};
|
|
643
|
-
|
|
644
|
-
const redactStrings = (value) => typeof value === "string"
|
|
645
|
-
? Redacted.make(value)
|
|
646
|
-
: Array.isArray(value)
|
|
647
|
-
? value.map(redactStrings)
|
|
648
|
-
: value;
|
|
649
|
-
|
|
650
|
-
const redactSelected = (sel, data) => {
|
|
651
|
-
if (sel.children.size === 0 || data === null || typeof data !== "object")
|
|
652
|
-
return data;
|
|
653
|
-
if (Array.isArray(data))
|
|
654
|
-
return data.map((item) => redactSelected(sel, item));
|
|
655
|
-
const out = { ...data };
|
|
656
|
-
for (const child of sel.children.values()) {
|
|
657
|
-
const key = child.alias ?? child.field;
|
|
658
|
-
out[key] = child.sensitive ? redactStrings(out[key]) : redactSelected(child, out[key]);
|
|
659
|
-
}
|
|
660
|
-
return out;
|
|
661
|
-
};
|
|
662
|
-
const extractAbs = (expr, data, rootAlias) => {
|
|
663
|
-
switch (expr._tag) {
|
|
664
|
-
case "Root": {
|
|
665
|
-
const raw = data[rootAlias.get(expr) ?? expr.field];
|
|
666
|
-
return expr.connection ? nodesFromConnection(raw) : raw;
|
|
667
|
-
}
|
|
668
|
-
case "Prop": {
|
|
669
|
-
const value = readField(extractAbs(expr.parent, data, rootAlias), expr.field.name);
|
|
670
|
-
return expr.field.kind === "connection" ? nodesFromConnection(value) : value;
|
|
671
|
-
}
|
|
672
|
-
default:
|
|
673
|
-
throw new GqlError(`extractAbs: ${expr._tag}`);
|
|
674
|
-
}
|
|
675
|
-
};
|
|
676
|
-
const extractRel = (expr, item) => {
|
|
677
|
-
switch (expr._tag) {
|
|
678
|
-
case "Item":
|
|
679
|
-
return item;
|
|
680
|
-
case "Prop": {
|
|
681
|
-
const value = readField(extractRel(expr.parent, item), expr.field.name);
|
|
682
|
-
return expr.field.kind === "connection" ? nodesFromConnection(value) : value;
|
|
683
|
-
}
|
|
684
|
-
default:
|
|
685
|
-
throw new GqlError(`extractRel: ${expr._tag}`);
|
|
686
|
-
}
|
|
687
|
-
};
|
|
688
|
-
const inItem = (expr) => {
|
|
689
|
-
switch (expr._tag) {
|
|
690
|
-
case "Item":
|
|
691
|
-
return true;
|
|
692
|
-
case "Prop":
|
|
693
|
-
case "MapValue":
|
|
694
|
-
case "MapItems":
|
|
695
|
-
case "Filter":
|
|
696
|
-
case "FlatMap":
|
|
697
|
-
return inItem(expr.parent);
|
|
698
|
-
default:
|
|
699
|
-
return false;
|
|
700
|
-
}
|
|
701
|
-
};
|
|
702
|
-
const interpretExpr = (expr, data, rootAlias, item) => {
|
|
703
|
-
switch (expr._tag) {
|
|
704
|
-
case "Literal":
|
|
705
|
-
return expr.value;
|
|
706
|
-
case "Item":
|
|
707
|
-
return item;
|
|
708
|
-
case "Root":
|
|
709
|
-
case "Prop":
|
|
710
|
-
return inItem(expr) ? extractRel(expr, item) : extractAbs(expr, data, rootAlias);
|
|
711
|
-
case "MapValue":
|
|
712
|
-
return expr.mapFn(interpretExpr(expr.parent, data, rootAlias, item));
|
|
713
|
-
case "Filter": {
|
|
714
|
-
const list = interpretExpr(expr.parent, data, rootAlias, item);
|
|
715
|
-
const arr = Array.isArray(list) ? list : [];
|
|
716
|
-
return arr.filter((row) => Boolean(interpretExpr(expr.predicate, data, rootAlias, row)));
|
|
717
|
-
}
|
|
718
|
-
case "MapItems": {
|
|
719
|
-
const list = interpretExpr(expr.parent, data, rootAlias, item);
|
|
720
|
-
if (expr.single) {
|
|
721
|
-
return list == null ? null : interpretValueSync(expr.mapped, data, rootAlias, list);
|
|
722
|
-
}
|
|
723
|
-
const arr = Array.isArray(list) ? list : [];
|
|
724
|
-
return arr.map((row) => interpretValueSync(expr.mapped, data, rootAlias, row));
|
|
725
|
-
}
|
|
726
|
-
case "FlatMap":
|
|
727
|
-
throw new GqlError("FlatMap must be interpreted as Effect");
|
|
728
|
-
}
|
|
729
|
-
};
|
|
730
|
-
const interpretValueSync = (plan, data, rootAlias, item) => {
|
|
731
|
-
if (isQuery(plan)) {
|
|
732
|
-
return interpretExpr(plan.expr, data, rootAlias, item);
|
|
733
|
-
}
|
|
734
|
-
if (plan !== null && typeof plan === "object" && !Effect.isEffect(plan)) {
|
|
735
|
-
const out = {};
|
|
736
|
-
for (const [key, nested] of Object.entries(plan)) {
|
|
737
|
-
out[key] = interpretValueSync(nested, data, rootAlias, item);
|
|
738
|
-
}
|
|
739
|
-
return out;
|
|
740
|
-
}
|
|
741
|
-
return plan;
|
|
742
|
-
};
|
|
743
|
-
const interpretPlan = (plan, data, rootAlias) => Effect.gen(function* () {
|
|
744
|
-
if (isQuery(plan)) {
|
|
745
|
-
const expr = plan.expr;
|
|
746
|
-
if (expr._tag === "FlatMap") {
|
|
747
|
-
const resolved = interpretExpr(expr.parent, data, rootAlias, undefined);
|
|
748
|
-
const next = expr.flatMapFn(resolved);
|
|
749
|
-
if (Effect.isEffect(next))
|
|
750
|
-
return yield* next;
|
|
751
|
-
if (isQuery(next))
|
|
752
|
-
return yield* interpretPlan(next, data, rootAlias);
|
|
753
|
-
return next;
|
|
754
|
-
}
|
|
755
|
-
return interpretExpr(expr, data, rootAlias, undefined);
|
|
756
|
-
}
|
|
757
|
-
if (Effect.isEffect(plan))
|
|
758
|
-
return yield* plan;
|
|
759
|
-
if (plan !== null && typeof plan === "object" && !Array.isArray(plan)) {
|
|
760
|
-
const out = {};
|
|
761
|
-
for (const [key, nested] of Object.entries(plan)) {
|
|
762
|
-
out[key] = yield* interpretPlan(nested, data, rootAlias);
|
|
763
|
-
}
|
|
764
|
-
return out;
|
|
765
|
-
}
|
|
766
|
-
return plan;
|
|
767
|
-
});
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
const evaluatePlan = (plan) => Effect.gen(function* () {
|
|
773
|
-
const forest = emptyForest();
|
|
774
|
-
visitPlan(plan, forest);
|
|
775
|
-
let data = {};
|
|
776
|
-
let rootAlias = new WeakMap();
|
|
777
|
-
if (forest.roots.children.size > 0) {
|
|
778
|
-
const compiled = emit(forest);
|
|
779
|
-
log("document\n" + compiled.document);
|
|
780
|
-
data = yield* execute(compiled);
|
|
781
|
-
rootAlias = compiled.rootAlias;
|
|
782
|
-
}
|
|
783
|
-
return (yield* interpretPlan(plan, data, rootAlias));
|
|
784
|
-
});
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
const execute = (compiled) => {
|
|
791
|
-
const once = Effect.gen(function* () {
|
|
792
|
-
const transport = yield* GqlTransport;
|
|
793
|
-
const response = yield* transport.execute(compiled);
|
|
794
|
-
const data = redactSelected(compiled.tree, response.data);
|
|
795
|
-
const raw = response.errors ?? [];
|
|
796
|
-
if (raw.length === 0)
|
|
797
|
-
return data;
|
|
798
|
-
const issues = raw.map((error) => classify(compiled, error, response));
|
|
799
|
-
const [first, ...rest] = issues;
|
|
800
|
-
return yield* Effect.fail(rest.every((issue) => issue._tag === first._tag)
|
|
801
|
-
? first
|
|
802
|
-
: new GraphQLFailure({
|
|
803
|
-
errors: [first, ...rest],
|
|
804
|
-
data,
|
|
805
|
-
status: response.status,
|
|
806
|
-
}));
|
|
807
|
-
});
|
|
808
|
-
return compiled.kind === "query"
|
|
809
|
-
? once.pipe(Effect.retry({
|
|
810
|
-
while: (error) => isRetryable(compiled, error),
|
|
811
|
-
times: 5,
|
|
812
|
-
schedule: Schedule.exponential("200 millis"),
|
|
813
|
-
}))
|
|
814
|
-
: once;
|
|
815
|
-
};
|
|
816
|
-
const isRetryable = (compiled, error) => {
|
|
817
|
-
if (error instanceof GraphQLTransportError) {
|
|
818
|
-
return error.status === undefined || error.status === 429 || error.status >= 500;
|
|
819
|
-
}
|
|
820
|
-
const retryable = new Set([...compiled.rootErrors.values()].flatMap((root) => root.errors.filter((spec) => spec.retryable).map((spec) => spec.tag)));
|
|
821
|
-
return error instanceof GraphQLFailure
|
|
822
|
-
? error.errors.every((issue) => retryable.has(issue._tag))
|
|
823
|
-
: retryable.has(error._tag);
|
|
824
|
-
};
|
|
825
|
-
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
const classify = (compiled, raw, response) => {
|
|
832
|
-
const path = Array.isArray(raw.path) ? raw.path : undefined;
|
|
833
|
-
const root = typeof path?.[0] === "string" ? compiled.rootErrors.get(path[0]) : undefined;
|
|
834
|
-
const candidates = root
|
|
835
|
-
? root.errors
|
|
836
|
-
: uniqueByTag([...compiled.rootErrors.values()].flatMap((entry) => entry.errors.filter((spec) => spec.global)));
|
|
837
|
-
const extensions = isRecord(raw.extensions) ? raw.extensions : {};
|
|
838
|
-
const code = typeof extensions.code === "string"
|
|
839
|
-
? extensions.code
|
|
840
|
-
: typeof extensions.errorCode === "string"
|
|
841
|
-
? extensions.errorCode
|
|
842
|
-
: undefined;
|
|
843
|
-
const message = typeof raw.message === "string" ? raw.message : "";
|
|
844
|
-
const retryHeader = response.headers?.["retry-after"];
|
|
845
|
-
const props = {
|
|
846
|
-
message,
|
|
847
|
-
code,
|
|
848
|
-
path,
|
|
849
|
-
locations: raw.locations,
|
|
850
|
-
extensions: raw.extensions,
|
|
851
|
-
traceId: typeof extensions.traceId === "string"
|
|
852
|
-
? extensions.traceId
|
|
853
|
-
: typeof raw.traceId === "string"
|
|
854
|
-
? raw.traceId
|
|
855
|
-
: undefined,
|
|
856
|
-
status: response.status,
|
|
857
|
-
retryAfter: retryHeader && /^\d+(\.\d+)?$/.test(retryHeader) ? Number(retryHeader) : undefined,
|
|
858
|
-
};
|
|
859
|
-
const matches = candidates.flatMap((spec) => spec.matchers
|
|
860
|
-
.filter((matcher) => (matcher.code === undefined || matcher.code === code) &&
|
|
861
|
-
(matcher.message === undefined || matcher.message === message) &&
|
|
862
|
-
(matcher.messageIncludes === undefined || message.includes(matcher.messageIncludes)))
|
|
863
|
-
.map((matcher) => ({
|
|
864
|
-
spec,
|
|
865
|
-
score: (matcher.code ? 1 : 0) + (matcher.message ? 4 : 0) + (matcher.messageIncludes ? 2 : 0),
|
|
866
|
-
})));
|
|
867
|
-
matches.sort((a, b) => b.score - a.score);
|
|
868
|
-
const best = matches[0];
|
|
869
|
-
if (best &&
|
|
870
|
-
!matches.some((match) => match.score === best.score && match.spec.tag !== best.spec.tag)) {
|
|
871
|
-
return best.spec.make(props);
|
|
872
|
-
}
|
|
873
|
-
return new UnknownGraphQLError({
|
|
874
|
-
...props,
|
|
875
|
-
coordinate: root?.field,
|
|
876
|
-
});
|
|
877
|
-
};
|
|
878
|
-
const uniqueByTag = (specs) => [
|
|
879
|
-
...new Map(specs.map((spec) => [spec.tag, spec])).values(),
|
|
880
|
-
];
|
|
881
|
-
|
|
882
|
-
export const ofQuery = (value) => make({ _tag: "Literal", value, type: { tag: "scalar" } });
|
|
883
|
-
const filterImpl = (source, predicate) => {
|
|
884
|
-
const proto = itemQuery(source);
|
|
885
|
-
const predicateQuery = predicate(proto);
|
|
886
|
-
log("filter pred", printExpr(predicateQuery.expr));
|
|
887
|
-
return make({
|
|
888
|
-
_tag: "Filter",
|
|
889
|
-
parent: source.expr,
|
|
890
|
-
predicate: predicateQuery.expr,
|
|
891
|
-
type: source.expr.type,
|
|
892
|
-
});
|
|
893
|
-
};
|
|
894
|
-
export function filterQuery(sourceOrPredicate, predicate) {
|
|
895
|
-
if (predicate === undefined) {
|
|
896
|
-
return (source) => filterImpl(source, sourceOrPredicate);
|
|
897
|
-
}
|
|
898
|
-
return filterImpl(sourceOrPredicate, predicate);
|
|
899
|
-
}
|
|
900
|
-
const mapImpl = (source, mapFn) => {
|
|
901
|
-
if (source.expr.type.tag === "object") {
|
|
902
|
-
const mapped = mapFn(new QueryNode({
|
|
903
|
-
_tag: "Item",
|
|
904
|
-
list: source.expr,
|
|
905
|
-
type: source.expr.type,
|
|
906
|
-
}));
|
|
907
|
-
log("map object", printExpr(source.expr));
|
|
908
|
-
return make({
|
|
909
|
-
_tag: "MapItems",
|
|
910
|
-
parent: source.expr,
|
|
911
|
-
mapped,
|
|
912
|
-
single: true,
|
|
913
|
-
type: { tag: "scalar" },
|
|
914
|
-
});
|
|
915
|
-
}
|
|
916
|
-
if (source.expr.type.tag === "list") {
|
|
917
|
-
const proto = itemQuery(source);
|
|
918
|
-
const mapped = mapFn(proto);
|
|
919
|
-
log("map items", printExpr(source.expr));
|
|
920
|
-
return make({
|
|
921
|
-
_tag: "MapItems",
|
|
922
|
-
parent: source.expr,
|
|
923
|
-
mapped,
|
|
924
|
-
type: source.expr.type,
|
|
925
|
-
});
|
|
926
|
-
}
|
|
927
|
-
log("map value", printExpr(source.expr));
|
|
928
|
-
return make({
|
|
929
|
-
_tag: "MapValue",
|
|
930
|
-
parent: source.expr,
|
|
931
|
-
mapFn,
|
|
932
|
-
type: { tag: "scalar" },
|
|
933
|
-
});
|
|
934
|
-
};
|
|
935
|
-
export function mapQuery(sourceOrMapFn, mapFn) {
|
|
936
|
-
if (mapFn === undefined) {
|
|
937
|
-
return (source) => mapImpl(source, sourceOrMapFn);
|
|
938
|
-
}
|
|
939
|
-
return mapImpl(sourceOrMapFn, mapFn);
|
|
940
|
-
}
|
|
941
|
-
const flatMapImpl = (source, flatMapFn) => {
|
|
942
|
-
log("flatMap", printExpr(source.expr));
|
|
943
|
-
return make({
|
|
944
|
-
_tag: "FlatMap",
|
|
945
|
-
parent: source.expr,
|
|
946
|
-
flatMapFn,
|
|
947
|
-
type: { tag: "scalar" },
|
|
948
|
-
});
|
|
949
|
-
};
|
|
950
|
-
export function flatMapQuery(sourceOrFlatMapFn, flatMapFn) {
|
|
951
|
-
if (flatMapFn === undefined) {
|
|
952
|
-
return (source) => flatMapImpl(source, sourceOrFlatMapFn);
|
|
953
|
-
}
|
|
954
|
-
return flatMapImpl(sourceOrFlatMapFn, flatMapFn);
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
export const queryFn = (build) => (...args) => {
|
|
962
|
-
const plan = build(...args);
|
|
963
|
-
return evaluatePlan(plan);
|
|
964
|
-
};
|
|
965
|
-
// U+2500U+2500 Pagination U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500
|
|
966
|
-
//
|
|
967
|
-
// A connection lens hides `edges`/`pageInfo`. `Query.pages` re-evaluates the
|
|
968
|
-
// query once per page: it sets the connection's `after` argument, paints
|
|
969
|
-
// `pageInfo { hasNextPage endCursor }` next to `edges`, and stops when
|
|
970
|
-
// `hasNextPage` is false.
|
|
971
|
-
|
|
972
|
-
const connectionOf = (expr) => {
|
|
973
|
-
const base = graphqlBase(expr);
|
|
974
|
-
const isConnection = (base._tag === "Root" && base.connection === true) ||
|
|
975
|
-
(base._tag === "Prop" && base.field.kind === "connection");
|
|
976
|
-
if (!isConnection || inItem(base)) {
|
|
977
|
-
throw new GqlError(`Query.pages needs a Relay connection, got ${printExpr(base)}`);
|
|
978
|
-
}
|
|
979
|
-
const argTypes = base._tag === "Root" ? base.argTypes : base.field.argTypes;
|
|
980
|
-
if (!argTypes || !("after" in argTypes)) {
|
|
981
|
-
throw new GqlError(`${printExpr(base)} takes no "after" cursor`);
|
|
982
|
-
}
|
|
983
|
-
return base;
|
|
984
|
-
};
|
|
985
|
-
|
|
986
|
-
const withCursor = (expr, connection, after) => {
|
|
987
|
-
if (expr === connection) {
|
|
988
|
-
const current = expr;
|
|
989
|
-
return { ...current, args: { ...current.args, after } };
|
|
990
|
-
}
|
|
991
|
-
switch (expr._tag) {
|
|
992
|
-
case "MapItems":
|
|
993
|
-
case "Filter":
|
|
994
|
-
case "MapValue":
|
|
995
|
-
case "FlatMap":
|
|
996
|
-
return { ...expr, parent: withCursor(expr.parent, connection, after) };
|
|
997
|
-
default:
|
|
998
|
-
throw new GqlError(`withCursor: ${expr._tag}`);
|
|
999
|
-
}
|
|
1000
|
-
};
|
|
1001
|
-
const readPath = (data, path) => {
|
|
1002
|
-
let current = data;
|
|
1003
|
-
for (const segment of path) {
|
|
1004
|
-
if (current == null)
|
|
1005
|
-
return undefined;
|
|
1006
|
-
if (Array.isArray(current)) {
|
|
1007
|
-
throw new GqlError("Query.pages cannot page a connection inside a list");
|
|
1008
|
-
}
|
|
1009
|
-
current = current[segment];
|
|
1010
|
-
}
|
|
1011
|
-
return current;
|
|
1012
|
-
};
|
|
1013
|
-
const fetchPage = (query, connection, state) => Effect.gen(function* () {
|
|
1014
|
-
const pageExpr = withCursor(query.expr, connection, state.after);
|
|
1015
|
-
const forest = emptyForest();
|
|
1016
|
-
visitExpr(forest, pageExpr);
|
|
1017
|
-
const nodePath = paintAbs(forest, withCursor(connection, connection, state.after));
|
|
1018
|
-
const connectionPath = nodePath.slice(0, -2);
|
|
1019
|
-
const pageInfo = ensureChild(forest, selAt(forest.roots, connectionPath), "pageInfo", {});
|
|
1020
|
-
ensureChild(forest, pageInfo, "hasNextPage", { isScalar: true });
|
|
1021
|
-
ensureChild(forest, pageInfo, "endCursor", { isScalar: true });
|
|
1022
|
-
const compiled = emit(forest);
|
|
1023
|
-
log("page\n" + compiled.document);
|
|
1024
|
-
const data = yield* execute(compiled);
|
|
1025
|
-
const items = (yield* interpretPlan(make(pageExpr), data, compiled.rootAlias));
|
|
1026
|
-
const info = readPath(data, [...connectionPath, "pageInfo"]);
|
|
1027
|
-
const cursor = info?.endCursor ?? null;
|
|
1028
|
-
if (info?.hasNextPage !== true) {
|
|
1029
|
-
return [items ?? [], Option.none()];
|
|
1030
|
-
}
|
|
1031
|
-
if (cursor === null || cursor === state.after || state.seen.has(cursor)) {
|
|
1032
|
-
return yield* new GraphQLPaginationError({
|
|
1033
|
-
message: `${printExpr(connection)} returned a non-advancing cursor`,
|
|
1034
|
-
cursor,
|
|
1035
|
-
});
|
|
1036
|
-
}
|
|
1037
|
-
return [
|
|
1038
|
-
items ?? [],
|
|
1039
|
-
Option.some({ after: cursor, seen: new Set([...state.seen, cursor]) }),
|
|
1040
|
-
];
|
|
1041
|
-
});
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
export const pagesQuery = (query) => Stream.unwrap(Effect.sync(() => {
|
|
1049
|
-
const connection = connectionOf(query.expr);
|
|
1050
|
-
const after = connection.args?.after;
|
|
1051
|
-
return Stream.paginate({
|
|
1052
|
-
after: typeof after === "string" ? after : undefined,
|
|
1053
|
-
seen: new Set(),
|
|
1054
|
-
}, (state) => fetchPage(query, connection, state).pipe(Effect.map(([items, next]) => [[items], next])));
|
|
1055
|
-
}));
|
|
1056
|
-
|
|
1057
|
-
export const itemsQuery = (query) => pagesQuery(query).pipe(Stream.flatMap(Stream.fromIterable));
|
|
1058
|
-
//# sourceMappingURL=graphql.js.map
|
|
1
|
+
export { graphql$GqlError as GqlError, graphql$GqlTransport as GqlTransport, graphql$GraphQLFailure as GraphQLFailure, graphql$GraphQLPaginationError as GraphQLPaginationError, graphql$GraphQLTransportError as GraphQLTransportError, graphql$QueryNode as QueryNode, graphql$QuerySymbol as QuerySymbol, graphql$UnknownGraphQLError as UnknownGraphQLError, graphql$connectionField as connectionField, graphql$errorFields as errorFields, graphql$errorSpec as errorSpec, graphql$filterQuery as filterQuery, graphql$flatMapQuery as flatMapQuery, graphql$isQuery as isQuery, graphql$itemsQuery as itemsQuery, graphql$listField as listField, graphql$mapQuery as mapQuery, graphql$objectField as objectField, graphql$ofQuery as ofQuery, graphql$pagesQuery as pagesQuery, graphql$queryFn as queryFn, graphql$root as root, graphql$rootConnection as rootConnection, graphql$rootLeaf as rootLeaf, graphql$rootList as rootList, graphql$scalarField as scalarField } from "./_bundle/graphql.js";
|