@gqloom/core 0.2.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/README.md +29 -0
- package/README.zh-CN.md +267 -0
- package/dist/index.cjs +1238 -0
- package/dist/index.d.cts +577 -0
- package/dist/index.d.ts +577 -0
- package/dist/index.js +1198 -0
- package/package.json +35 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
ContextMemoization: () => ContextMemoization,
|
|
24
|
+
LoomObjectType: () => LoomObjectType,
|
|
25
|
+
ResolverOptionsMap: () => ResolverOptionsMap,
|
|
26
|
+
SYMBOLS: () => symbols_exports,
|
|
27
|
+
SchemaWeaver: () => SchemaWeaver,
|
|
28
|
+
applyMiddlewares: () => applyMiddlewares,
|
|
29
|
+
baseResolver: () => baseResolver,
|
|
30
|
+
collectNames: () => collectNames,
|
|
31
|
+
compose: () => compose,
|
|
32
|
+
createFieldBobbin: () => createFieldBobbin,
|
|
33
|
+
createInputParser: () => createInputParser,
|
|
34
|
+
createLoom: () => createLoom,
|
|
35
|
+
createMemoization: () => createMemoization,
|
|
36
|
+
createMutationBobbin: () => createMutationBobbin,
|
|
37
|
+
createQueryBobbin: () => createQueryBobbin,
|
|
38
|
+
createResolverBobbin: () => createResolverBobbin,
|
|
39
|
+
createSubscriptionBobbin: () => createSubscriptionBobbin,
|
|
40
|
+
deepMerge: () => deepMerge,
|
|
41
|
+
defaultSubscriptionResolve: () => defaultSubscriptionResolve,
|
|
42
|
+
ensureInputObjectType: () => ensureInputObjectType,
|
|
43
|
+
ensureInputType: () => ensureInputType,
|
|
44
|
+
ensureInterfaceType: () => ensureInterfaceType,
|
|
45
|
+
getCacheType: () => getCacheType,
|
|
46
|
+
getFieldOptions: () => getFieldOptions,
|
|
47
|
+
getGraphQLType: () => getGraphQLType,
|
|
48
|
+
getOperationOptions: () => getOperationOptions,
|
|
49
|
+
getSubscriptionOptions: () => getSubscriptionOptions,
|
|
50
|
+
initWeaverContext: () => initWeaverContext,
|
|
51
|
+
inputToArgs: () => inputToArgs,
|
|
52
|
+
isOnlyMemoryPayload: () => isOnlyMemoryPayload,
|
|
53
|
+
isSilk: () => isSilk,
|
|
54
|
+
listSilk: () => listSilk,
|
|
55
|
+
loom: () => loom,
|
|
56
|
+
mapValue: () => mapValue,
|
|
57
|
+
markErrorLocation: () => markErrorLocation,
|
|
58
|
+
markLocation: () => markLocation,
|
|
59
|
+
mergeExtensions: () => mergeExtensions,
|
|
60
|
+
nonNullSilk: () => nonNullSilk,
|
|
61
|
+
notNullish: () => notNullish,
|
|
62
|
+
nullableSilk: () => nullableSilk,
|
|
63
|
+
onlyMemoization: () => onlyMemoization,
|
|
64
|
+
parseInputValue: () => parseInputValue,
|
|
65
|
+
parseSilk: () => parseSilk,
|
|
66
|
+
provideWeaverContext: () => provideWeaverContext,
|
|
67
|
+
resolverPayloadStorage: () => resolverPayloadStorage,
|
|
68
|
+
silk: () => silk,
|
|
69
|
+
silkField: () => silkField,
|
|
70
|
+
silkMutation: () => silkMutation,
|
|
71
|
+
silkQuery: () => silkQuery,
|
|
72
|
+
silkResolver: () => silkResolver,
|
|
73
|
+
silkSubscription: () => silkSubscription,
|
|
74
|
+
toObjMap: () => toObjMap,
|
|
75
|
+
tryIn: () => tryIn,
|
|
76
|
+
useContext: () => useContext,
|
|
77
|
+
useMemoizationMap: () => useMemoizationMap,
|
|
78
|
+
useResolverPayload: () => useResolverPayload,
|
|
79
|
+
weave: () => weave,
|
|
80
|
+
weaverContext: () => weaverContext
|
|
81
|
+
});
|
|
82
|
+
module.exports = __toCommonJS(src_exports);
|
|
83
|
+
|
|
84
|
+
// src/resolver/silk.ts
|
|
85
|
+
var import_graphql = require("graphql");
|
|
86
|
+
|
|
87
|
+
// src/utils/symbols.ts
|
|
88
|
+
var symbols_exports = {};
|
|
89
|
+
__export(symbols_exports, {
|
|
90
|
+
CONTEXT_MEMORY_MAP_KEY: () => CONTEXT_MEMORY_MAP_KEY,
|
|
91
|
+
GET_GRAPHQL_TYPE: () => GET_GRAPHQL_TYPE,
|
|
92
|
+
PARSE: () => PARSE,
|
|
93
|
+
RESOLVER_OPTIONS_KEY: () => RESOLVER_OPTIONS_KEY,
|
|
94
|
+
WEAVER_CONFIG: () => WEAVER_CONFIG
|
|
95
|
+
});
|
|
96
|
+
var GET_GRAPHQL_TYPE = Symbol.for("gqloom.get_graphql_type");
|
|
97
|
+
var PARSE = Symbol.for("gqloom.parse");
|
|
98
|
+
var WEAVER_CONFIG = Symbol.for("gqloom.weaver_config");
|
|
99
|
+
var RESOLVER_OPTIONS_KEY = Symbol.for("gqloom.resolver-options");
|
|
100
|
+
var CONTEXT_MEMORY_MAP_KEY = Symbol.for("gqloom.context-memory");
|
|
101
|
+
|
|
102
|
+
// src/resolver/silk.ts
|
|
103
|
+
function silk(type, parse) {
|
|
104
|
+
return {
|
|
105
|
+
[GET_GRAPHQL_TYPE]: typeof type === "function" ? type : () => type,
|
|
106
|
+
[PARSE]: parse
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
silk.parse = parseSilk;
|
|
110
|
+
silk.getType = getGraphQLType;
|
|
111
|
+
silk.nonNull = nonNullSilk;
|
|
112
|
+
silk.list = listSilk;
|
|
113
|
+
silk.nullable = nullableSilk;
|
|
114
|
+
function nonNullSilk(origin) {
|
|
115
|
+
return {
|
|
116
|
+
[GET_GRAPHQL_TYPE]: () => {
|
|
117
|
+
const originType = getGraphQLType(origin);
|
|
118
|
+
if (originType instanceof import_graphql.GraphQLNonNull) {
|
|
119
|
+
return originType;
|
|
120
|
+
} else {
|
|
121
|
+
return new import_graphql.GraphQLNonNull(originType);
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
[PARSE]: (input) => origin[PARSE]?.(input)
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function listSilk(origin) {
|
|
128
|
+
return {
|
|
129
|
+
[GET_GRAPHQL_TYPE]: () => {
|
|
130
|
+
let originType = getGraphQLType(origin);
|
|
131
|
+
if (originType instanceof import_graphql.GraphQLNonNull && originType.ofType instanceof import_graphql.GraphQLList) {
|
|
132
|
+
originType = originType.ofType;
|
|
133
|
+
}
|
|
134
|
+
if (originType instanceof import_graphql.GraphQLList) {
|
|
135
|
+
originType = originType.ofType;
|
|
136
|
+
}
|
|
137
|
+
return new import_graphql.GraphQLNonNull(new import_graphql.GraphQLList(originType));
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
function nullableSilk(origin) {
|
|
142
|
+
return {
|
|
143
|
+
[GET_GRAPHQL_TYPE]: () => {
|
|
144
|
+
const originType = getGraphQLType(origin);
|
|
145
|
+
if (originType instanceof import_graphql.GraphQLNonNull) {
|
|
146
|
+
return originType.ofType;
|
|
147
|
+
} else {
|
|
148
|
+
return originType;
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
[PARSE]: (input) => origin[PARSE]?.(input)
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function getGraphQLType(silk2) {
|
|
155
|
+
return silk2[GET_GRAPHQL_TYPE]();
|
|
156
|
+
}
|
|
157
|
+
function parseSilk(silk2, input) {
|
|
158
|
+
if (silk2[PARSE] == null) return input;
|
|
159
|
+
return silk2[PARSE](input);
|
|
160
|
+
}
|
|
161
|
+
function isSilk(target) {
|
|
162
|
+
if (typeof target !== "object") return false;
|
|
163
|
+
if (target == null) return false;
|
|
164
|
+
return GET_GRAPHQL_TYPE in target;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// src/utils/args.ts
|
|
168
|
+
function getOperationOptions(resolveOrOptions) {
|
|
169
|
+
if (typeof resolveOrOptions === "function") {
|
|
170
|
+
return { resolve: resolveOrOptions };
|
|
171
|
+
}
|
|
172
|
+
return resolveOrOptions;
|
|
173
|
+
}
|
|
174
|
+
function getSubscriptionOptions(subscribeOrOptions) {
|
|
175
|
+
if (typeof subscribeOrOptions === "function") {
|
|
176
|
+
return { subscribe: subscribeOrOptions };
|
|
177
|
+
}
|
|
178
|
+
return subscribeOrOptions;
|
|
179
|
+
}
|
|
180
|
+
function getFieldOptions({
|
|
181
|
+
description,
|
|
182
|
+
deprecationReason,
|
|
183
|
+
extensions
|
|
184
|
+
}) {
|
|
185
|
+
return {
|
|
186
|
+
description,
|
|
187
|
+
deprecationReason,
|
|
188
|
+
extensions
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// src/utils/middleware.ts
|
|
193
|
+
function applyMiddlewares(middlewares, resolveFunction, payload) {
|
|
194
|
+
const next = (index) => {
|
|
195
|
+
if (index >= middlewares.length) {
|
|
196
|
+
return resolveFunction();
|
|
197
|
+
}
|
|
198
|
+
const middleware = middlewares[index];
|
|
199
|
+
return middleware(() => next(index + 1), payload);
|
|
200
|
+
};
|
|
201
|
+
return next(0);
|
|
202
|
+
}
|
|
203
|
+
function compose(...lists) {
|
|
204
|
+
const list = [];
|
|
205
|
+
for (const item of lists) {
|
|
206
|
+
if (item != null) {
|
|
207
|
+
list.push(...item);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return list;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// src/utils/context.ts
|
|
214
|
+
var import_async_hooks = require("async_hooks");
|
|
215
|
+
function onlyMemoization() {
|
|
216
|
+
return { memoization: /* @__PURE__ */ new WeakMap(), isMemoization: true };
|
|
217
|
+
}
|
|
218
|
+
function isOnlyMemoryPayload(payload) {
|
|
219
|
+
return payload.isMemoization === true;
|
|
220
|
+
}
|
|
221
|
+
var resolverPayloadStorage = new import_async_hooks.AsyncLocalStorage();
|
|
222
|
+
function useResolverPayload() {
|
|
223
|
+
const payload = resolverPayloadStorage.getStore();
|
|
224
|
+
if (payload === void 0 || isOnlyMemoryPayload(payload)) return;
|
|
225
|
+
return payload;
|
|
226
|
+
}
|
|
227
|
+
function useContext() {
|
|
228
|
+
return useResolverPayload()?.context;
|
|
229
|
+
}
|
|
230
|
+
function useMemoizationMap() {
|
|
231
|
+
const payload = resolverPayloadStorage.getStore();
|
|
232
|
+
if (payload == null) return;
|
|
233
|
+
if (isOnlyMemoryPayload(payload)) return payload.memoization;
|
|
234
|
+
return ContextMemoization.assignMemoizationMap(payload.context);
|
|
235
|
+
}
|
|
236
|
+
var ContextMemoization = class {
|
|
237
|
+
constructor(getter, options = {}) {
|
|
238
|
+
this.getter = getter;
|
|
239
|
+
this.getter = getter;
|
|
240
|
+
this.getMemoizationMap = options.getMemoizationMap ?? useMemoizationMap;
|
|
241
|
+
this.key = options.key ?? this.getter;
|
|
242
|
+
}
|
|
243
|
+
getMemoizationMap;
|
|
244
|
+
key;
|
|
245
|
+
/**
|
|
246
|
+
* Get the value in memoization or call the getter function
|
|
247
|
+
* @returns the value of the getter function
|
|
248
|
+
*/
|
|
249
|
+
get() {
|
|
250
|
+
const map = this.getMemoizationMap();
|
|
251
|
+
if (!map) return this.getter();
|
|
252
|
+
if (!map.has(this.key)) {
|
|
253
|
+
map.set(this.key, this.getter());
|
|
254
|
+
}
|
|
255
|
+
return map.get(this.key);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Clear the memoization
|
|
259
|
+
* @returns true if the memoization is cleared, undefined if the context is not found
|
|
260
|
+
*/
|
|
261
|
+
clear() {
|
|
262
|
+
const map = this.getMemoizationMap();
|
|
263
|
+
if (!map) return;
|
|
264
|
+
return map.delete(this.key);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Check if the memoization exists
|
|
268
|
+
* @returns true if the memoization exists, undefined if the context is not found
|
|
269
|
+
*/
|
|
270
|
+
exists() {
|
|
271
|
+
const map = this.getMemoizationMap();
|
|
272
|
+
if (!map) return;
|
|
273
|
+
return map.has(this.key);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Set a new value to the memoization
|
|
277
|
+
* @param value the new value to set
|
|
278
|
+
* @returns the memoization map or undefined if the context is not found
|
|
279
|
+
*/
|
|
280
|
+
set(value) {
|
|
281
|
+
const map = this.getMemoizationMap();
|
|
282
|
+
if (!map) return;
|
|
283
|
+
return map.set(this.key, value);
|
|
284
|
+
}
|
|
285
|
+
static assignMemoizationMap(target) {
|
|
286
|
+
target[CONTEXT_MEMORY_MAP_KEY] ??= /* @__PURE__ */ new WeakMap();
|
|
287
|
+
return target[CONTEXT_MEMORY_MAP_KEY];
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
function createMemoization(...args) {
|
|
291
|
+
const memoization = new ContextMemoization(...args);
|
|
292
|
+
const callable = () => memoization.get();
|
|
293
|
+
return Object.assign(callable, {
|
|
294
|
+
get: () => memoization.get(),
|
|
295
|
+
set: (value) => memoization.set(value),
|
|
296
|
+
clear: () => memoization.clear(),
|
|
297
|
+
exists: () => memoization.exists(),
|
|
298
|
+
getter: memoization.getter
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// src/utils/object.ts
|
|
303
|
+
function mapValue(record, fn) {
|
|
304
|
+
const result = /* @__PURE__ */ Object.create(null);
|
|
305
|
+
for (const key of Object.keys(record)) {
|
|
306
|
+
const value = fn(record[key], key);
|
|
307
|
+
if (value === SKIP) continue;
|
|
308
|
+
result[key] = value;
|
|
309
|
+
}
|
|
310
|
+
return result;
|
|
311
|
+
}
|
|
312
|
+
var SKIP = Symbol.for("mapValue.skip");
|
|
313
|
+
mapValue.SKIP = SKIP;
|
|
314
|
+
function toObjMap(obj) {
|
|
315
|
+
if (obj == null) {
|
|
316
|
+
return /* @__PURE__ */ Object.create(null);
|
|
317
|
+
}
|
|
318
|
+
if (Object.getPrototypeOf(obj) === null) {
|
|
319
|
+
return obj;
|
|
320
|
+
}
|
|
321
|
+
const map = /* @__PURE__ */ Object.create(null);
|
|
322
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
323
|
+
map[key] = value;
|
|
324
|
+
}
|
|
325
|
+
return map;
|
|
326
|
+
}
|
|
327
|
+
function notNullish(x) {
|
|
328
|
+
return x != null;
|
|
329
|
+
}
|
|
330
|
+
function deepMerge(...objects) {
|
|
331
|
+
const result = {};
|
|
332
|
+
for (const obj of objects) {
|
|
333
|
+
if (obj == null) continue;
|
|
334
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
335
|
+
if (value !== null && typeof value === "object") {
|
|
336
|
+
if (Array.isArray(value)) {
|
|
337
|
+
if (!Array.isArray(result[key])) {
|
|
338
|
+
result[key] = [];
|
|
339
|
+
}
|
|
340
|
+
result[key] = [...result[key], ...value];
|
|
341
|
+
} else {
|
|
342
|
+
result[key] = deepMerge(result[key], value);
|
|
343
|
+
}
|
|
344
|
+
} else {
|
|
345
|
+
result[key] = value;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return result;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// src/utils/error.ts
|
|
353
|
+
function markErrorLocation(error, ...locations) {
|
|
354
|
+
if (error instanceof Error) {
|
|
355
|
+
error.message = markLocation(error.message, ...locations);
|
|
356
|
+
}
|
|
357
|
+
return error;
|
|
358
|
+
}
|
|
359
|
+
function tryIn(func, ...locations) {
|
|
360
|
+
try {
|
|
361
|
+
return func();
|
|
362
|
+
} catch (error) {
|
|
363
|
+
throw markErrorLocation(error, ...locations);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function markLocation(message, ...locations) {
|
|
367
|
+
if (locations.length === 0) {
|
|
368
|
+
return message;
|
|
369
|
+
}
|
|
370
|
+
const [existingPrefix, newMessage] = (() => {
|
|
371
|
+
const existingPrefixPattern = /^\[(.*?)\]/;
|
|
372
|
+
const match = existingPrefixPattern.exec(message);
|
|
373
|
+
if (match) return [match[1], message.slice(match[0].length).trim()];
|
|
374
|
+
return [void 0, message];
|
|
375
|
+
})();
|
|
376
|
+
const combinedLocation = locations.concat(existingPrefix ? [existingPrefix] : []).join(".");
|
|
377
|
+
return `[${combinedLocation}] ${newMessage}`;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// src/resolver/input.ts
|
|
381
|
+
function createInputParser(schema, value) {
|
|
382
|
+
let result;
|
|
383
|
+
const parse = async () => {
|
|
384
|
+
if (result !== void 0) return result;
|
|
385
|
+
result = await parseInputValue(schema, value);
|
|
386
|
+
return result;
|
|
387
|
+
};
|
|
388
|
+
Object.assign(parse, { schema, value });
|
|
389
|
+
Object.defineProperty(parse, "result", {
|
|
390
|
+
get: () => result,
|
|
391
|
+
set: (value2) => result = value2
|
|
392
|
+
});
|
|
393
|
+
return parse;
|
|
394
|
+
}
|
|
395
|
+
function parseInputValue(inputSchema, input) {
|
|
396
|
+
if (inputSchema === void 0) {
|
|
397
|
+
return input;
|
|
398
|
+
}
|
|
399
|
+
if (isSilk(inputSchema)) {
|
|
400
|
+
if (typeof inputSchema[symbols_exports.PARSE] === "function") {
|
|
401
|
+
return inputSchema[symbols_exports.PARSE](input);
|
|
402
|
+
}
|
|
403
|
+
return input;
|
|
404
|
+
}
|
|
405
|
+
return parseInputEntries(inputSchema, input);
|
|
406
|
+
}
|
|
407
|
+
async function parseInputEntries(inputSchema, input = {}) {
|
|
408
|
+
const result = {};
|
|
409
|
+
await Promise.all(
|
|
410
|
+
Object.entries(inputSchema).map(async ([key, value]) => {
|
|
411
|
+
if (typeof value[symbols_exports.PARSE] === "function") {
|
|
412
|
+
result[key] = await value[symbols_exports.PARSE](input[key]);
|
|
413
|
+
} else {
|
|
414
|
+
result[key] = input[key];
|
|
415
|
+
}
|
|
416
|
+
})
|
|
417
|
+
);
|
|
418
|
+
return result;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// src/resolver/resolver.ts
|
|
422
|
+
var silkQuery = (output, resolveOrOptions) => {
|
|
423
|
+
const options = getOperationOptions(resolveOrOptions);
|
|
424
|
+
const type = "query";
|
|
425
|
+
return {
|
|
426
|
+
...getFieldOptions(options),
|
|
427
|
+
input: options.input,
|
|
428
|
+
output,
|
|
429
|
+
resolve: (inputValue, extraOptions) => {
|
|
430
|
+
const parseInput = createInputParser(options.input, inputValue);
|
|
431
|
+
return applyMiddlewares(
|
|
432
|
+
compose(extraOptions?.middlewares, options.middlewares),
|
|
433
|
+
async () => options.resolve(await parseInput()),
|
|
434
|
+
{ parseInput, parent: void 0, outputSilk: output, type }
|
|
435
|
+
);
|
|
436
|
+
},
|
|
437
|
+
type
|
|
438
|
+
};
|
|
439
|
+
};
|
|
440
|
+
var silkMutation = (output, resolveOrOptions) => {
|
|
441
|
+
const options = getOperationOptions(resolveOrOptions);
|
|
442
|
+
const type = "mutation";
|
|
443
|
+
return {
|
|
444
|
+
...getFieldOptions(options),
|
|
445
|
+
input: options.input,
|
|
446
|
+
output,
|
|
447
|
+
resolve: (inputValue, extraOptions) => {
|
|
448
|
+
const parseInput = createInputParser(options.input, inputValue);
|
|
449
|
+
return applyMiddlewares(
|
|
450
|
+
compose(extraOptions?.middlewares, options.middlewares),
|
|
451
|
+
async () => options.resolve(await parseInput()),
|
|
452
|
+
{ parseInput, parent: void 0, outputSilk: output, type }
|
|
453
|
+
);
|
|
454
|
+
},
|
|
455
|
+
type
|
|
456
|
+
};
|
|
457
|
+
};
|
|
458
|
+
var silkField = (output, resolveOrOptions) => {
|
|
459
|
+
const options = getOperationOptions(resolveOrOptions);
|
|
460
|
+
const type = "field";
|
|
461
|
+
return {
|
|
462
|
+
...getFieldOptions(options),
|
|
463
|
+
input: options.input,
|
|
464
|
+
output,
|
|
465
|
+
resolve: (parent, inputValue, extraOptions) => {
|
|
466
|
+
const parseInput = createInputParser(options.input, inputValue);
|
|
467
|
+
return applyMiddlewares(
|
|
468
|
+
compose(extraOptions?.middlewares, options.middlewares),
|
|
469
|
+
async () => options.resolve(parent, await parseInput()),
|
|
470
|
+
{ parseInput, parent, outputSilk: output, type }
|
|
471
|
+
);
|
|
472
|
+
},
|
|
473
|
+
type
|
|
474
|
+
};
|
|
475
|
+
};
|
|
476
|
+
var defaultSubscriptionResolve = (source) => source;
|
|
477
|
+
var silkSubscription = (output, subscribeOrOptions) => {
|
|
478
|
+
const options = getSubscriptionOptions(subscribeOrOptions);
|
|
479
|
+
const type = "subscription";
|
|
480
|
+
return {
|
|
481
|
+
...getFieldOptions(options),
|
|
482
|
+
input: options.input,
|
|
483
|
+
output,
|
|
484
|
+
subscribe: (inputValue, extraOptions) => {
|
|
485
|
+
const parseInput = createInputParser(options.input, inputValue);
|
|
486
|
+
return applyMiddlewares(
|
|
487
|
+
compose(
|
|
488
|
+
extraOptions?.middlewares,
|
|
489
|
+
options.middlewares
|
|
490
|
+
),
|
|
491
|
+
async () => options.subscribe(await parseInput()),
|
|
492
|
+
{ parseInput, parent: void 0, outputSilk: output, type }
|
|
493
|
+
);
|
|
494
|
+
},
|
|
495
|
+
resolve: options.resolve ?? defaultSubscriptionResolve,
|
|
496
|
+
type
|
|
497
|
+
};
|
|
498
|
+
};
|
|
499
|
+
var ResolverOptionsMap = /* @__PURE__ */ new WeakMap();
|
|
500
|
+
function baseResolver(operations, options) {
|
|
501
|
+
const record = {};
|
|
502
|
+
Object.entries(operations).forEach(([name, operation]) => {
|
|
503
|
+
record[name] = extraOperationOptions(operation, options);
|
|
504
|
+
});
|
|
505
|
+
if (options) ResolverOptionsMap.set(record, options);
|
|
506
|
+
return record;
|
|
507
|
+
}
|
|
508
|
+
function extraOperationOptions(operation, options) {
|
|
509
|
+
const composeMiddlewares = (extraOptions) => compose(extraOptions?.middlewares, options?.middlewares);
|
|
510
|
+
switch (operation.type) {
|
|
511
|
+
case "field":
|
|
512
|
+
return {
|
|
513
|
+
...operation,
|
|
514
|
+
resolve: (parent, input, extraOptions) => operation.resolve(parent, input, {
|
|
515
|
+
...extraOptions,
|
|
516
|
+
middlewares: composeMiddlewares(extraOptions)
|
|
517
|
+
})
|
|
518
|
+
};
|
|
519
|
+
case "subscription":
|
|
520
|
+
return {
|
|
521
|
+
...operation,
|
|
522
|
+
subscribe: (input, extraOptions) => operation.subscribe(input, {
|
|
523
|
+
...extraOptions,
|
|
524
|
+
middlewares: composeMiddlewares(extraOptions)
|
|
525
|
+
})
|
|
526
|
+
};
|
|
527
|
+
default:
|
|
528
|
+
return {
|
|
529
|
+
...operation,
|
|
530
|
+
resolve: (input, extraOptions) => operation.resolve(input, {
|
|
531
|
+
...extraOptions,
|
|
532
|
+
middlewares: composeMiddlewares(extraOptions)
|
|
533
|
+
})
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
var silkResolver = Object.assign(
|
|
538
|
+
baseResolver,
|
|
539
|
+
{
|
|
540
|
+
of: (parent, operations, options) => baseResolver(
|
|
541
|
+
operations,
|
|
542
|
+
{ ...options, parent }
|
|
543
|
+
)
|
|
544
|
+
}
|
|
545
|
+
);
|
|
546
|
+
var loom = {
|
|
547
|
+
query: silkQuery,
|
|
548
|
+
resolver: silkResolver,
|
|
549
|
+
field: silkField,
|
|
550
|
+
subscription: silkSubscription,
|
|
551
|
+
mutation: silkMutation
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
// src/helper/create-loom.ts
|
|
555
|
+
function toSilkInput(schema, toSilk, isSchema) {
|
|
556
|
+
if (schema == null) {
|
|
557
|
+
return schema;
|
|
558
|
+
}
|
|
559
|
+
if (isSchema(schema)) {
|
|
560
|
+
return toSilk(schema);
|
|
561
|
+
}
|
|
562
|
+
const record = {};
|
|
563
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
564
|
+
record[key] = toSilk(value);
|
|
565
|
+
}
|
|
566
|
+
return record;
|
|
567
|
+
}
|
|
568
|
+
function createResolverBobbin(toSilk) {
|
|
569
|
+
return Object.assign(baseResolver, {
|
|
570
|
+
of: (parent, operations, options) => baseResolver(
|
|
571
|
+
operations,
|
|
572
|
+
{ ...options, parent: toSilk(parent) }
|
|
573
|
+
)
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
function createFieldBobbin(toSilk, isSchema) {
|
|
577
|
+
return (output, resolveOrOptions) => {
|
|
578
|
+
const options = getOperationOptions(
|
|
579
|
+
resolveOrOptions
|
|
580
|
+
);
|
|
581
|
+
return silkField(toSilk(output), {
|
|
582
|
+
...options,
|
|
583
|
+
input: toSilkInput(options.input, toSilk, isSchema)
|
|
584
|
+
});
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
function createQueryBobbin(toSilk, isSchema) {
|
|
588
|
+
return (output, resolveOrOptions) => {
|
|
589
|
+
const options = getOperationOptions(resolveOrOptions);
|
|
590
|
+
return silkQuery(toSilk(output), {
|
|
591
|
+
...options,
|
|
592
|
+
input: toSilkInput(options.input, toSilk, isSchema)
|
|
593
|
+
});
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
function createMutationBobbin(toSilk, isSchema) {
|
|
597
|
+
return (output, resolveOrOptions) => {
|
|
598
|
+
const options = getOperationOptions(resolveOrOptions);
|
|
599
|
+
return silkMutation(toSilk(output), {
|
|
600
|
+
...options,
|
|
601
|
+
input: toSilkInput(options.input, toSilk, isSchema)
|
|
602
|
+
});
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
function createSubscriptionBobbin(toSilk, isSchema) {
|
|
606
|
+
return (output, resolveOrOptions) => {
|
|
607
|
+
const options = getSubscriptionOptions(resolveOrOptions);
|
|
608
|
+
return silkSubscription(toSilk(output), {
|
|
609
|
+
...options,
|
|
610
|
+
input: toSilkInput(options.input, toSilk, isSchema)
|
|
611
|
+
});
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
function createLoom(toSilk, isSchema) {
|
|
615
|
+
return {
|
|
616
|
+
query: createQueryBobbin(toSilk, isSchema),
|
|
617
|
+
mutation: createMutationBobbin(toSilk, isSchema),
|
|
618
|
+
field: createFieldBobbin(toSilk, isSchema),
|
|
619
|
+
resolver: createResolverBobbin(toSilk),
|
|
620
|
+
subscription: createSubscriptionBobbin(toSilk, isSchema)
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// src/schema/object.ts
|
|
625
|
+
var import_graphql4 = require("graphql");
|
|
626
|
+
|
|
627
|
+
// src/schema/weaver-context.ts
|
|
628
|
+
var import_graphql2 = require("graphql");
|
|
629
|
+
var ref;
|
|
630
|
+
var names = /* @__PURE__ */ new WeakMap();
|
|
631
|
+
function initWeaverContext() {
|
|
632
|
+
return {
|
|
633
|
+
loomObjectMap: /* @__PURE__ */ new Map(),
|
|
634
|
+
loomUnionMap: /* @__PURE__ */ new Map(),
|
|
635
|
+
inputMap: /* @__PURE__ */ new Map(),
|
|
636
|
+
interfaceMap: /* @__PURE__ */ new Map(),
|
|
637
|
+
configs: /* @__PURE__ */ new Map(),
|
|
638
|
+
getConfig(key) {
|
|
639
|
+
return this.configs.get(key);
|
|
640
|
+
},
|
|
641
|
+
setConfig(config) {
|
|
642
|
+
const key = config[WEAVER_CONFIG];
|
|
643
|
+
this.configs.set(key, config);
|
|
644
|
+
},
|
|
645
|
+
deleteConfig(key) {
|
|
646
|
+
this.configs.delete(key);
|
|
647
|
+
},
|
|
648
|
+
names,
|
|
649
|
+
namedTypes: /* @__PURE__ */ new Map(),
|
|
650
|
+
memoNamedType(gqlTypeValue) {
|
|
651
|
+
const gqlType = gqlTypeValue;
|
|
652
|
+
if ((0, import_graphql2.isObjectType)(gqlType) || (0, import_graphql2.isUnionType)(gqlType) || (0, import_graphql2.isEnumType)(gqlType)) {
|
|
653
|
+
this.namedTypes.set(gqlType.name, gqlType);
|
|
654
|
+
}
|
|
655
|
+
return gqlTypeValue;
|
|
656
|
+
},
|
|
657
|
+
getNamedType(name) {
|
|
658
|
+
return this.namedTypes.get(name);
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
var weaverContext = {
|
|
663
|
+
get loomObjectMap() {
|
|
664
|
+
return ref?.loomObjectMap;
|
|
665
|
+
},
|
|
666
|
+
get loomUnionMap() {
|
|
667
|
+
return ref?.loomUnionMap;
|
|
668
|
+
},
|
|
669
|
+
get inputMap() {
|
|
670
|
+
return ref?.inputMap;
|
|
671
|
+
},
|
|
672
|
+
get interfaceMap() {
|
|
673
|
+
return ref?.interfaceMap;
|
|
674
|
+
},
|
|
675
|
+
get configs() {
|
|
676
|
+
return ref?.configs;
|
|
677
|
+
},
|
|
678
|
+
getConfig(key) {
|
|
679
|
+
return ref?.getConfig(key);
|
|
680
|
+
},
|
|
681
|
+
setConfig(config) {
|
|
682
|
+
ref?.setConfig(config);
|
|
683
|
+
},
|
|
684
|
+
deleteConfig(key) {
|
|
685
|
+
ref?.deleteConfig(key);
|
|
686
|
+
},
|
|
687
|
+
get value() {
|
|
688
|
+
return ref;
|
|
689
|
+
},
|
|
690
|
+
useConfig(config, callback) {
|
|
691
|
+
const context = weaverContext.value ?? initWeaverContext();
|
|
692
|
+
context.setConfig(config);
|
|
693
|
+
const result = provideWeaverContext(callback, context);
|
|
694
|
+
context.deleteConfig(config[WEAVER_CONFIG]);
|
|
695
|
+
return result;
|
|
696
|
+
},
|
|
697
|
+
names,
|
|
698
|
+
getNamedType(name) {
|
|
699
|
+
return ref?.getNamedType(name);
|
|
700
|
+
},
|
|
701
|
+
memoNamedType(gqlType) {
|
|
702
|
+
return ref?.memoNamedType(gqlType) ?? gqlType;
|
|
703
|
+
},
|
|
704
|
+
GraphQLTypes: /* @__PURE__ */ new WeakMap(),
|
|
705
|
+
getGraphQLType(origin) {
|
|
706
|
+
return this.GraphQLTypes.get(origin);
|
|
707
|
+
},
|
|
708
|
+
memoGraphQLType(origin, gqlType) {
|
|
709
|
+
this.GraphQLTypes.set(origin, gqlType);
|
|
710
|
+
return gqlType;
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
function provideWeaverContext(func, value) {
|
|
714
|
+
const lastRef = ref;
|
|
715
|
+
ref = value;
|
|
716
|
+
try {
|
|
717
|
+
return func();
|
|
718
|
+
} finally {
|
|
719
|
+
ref = lastRef;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
function collectNames(...namesList) {
|
|
723
|
+
const namesRecord = {};
|
|
724
|
+
for (const namesItem of namesList) {
|
|
725
|
+
for (const [name, schema] of Object.entries(namesItem)) {
|
|
726
|
+
names.set(schema, name);
|
|
727
|
+
namesRecord[name] = schema;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
return namesRecord;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// src/schema/input.ts
|
|
734
|
+
var import_graphql3 = require("graphql");
|
|
735
|
+
function inputToArgs(input) {
|
|
736
|
+
if (input === void 0) return void 0;
|
|
737
|
+
if (isSilk(input)) {
|
|
738
|
+
let inputType = getGraphQLType(input);
|
|
739
|
+
if ((0, import_graphql3.isNonNullType)(inputType)) inputType = inputType.ofType;
|
|
740
|
+
if ((0, import_graphql3.isObjectType)(inputType)) {
|
|
741
|
+
return mapValue(
|
|
742
|
+
inputType.toConfig().fields,
|
|
743
|
+
(it) => toInputFieldConfig(it)
|
|
744
|
+
);
|
|
745
|
+
}
|
|
746
|
+
throw new Error(`Cannot convert ${inputType.toString()} to input type`);
|
|
747
|
+
}
|
|
748
|
+
const args = {};
|
|
749
|
+
Object.entries(input).forEach(([name, field]) => {
|
|
750
|
+
tryIn(() => {
|
|
751
|
+
args[name] = {
|
|
752
|
+
...field,
|
|
753
|
+
type: ensureInputType(field)
|
|
754
|
+
};
|
|
755
|
+
}, name);
|
|
756
|
+
});
|
|
757
|
+
return args;
|
|
758
|
+
}
|
|
759
|
+
function ensureInputType(silkOrType) {
|
|
760
|
+
const gqlType = (() => {
|
|
761
|
+
if (isSilk(silkOrType)) {
|
|
762
|
+
return getGraphQLType(silkOrType);
|
|
763
|
+
}
|
|
764
|
+
return silkOrType;
|
|
765
|
+
})();
|
|
766
|
+
if ((0, import_graphql3.isUnionType)(gqlType))
|
|
767
|
+
throw new Error(`Cannot convert union type ${gqlType.name} to input type`);
|
|
768
|
+
if ((0, import_graphql3.isNonNullType)(gqlType)) {
|
|
769
|
+
return new import_graphql3.GraphQLNonNull(ensureInputType(gqlType.ofType));
|
|
770
|
+
}
|
|
771
|
+
if ((0, import_graphql3.isListType)(gqlType)) {
|
|
772
|
+
return new import_graphql3.GraphQLList(ensureInputType(gqlType.ofType));
|
|
773
|
+
}
|
|
774
|
+
if ((0, import_graphql3.isObjectType)(gqlType) || (0, import_graphql3.isInterfaceType)(gqlType))
|
|
775
|
+
return ensureInputObjectType(gqlType);
|
|
776
|
+
return gqlType;
|
|
777
|
+
}
|
|
778
|
+
function ensureInputObjectType(object) {
|
|
779
|
+
if ((0, import_graphql3.isInputObjectType)(object)) return object;
|
|
780
|
+
const existing = weaverContext.inputMap?.get(object);
|
|
781
|
+
if (existing != null) return existing;
|
|
782
|
+
const {
|
|
783
|
+
astNode: _,
|
|
784
|
+
extensionASTNodes: __,
|
|
785
|
+
fields,
|
|
786
|
+
...config
|
|
787
|
+
} = object.toConfig();
|
|
788
|
+
const getInputObjectName = weaverContext.getConfig("gqloom.core.schema")?.getInputObjectName ?? ((name) => name);
|
|
789
|
+
const input = new import_graphql3.GraphQLInputObjectType({
|
|
790
|
+
...config,
|
|
791
|
+
name: getInputObjectName(object.name),
|
|
792
|
+
fields: mapValue(fields, (it) => toInputFieldConfig(it))
|
|
793
|
+
});
|
|
794
|
+
weaverContext.inputMap?.set(object, input);
|
|
795
|
+
return input;
|
|
796
|
+
}
|
|
797
|
+
function toInputFieldConfig({
|
|
798
|
+
astNode: _,
|
|
799
|
+
resolve: _1,
|
|
800
|
+
...config
|
|
801
|
+
}) {
|
|
802
|
+
return { ...config, type: ensureInputType(config.type) };
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
// src/schema/object.ts
|
|
806
|
+
var LoomObjectType = class extends import_graphql4.GraphQLObjectType {
|
|
807
|
+
extraFields = /* @__PURE__ */ new Map();
|
|
808
|
+
weaverContext;
|
|
809
|
+
resolverOptions;
|
|
810
|
+
constructor(objectOrGetter, options = {}) {
|
|
811
|
+
const origin = typeof objectOrGetter === "function" ? objectOrGetter() : objectOrGetter;
|
|
812
|
+
const config = (() => {
|
|
813
|
+
if ((0, import_graphql4.isObjectType)(origin)) {
|
|
814
|
+
return origin.toConfig();
|
|
815
|
+
} else if (typeof origin === "string") {
|
|
816
|
+
return { name: origin, fields: {} };
|
|
817
|
+
} else {
|
|
818
|
+
return origin;
|
|
819
|
+
}
|
|
820
|
+
})();
|
|
821
|
+
super(config);
|
|
822
|
+
this.resolverOptions = options.resolverOptions;
|
|
823
|
+
this.weaverContext = options.weaverContext ?? initWeaverContext();
|
|
824
|
+
}
|
|
825
|
+
addField(name, resolver) {
|
|
826
|
+
const existing = this.extraFields.get(name);
|
|
827
|
+
if (existing && existing !== resolver) {
|
|
828
|
+
throw new Error(`Field ${name} already exists in ${this.name}`);
|
|
829
|
+
}
|
|
830
|
+
this.extraFields.set(name, resolver);
|
|
831
|
+
}
|
|
832
|
+
mergeExtensions(extensions) {
|
|
833
|
+
this.extensions = deepMerge(this.extensions, extensions);
|
|
834
|
+
}
|
|
835
|
+
extraField;
|
|
836
|
+
getFields() {
|
|
837
|
+
const fields = super.getFields();
|
|
838
|
+
Object.values(fields).forEach(
|
|
839
|
+
(field) => field.type = this.getCacheType(field.type)
|
|
840
|
+
);
|
|
841
|
+
const extraField = provideWeaverContext(
|
|
842
|
+
() => defineFieldMap(this.mapToFieldConfig(this.extraFields)),
|
|
843
|
+
this.weaverContext
|
|
844
|
+
);
|
|
845
|
+
if (Object.keys(this.extraField ?? {}).join() !== Object.keys(extraField).join()) {
|
|
846
|
+
this.extraField = extraField;
|
|
847
|
+
}
|
|
848
|
+
return {
|
|
849
|
+
...fields,
|
|
850
|
+
...this.extraField
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
mapToFieldConfig(map) {
|
|
854
|
+
const record = {};
|
|
855
|
+
for (const [name, field] of map.entries()) {
|
|
856
|
+
record[name] = this.toFieldConfig(field);
|
|
857
|
+
}
|
|
858
|
+
return record;
|
|
859
|
+
}
|
|
860
|
+
toFieldConfig(field) {
|
|
861
|
+
try {
|
|
862
|
+
const outputType = this.getCacheType(getGraphQLType(field.output));
|
|
863
|
+
return {
|
|
864
|
+
...extract(field),
|
|
865
|
+
type: outputType,
|
|
866
|
+
args: inputToArgs(field.input),
|
|
867
|
+
...this.provideForResolve(field),
|
|
868
|
+
...this.provideForSubscribe(field)
|
|
869
|
+
};
|
|
870
|
+
} catch (error) {
|
|
871
|
+
throw markErrorLocation(error);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
provideForResolve(field) {
|
|
875
|
+
if (field?.resolve == null) return;
|
|
876
|
+
if (field.resolve === defaultSubscriptionResolve)
|
|
877
|
+
return { resolve: defaultSubscriptionResolve };
|
|
878
|
+
const resolve = field.type === "field" ? (root, args, context, info) => resolverPayloadStorage.run(
|
|
879
|
+
{ root, args, context, info, field },
|
|
880
|
+
() => field.resolve(root, args, this.resolverOptions)
|
|
881
|
+
) : field.type === "subscription" ? (root, args, context, info) => resolverPayloadStorage.run(
|
|
882
|
+
{ root, args, context, info, field },
|
|
883
|
+
() => field.resolve(root, args)
|
|
884
|
+
) : (root, args, context, info) => resolverPayloadStorage.run(
|
|
885
|
+
{ root, args, context, info, field },
|
|
886
|
+
() => field.resolve(args, this.resolverOptions)
|
|
887
|
+
);
|
|
888
|
+
return { resolve };
|
|
889
|
+
}
|
|
890
|
+
provideForSubscribe(field) {
|
|
891
|
+
if (field?.subscribe == null) return;
|
|
892
|
+
return {
|
|
893
|
+
subscribe: (root, args, context, info) => resolverPayloadStorage.run(
|
|
894
|
+
{ root, args, context, info, field },
|
|
895
|
+
() => field.subscribe?.(args, this.resolverOptions)
|
|
896
|
+
)
|
|
897
|
+
};
|
|
898
|
+
}
|
|
899
|
+
getCacheType(gqlType) {
|
|
900
|
+
return getCacheType(gqlType, this.options);
|
|
901
|
+
}
|
|
902
|
+
get options() {
|
|
903
|
+
const { resolverOptions, weaverContext: weaverContext2 } = this;
|
|
904
|
+
return { resolverOptions, weaverContext: weaverContext2 };
|
|
905
|
+
}
|
|
906
|
+
};
|
|
907
|
+
function extract({
|
|
908
|
+
deprecationReason,
|
|
909
|
+
description,
|
|
910
|
+
extensions
|
|
911
|
+
}) {
|
|
912
|
+
return {
|
|
913
|
+
description,
|
|
914
|
+
deprecationReason,
|
|
915
|
+
extensions
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
function defineFieldMap(fields) {
|
|
919
|
+
const fieldMap = (0, import_graphql4.resolveObjMapThunk)(fields);
|
|
920
|
+
return mapValue(fieldMap, (fieldConfig, fieldName) => {
|
|
921
|
+
const argsConfig = fieldConfig.args ?? {};
|
|
922
|
+
return {
|
|
923
|
+
name: (0, import_graphql4.assertName)(fieldName),
|
|
924
|
+
description: fieldConfig.description,
|
|
925
|
+
type: fieldConfig.type,
|
|
926
|
+
args: defineArguments(argsConfig),
|
|
927
|
+
resolve: fieldConfig.resolve,
|
|
928
|
+
subscribe: fieldConfig.subscribe,
|
|
929
|
+
deprecationReason: fieldConfig.deprecationReason,
|
|
930
|
+
extensions: toObjMap(fieldConfig.extensions),
|
|
931
|
+
astNode: fieldConfig.astNode
|
|
932
|
+
};
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
function defineArguments(args) {
|
|
936
|
+
return Object.entries(args).map(([argName, argConfig]) => ({
|
|
937
|
+
name: (0, import_graphql4.assertName)(argName),
|
|
938
|
+
description: argConfig.description,
|
|
939
|
+
type: argConfig.type,
|
|
940
|
+
defaultValue: argConfig.defaultValue,
|
|
941
|
+
deprecationReason: argConfig.deprecationReason,
|
|
942
|
+
extensions: toObjMap(argConfig.extensions),
|
|
943
|
+
astNode: argConfig.astNode
|
|
944
|
+
}));
|
|
945
|
+
}
|
|
946
|
+
function getCacheType(gqlType, options = {}) {
|
|
947
|
+
const context = options.weaverContext ?? weaverContext;
|
|
948
|
+
if (gqlType instanceof LoomObjectType) return gqlType;
|
|
949
|
+
if ((0, import_graphql4.isObjectType)(gqlType)) {
|
|
950
|
+
const gqlObject = context.loomObjectMap?.get(gqlType);
|
|
951
|
+
if (gqlObject != null) return gqlObject;
|
|
952
|
+
const loomObject = new LoomObjectType(gqlType, options);
|
|
953
|
+
context.loomObjectMap?.set(gqlType, loomObject);
|
|
954
|
+
return loomObject;
|
|
955
|
+
} else if ((0, import_graphql4.isListType)(gqlType)) {
|
|
956
|
+
return new import_graphql4.GraphQLList(getCacheType(gqlType.ofType, options));
|
|
957
|
+
} else if ((0, import_graphql4.isNonNullType)(gqlType)) {
|
|
958
|
+
return new import_graphql4.GraphQLNonNull(getCacheType(gqlType.ofType, options));
|
|
959
|
+
} else if ((0, import_graphql4.isUnionType)(gqlType)) {
|
|
960
|
+
const existing = context.loomUnionMap?.get(gqlType);
|
|
961
|
+
if (existing != null) return existing;
|
|
962
|
+
const config = gqlType.toConfig();
|
|
963
|
+
const unionType = new import_graphql4.GraphQLUnionType({
|
|
964
|
+
...config,
|
|
965
|
+
types: config.types.map(
|
|
966
|
+
(type) => getCacheType(type, options)
|
|
967
|
+
)
|
|
968
|
+
});
|
|
969
|
+
context.loomUnionMap?.set(gqlType, unionType);
|
|
970
|
+
return unionType;
|
|
971
|
+
}
|
|
972
|
+
return gqlType;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
// src/schema/schema-weaver.ts
|
|
976
|
+
var import_graphql5 = require("graphql");
|
|
977
|
+
var SchemaWeaver = class _SchemaWeaver {
|
|
978
|
+
query;
|
|
979
|
+
mutation;
|
|
980
|
+
subscription;
|
|
981
|
+
types;
|
|
982
|
+
context;
|
|
983
|
+
resolverOptions;
|
|
984
|
+
/**
|
|
985
|
+
* Create a Schema Weaver config object
|
|
986
|
+
* @param config Schema Weaver config options
|
|
987
|
+
* @returns a Schema Weaver config object
|
|
988
|
+
*/
|
|
989
|
+
static config(config) {
|
|
990
|
+
return {
|
|
991
|
+
...config,
|
|
992
|
+
[WEAVER_CONFIG]: "gqloom.core.schema"
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
constructor({ query, mutation, subscription, types } = {}, context) {
|
|
996
|
+
if (query != null) this.query = query;
|
|
997
|
+
if (mutation != null) this.mutation = mutation;
|
|
998
|
+
if (subscription != null) this.subscription = subscription;
|
|
999
|
+
if (types != null) this.types = types.slice();
|
|
1000
|
+
this.context = context ?? initWeaverContext();
|
|
1001
|
+
}
|
|
1002
|
+
use(...middlewares) {
|
|
1003
|
+
this.resolverOptions ??= {};
|
|
1004
|
+
this.resolverOptions.middlewares ??= [];
|
|
1005
|
+
this.resolverOptions.middlewares.push(...middlewares);
|
|
1006
|
+
return this;
|
|
1007
|
+
}
|
|
1008
|
+
add(resolver) {
|
|
1009
|
+
provideWeaverContext(() => this.addResolver(resolver), this.context);
|
|
1010
|
+
return this;
|
|
1011
|
+
}
|
|
1012
|
+
addType(silk2) {
|
|
1013
|
+
const gqlType = provideWeaverContext(() => {
|
|
1014
|
+
let gqlType2 = getGraphQLType(silk2);
|
|
1015
|
+
if ((0, import_graphql5.isNonNullType)(gqlType2)) gqlType2 = gqlType2.ofType;
|
|
1016
|
+
if ((0, import_graphql5.isObjectType)(gqlType2)) {
|
|
1017
|
+
const existing = this.context.loomObjectMap.get(gqlType2);
|
|
1018
|
+
if (existing != null) return existing;
|
|
1019
|
+
const extraObject = new LoomObjectType(gqlType2, this.fieldOptions);
|
|
1020
|
+
this.context.loomObjectMap.set(gqlType2, extraObject);
|
|
1021
|
+
return extraObject;
|
|
1022
|
+
} else if ((0, import_graphql5.isUnionType)(gqlType2) || (0, import_graphql5.isEnumType)(gqlType2)) {
|
|
1023
|
+
return gqlType2;
|
|
1024
|
+
}
|
|
1025
|
+
throw new Error(
|
|
1026
|
+
`${gqlType2?.name ?? gqlType2.toString()} is not a named type`
|
|
1027
|
+
);
|
|
1028
|
+
}, this.context);
|
|
1029
|
+
this.types ??= [];
|
|
1030
|
+
this.types.push(gqlType);
|
|
1031
|
+
return this;
|
|
1032
|
+
}
|
|
1033
|
+
setConfig(config) {
|
|
1034
|
+
this.context.setConfig(config);
|
|
1035
|
+
return this;
|
|
1036
|
+
}
|
|
1037
|
+
weaveGraphQLSchema() {
|
|
1038
|
+
const { query, mutation, subscription, types } = this;
|
|
1039
|
+
const config = this.context.getConfig("gqloom.core.schema");
|
|
1040
|
+
const schema = new import_graphql5.GraphQLSchema({
|
|
1041
|
+
query,
|
|
1042
|
+
mutation,
|
|
1043
|
+
subscription,
|
|
1044
|
+
types: [...types ?? [], ...config?.types ?? []],
|
|
1045
|
+
...config
|
|
1046
|
+
});
|
|
1047
|
+
return schema;
|
|
1048
|
+
}
|
|
1049
|
+
addResolver(resolver) {
|
|
1050
|
+
const resolverOptions = ResolverOptionsMap.get(resolver);
|
|
1051
|
+
const parent = resolverOptions?.parent;
|
|
1052
|
+
const parentObject = (() => {
|
|
1053
|
+
if (parent == null) return void 0;
|
|
1054
|
+
let gqlType = getGraphQLType(parent);
|
|
1055
|
+
if ((0, import_graphql5.isNonNullType)(gqlType)) gqlType = gqlType.ofType;
|
|
1056
|
+
if ((0, import_graphql5.isObjectType)(gqlType)) {
|
|
1057
|
+
const existing = this.context.loomObjectMap.get(gqlType);
|
|
1058
|
+
if (existing != null) return existing;
|
|
1059
|
+
const extraObject = new LoomObjectType(gqlType, this.fieldOptions);
|
|
1060
|
+
this.context.loomObjectMap.set(gqlType, extraObject);
|
|
1061
|
+
return extraObject;
|
|
1062
|
+
}
|
|
1063
|
+
throw new Error(
|
|
1064
|
+
`${gqlType?.name ?? gqlType.toString()} is not an object type`
|
|
1065
|
+
);
|
|
1066
|
+
})();
|
|
1067
|
+
if (resolverOptions?.extensions && parentObject)
|
|
1068
|
+
parentObject.mergeExtensions(resolverOptions.extensions);
|
|
1069
|
+
Object.entries(resolver).forEach(([name, operation]) => {
|
|
1070
|
+
if (operation.type === "field") {
|
|
1071
|
+
if (parentObject == null) return;
|
|
1072
|
+
parentObject.addField(name, operation);
|
|
1073
|
+
} else {
|
|
1074
|
+
const operationObject = this.getOperationObject(operation.type);
|
|
1075
|
+
operationObject.addField(name, operation);
|
|
1076
|
+
}
|
|
1077
|
+
});
|
|
1078
|
+
return this;
|
|
1079
|
+
}
|
|
1080
|
+
getOperationObject(type) {
|
|
1081
|
+
switch (type) {
|
|
1082
|
+
case "query": {
|
|
1083
|
+
this.query ??= new LoomObjectType(
|
|
1084
|
+
{ name: "Query", fields: {} },
|
|
1085
|
+
this.fieldOptions
|
|
1086
|
+
);
|
|
1087
|
+
return this.query;
|
|
1088
|
+
}
|
|
1089
|
+
case "mutation": {
|
|
1090
|
+
this.mutation ??= new LoomObjectType(
|
|
1091
|
+
{ name: "Mutation", fields: {} },
|
|
1092
|
+
this.fieldOptions
|
|
1093
|
+
);
|
|
1094
|
+
return this.mutation;
|
|
1095
|
+
}
|
|
1096
|
+
case "subscription": {
|
|
1097
|
+
this.subscription ??= new LoomObjectType(
|
|
1098
|
+
{ name: "Subscription", fields: {} },
|
|
1099
|
+
this.fieldOptions
|
|
1100
|
+
);
|
|
1101
|
+
return this.subscription;
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
get fieldOptions() {
|
|
1106
|
+
const { resolverOptions, context } = this;
|
|
1107
|
+
return { resolverOptions, weaverContext: context };
|
|
1108
|
+
}
|
|
1109
|
+
static optionsFrom(...inputs) {
|
|
1110
|
+
const configs = /* @__PURE__ */ new Set();
|
|
1111
|
+
const middlewares = /* @__PURE__ */ new Set();
|
|
1112
|
+
const resolvers = /* @__PURE__ */ new Set();
|
|
1113
|
+
const silks = /* @__PURE__ */ new Set();
|
|
1114
|
+
let context;
|
|
1115
|
+
for (const item of inputs) {
|
|
1116
|
+
if (typeof item === "function") {
|
|
1117
|
+
middlewares.add(item);
|
|
1118
|
+
} else if (WEAVER_CONFIG in item) {
|
|
1119
|
+
configs.add(item);
|
|
1120
|
+
if (item[WEAVER_CONFIG] === "gqloom.core.schema") {
|
|
1121
|
+
context = item.weaverContext;
|
|
1122
|
+
}
|
|
1123
|
+
} else if (isSilk(item)) {
|
|
1124
|
+
silks.add(item);
|
|
1125
|
+
} else {
|
|
1126
|
+
resolvers.add(item);
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
return { context, configs, middlewares, resolvers, silks };
|
|
1130
|
+
}
|
|
1131
|
+
/**
|
|
1132
|
+
* Weave a GraphQL Schema from resolvers
|
|
1133
|
+
* @param inputs Resolvers, Global Middlewares or WeaverConfigs
|
|
1134
|
+
* @returns GraphQ LSchema
|
|
1135
|
+
*/
|
|
1136
|
+
static weave(...inputs) {
|
|
1137
|
+
const { context, configs, middlewares, resolvers, silks } = _SchemaWeaver.optionsFrom(...inputs);
|
|
1138
|
+
const weaver = new _SchemaWeaver({}, context);
|
|
1139
|
+
configs.forEach((it) => weaver.setConfig(it));
|
|
1140
|
+
middlewares.forEach((it) => weaver.use(it));
|
|
1141
|
+
resolvers.forEach((it) => weaver.add(it));
|
|
1142
|
+
silks.forEach((it) => weaver.addType(it));
|
|
1143
|
+
return weaver.weaveGraphQLSchema();
|
|
1144
|
+
}
|
|
1145
|
+
};
|
|
1146
|
+
var weave = SchemaWeaver.weave;
|
|
1147
|
+
|
|
1148
|
+
// src/schema/interface.ts
|
|
1149
|
+
var import_graphql6 = require("graphql");
|
|
1150
|
+
function ensureInterfaceType(gqlType, interfaceConfig) {
|
|
1151
|
+
if ((0, import_graphql6.isInterfaceType)(gqlType)) return gqlType;
|
|
1152
|
+
if (!(0, import_graphql6.isObjectType)(gqlType))
|
|
1153
|
+
throw new Error(`${gqlType.toString()} is not an object`);
|
|
1154
|
+
const key = gqlType;
|
|
1155
|
+
const existing = weaverContext.interfaceMap?.get(key);
|
|
1156
|
+
if (existing != null) return existing;
|
|
1157
|
+
const {
|
|
1158
|
+
astNode: _,
|
|
1159
|
+
extensionASTNodes: _1,
|
|
1160
|
+
fields,
|
|
1161
|
+
...config
|
|
1162
|
+
} = gqlType.toConfig();
|
|
1163
|
+
const interfaceType = new import_graphql6.GraphQLInterfaceType({
|
|
1164
|
+
...config,
|
|
1165
|
+
...interfaceConfig,
|
|
1166
|
+
fields: mapValue(fields, (field) => {
|
|
1167
|
+
return { ...field, type: getCacheType(field.type) };
|
|
1168
|
+
})
|
|
1169
|
+
});
|
|
1170
|
+
weaverContext.interfaceMap?.set(key, interfaceType);
|
|
1171
|
+
return interfaceType;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
// src/schema/extensions.ts
|
|
1175
|
+
function mergeExtensions(...extensionsList) {
|
|
1176
|
+
return deepMerge(...extensionsList);
|
|
1177
|
+
}
|
|
1178
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1179
|
+
0 && (module.exports = {
|
|
1180
|
+
ContextMemoization,
|
|
1181
|
+
LoomObjectType,
|
|
1182
|
+
ResolverOptionsMap,
|
|
1183
|
+
SYMBOLS,
|
|
1184
|
+
SchemaWeaver,
|
|
1185
|
+
applyMiddlewares,
|
|
1186
|
+
baseResolver,
|
|
1187
|
+
collectNames,
|
|
1188
|
+
compose,
|
|
1189
|
+
createFieldBobbin,
|
|
1190
|
+
createInputParser,
|
|
1191
|
+
createLoom,
|
|
1192
|
+
createMemoization,
|
|
1193
|
+
createMutationBobbin,
|
|
1194
|
+
createQueryBobbin,
|
|
1195
|
+
createResolverBobbin,
|
|
1196
|
+
createSubscriptionBobbin,
|
|
1197
|
+
deepMerge,
|
|
1198
|
+
defaultSubscriptionResolve,
|
|
1199
|
+
ensureInputObjectType,
|
|
1200
|
+
ensureInputType,
|
|
1201
|
+
ensureInterfaceType,
|
|
1202
|
+
getCacheType,
|
|
1203
|
+
getFieldOptions,
|
|
1204
|
+
getGraphQLType,
|
|
1205
|
+
getOperationOptions,
|
|
1206
|
+
getSubscriptionOptions,
|
|
1207
|
+
initWeaverContext,
|
|
1208
|
+
inputToArgs,
|
|
1209
|
+
isOnlyMemoryPayload,
|
|
1210
|
+
isSilk,
|
|
1211
|
+
listSilk,
|
|
1212
|
+
loom,
|
|
1213
|
+
mapValue,
|
|
1214
|
+
markErrorLocation,
|
|
1215
|
+
markLocation,
|
|
1216
|
+
mergeExtensions,
|
|
1217
|
+
nonNullSilk,
|
|
1218
|
+
notNullish,
|
|
1219
|
+
nullableSilk,
|
|
1220
|
+
onlyMemoization,
|
|
1221
|
+
parseInputValue,
|
|
1222
|
+
parseSilk,
|
|
1223
|
+
provideWeaverContext,
|
|
1224
|
+
resolverPayloadStorage,
|
|
1225
|
+
silk,
|
|
1226
|
+
silkField,
|
|
1227
|
+
silkMutation,
|
|
1228
|
+
silkQuery,
|
|
1229
|
+
silkResolver,
|
|
1230
|
+
silkSubscription,
|
|
1231
|
+
toObjMap,
|
|
1232
|
+
tryIn,
|
|
1233
|
+
useContext,
|
|
1234
|
+
useMemoizationMap,
|
|
1235
|
+
useResolverPayload,
|
|
1236
|
+
weave,
|
|
1237
|
+
weaverContext
|
|
1238
|
+
});
|