@gqloom/core 0.3.0 → 0.4.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/dist/index.cjs +311 -258
- package/dist/index.d.cts +169 -74
- package/dist/index.d.ts +169 -74
- package/dist/index.js +279 -228
- package/package.json +7 -6
package/dist/index.js
CHANGED
|
@@ -6,8 +6,16 @@ var __export = (target, all) => {
|
|
|
6
6
|
|
|
7
7
|
// src/resolver/silk.ts
|
|
8
8
|
import {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
GraphQLList,
|
|
10
|
+
GraphQLNonNull
|
|
11
|
+
} from "graphql";
|
|
12
|
+
|
|
13
|
+
// src/schema/weaver-context.ts
|
|
14
|
+
import {
|
|
15
|
+
isEnumType,
|
|
16
|
+
isObjectType,
|
|
17
|
+
isScalarType,
|
|
18
|
+
isUnionType
|
|
11
19
|
} from "graphql";
|
|
12
20
|
|
|
13
21
|
// src/utils/symbols.ts
|
|
@@ -16,22 +24,146 @@ __export(symbols_exports, {
|
|
|
16
24
|
CONTEXT_MEMORY_MAP_KEY: () => CONTEXT_MEMORY_MAP_KEY,
|
|
17
25
|
FIELD_HIDDEN: () => FIELD_HIDDEN,
|
|
18
26
|
GET_GRAPHQL_TYPE: () => GET_GRAPHQL_TYPE,
|
|
19
|
-
PARSE: () => PARSE,
|
|
20
27
|
RESOLVER_OPTIONS_KEY: () => RESOLVER_OPTIONS_KEY,
|
|
21
28
|
WEAVER_CONFIG: () => WEAVER_CONFIG
|
|
22
29
|
});
|
|
23
30
|
var GET_GRAPHQL_TYPE = Symbol.for("gqloom.get_graphql_type");
|
|
24
|
-
var PARSE = Symbol.for("gqloom.parse");
|
|
25
31
|
var WEAVER_CONFIG = Symbol.for("gqloom.weaver_config");
|
|
26
32
|
var RESOLVER_OPTIONS_KEY = Symbol.for("gqloom.resolver-options");
|
|
27
33
|
var CONTEXT_MEMORY_MAP_KEY = Symbol.for("gqloom.context-memory");
|
|
28
34
|
var FIELD_HIDDEN = Symbol.for("gqloom.field-hidden");
|
|
29
35
|
|
|
36
|
+
// src/schema/weaver-context.ts
|
|
37
|
+
var ref;
|
|
38
|
+
var names = /* @__PURE__ */ new WeakMap();
|
|
39
|
+
function initWeaverContext() {
|
|
40
|
+
return {
|
|
41
|
+
id: initWeaverContext.increasingID++,
|
|
42
|
+
loomObjectMap: /* @__PURE__ */ new Map(),
|
|
43
|
+
loomUnionMap: /* @__PURE__ */ new Map(),
|
|
44
|
+
inputMap: /* @__PURE__ */ new Map(),
|
|
45
|
+
interfaceMap: /* @__PURE__ */ new Map(),
|
|
46
|
+
configs: /* @__PURE__ */ new Map(),
|
|
47
|
+
getConfig(key) {
|
|
48
|
+
return this.configs.get(key);
|
|
49
|
+
},
|
|
50
|
+
setConfig(config) {
|
|
51
|
+
const key = config[WEAVER_CONFIG];
|
|
52
|
+
this.configs.set(key, config);
|
|
53
|
+
},
|
|
54
|
+
deleteConfig(key) {
|
|
55
|
+
this.configs.delete(key);
|
|
56
|
+
},
|
|
57
|
+
names,
|
|
58
|
+
namedTypes: /* @__PURE__ */ new Map(),
|
|
59
|
+
memoNamedType(gqlTypeValue) {
|
|
60
|
+
const gqlType = gqlTypeValue;
|
|
61
|
+
if (isObjectType(gqlType) || isUnionType(gqlType) || isEnumType(gqlType) || isScalarType(gqlType)) {
|
|
62
|
+
this.namedTypes.set(gqlType.name, gqlType);
|
|
63
|
+
}
|
|
64
|
+
return gqlTypeValue;
|
|
65
|
+
},
|
|
66
|
+
getNamedType(name) {
|
|
67
|
+
return this.namedTypes.get(name);
|
|
68
|
+
},
|
|
69
|
+
vendorWeavers: /* @__PURE__ */ new Map()
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
initWeaverContext.increasingID = 1;
|
|
73
|
+
var weaverContext = {
|
|
74
|
+
get id() {
|
|
75
|
+
return ref?.id;
|
|
76
|
+
},
|
|
77
|
+
get loomObjectMap() {
|
|
78
|
+
return ref?.loomObjectMap;
|
|
79
|
+
},
|
|
80
|
+
get loomUnionMap() {
|
|
81
|
+
return ref?.loomUnionMap;
|
|
82
|
+
},
|
|
83
|
+
get inputMap() {
|
|
84
|
+
return ref?.inputMap;
|
|
85
|
+
},
|
|
86
|
+
get interfaceMap() {
|
|
87
|
+
return ref?.interfaceMap;
|
|
88
|
+
},
|
|
89
|
+
get configs() {
|
|
90
|
+
return ref?.configs;
|
|
91
|
+
},
|
|
92
|
+
get vendorWeavers() {
|
|
93
|
+
return ref?.vendorWeavers;
|
|
94
|
+
},
|
|
95
|
+
getConfig(key) {
|
|
96
|
+
return ref?.getConfig(key);
|
|
97
|
+
},
|
|
98
|
+
setConfig(config) {
|
|
99
|
+
ref?.setConfig(config);
|
|
100
|
+
},
|
|
101
|
+
deleteConfig(key) {
|
|
102
|
+
ref?.deleteConfig(key);
|
|
103
|
+
},
|
|
104
|
+
get value() {
|
|
105
|
+
return ref;
|
|
106
|
+
},
|
|
107
|
+
useConfig(config, callback) {
|
|
108
|
+
const context = weaverContext.value ?? initWeaverContext();
|
|
109
|
+
context.setConfig(config);
|
|
110
|
+
const result = provideWeaverContext(callback, context);
|
|
111
|
+
context.deleteConfig(config[WEAVER_CONFIG]);
|
|
112
|
+
return result;
|
|
113
|
+
},
|
|
114
|
+
names,
|
|
115
|
+
getNamedType(name) {
|
|
116
|
+
return ref?.getNamedType(name);
|
|
117
|
+
},
|
|
118
|
+
memoNamedType(gqlType) {
|
|
119
|
+
return ref?.memoNamedType(gqlType) ?? gqlType;
|
|
120
|
+
},
|
|
121
|
+
GraphQLTypes: /* @__PURE__ */ new WeakMap(),
|
|
122
|
+
getGraphQLType(origin) {
|
|
123
|
+
return this.GraphQLTypes.get(origin);
|
|
124
|
+
},
|
|
125
|
+
memoGraphQLType(origin, gqlType) {
|
|
126
|
+
this.GraphQLTypes.set(origin, gqlType);
|
|
127
|
+
return gqlType;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
function provideWeaverContext(func, value) {
|
|
131
|
+
const lastRef = ref;
|
|
132
|
+
ref = value;
|
|
133
|
+
try {
|
|
134
|
+
return func();
|
|
135
|
+
} finally {
|
|
136
|
+
ref = lastRef;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
provideWeaverContext.inherit = (func) => {
|
|
140
|
+
const weaverContextRef = weaverContext.value;
|
|
141
|
+
return () => provideWeaverContext(func, weaverContextRef);
|
|
142
|
+
};
|
|
143
|
+
function collectNames(...namesList) {
|
|
144
|
+
const namesRecord = {};
|
|
145
|
+
for (const namesItem of namesList) {
|
|
146
|
+
for (const [name, schema] of Object.entries(namesItem)) {
|
|
147
|
+
names.set(schema, name);
|
|
148
|
+
namesRecord[name] = schema;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return namesRecord;
|
|
152
|
+
}
|
|
153
|
+
function collectName(name, schema) {
|
|
154
|
+
names.set(schema, name);
|
|
155
|
+
return schema;
|
|
156
|
+
}
|
|
157
|
+
|
|
30
158
|
// src/resolver/silk.ts
|
|
31
|
-
function silk(type,
|
|
159
|
+
function silk(type, validate = (value) => ({ value: value ?? void 0 })) {
|
|
32
160
|
return {
|
|
33
161
|
[GET_GRAPHQL_TYPE]: typeof type === "function" ? type : () => type,
|
|
34
|
-
|
|
162
|
+
"~standard": {
|
|
163
|
+
version: 1,
|
|
164
|
+
vendor: "gqloom.silk",
|
|
165
|
+
validate
|
|
166
|
+
}
|
|
35
167
|
};
|
|
36
168
|
}
|
|
37
169
|
silk.parse = parseSilk;
|
|
@@ -41,6 +173,7 @@ silk.list = listSilk;
|
|
|
41
173
|
silk.nullable = nullableSilk;
|
|
42
174
|
function nonNullSilk(origin) {
|
|
43
175
|
return {
|
|
176
|
+
...origin,
|
|
44
177
|
[GET_GRAPHQL_TYPE]: () => {
|
|
45
178
|
const originType = getGraphQLType(origin);
|
|
46
179
|
if (originType instanceof GraphQLNonNull) {
|
|
@@ -48,12 +181,12 @@ function nonNullSilk(origin) {
|
|
|
48
181
|
} else {
|
|
49
182
|
return new GraphQLNonNull(originType);
|
|
50
183
|
}
|
|
51
|
-
}
|
|
52
|
-
[PARSE]: (input) => origin[PARSE]?.(input)
|
|
184
|
+
}
|
|
53
185
|
};
|
|
54
186
|
}
|
|
55
187
|
function listSilk(origin) {
|
|
56
188
|
return {
|
|
189
|
+
...origin,
|
|
57
190
|
[GET_GRAPHQL_TYPE]: () => {
|
|
58
191
|
let originType = getGraphQLType(origin);
|
|
59
192
|
if (originType instanceof GraphQLNonNull && originType.ofType instanceof GraphQLList) {
|
|
@@ -68,6 +201,7 @@ function listSilk(origin) {
|
|
|
68
201
|
}
|
|
69
202
|
function nullableSilk(origin) {
|
|
70
203
|
return {
|
|
204
|
+
...origin,
|
|
71
205
|
[GET_GRAPHQL_TYPE]: () => {
|
|
72
206
|
const originType = getGraphQLType(origin);
|
|
73
207
|
if (originType instanceof GraphQLNonNull) {
|
|
@@ -75,21 +209,30 @@ function nullableSilk(origin) {
|
|
|
75
209
|
} else {
|
|
76
210
|
return originType;
|
|
77
211
|
}
|
|
78
|
-
}
|
|
79
|
-
[PARSE]: (input) => origin[PARSE]?.(input)
|
|
212
|
+
}
|
|
80
213
|
};
|
|
81
214
|
}
|
|
82
215
|
function getGraphQLType(silk2) {
|
|
83
|
-
|
|
216
|
+
if (GET_GRAPHQL_TYPE in silk2 && silk2[GET_GRAPHQL_TYPE] != null)
|
|
217
|
+
return silk2[GET_GRAPHQL_TYPE]();
|
|
218
|
+
const vendorWeavers = weaverContext.vendorWeavers;
|
|
219
|
+
if (vendorWeavers == null) throw new Error("Schema Weaver is not initialized");
|
|
220
|
+
const weaver = vendorWeavers.get(silk2["~standard"].vendor);
|
|
221
|
+
if (weaver == null)
|
|
222
|
+
throw new Error(
|
|
223
|
+
`Schema Weaver for ${silk2["~standard"].vendor} is not found`
|
|
224
|
+
);
|
|
225
|
+
return weaver.getGraphQLType(silk2);
|
|
84
226
|
}
|
|
85
227
|
function parseSilk(silk2, input) {
|
|
86
|
-
|
|
87
|
-
return silk2[PARSE](input);
|
|
228
|
+
return silk2["~standard"].validate(input);
|
|
88
229
|
}
|
|
89
230
|
function isSilk(target) {
|
|
90
231
|
if (typeof target !== "object") return false;
|
|
91
232
|
if (target == null) return false;
|
|
92
|
-
|
|
233
|
+
if (GET_GRAPHQL_TYPE in target) return true;
|
|
234
|
+
if (!("~standard" in target)) return false;
|
|
235
|
+
return "vendor" in target["~standard"] && typeof target["~standard"].vendor === "string" && "version" in target["~standard"] && typeof target["~standard"].version === "number";
|
|
93
236
|
}
|
|
94
237
|
|
|
95
238
|
// src/utils/args.ts
|
|
@@ -306,6 +449,7 @@ function markLocation(message, ...locations) {
|
|
|
306
449
|
}
|
|
307
450
|
|
|
308
451
|
// src/resolver/input.ts
|
|
452
|
+
import { GraphQLError } from "graphql";
|
|
309
453
|
function createInputParser(schema, value) {
|
|
310
454
|
let result;
|
|
311
455
|
const parse = async () => {
|
|
@@ -322,32 +466,43 @@ function createInputParser(schema, value) {
|
|
|
322
466
|
}
|
|
323
467
|
function parseInputValue(inputSchema, input) {
|
|
324
468
|
if (inputSchema === void 0) {
|
|
325
|
-
return input;
|
|
469
|
+
return { value: input };
|
|
326
470
|
}
|
|
327
471
|
if (isSilk(inputSchema)) {
|
|
328
|
-
|
|
329
|
-
return inputSchema[symbols_exports.PARSE](input);
|
|
330
|
-
}
|
|
331
|
-
return input;
|
|
472
|
+
return inputSchema["~standard"].validate(input);
|
|
332
473
|
}
|
|
333
474
|
return parseInputEntries(inputSchema, input);
|
|
334
475
|
}
|
|
335
476
|
async function parseInputEntries(inputSchema, input = {}) {
|
|
336
477
|
const result = {};
|
|
478
|
+
const issues = [];
|
|
337
479
|
await Promise.all(
|
|
338
480
|
Object.entries(inputSchema).map(async ([key, value]) => {
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
481
|
+
const res = await value["~standard"].validate(input[key]);
|
|
482
|
+
if ("value" in res) {
|
|
483
|
+
result[key] = res.value;
|
|
484
|
+
}
|
|
485
|
+
if (res.issues) {
|
|
486
|
+
issues.push(...res.issues.slice());
|
|
343
487
|
}
|
|
344
488
|
})
|
|
345
489
|
);
|
|
346
|
-
return result;
|
|
490
|
+
return { value: result, ...issues.length > 0 ? { issues } : null };
|
|
491
|
+
}
|
|
492
|
+
function getStandardValue(result) {
|
|
493
|
+
if (result == null) return result;
|
|
494
|
+
const { issues } = result;
|
|
495
|
+
if (issues?.length) {
|
|
496
|
+
throw new GraphQLError(issues?.[0]?.message ?? "Invalid input", {
|
|
497
|
+
extensions: { issues }
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
if ("value" in result) return result.value;
|
|
501
|
+
else throw new GraphQLError("Invalid input");
|
|
347
502
|
}
|
|
348
503
|
|
|
349
504
|
// src/resolver/resolver.ts
|
|
350
|
-
var
|
|
505
|
+
var query = (output, resolveOrOptions) => {
|
|
351
506
|
const options = getOperationOptions(resolveOrOptions);
|
|
352
507
|
const type = "query";
|
|
353
508
|
return {
|
|
@@ -358,14 +513,14 @@ var silkQuery = (output, resolveOrOptions) => {
|
|
|
358
513
|
const parseInput = createInputParser(options.input, inputValue);
|
|
359
514
|
return applyMiddlewares(
|
|
360
515
|
compose(extraOptions?.middlewares, options.middlewares),
|
|
361
|
-
async () => options.resolve(await parseInput()),
|
|
516
|
+
async () => options.resolve(getStandardValue(await parseInput())),
|
|
362
517
|
{ parseInput, parent: void 0, outputSilk: output, type }
|
|
363
518
|
);
|
|
364
519
|
},
|
|
365
520
|
type
|
|
366
521
|
};
|
|
367
522
|
};
|
|
368
|
-
var
|
|
523
|
+
var mutation = (output, resolveOrOptions) => {
|
|
369
524
|
const options = getOperationOptions(resolveOrOptions);
|
|
370
525
|
const type = "mutation";
|
|
371
526
|
return {
|
|
@@ -376,7 +531,7 @@ var silkMutation = (output, resolveOrOptions) => {
|
|
|
376
531
|
const parseInput = createInputParser(options.input, inputValue);
|
|
377
532
|
return applyMiddlewares(
|
|
378
533
|
compose(extraOptions?.middlewares, options.middlewares),
|
|
379
|
-
async () => options.resolve(await parseInput()),
|
|
534
|
+
async () => options.resolve(getStandardValue(await parseInput())),
|
|
380
535
|
{ parseInput, parent: void 0, outputSilk: output, type }
|
|
381
536
|
);
|
|
382
537
|
},
|
|
@@ -394,21 +549,21 @@ var baseSilkField = (output, resolveOrOptions) => {
|
|
|
394
549
|
const parseInput = createInputParser(options.input, inputValue);
|
|
395
550
|
return applyMiddlewares(
|
|
396
551
|
compose(extraOptions?.middlewares, options.middlewares),
|
|
397
|
-
async () => options.resolve(parent, await parseInput()),
|
|
552
|
+
async () => options.resolve(parent, getStandardValue(await parseInput())),
|
|
398
553
|
{ parseInput, parent, outputSilk: output, type }
|
|
399
554
|
);
|
|
400
555
|
},
|
|
401
556
|
type
|
|
402
557
|
};
|
|
403
558
|
};
|
|
404
|
-
var
|
|
559
|
+
var field = Object.assign(
|
|
405
560
|
baseSilkField,
|
|
406
561
|
{
|
|
407
562
|
hidden: FIELD_HIDDEN
|
|
408
563
|
}
|
|
409
564
|
);
|
|
410
565
|
var defaultSubscriptionResolve = (source) => source;
|
|
411
|
-
var
|
|
566
|
+
var subscription = (output, subscribeOrOptions) => {
|
|
412
567
|
const options = getSubscriptionOptions(subscribeOrOptions);
|
|
413
568
|
const type = "subscription";
|
|
414
569
|
return {
|
|
@@ -422,7 +577,7 @@ var silkSubscription = (output, subscribeOrOptions) => {
|
|
|
422
577
|
extraOptions?.middlewares,
|
|
423
578
|
options.middlewares
|
|
424
579
|
),
|
|
425
|
-
async () => options.subscribe(await parseInput()),
|
|
580
|
+
async () => options.subscribe(getStandardValue(await parseInput())),
|
|
426
581
|
{ parseInput, parent: void 0, outputSilk: output, type }
|
|
427
582
|
);
|
|
428
583
|
},
|
|
@@ -469,7 +624,7 @@ function extraOperationOptions(operation, options) {
|
|
|
469
624
|
};
|
|
470
625
|
}
|
|
471
626
|
}
|
|
472
|
-
var
|
|
627
|
+
var resolver = Object.assign(
|
|
473
628
|
baseResolver,
|
|
474
629
|
{
|
|
475
630
|
of: (parent, operations, options) => baseResolver(
|
|
@@ -479,11 +634,11 @@ var silkResolver = Object.assign(
|
|
|
479
634
|
}
|
|
480
635
|
);
|
|
481
636
|
var loom = {
|
|
482
|
-
query
|
|
483
|
-
resolver
|
|
484
|
-
field
|
|
485
|
-
subscription
|
|
486
|
-
mutation
|
|
637
|
+
query,
|
|
638
|
+
resolver,
|
|
639
|
+
field,
|
|
640
|
+
subscription,
|
|
641
|
+
mutation
|
|
487
642
|
};
|
|
488
643
|
|
|
489
644
|
// src/helper/create-loom.ts
|
|
@@ -513,7 +668,7 @@ function createFieldFactory(toSilk, isSchema) {
|
|
|
513
668
|
const options = getOperationOptions(
|
|
514
669
|
resolveOrOptions
|
|
515
670
|
);
|
|
516
|
-
return
|
|
671
|
+
return field(toSilk(output), {
|
|
517
672
|
...options,
|
|
518
673
|
input: toSilkInput(options.input, toSilk, isSchema)
|
|
519
674
|
});
|
|
@@ -525,7 +680,7 @@ function createFieldFactory(toSilk, isSchema) {
|
|
|
525
680
|
function createQueryFactory(toSilk, isSchema) {
|
|
526
681
|
return (output, resolveOrOptions) => {
|
|
527
682
|
const options = getOperationOptions(resolveOrOptions);
|
|
528
|
-
return
|
|
683
|
+
return query(toSilk(output), {
|
|
529
684
|
...options,
|
|
530
685
|
input: toSilkInput(options.input, toSilk, isSchema)
|
|
531
686
|
});
|
|
@@ -534,7 +689,7 @@ function createQueryFactory(toSilk, isSchema) {
|
|
|
534
689
|
function createMutationFactory(toSilk, isSchema) {
|
|
535
690
|
return (output, resolveOrOptions) => {
|
|
536
691
|
const options = getOperationOptions(resolveOrOptions);
|
|
537
|
-
return
|
|
692
|
+
return mutation(toSilk(output), {
|
|
538
693
|
...options,
|
|
539
694
|
input: toSilkInput(options.input, toSilk, isSchema)
|
|
540
695
|
});
|
|
@@ -543,7 +698,7 @@ function createMutationFactory(toSilk, isSchema) {
|
|
|
543
698
|
function createSubscriptionFactory(toSilk, isSchema) {
|
|
544
699
|
return (output, resolveOrOptions) => {
|
|
545
700
|
const options = getSubscriptionOptions(resolveOrOptions);
|
|
546
|
-
return
|
|
701
|
+
return subscription(toSilk(output), {
|
|
547
702
|
...options,
|
|
548
703
|
input: toSilkInput(options.input, toSilk, isSchema)
|
|
549
704
|
});
|
|
@@ -561,153 +716,29 @@ function createLoom(toSilk, isSchema) {
|
|
|
561
716
|
|
|
562
717
|
// src/schema/object.ts
|
|
563
718
|
import {
|
|
719
|
+
GraphQLList as GraphQLList3,
|
|
720
|
+
GraphQLNonNull as GraphQLNonNull3,
|
|
564
721
|
GraphQLObjectType,
|
|
722
|
+
GraphQLUnionType,
|
|
565
723
|
assertName,
|
|
566
|
-
isObjectType as isObjectType3,
|
|
567
|
-
resolveObjMapThunk,
|
|
568
724
|
isListType as isListType2,
|
|
569
|
-
GraphQLList as GraphQLList3,
|
|
570
|
-
GraphQLNonNull as GraphQLNonNull3,
|
|
571
725
|
isNonNullType as isNonNullType2,
|
|
726
|
+
isObjectType as isObjectType3,
|
|
572
727
|
isUnionType as isUnionType3,
|
|
573
|
-
|
|
728
|
+
resolveObjMapThunk
|
|
574
729
|
} from "graphql";
|
|
575
730
|
|
|
576
|
-
// src/schema/weaver-context.ts
|
|
577
|
-
import {
|
|
578
|
-
isEnumType,
|
|
579
|
-
isObjectType,
|
|
580
|
-
isUnionType,
|
|
581
|
-
isScalarType
|
|
582
|
-
} from "graphql";
|
|
583
|
-
var ref;
|
|
584
|
-
var names = /* @__PURE__ */ new WeakMap();
|
|
585
|
-
function initWeaverContext() {
|
|
586
|
-
return {
|
|
587
|
-
id: initWeaverContext.increasingID++,
|
|
588
|
-
loomObjectMap: /* @__PURE__ */ new Map(),
|
|
589
|
-
loomUnionMap: /* @__PURE__ */ new Map(),
|
|
590
|
-
inputMap: /* @__PURE__ */ new Map(),
|
|
591
|
-
interfaceMap: /* @__PURE__ */ new Map(),
|
|
592
|
-
configs: /* @__PURE__ */ new Map(),
|
|
593
|
-
getConfig(key) {
|
|
594
|
-
return this.configs.get(key);
|
|
595
|
-
},
|
|
596
|
-
setConfig(config) {
|
|
597
|
-
const key = config[WEAVER_CONFIG];
|
|
598
|
-
this.configs.set(key, config);
|
|
599
|
-
},
|
|
600
|
-
deleteConfig(key) {
|
|
601
|
-
this.configs.delete(key);
|
|
602
|
-
},
|
|
603
|
-
names,
|
|
604
|
-
namedTypes: /* @__PURE__ */ new Map(),
|
|
605
|
-
memoNamedType(gqlTypeValue) {
|
|
606
|
-
const gqlType = gqlTypeValue;
|
|
607
|
-
if (isObjectType(gqlType) || isUnionType(gqlType) || isEnumType(gqlType) || isScalarType(gqlType)) {
|
|
608
|
-
this.namedTypes.set(gqlType.name, gqlType);
|
|
609
|
-
}
|
|
610
|
-
return gqlTypeValue;
|
|
611
|
-
},
|
|
612
|
-
getNamedType(name) {
|
|
613
|
-
return this.namedTypes.get(name);
|
|
614
|
-
}
|
|
615
|
-
};
|
|
616
|
-
}
|
|
617
|
-
initWeaverContext.increasingID = 1;
|
|
618
|
-
var weaverContext = {
|
|
619
|
-
get id() {
|
|
620
|
-
return ref?.id;
|
|
621
|
-
},
|
|
622
|
-
get loomObjectMap() {
|
|
623
|
-
return ref?.loomObjectMap;
|
|
624
|
-
},
|
|
625
|
-
get loomUnionMap() {
|
|
626
|
-
return ref?.loomUnionMap;
|
|
627
|
-
},
|
|
628
|
-
get inputMap() {
|
|
629
|
-
return ref?.inputMap;
|
|
630
|
-
},
|
|
631
|
-
get interfaceMap() {
|
|
632
|
-
return ref?.interfaceMap;
|
|
633
|
-
},
|
|
634
|
-
get configs() {
|
|
635
|
-
return ref?.configs;
|
|
636
|
-
},
|
|
637
|
-
getConfig(key) {
|
|
638
|
-
return ref?.getConfig(key);
|
|
639
|
-
},
|
|
640
|
-
setConfig(config) {
|
|
641
|
-
ref?.setConfig(config);
|
|
642
|
-
},
|
|
643
|
-
deleteConfig(key) {
|
|
644
|
-
ref?.deleteConfig(key);
|
|
645
|
-
},
|
|
646
|
-
get value() {
|
|
647
|
-
return ref;
|
|
648
|
-
},
|
|
649
|
-
useConfig(config, callback) {
|
|
650
|
-
const context = weaverContext.value ?? initWeaverContext();
|
|
651
|
-
context.setConfig(config);
|
|
652
|
-
const result = provideWeaverContext(callback, context);
|
|
653
|
-
context.deleteConfig(config[WEAVER_CONFIG]);
|
|
654
|
-
return result;
|
|
655
|
-
},
|
|
656
|
-
names,
|
|
657
|
-
getNamedType(name) {
|
|
658
|
-
return ref?.getNamedType(name);
|
|
659
|
-
},
|
|
660
|
-
memoNamedType(gqlType) {
|
|
661
|
-
return ref?.memoNamedType(gqlType) ?? gqlType;
|
|
662
|
-
},
|
|
663
|
-
GraphQLTypes: /* @__PURE__ */ new WeakMap(),
|
|
664
|
-
getGraphQLType(origin) {
|
|
665
|
-
return this.GraphQLTypes.get(origin);
|
|
666
|
-
},
|
|
667
|
-
memoGraphQLType(origin, gqlType) {
|
|
668
|
-
this.GraphQLTypes.set(origin, gqlType);
|
|
669
|
-
return gqlType;
|
|
670
|
-
}
|
|
671
|
-
};
|
|
672
|
-
function provideWeaverContext(func, value) {
|
|
673
|
-
const lastRef = ref;
|
|
674
|
-
ref = value;
|
|
675
|
-
try {
|
|
676
|
-
return func();
|
|
677
|
-
} finally {
|
|
678
|
-
ref = lastRef;
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
provideWeaverContext.inherit = (func) => {
|
|
682
|
-
const weaverContextRef = weaverContext.value;
|
|
683
|
-
return () => provideWeaverContext(func, weaverContextRef);
|
|
684
|
-
};
|
|
685
|
-
function collectNames(...namesList) {
|
|
686
|
-
const namesRecord = {};
|
|
687
|
-
for (const namesItem of namesList) {
|
|
688
|
-
for (const [name, schema] of Object.entries(namesItem)) {
|
|
689
|
-
names.set(schema, name);
|
|
690
|
-
namesRecord[name] = schema;
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
return namesRecord;
|
|
694
|
-
}
|
|
695
|
-
function collectName(name, schema) {
|
|
696
|
-
names.set(schema, name);
|
|
697
|
-
return schema;
|
|
698
|
-
}
|
|
699
|
-
|
|
700
731
|
// src/schema/input.ts
|
|
701
732
|
import {
|
|
702
733
|
GraphQLInputObjectType,
|
|
703
734
|
GraphQLList as GraphQLList2,
|
|
704
735
|
GraphQLNonNull as GraphQLNonNull2,
|
|
736
|
+
isInputObjectType,
|
|
705
737
|
isInterfaceType,
|
|
706
738
|
isListType,
|
|
707
739
|
isNonNullType,
|
|
708
740
|
isObjectType as isObjectType2,
|
|
709
|
-
isUnionType as isUnionType2
|
|
710
|
-
isInputObjectType
|
|
741
|
+
isUnionType as isUnionType2
|
|
711
742
|
} from "graphql";
|
|
712
743
|
function inputToArgs(input) {
|
|
713
744
|
if (input === void 0) return void 0;
|
|
@@ -723,11 +754,11 @@ function inputToArgs(input) {
|
|
|
723
754
|
throw new Error(`Cannot convert ${inputType.toString()} to input type`);
|
|
724
755
|
}
|
|
725
756
|
const args = {};
|
|
726
|
-
Object.entries(input).forEach(([name,
|
|
757
|
+
Object.entries(input).forEach(([name, field2]) => {
|
|
727
758
|
tryIn(() => {
|
|
728
759
|
args[name] = {
|
|
729
|
-
...
|
|
730
|
-
type: ensureInputType(
|
|
760
|
+
...field2,
|
|
761
|
+
type: ensureInputType(field2)
|
|
731
762
|
};
|
|
732
763
|
}, name);
|
|
733
764
|
});
|
|
@@ -805,12 +836,12 @@ var LoomObjectType = class extends GraphQLObjectType {
|
|
|
805
836
|
hideField(name) {
|
|
806
837
|
this.hiddenFields.add(name);
|
|
807
838
|
}
|
|
808
|
-
addField(name,
|
|
839
|
+
addField(name, resolver2) {
|
|
809
840
|
const existing = this.extraFields.get(name);
|
|
810
|
-
if (existing && existing !==
|
|
841
|
+
if (existing && existing !== resolver2) {
|
|
811
842
|
throw new Error(`Field ${name} already exists in ${this.name}`);
|
|
812
843
|
}
|
|
813
|
-
this.extraFields.set(name,
|
|
844
|
+
this.extraFields.set(name, resolver2);
|
|
814
845
|
}
|
|
815
846
|
mergeExtensions(extensions) {
|
|
816
847
|
this.extensions = deepMerge(this.extensions, extensions);
|
|
@@ -819,7 +850,7 @@ var LoomObjectType = class extends GraphQLObjectType {
|
|
|
819
850
|
getFields() {
|
|
820
851
|
const fieldsBySuper = super.getFields();
|
|
821
852
|
Object.values(fieldsBySuper).forEach(
|
|
822
|
-
(
|
|
853
|
+
(field2) => field2.type = this.getCacheType(field2.type)
|
|
823
854
|
);
|
|
824
855
|
const extraFields = provideWeaverContext(
|
|
825
856
|
() => defineFieldMap(this.mapToFieldConfig(this.extraFields)),
|
|
@@ -839,47 +870,47 @@ var LoomObjectType = class extends GraphQLObjectType {
|
|
|
839
870
|
}
|
|
840
871
|
mapToFieldConfig(map) {
|
|
841
872
|
const record = {};
|
|
842
|
-
for (const [name,
|
|
843
|
-
record[name] = this.toFieldConfig(
|
|
873
|
+
for (const [name, field2] of map.entries()) {
|
|
874
|
+
record[name] = this.toFieldConfig(field2);
|
|
844
875
|
}
|
|
845
876
|
return record;
|
|
846
877
|
}
|
|
847
|
-
toFieldConfig(
|
|
878
|
+
toFieldConfig(field2) {
|
|
848
879
|
try {
|
|
849
|
-
const outputType = this.getCacheType(getGraphQLType(
|
|
880
|
+
const outputType = this.getCacheType(getGraphQLType(field2.output));
|
|
850
881
|
return {
|
|
851
|
-
...extract(
|
|
882
|
+
...extract(field2),
|
|
852
883
|
type: outputType,
|
|
853
|
-
args: inputToArgs(
|
|
854
|
-
...this.provideForResolve(
|
|
855
|
-
...this.provideForSubscribe(
|
|
884
|
+
args: inputToArgs(field2.input),
|
|
885
|
+
...this.provideForResolve(field2),
|
|
886
|
+
...this.provideForSubscribe(field2)
|
|
856
887
|
};
|
|
857
888
|
} catch (error) {
|
|
858
889
|
throw markErrorLocation(error);
|
|
859
890
|
}
|
|
860
891
|
}
|
|
861
|
-
provideForResolve(
|
|
862
|
-
if (
|
|
863
|
-
if (
|
|
892
|
+
provideForResolve(field2) {
|
|
893
|
+
if (field2?.resolve == null) return;
|
|
894
|
+
if (field2.resolve === defaultSubscriptionResolve)
|
|
864
895
|
return { resolve: defaultSubscriptionResolve };
|
|
865
|
-
const resolve =
|
|
866
|
-
{ root, args, context, info, field },
|
|
867
|
-
() =>
|
|
868
|
-
) :
|
|
869
|
-
{ root, args, context, info, field },
|
|
870
|
-
() =>
|
|
896
|
+
const resolve = field2.type === "field" ? (root, args, context, info) => resolverPayloadStorage.run(
|
|
897
|
+
{ root, args, context, info, field: field2 },
|
|
898
|
+
() => field2.resolve(root, args, this.resolverOptions)
|
|
899
|
+
) : field2.type === "subscription" ? (root, args, context, info) => resolverPayloadStorage.run(
|
|
900
|
+
{ root, args, context, info, field: field2 },
|
|
901
|
+
() => field2.resolve(root, args)
|
|
871
902
|
) : (root, args, context, info) => resolverPayloadStorage.run(
|
|
872
|
-
{ root, args, context, info, field },
|
|
873
|
-
() =>
|
|
903
|
+
{ root, args, context, info, field: field2 },
|
|
904
|
+
() => field2.resolve(args, this.resolverOptions)
|
|
874
905
|
);
|
|
875
906
|
return { resolve };
|
|
876
907
|
}
|
|
877
|
-
provideForSubscribe(
|
|
878
|
-
if (
|
|
908
|
+
provideForSubscribe(field2) {
|
|
909
|
+
if (field2?.subscribe == null) return;
|
|
879
910
|
return {
|
|
880
911
|
subscribe: (root, args, context, info) => resolverPayloadStorage.run(
|
|
881
|
-
{ root, args, context, info, field },
|
|
882
|
-
() =>
|
|
912
|
+
{ root, args, context, info, field: field2 },
|
|
913
|
+
() => field2.subscribe?.(args, this.resolverOptions)
|
|
883
914
|
)
|
|
884
915
|
};
|
|
885
916
|
}
|
|
@@ -959,12 +990,21 @@ function getCacheType(gqlType, options = {}) {
|
|
|
959
990
|
return gqlType;
|
|
960
991
|
}
|
|
961
992
|
|
|
993
|
+
// src/schema/schema-vendor-weaver.ts
|
|
994
|
+
function isSchemaVendorWeaver(some) {
|
|
995
|
+
if (typeof some !== "object" && typeof some !== "function") return false;
|
|
996
|
+
if (!("getGraphQLType" in some) || typeof some.getGraphQLType !== "function")
|
|
997
|
+
return false;
|
|
998
|
+
if (!("vendor" in some) || typeof some.vendor !== "string") return false;
|
|
999
|
+
return true;
|
|
1000
|
+
}
|
|
1001
|
+
|
|
962
1002
|
// src/schema/schema-weaver.ts
|
|
963
1003
|
import {
|
|
964
1004
|
GraphQLSchema,
|
|
965
|
-
isObjectType as isObjectType4,
|
|
966
|
-
isNonNullType as isNonNullType3,
|
|
967
1005
|
isEnumType as isEnumType2,
|
|
1006
|
+
isNonNullType as isNonNullType3,
|
|
1007
|
+
isObjectType as isObjectType4,
|
|
968
1008
|
isUnionType as isUnionType4
|
|
969
1009
|
} from "graphql";
|
|
970
1010
|
var SchemaWeaver = class _SchemaWeaver {
|
|
@@ -985,10 +1025,10 @@ var SchemaWeaver = class _SchemaWeaver {
|
|
|
985
1025
|
[WEAVER_CONFIG]: "gqloom.core.schema"
|
|
986
1026
|
};
|
|
987
1027
|
}
|
|
988
|
-
constructor({ query, mutation, subscription, types } = {}, context) {
|
|
989
|
-
if (
|
|
990
|
-
if (
|
|
991
|
-
if (
|
|
1028
|
+
constructor({ query: query2, mutation: mutation2, subscription: subscription2, types } = {}, context) {
|
|
1029
|
+
if (query2 != null) this.query = query2;
|
|
1030
|
+
if (mutation2 != null) this.mutation = mutation2;
|
|
1031
|
+
if (subscription2 != null) this.subscription = subscription2;
|
|
992
1032
|
if (types != null) this.types = types.slice();
|
|
993
1033
|
this.context = context ?? initWeaverContext();
|
|
994
1034
|
}
|
|
@@ -998,8 +1038,12 @@ var SchemaWeaver = class _SchemaWeaver {
|
|
|
998
1038
|
this.resolverOptions.middlewares.push(...middlewares);
|
|
999
1039
|
return this;
|
|
1000
1040
|
}
|
|
1001
|
-
add(
|
|
1002
|
-
provideWeaverContext(() => this.addResolver(
|
|
1041
|
+
add(resolver2) {
|
|
1042
|
+
provideWeaverContext(() => this.addResolver(resolver2), this.context);
|
|
1043
|
+
return this;
|
|
1044
|
+
}
|
|
1045
|
+
addVendor(weaver) {
|
|
1046
|
+
this.context.vendorWeavers.set(weaver.vendor, weaver);
|
|
1003
1047
|
return this;
|
|
1004
1048
|
}
|
|
1005
1049
|
addType(silk2) {
|
|
@@ -1028,19 +1072,19 @@ var SchemaWeaver = class _SchemaWeaver {
|
|
|
1028
1072
|
return this;
|
|
1029
1073
|
}
|
|
1030
1074
|
weaveGraphQLSchema() {
|
|
1031
|
-
const { query, mutation, subscription, types } = this;
|
|
1075
|
+
const { query: query2, mutation: mutation2, subscription: subscription2, types } = this;
|
|
1032
1076
|
const config = this.context.getConfig("gqloom.core.schema");
|
|
1033
1077
|
const schema = new GraphQLSchema({
|
|
1034
|
-
query,
|
|
1035
|
-
mutation,
|
|
1036
|
-
subscription,
|
|
1078
|
+
query: query2,
|
|
1079
|
+
mutation: mutation2,
|
|
1080
|
+
subscription: subscription2,
|
|
1037
1081
|
types: [...types ?? [], ...config?.types ?? []],
|
|
1038
1082
|
...config
|
|
1039
1083
|
});
|
|
1040
1084
|
return schema;
|
|
1041
1085
|
}
|
|
1042
|
-
addResolver(
|
|
1043
|
-
const resolverOptions = ResolverOptionsMap.get(
|
|
1086
|
+
addResolver(resolver2) {
|
|
1087
|
+
const resolverOptions = ResolverOptionsMap.get(resolver2);
|
|
1044
1088
|
const parent = resolverOptions?.parent;
|
|
1045
1089
|
const parentObject = (() => {
|
|
1046
1090
|
if (parent == null) return void 0;
|
|
@@ -1059,7 +1103,7 @@ var SchemaWeaver = class _SchemaWeaver {
|
|
|
1059
1103
|
})();
|
|
1060
1104
|
if (resolverOptions?.extensions && parentObject)
|
|
1061
1105
|
parentObject.mergeExtensions(resolverOptions.extensions);
|
|
1062
|
-
Object.entries(
|
|
1106
|
+
Object.entries(resolver2).forEach(([name, operation]) => {
|
|
1063
1107
|
if (operation === FIELD_HIDDEN) {
|
|
1064
1108
|
if (parentObject == null) return;
|
|
1065
1109
|
parentObject.hideField(name);
|
|
@@ -1107,12 +1151,16 @@ var SchemaWeaver = class _SchemaWeaver {
|
|
|
1107
1151
|
const middlewares = /* @__PURE__ */ new Set();
|
|
1108
1152
|
const resolvers = /* @__PURE__ */ new Set();
|
|
1109
1153
|
const silks = /* @__PURE__ */ new Set();
|
|
1154
|
+
const weavers = /* @__PURE__ */ new Set();
|
|
1110
1155
|
let context;
|
|
1111
1156
|
for (const item of inputs) {
|
|
1112
|
-
if (
|
|
1157
|
+
if (isSchemaVendorWeaver(item)) {
|
|
1158
|
+
weavers.add(item);
|
|
1159
|
+
} else if (typeof item === "function") {
|
|
1113
1160
|
middlewares.add(item);
|
|
1114
1161
|
} else if (WEAVER_CONFIG in item) {
|
|
1115
1162
|
configs.add(item);
|
|
1163
|
+
if (item.vendorWeaver) weavers.add(item.vendorWeaver);
|
|
1116
1164
|
if (item[WEAVER_CONFIG] === "gqloom.core.schema") {
|
|
1117
1165
|
context = item.weaverContext;
|
|
1118
1166
|
}
|
|
@@ -1122,7 +1170,7 @@ var SchemaWeaver = class _SchemaWeaver {
|
|
|
1122
1170
|
resolvers.add(item);
|
|
1123
1171
|
}
|
|
1124
1172
|
}
|
|
1125
|
-
return { context, configs, middlewares, resolvers, silks };
|
|
1173
|
+
return { context, configs, middlewares, resolvers, silks, weavers };
|
|
1126
1174
|
}
|
|
1127
1175
|
/**
|
|
1128
1176
|
* Weave a GraphQL Schema from resolvers
|
|
@@ -1130,9 +1178,10 @@ var SchemaWeaver = class _SchemaWeaver {
|
|
|
1130
1178
|
* @returns GraphQ LSchema
|
|
1131
1179
|
*/
|
|
1132
1180
|
static weave(...inputs) {
|
|
1133
|
-
const { context, configs, middlewares, resolvers, silks } = _SchemaWeaver.optionsFrom(...inputs);
|
|
1181
|
+
const { context, configs, middlewares, resolvers, silks, weavers } = _SchemaWeaver.optionsFrom(...inputs);
|
|
1134
1182
|
const weaver = new _SchemaWeaver({}, context);
|
|
1135
1183
|
configs.forEach((it) => weaver.setConfig(it));
|
|
1184
|
+
weavers.forEach((it) => weaver.addVendor(it));
|
|
1136
1185
|
middlewares.forEach((it) => weaver.use(it));
|
|
1137
1186
|
resolvers.forEach((it) => weaver.add(it));
|
|
1138
1187
|
silks.forEach((it) => weaver.addType(it));
|
|
@@ -1163,8 +1212,8 @@ function ensureInterfaceType(gqlType, interfaceConfig) {
|
|
|
1163
1212
|
const interfaceType = new GraphQLInterfaceType({
|
|
1164
1213
|
...config,
|
|
1165
1214
|
...interfaceConfig,
|
|
1166
|
-
fields: mapValue(fields, (
|
|
1167
|
-
return { ...
|
|
1215
|
+
fields: mapValue(fields, (field2) => {
|
|
1216
|
+
return { ...field2, type: getCacheType(field2.type) };
|
|
1168
1217
|
})
|
|
1169
1218
|
});
|
|
1170
1219
|
weaverContext.interfaceMap?.set(key, interfaceType);
|
|
@@ -1194,20 +1243,24 @@ export {
|
|
|
1194
1243
|
ensureInputObjectType,
|
|
1195
1244
|
ensureInputType,
|
|
1196
1245
|
ensureInterfaceType,
|
|
1246
|
+
field,
|
|
1197
1247
|
getCacheType,
|
|
1198
1248
|
getFieldOptions,
|
|
1199
1249
|
getGraphQLType,
|
|
1200
1250
|
getOperationOptions,
|
|
1251
|
+
getStandardValue,
|
|
1201
1252
|
getSubscriptionOptions,
|
|
1202
1253
|
initWeaverContext,
|
|
1203
1254
|
inputToArgs,
|
|
1204
1255
|
isOnlyMemoryPayload,
|
|
1256
|
+
isSchemaVendorWeaver,
|
|
1205
1257
|
isSilk,
|
|
1206
1258
|
listSilk,
|
|
1207
1259
|
loom,
|
|
1208
1260
|
mapValue,
|
|
1209
1261
|
markErrorLocation,
|
|
1210
1262
|
markLocation,
|
|
1263
|
+
mutation,
|
|
1211
1264
|
nonNullSilk,
|
|
1212
1265
|
notNullish,
|
|
1213
1266
|
nullableSilk,
|
|
@@ -1215,13 +1268,11 @@ export {
|
|
|
1215
1268
|
parseInputValue,
|
|
1216
1269
|
parseSilk,
|
|
1217
1270
|
provideWeaverContext,
|
|
1271
|
+
query,
|
|
1272
|
+
resolver,
|
|
1218
1273
|
resolverPayloadStorage,
|
|
1219
1274
|
silk,
|
|
1220
|
-
|
|
1221
|
-
silkMutation,
|
|
1222
|
-
silkQuery,
|
|
1223
|
-
silkResolver,
|
|
1224
|
-
silkSubscription,
|
|
1275
|
+
subscription,
|
|
1225
1276
|
toObjMap,
|
|
1226
1277
|
tryIn,
|
|
1227
1278
|
useContext,
|