@baeta/plugin-graphql 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/dist/index.cjs +883 -0
- package/{index.d.ts → dist/index.d.ts} +0 -0
- package/{index.js → dist/index.js} +216 -538
- package/package.json +23 -27
- package/index.cjs +0 -1215
|
@@ -14,12 +14,112 @@ import { normalizeConfig, normalizeInstanceOrArray } from "@graphql-codegen/plug
|
|
|
14
14
|
import * as typescriptPlugin from "@graphql-codegen/typescript";
|
|
15
15
|
import path from "path";
|
|
16
16
|
|
|
17
|
-
//
|
|
17
|
+
// utils/cache.ts
|
|
18
|
+
function createCache() {
|
|
19
|
+
const cache = /* @__PURE__ */ new Map();
|
|
20
|
+
return function ensure(namespace, key, factory) {
|
|
21
|
+
const cacheKey = `${namespace}:${key}`;
|
|
22
|
+
const cachedValue = cache.get(cacheKey);
|
|
23
|
+
if (cachedValue) {
|
|
24
|
+
return cachedValue;
|
|
25
|
+
}
|
|
26
|
+
const value = factory();
|
|
27
|
+
cache.set(cacheKey, value);
|
|
28
|
+
return value;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// utils/load.ts
|
|
33
|
+
import { getCachedDocumentNodeFromSchema as getCachedDocumentNodeFromSchema2 } from "@graphql-codegen/plugin-helpers";
|
|
34
|
+
import { ApolloEngineLoader } from "@graphql-tools/apollo-engine-loader";
|
|
35
|
+
import { CodeFileLoader } from "@graphql-tools/code-file-loader";
|
|
36
|
+
import { GitLoader } from "@graphql-tools/git-loader";
|
|
37
|
+
import { GithubLoader } from "@graphql-tools/github-loader";
|
|
38
|
+
import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader";
|
|
39
|
+
import { JsonFileLoader } from "@graphql-tools/json-file-loader";
|
|
40
|
+
import { loadSchema as loadSchemaToolkit } from "@graphql-tools/load";
|
|
41
|
+
import { PrismaLoader } from "@graphql-tools/prisma-loader";
|
|
42
|
+
import { UrlLoader } from "@graphql-tools/url-loader";
|
|
43
|
+
|
|
44
|
+
// utils/hash.ts
|
|
45
|
+
import { getCachedDocumentNodeFromSchema } from "@graphql-codegen/plugin-helpers";
|
|
46
|
+
import { print } from "graphql";
|
|
47
|
+
import { createHash } from "node:crypto";
|
|
48
|
+
function hashContent(content) {
|
|
49
|
+
return createHash("sha256").update(content).digest("hex");
|
|
50
|
+
}
|
|
51
|
+
function hashSchema(schema) {
|
|
52
|
+
return hashContent(print(getCachedDocumentNodeFromSchema(schema)));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// utils/load.ts
|
|
56
|
+
async function loadSchema(schemaPointerMap, cwd) {
|
|
57
|
+
const outputSchemaAst = await loadSchemaToolkit(schemaPointerMap, {
|
|
58
|
+
loaders: [
|
|
59
|
+
new CodeFileLoader(),
|
|
60
|
+
new GitLoader(),
|
|
61
|
+
new GithubLoader(),
|
|
62
|
+
new GraphQLFileLoader(),
|
|
63
|
+
new JsonFileLoader(),
|
|
64
|
+
new UrlLoader(),
|
|
65
|
+
new ApolloEngineLoader(),
|
|
66
|
+
new PrismaLoader()
|
|
67
|
+
],
|
|
68
|
+
cwd,
|
|
69
|
+
includeSources: true
|
|
70
|
+
});
|
|
71
|
+
if (!outputSchemaAst.extensions) {
|
|
72
|
+
outputSchemaAst.extensions = {};
|
|
73
|
+
}
|
|
74
|
+
outputSchemaAst.extensions["hash"] = hashSchema(outputSchemaAst);
|
|
75
|
+
return {
|
|
76
|
+
outputSchemaAst,
|
|
77
|
+
outputSchema: getCachedDocumentNodeFromSchema2(outputSchemaAst)
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// lib/context/index.ts
|
|
82
|
+
var context_exports = {};
|
|
83
|
+
__export(context_exports, {
|
|
84
|
+
default: () => context_default,
|
|
85
|
+
plugin: () => plugin
|
|
86
|
+
});
|
|
87
|
+
import { buildMapperImport, parseMapper } from "@graphql-codegen/visitor-plugin-common";
|
|
88
|
+
var plugin = async (schema, documents, config) => {
|
|
89
|
+
const prepend = [];
|
|
90
|
+
const mapper = parseMapper(config.contextType || "any");
|
|
91
|
+
if (mapper.isExternal && mapper.source) {
|
|
92
|
+
const identifier = mapper.default ? "ContextType" : `${mapper.import} as ContextType`;
|
|
93
|
+
const result = buildMapperImport(
|
|
94
|
+
mapper.source,
|
|
95
|
+
[
|
|
96
|
+
{
|
|
97
|
+
identifier,
|
|
98
|
+
asDefault: mapper.default
|
|
99
|
+
}
|
|
100
|
+
],
|
|
101
|
+
true
|
|
102
|
+
);
|
|
103
|
+
if (result) {
|
|
104
|
+
prepend.push(result);
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
prepend.push(`type ContextType = ${mapper.type}`);
|
|
108
|
+
}
|
|
109
|
+
prepend.push("export type { ContextType }");
|
|
110
|
+
return {
|
|
111
|
+
content: "",
|
|
112
|
+
prepend
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
var context_default = { plugin };
|
|
116
|
+
|
|
117
|
+
// lib/modules/index.ts
|
|
18
118
|
import { BaseVisitor, getConfigValue } from "@graphql-codegen/visitor-plugin-common";
|
|
19
119
|
import { concatAST, isScalarType as isScalarType2 } from "graphql";
|
|
20
120
|
import { join, relative, resolve } from "path";
|
|
21
121
|
|
|
22
|
-
// modules/builder.ts
|
|
122
|
+
// lib/modules/builder.ts
|
|
23
123
|
import { pascalCase } from "change-case-all";
|
|
24
124
|
import {
|
|
25
125
|
isScalarType,
|
|
@@ -27,7 +127,9 @@ import {
|
|
|
27
127
|
visit
|
|
28
128
|
} from "graphql";
|
|
29
129
|
|
|
30
|
-
// modules/utils.ts
|
|
130
|
+
// lib/modules/utils.ts
|
|
131
|
+
import { getBaseType } from "@graphql-codegen/plugin-helpers";
|
|
132
|
+
import { DEFAULT_SCALARS, wrapTypeWithModifiers } from "@graphql-codegen/visitor-plugin-common";
|
|
31
133
|
import {
|
|
32
134
|
Kind
|
|
33
135
|
} from "graphql";
|
|
@@ -85,6 +187,55 @@ function collectUsedTypes(doc) {
|
|
|
85
187
|
}
|
|
86
188
|
return used;
|
|
87
189
|
}
|
|
190
|
+
function collectObjectFieldType(node, fieldDefinition, fieldsMap, fieldTypes) {
|
|
191
|
+
const objectName = node.name.value;
|
|
192
|
+
const fieldName = fieldDefinition.name.value;
|
|
193
|
+
const field = fieldsMap[fieldDefinition.name.value];
|
|
194
|
+
const baseName = getBaseType(field.type).name;
|
|
195
|
+
const isDefaultScalar = DEFAULT_SCALARS[baseName] != null;
|
|
196
|
+
const name = isDefaultScalar ? `Types.Scalars["${baseName}"]` : baseName;
|
|
197
|
+
const type = wrapTypeWithModifiers(name, field.type, {
|
|
198
|
+
wrapOptional: (str) => {
|
|
199
|
+
return `Types.Maybe<${str}>`;
|
|
200
|
+
},
|
|
201
|
+
wrapArray: (str) => {
|
|
202
|
+
return `Array<${str}>`;
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
if (fieldTypes[objectName] == null) {
|
|
206
|
+
fieldTypes[objectName] = {};
|
|
207
|
+
}
|
|
208
|
+
fieldTypes[objectName][fieldName] = type;
|
|
209
|
+
}
|
|
210
|
+
function collectObjectFieldArguments(node, fieldDefinition, fieldArguments) {
|
|
211
|
+
const objectName = node.name.value;
|
|
212
|
+
const fieldName = fieldDefinition.name.value;
|
|
213
|
+
const hasArguments = fieldDefinition.arguments != null && fieldDefinition.arguments.length > 0;
|
|
214
|
+
if (fieldArguments[objectName] == null) {
|
|
215
|
+
fieldArguments[objectName] = {};
|
|
216
|
+
}
|
|
217
|
+
fieldArguments[objectName][fieldName] = hasArguments;
|
|
218
|
+
}
|
|
219
|
+
function collectObjectFieldTypesAndArguments(schema) {
|
|
220
|
+
const fieldTypes = {};
|
|
221
|
+
const fieldArguments = {};
|
|
222
|
+
if (!schema) {
|
|
223
|
+
return { fieldTypes, fieldArguments };
|
|
224
|
+
}
|
|
225
|
+
const schemaTypes = schema.getTypeMap();
|
|
226
|
+
for (const type of Object.values(schemaTypes)) {
|
|
227
|
+
if (type.astNode?.kind !== Kind.OBJECT_TYPE_DEFINITION) {
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
const schemaType = schemaTypes[type.name];
|
|
231
|
+
const fieldsMap = schemaType.getFields();
|
|
232
|
+
for (const field of type.astNode.fields ?? []) {
|
|
233
|
+
collectObjectFieldArguments(type.astNode, field, fieldArguments);
|
|
234
|
+
collectObjectFieldType(type.astNode, field, fieldsMap, fieldTypes);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return { fieldTypes, fieldArguments };
|
|
238
|
+
}
|
|
88
239
|
function resolveTypeNode(node) {
|
|
89
240
|
if (node.kind === Kind.LIST_TYPE) {
|
|
90
241
|
return resolveTypeNode(node.type);
|
|
@@ -178,7 +329,7 @@ function createObject(keys, valueFn) {
|
|
|
178
329
|
return obj;
|
|
179
330
|
}
|
|
180
331
|
|
|
181
|
-
// modules/builder.ts
|
|
332
|
+
// lib/modules/builder.ts
|
|
182
333
|
var registryKeys = [
|
|
183
334
|
"objects",
|
|
184
335
|
"inputs",
|
|
@@ -187,11 +338,6 @@ var registryKeys = [
|
|
|
187
338
|
"unions",
|
|
188
339
|
"enums"
|
|
189
340
|
];
|
|
190
|
-
var resolverKeys = [
|
|
191
|
-
"scalars",
|
|
192
|
-
"objects",
|
|
193
|
-
"enums"
|
|
194
|
-
];
|
|
195
341
|
function buildModule(name, doc, {
|
|
196
342
|
importNamespace,
|
|
197
343
|
importPath,
|
|
@@ -200,7 +346,9 @@ function buildModule(name, doc, {
|
|
|
200
346
|
shouldDeclare,
|
|
201
347
|
rootTypes,
|
|
202
348
|
schema,
|
|
203
|
-
baseVisitor
|
|
349
|
+
baseVisitor,
|
|
350
|
+
fieldTypes,
|
|
351
|
+
fieldArguments
|
|
204
352
|
}) {
|
|
205
353
|
const picks = createObject(
|
|
206
354
|
registryKeys,
|
|
@@ -262,9 +410,6 @@ function buildModule(name, doc, {
|
|
|
262
410
|
printDefinedEnumValues(),
|
|
263
411
|
printDefinedInputFields(),
|
|
264
412
|
printSchemaTypes(usedTypes),
|
|
265
|
-
printScalars(visited),
|
|
266
|
-
printResolveSignaturesPerType(visited),
|
|
267
|
-
printResolversType(visited),
|
|
268
413
|
printMetadata()
|
|
269
414
|
].filter(Boolean).join("\n\n");
|
|
270
415
|
const moduleNamespace = baseVisitor.convertName(name, {
|
|
@@ -312,23 +457,32 @@ export const ${getModuleFn} = Baeta.createSingletonModule(${createModuleFn});
|
|
|
312
457
|
`;
|
|
313
458
|
}
|
|
314
459
|
function printObjectFieldResolverBuilder(typeName, field) {
|
|
315
|
-
const
|
|
316
|
-
|
|
460
|
+
const parentType = getParentType(typeName);
|
|
461
|
+
const resultType = getResultType(typeName, field);
|
|
462
|
+
const argumentsType = getArgsType(typeName, field);
|
|
463
|
+
const contextType = getContextType();
|
|
464
|
+
return `${field}: module.createResolverBuilder<${resultType}, ${parentType}, ${contextType}, ${argumentsType}>("${typeName}", "${field}"),`;
|
|
317
465
|
}
|
|
318
466
|
function printObjectResolverBuilder(typeName, objects) {
|
|
319
467
|
const fields = objects[typeName]?.filter(unique).map((field) => printObjectFieldResolverBuilder(typeName, field)) ?? [];
|
|
320
468
|
if (fields.length === 0) {
|
|
321
469
|
return "";
|
|
322
470
|
}
|
|
323
|
-
const
|
|
471
|
+
const parentType = getParentType(typeName);
|
|
472
|
+
const contextType = getContextType();
|
|
473
|
+
const addons = [`...module.createTypeMethods<${parentType}, ${contextType}>("${typeName}"),`];
|
|
474
|
+
const contentBody = [...addons, ...fields].map(indent(2)).join("\n");
|
|
324
475
|
const content2 = `{
|
|
325
|
-
${
|
|
476
|
+
${contentBody}
|
|
326
477
|
}`;
|
|
327
|
-
return `${typeName}:
|
|
478
|
+
return `${typeName}: ${content2},`;
|
|
328
479
|
}
|
|
329
480
|
function printSubscriptionFieldBuilder(field) {
|
|
330
|
-
const
|
|
331
|
-
|
|
481
|
+
const parentType = getParentType("Subscription");
|
|
482
|
+
const resultType = getResultType("Subscription", field);
|
|
483
|
+
const argumentsType = getArgsType("Subscription", field);
|
|
484
|
+
const contextType = getContextType();
|
|
485
|
+
return `${field}: module.createSubscriptionBuilder<${resultType}, ${parentType}, ${contextType}, ${argumentsType}>("${field}"),`;
|
|
332
486
|
}
|
|
333
487
|
function printSubscriptionObjectBuilder() {
|
|
334
488
|
const subscriptions = picks.objects["Subscription"]?.filter(unique) ?? [];
|
|
@@ -336,20 +490,21 @@ ${fields.map(indent(2)).join("\n")}
|
|
|
336
490
|
return "";
|
|
337
491
|
}
|
|
338
492
|
const fields = subscriptions.map((subscription) => printSubscriptionFieldBuilder(subscription));
|
|
339
|
-
const
|
|
493
|
+
const parentType = getParentType("Subscription");
|
|
494
|
+
const contextType = getContextType();
|
|
495
|
+
const addons = [`...module.createSubscriptionMethods<${parentType}, ${contextType}>(),`];
|
|
496
|
+
const contentBody = [...addons, ...fields].map(indent(2)).join("\n");
|
|
340
497
|
const content2 = `{
|
|
341
|
-
${
|
|
498
|
+
${contentBody}
|
|
342
499
|
}`;
|
|
343
|
-
return `Subscription:
|
|
500
|
+
return `Subscription: ${content2},`;
|
|
344
501
|
}
|
|
345
502
|
function printScalarBuilder() {
|
|
346
503
|
const scalars = visited.scalars;
|
|
347
504
|
if (scalars.length === 0) {
|
|
348
505
|
return "";
|
|
349
506
|
}
|
|
350
|
-
const fields = scalars.map(
|
|
351
|
-
(scalar) => `${scalar}: Baeta.createScalarBuilder(module, "${scalar}"),`
|
|
352
|
-
);
|
|
507
|
+
const fields = scalars.map((scalar) => `${scalar}: module.createScalarBuilder("${scalar}"),`);
|
|
353
508
|
const content2 = fields.map(indent(2)).join("\n");
|
|
354
509
|
return `Scalar: {
|
|
355
510
|
${content2}
|
|
@@ -357,14 +512,21 @@ ${content2}
|
|
|
357
512
|
}
|
|
358
513
|
function printBaetaManager() {
|
|
359
514
|
const objects = visited.objects.filter((type) => type !== "Subscription").map((typeName) => printObjectResolverBuilder(typeName, picks.objects)).filter(Boolean);
|
|
360
|
-
const
|
|
515
|
+
const contextType = getContextType();
|
|
516
|
+
const addons = [`...module.createModuleMethods<${contextType}>(),`];
|
|
517
|
+
const bodyFields = [
|
|
518
|
+
...addons,
|
|
519
|
+
...objects,
|
|
520
|
+
printScalarBuilder(),
|
|
521
|
+
printSubscriptionObjectBuilder()
|
|
522
|
+
];
|
|
361
523
|
const body = bodyFields.filter(Boolean).map(indent(6)).join("\n");
|
|
362
524
|
const content2 = `{
|
|
363
525
|
${body}
|
|
364
526
|
}`;
|
|
365
527
|
return `
|
|
366
528
|
export function createManager(module: Baeta.ModuleBuilder) {
|
|
367
|
-
return
|
|
529
|
+
return ${content2};
|
|
368
530
|
}`;
|
|
369
531
|
}
|
|
370
532
|
function printDefinedEnumValues() {
|
|
@@ -390,72 +552,6 @@ ${body}
|
|
|
390
552
|
function printSchemaTypes(types) {
|
|
391
553
|
return types.filter((type) => !visited.scalars.includes(type)).map(printExportType).join("\n");
|
|
392
554
|
}
|
|
393
|
-
function printResolveSignaturesPerType(registry) {
|
|
394
|
-
return [
|
|
395
|
-
[...registry.objects, ...registry.interfaces].map(
|
|
396
|
-
(name2) => printResolverType(
|
|
397
|
-
name2,
|
|
398
|
-
"DefinedFields",
|
|
399
|
-
requireRootResolvers && rootTypes.includes(name2),
|
|
400
|
-
!rootTypes.includes(name2) && defined.objects.includes(name2) ? ` | '__isTypeOf'` : ""
|
|
401
|
-
)
|
|
402
|
-
).join("\n")
|
|
403
|
-
].join("\n");
|
|
404
|
-
}
|
|
405
|
-
function printScalars(registry) {
|
|
406
|
-
if (!registry.scalars.length) {
|
|
407
|
-
return "";
|
|
408
|
-
}
|
|
409
|
-
return [
|
|
410
|
-
`export type ${encapsulateTypeName(
|
|
411
|
-
"Scalars"
|
|
412
|
-
)} = Pick<${importNamespace}.Scalars, ${registry.scalars.map(withQuotes).join(" | ")}>;`,
|
|
413
|
-
...registry.scalars.map((scalar) => {
|
|
414
|
-
const convertedName = baseVisitor.convertName(scalar, {
|
|
415
|
-
suffix: "ScalarConfig"
|
|
416
|
-
});
|
|
417
|
-
return `export type ${encapsulateTypeName(
|
|
418
|
-
convertedName
|
|
419
|
-
)} = ${importNamespace}.${convertedName};`;
|
|
420
|
-
})
|
|
421
|
-
].join("\n");
|
|
422
|
-
}
|
|
423
|
-
function printResolversType(registry) {
|
|
424
|
-
const lines = [];
|
|
425
|
-
for (const kind in registry) {
|
|
426
|
-
if (!Object.prototype.hasOwnProperty.call(registry, kind)) {
|
|
427
|
-
continue;
|
|
428
|
-
}
|
|
429
|
-
if (!resolverKeys.includes(kind)) {
|
|
430
|
-
continue;
|
|
431
|
-
}
|
|
432
|
-
const k = kind;
|
|
433
|
-
const types = registry[k];
|
|
434
|
-
types.forEach((typeName) => {
|
|
435
|
-
if (k === "enums") {
|
|
436
|
-
return;
|
|
437
|
-
}
|
|
438
|
-
if (k === "scalars") {
|
|
439
|
-
lines.push(
|
|
440
|
-
`${typeName}?: ${encapsulateTypeName(importNamespace)}.Resolvers['${typeName}'];`
|
|
441
|
-
);
|
|
442
|
-
} else {
|
|
443
|
-
const fieldModifier = requireRootResolvers && rootTypes.includes(typeName) ? "" : "?";
|
|
444
|
-
lines.push(`${typeName}${fieldModifier}: ${encapsulateTypeName(typeName)}Resolvers;`);
|
|
445
|
-
}
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
return buildBlock({
|
|
449
|
-
name: `export interface ${encapsulateTypeName("Resolvers")}`,
|
|
450
|
-
lines
|
|
451
|
-
});
|
|
452
|
-
}
|
|
453
|
-
function printResolverType(typeName, picksTypeName, requireFieldsResolvers = false, extraKeys = "") {
|
|
454
|
-
const typeSignature = `Pick<${importNamespace}.${baseVisitor.convertName(typeName, {
|
|
455
|
-
suffix: "Resolvers"
|
|
456
|
-
})}, ${picksTypeName}['${typeName}']${extraKeys}>`;
|
|
457
|
-
return `export type ${encapsulateTypeName(`${typeName}Resolvers`)} = ${requireFieldsResolvers ? `Required<${typeSignature}>` : typeSignature};`;
|
|
458
|
-
}
|
|
459
555
|
function printPicks(typeName, records) {
|
|
460
556
|
return records[typeName].filter(unique).map(withQuotes).join(" | ");
|
|
461
557
|
}
|
|
@@ -575,9 +671,29 @@ ${body}
|
|
|
575
671
|
}
|
|
576
672
|
}
|
|
577
673
|
}
|
|
674
|
+
function getParentType(type) {
|
|
675
|
+
if (["Query", "Mutation", "Subscription"].includes(type)) {
|
|
676
|
+
return "{}";
|
|
677
|
+
}
|
|
678
|
+
return type;
|
|
679
|
+
}
|
|
680
|
+
function getResultType(type, field) {
|
|
681
|
+
return fieldTypes?.[type]?.[field] || "{}";
|
|
682
|
+
}
|
|
683
|
+
function getArgsType(type, field) {
|
|
684
|
+
const hasArgs = fieldArguments?.[type]?.[field] ?? false;
|
|
685
|
+
if (!hasArgs) {
|
|
686
|
+
return "{}";
|
|
687
|
+
}
|
|
688
|
+
const fieldUpper = field[0].toUpperCase() + field.slice(1);
|
|
689
|
+
return `Types.${type}${fieldUpper}Args`;
|
|
690
|
+
}
|
|
691
|
+
function getContextType() {
|
|
692
|
+
return "Types.ContextType";
|
|
693
|
+
}
|
|
578
694
|
}
|
|
579
695
|
|
|
580
|
-
// modules/index.ts
|
|
696
|
+
// lib/modules/index.ts
|
|
581
697
|
var preset = {
|
|
582
698
|
buildGeneratesSection: (options) => {
|
|
583
699
|
const { baseOutputDir } = options;
|
|
@@ -598,6 +714,7 @@ var preset = {
|
|
|
598
714
|
const sourcesByModuleMap = groupSourcesByModule(sources, baseOutputDir);
|
|
599
715
|
const modules = Object.keys(sourcesByModuleMap);
|
|
600
716
|
const baseVisitor = new BaseVisitor(options.config, {});
|
|
717
|
+
const { fieldTypes, fieldArguments } = collectObjectFieldTypesAndArguments(options.schemaAst);
|
|
601
718
|
const baseOutput = {
|
|
602
719
|
filename: resolve(cwd, baseOutputDir, baseTypesPath),
|
|
603
720
|
schema: options.schema,
|
|
@@ -662,6 +779,8 @@ var preset = {
|
|
|
662
779
|
schema,
|
|
663
780
|
baseVisitor,
|
|
664
781
|
useGraphQLModules: false,
|
|
782
|
+
fieldTypes,
|
|
783
|
+
fieldArguments,
|
|
665
784
|
rootTypes: [
|
|
666
785
|
schema.getQueryType()?.name || "",
|
|
667
786
|
schema.getMutationType()?.name || "",
|
|
@@ -678,439 +797,6 @@ var preset = {
|
|
|
678
797
|
}
|
|
679
798
|
};
|
|
680
799
|
|
|
681
|
-
// resolvers/index.ts
|
|
682
|
-
var resolvers_exports = {};
|
|
683
|
-
__export(resolvers_exports, {
|
|
684
|
-
plugin: () => plugin
|
|
685
|
-
});
|
|
686
|
-
import {
|
|
687
|
-
addFederationReferencesToSchema,
|
|
688
|
-
getCachedDocumentNodeFromSchema,
|
|
689
|
-
oldVisit
|
|
690
|
-
} from "@graphql-codegen/plugin-helpers";
|
|
691
|
-
import { parseMapper } from "@graphql-codegen/visitor-plugin-common";
|
|
692
|
-
|
|
693
|
-
// resolvers/visitor.ts
|
|
694
|
-
import { TypeScriptOperationVariablesToObject } from "@graphql-codegen/typescript";
|
|
695
|
-
import {
|
|
696
|
-
BaseResolversVisitor,
|
|
697
|
-
getConfigValue as getConfigValue2
|
|
698
|
-
} from "@graphql-codegen/visitor-plugin-common";
|
|
699
|
-
import autoBind from "auto-bind";
|
|
700
|
-
var ENUM_RESOLVERS_SIGNATURE = "export type EnumResolverSignature<T, AllowedValues = any> = { [key in keyof T]?: AllowedValues };";
|
|
701
|
-
var TypeScriptResolversVisitor = class extends BaseResolversVisitor {
|
|
702
|
-
constructor(pluginConfig, schema) {
|
|
703
|
-
super(
|
|
704
|
-
pluginConfig,
|
|
705
|
-
{
|
|
706
|
-
avoidOptionals: getConfigValue2(pluginConfig.avoidOptionals, false),
|
|
707
|
-
useIndexSignature: getConfigValue2(pluginConfig.useIndexSignature, false),
|
|
708
|
-
wrapFieldDefinitions: getConfigValue2(pluginConfig.wrapFieldDefinitions, false),
|
|
709
|
-
allowParentTypeOverride: getConfigValue2(pluginConfig.allowParentTypeOverride, false),
|
|
710
|
-
optionalInfoArgument: getConfigValue2(pluginConfig.optionalInfoArgument, false)
|
|
711
|
-
},
|
|
712
|
-
schema
|
|
713
|
-
);
|
|
714
|
-
autoBind(this);
|
|
715
|
-
this.setVariablesTransformer(
|
|
716
|
-
new TypeScriptOperationVariablesToObject(
|
|
717
|
-
this.scalars,
|
|
718
|
-
this.convertName,
|
|
719
|
-
this.config.avoidOptionals,
|
|
720
|
-
this.config.immutableTypes,
|
|
721
|
-
this.config.namespacedImportName,
|
|
722
|
-
[],
|
|
723
|
-
this.config.enumPrefix,
|
|
724
|
-
this.config.enumValues
|
|
725
|
-
)
|
|
726
|
-
);
|
|
727
|
-
if (this.config.useIndexSignature) {
|
|
728
|
-
this._declarationBlockConfig = {
|
|
729
|
-
blockTransformer(block) {
|
|
730
|
-
return `ResolversObject<${block}>`;
|
|
731
|
-
}
|
|
732
|
-
};
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
transformParentGenericType(parentType) {
|
|
736
|
-
if (this.config.allowParentTypeOverride) {
|
|
737
|
-
return `ParentType = ${parentType}`;
|
|
738
|
-
}
|
|
739
|
-
return `ParentType extends ${parentType} = ${parentType}`;
|
|
740
|
-
}
|
|
741
|
-
formatRootResolver(schemaTypeName, resolverType, declarationKind) {
|
|
742
|
-
const avoidOptionals = this.config.avoidOptionals?.resolvers ?? this.config.avoidOptionals === true;
|
|
743
|
-
return `${schemaTypeName}${avoidOptionals ? "" : "?"}: ${resolverType}${this.getPunctuation(
|
|
744
|
-
declarationKind
|
|
745
|
-
)}`;
|
|
746
|
-
}
|
|
747
|
-
clearOptional(str) {
|
|
748
|
-
if (str.startsWith("Maybe")) {
|
|
749
|
-
return str.replace(/Maybe<(.*?)>$/, "$1");
|
|
750
|
-
}
|
|
751
|
-
return str;
|
|
752
|
-
}
|
|
753
|
-
ListType(node) {
|
|
754
|
-
return `Maybe<${super.ListType(node)}>`;
|
|
755
|
-
}
|
|
756
|
-
wrapWithListType(str) {
|
|
757
|
-
return `${this.config.immutableTypes ? "ReadonlyArray" : "Array"}<${str}>`;
|
|
758
|
-
}
|
|
759
|
-
getParentTypeForSignature(node) {
|
|
760
|
-
if (this._federation.isResolveReferenceField(node) && this.config.wrapFieldDefinitions) {
|
|
761
|
-
return "UnwrappedObject<ParentType>";
|
|
762
|
-
}
|
|
763
|
-
return "ParentType";
|
|
764
|
-
}
|
|
765
|
-
NamedType(node) {
|
|
766
|
-
return `Maybe<${super.NamedType(node)}>`;
|
|
767
|
-
}
|
|
768
|
-
NonNullType(node) {
|
|
769
|
-
const baseValue = super.NonNullType(node);
|
|
770
|
-
return this.clearOptional(baseValue);
|
|
771
|
-
}
|
|
772
|
-
getPunctuation(_declarationKind) {
|
|
773
|
-
return ";";
|
|
774
|
-
}
|
|
775
|
-
buildEnumResolverContentBlock(node, mappedEnumType) {
|
|
776
|
-
const valuesMap = `{ ${(node.values || []).map((v) => `${v.name}${this.config.avoidOptionals ? "" : "?"}: any`).join(", ")} }`;
|
|
777
|
-
this._globalDeclarations.add(ENUM_RESOLVERS_SIGNATURE);
|
|
778
|
-
return `EnumResolverSignature<${valuesMap}, ${mappedEnumType}>`;
|
|
779
|
-
}
|
|
780
|
-
buildEnumResolversExplicitMappedValues(node, valuesMapping) {
|
|
781
|
-
return `{ ${(node.values || []).map((v) => {
|
|
782
|
-
const valueName = v.name;
|
|
783
|
-
const mappedValue = valuesMapping[valueName];
|
|
784
|
-
return `${valueName}: ${typeof mappedValue === "number" ? mappedValue : `'${mappedValue}'`}`;
|
|
785
|
-
}).join(", ")} }`;
|
|
786
|
-
}
|
|
787
|
-
};
|
|
788
|
-
|
|
789
|
-
// resolvers/index.ts
|
|
790
|
-
var capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
791
|
-
var plugin = (schema, documents, config) => {
|
|
792
|
-
const imports = [];
|
|
793
|
-
if (!config.customResolveInfo) {
|
|
794
|
-
imports.push("GraphQLResolveInfo");
|
|
795
|
-
}
|
|
796
|
-
const showUnusedMappers = typeof config.showUnusedMappers === "boolean" ? config.showUnusedMappers : true;
|
|
797
|
-
const noSchemaStitching = typeof config.noSchemaStitching === "boolean" ? config.noSchemaStitching : true;
|
|
798
|
-
const indexSignature = config.useIndexSignature ? [
|
|
799
|
-
"export type WithIndex<TObject> = TObject & Record<string, any>;",
|
|
800
|
-
"export type ResolversObject<TObject> = WithIndex<TObject>;"
|
|
801
|
-
].join("\n") : "";
|
|
802
|
-
const importType = config.useTypeImports ? "import type" : "import";
|
|
803
|
-
const prepend = [];
|
|
804
|
-
const defsToInclude = [];
|
|
805
|
-
const directiveResolverMappings = {};
|
|
806
|
-
if (config.directiveResolverMappings) {
|
|
807
|
-
for (const [directiveName, mapper] of Object.entries(config.directiveResolverMappings)) {
|
|
808
|
-
const parsedMapper = parseMapper(mapper);
|
|
809
|
-
const capitalizedDirectiveName = capitalize(directiveName);
|
|
810
|
-
const resolverFnName = `ResolverFn${capitalizedDirectiveName}`;
|
|
811
|
-
const resolverFnUsage2 = `${resolverFnName}<TResult, TParent, TContext, TArgs>`;
|
|
812
|
-
const resolverWithResolveUsage2 = `Resolver${capitalizedDirectiveName}WithResolve<TResult, TParent, TContext, TArgs>`;
|
|
813
|
-
const resolverWithResolve2 = `
|
|
814
|
-
export type Resolver${capitalizedDirectiveName}WithResolve<TResult, TParent, TContext, TArgs> = {
|
|
815
|
-
resolve: ${resolverFnName}<TResult, TParent, TContext, TArgs>;
|
|
816
|
-
};`;
|
|
817
|
-
const resolverTypeName = `Resolver${capitalizedDirectiveName}`;
|
|
818
|
-
const resolverType2 = `export type ${resolverTypeName}<TResult, TParent = {}, TContext = {}, TArgs = {}> =`;
|
|
819
|
-
if (parsedMapper.isExternal) {
|
|
820
|
-
if (parsedMapper.default) {
|
|
821
|
-
prepend.push(`${importType} ${resolverFnName} from '${parsedMapper.source}';`);
|
|
822
|
-
} else {
|
|
823
|
-
prepend.push(
|
|
824
|
-
`${importType} { ${parsedMapper.import} ${parsedMapper.import !== resolverFnName ? `as ${resolverFnName} ` : ""}} from '${parsedMapper.source}';`
|
|
825
|
-
);
|
|
826
|
-
}
|
|
827
|
-
prepend.push(`export${config.useTypeImports ? " type" : ""} { ${resolverFnName} };`);
|
|
828
|
-
} else {
|
|
829
|
-
defsToInclude.push(
|
|
830
|
-
`export type ${resolverFnName}<TResult, TParent, TContext, TArgs> = ${parsedMapper.type}`
|
|
831
|
-
);
|
|
832
|
-
}
|
|
833
|
-
if (config.makeResolverTypeCallable) {
|
|
834
|
-
defsToInclude.push(`${resolverType2} ${resolverFnUsage2};`);
|
|
835
|
-
} else {
|
|
836
|
-
defsToInclude.push(resolverWithResolve2);
|
|
837
|
-
defsToInclude.push(`${resolverType2} ${resolverFnUsage2} | ${resolverWithResolveUsage2};`);
|
|
838
|
-
}
|
|
839
|
-
directiveResolverMappings[directiveName] = resolverTypeName;
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
const transformedSchema = config.federation ? addFederationReferencesToSchema(schema) : schema;
|
|
843
|
-
const visitor = new TypeScriptResolversVisitor(
|
|
844
|
-
{ ...config, directiveResolverMappings },
|
|
845
|
-
transformedSchema
|
|
846
|
-
);
|
|
847
|
-
const namespacedImportPrefix = visitor.config.namespacedImportName ? `${visitor.config.namespacedImportName}.` : "";
|
|
848
|
-
const astNode = getCachedDocumentNodeFromSchema(transformedSchema);
|
|
849
|
-
const visitorResult = oldVisit(astNode, { leave: visitor });
|
|
850
|
-
const optionalSignForInfoArg = visitor.config.optionalInfoArgument ? "?" : "";
|
|
851
|
-
const legacyStitchingResolverType = `
|
|
852
|
-
export type LegacyStitchingResolver<TResult, TParent, TContext, TArgs> = {
|
|
853
|
-
fragment: string;
|
|
854
|
-
resolve: ResolverFn<TResult, TParent, TContext, TArgs>;
|
|
855
|
-
};`;
|
|
856
|
-
const newStitchingResolverType = `
|
|
857
|
-
export type NewStitchingResolver<TResult, TParent, TContext, TArgs> = {
|
|
858
|
-
selectionSet: string | ((fieldNode: FieldNode) => SelectionSetNode);
|
|
859
|
-
resolve: ResolverFn<TResult, TParent, TContext, TArgs>;
|
|
860
|
-
};`;
|
|
861
|
-
const stitchingResolverType = "export type StitchingResolver<TResult, TParent, TContext, TArgs> = LegacyStitchingResolver<TResult, TParent, TContext, TArgs> | NewStitchingResolver<TResult, TParent, TContext, TArgs>;";
|
|
862
|
-
const resolverWithResolve = `
|
|
863
|
-
export type ResolverWithResolve<TResult, TParent, TContext, TArgs> = {
|
|
864
|
-
resolve: ResolverFn<TResult, TParent, TContext, TArgs>;
|
|
865
|
-
};`;
|
|
866
|
-
const resolverType = "export type Resolver<TResult, TParent = {}, TContext = {}, TArgs = {}> =";
|
|
867
|
-
const resolverFnUsage = "ResolverFn<TResult, TParent, TContext, TArgs>";
|
|
868
|
-
const resolverWithResolveUsage = "ResolverWithResolve<TResult, TParent, TContext, TArgs>";
|
|
869
|
-
const stitchingResolverUsage = "StitchingResolver<TResult, TParent, TContext, TArgs>";
|
|
870
|
-
if (visitor.hasFederation()) {
|
|
871
|
-
if (visitor.config.wrapFieldDefinitions) {
|
|
872
|
-
defsToInclude.push(`export type UnwrappedObject<T> = {
|
|
873
|
-
[P in keyof T]: T[P] extends infer R | Promise<infer R> | (() => infer R2 | Promise<infer R2>)
|
|
874
|
-
? R & R2 : T[P]
|
|
875
|
-
};`);
|
|
876
|
-
}
|
|
877
|
-
defsToInclude.push(`export type ReferenceResolver<TResult, TReference, TContext> = (
|
|
878
|
-
reference: TReference,
|
|
879
|
-
context: TContext,
|
|
880
|
-
info${optionalSignForInfoArg}: GraphQLResolveInfo
|
|
881
|
-
) => Promise<TResult> | TResult;`);
|
|
882
|
-
defsToInclude.push(`
|
|
883
|
-
type ScalarCheck<T, S> = S extends true ? T : NullableCheck<T, S>;
|
|
884
|
-
type NullableCheck<T, S> = ${namespacedImportPrefix}Maybe<T> extends T ? ${namespacedImportPrefix}Maybe<ListCheck<NonNullable<T>, S>> : ListCheck<T, S>;
|
|
885
|
-
type ListCheck<T, S> = T extends (infer U)[] ? NullableCheck<U, S>[] : GraphQLRecursivePick<T, S>;
|
|
886
|
-
export type GraphQLRecursivePick<T, S> = { [K in keyof T & keyof S]: ScalarCheck<T[K], S[K]> };
|
|
887
|
-
`);
|
|
888
|
-
}
|
|
889
|
-
if (!config.makeResolverTypeCallable) {
|
|
890
|
-
defsToInclude.push(resolverWithResolve);
|
|
891
|
-
}
|
|
892
|
-
if (noSchemaStitching) {
|
|
893
|
-
const defs = config.makeResolverTypeCallable ? `${resolverType} ${resolverFnUsage};` : `${resolverType} ${resolverFnUsage} | ${resolverWithResolveUsage};`;
|
|
894
|
-
defsToInclude.push(defs);
|
|
895
|
-
} else {
|
|
896
|
-
defsToInclude.push(
|
|
897
|
-
[
|
|
898
|
-
legacyStitchingResolverType,
|
|
899
|
-
newStitchingResolverType,
|
|
900
|
-
stitchingResolverType,
|
|
901
|
-
resolverType,
|
|
902
|
-
` | ${resolverFnUsage}`,
|
|
903
|
-
config.makeResolverTypeCallable ? "" : ` | ${resolverWithResolveUsage}`,
|
|
904
|
-
` | ${stitchingResolverUsage};`
|
|
905
|
-
].join("\n")
|
|
906
|
-
);
|
|
907
|
-
imports.push("SelectionSetNode", "FieldNode");
|
|
908
|
-
}
|
|
909
|
-
if (config.customResolverFn) {
|
|
910
|
-
const parsedMapper = parseMapper(config.customResolverFn);
|
|
911
|
-
if (parsedMapper.isExternal) {
|
|
912
|
-
if (parsedMapper.default) {
|
|
913
|
-
prepend.push(`${importType} ResolverFn from '${parsedMapper.source}';`);
|
|
914
|
-
} else {
|
|
915
|
-
prepend.push(
|
|
916
|
-
`${importType} { ${parsedMapper.import} ${parsedMapper.import !== "ResolverFn" ? "as ResolverFn " : ""}} from '${parsedMapper.source}';`
|
|
917
|
-
);
|
|
918
|
-
}
|
|
919
|
-
prepend.push(`export${config.useTypeImports ? " type" : ""} { ResolverFn };`);
|
|
920
|
-
} else {
|
|
921
|
-
prepend.push(
|
|
922
|
-
`export type ResolverFn<TResult, TParent, TContext, TArgs> = ${parsedMapper.type}`
|
|
923
|
-
);
|
|
924
|
-
}
|
|
925
|
-
} else {
|
|
926
|
-
const defaultResolverFn = `
|
|
927
|
-
export type ResolverFn<TResult, TParent, TContext, TArgs> = (
|
|
928
|
-
parent: TParent,
|
|
929
|
-
args: TArgs,
|
|
930
|
-
context: TContext,
|
|
931
|
-
info${optionalSignForInfoArg}: GraphQLResolveInfo
|
|
932
|
-
) => Promise<TResult> | TResult;`;
|
|
933
|
-
defsToInclude.push(defaultResolverFn);
|
|
934
|
-
}
|
|
935
|
-
if (config.customSubscriptionResolver) {
|
|
936
|
-
const parsedMapper = parseMapper(config.customSubscriptionResolver);
|
|
937
|
-
if (parsedMapper.isExternal) {
|
|
938
|
-
if (parsedMapper.default) {
|
|
939
|
-
prepend.push(`${importType} SubscriptionResolver from '${parsedMapper.source}';`);
|
|
940
|
-
} else {
|
|
941
|
-
prepend.push(
|
|
942
|
-
`${importType} { ${parsedMapper.import} ${parsedMapper.import !== "SubscriptionResolver" ? "as SubscriptionResolver " : ""}} from '${parsedMapper.source}';`
|
|
943
|
-
);
|
|
944
|
-
}
|
|
945
|
-
prepend.push(`export${config.useTypeImports ? " type" : ""} { SubscriptionResolver };`);
|
|
946
|
-
} else {
|
|
947
|
-
prepend.push(
|
|
948
|
-
`export type SubscriptionResolver<TResult, TKey extends string, TParent = {}, TContext = {}, TArgs = {}> = ${parsedMapper.type}`
|
|
949
|
-
);
|
|
950
|
-
}
|
|
951
|
-
} else {
|
|
952
|
-
const defaultSubscriptionDef = `
|
|
953
|
-
export type SubscriptionSubscribeFn<TResult, TParent, TContext, TArgs> = (
|
|
954
|
-
parent: TParent,
|
|
955
|
-
args: TArgs,
|
|
956
|
-
context: TContext,
|
|
957
|
-
info${optionalSignForInfoArg}: GraphQLResolveInfo
|
|
958
|
-
) => AsyncIterable<TResult> | Promise<AsyncIterable<TResult>>;
|
|
959
|
-
|
|
960
|
-
export type SubscriptionResolveFn<TResult, TParent, TContext, TArgs> = (
|
|
961
|
-
parent: TParent,
|
|
962
|
-
args: TArgs,
|
|
963
|
-
context: TContext,
|
|
964
|
-
info${optionalSignForInfoArg}: GraphQLResolveInfo
|
|
965
|
-
) => TResult | Promise<TResult>;
|
|
966
|
-
|
|
967
|
-
export interface SubscriptionSubscriberObject<TResult, TKey extends string, TParent, TContext, TArgs> {
|
|
968
|
-
subscribe: SubscriptionSubscribeFn<{ [key in TKey]: TResult }, TParent, TContext, TArgs>;
|
|
969
|
-
resolve?: SubscriptionResolveFn<TResult, { [key in TKey]: TResult }, TContext, TArgs>;
|
|
970
|
-
}
|
|
971
|
-
|
|
972
|
-
export interface SubscriptionResolverObject<TResult, TParent, TContext, TArgs> {
|
|
973
|
-
subscribe: SubscriptionSubscribeFn<any, TParent, TContext, TArgs>;
|
|
974
|
-
resolve: SubscriptionResolveFn<TResult, any, TContext, TArgs>;
|
|
975
|
-
}
|
|
976
|
-
|
|
977
|
-
export type SubscriptionObject<TResult, TKey extends string, TParent, TContext, TArgs> =
|
|
978
|
-
| SubscriptionSubscriberObject<TResult, TKey, TParent, TContext, TArgs>
|
|
979
|
-
| SubscriptionResolverObject<TResult, TParent, TContext, TArgs>;
|
|
980
|
-
|
|
981
|
-
export type SubscriptionResolver<TResult, TKey extends string, TParent = {}, TContext = {}, TArgs = {}> =
|
|
982
|
-
| ((...args: any[]) => SubscriptionObject<TResult, TKey, TParent, TContext, TArgs>)
|
|
983
|
-
| SubscriptionObject<TResult, TKey, TParent, TContext, TArgs>;
|
|
984
|
-
`;
|
|
985
|
-
defsToInclude.push(defaultSubscriptionDef);
|
|
986
|
-
}
|
|
987
|
-
const header = `${indexSignature}
|
|
988
|
-
|
|
989
|
-
${visitor.getResolverTypeWrapperSignature()}
|
|
990
|
-
|
|
991
|
-
${defsToInclude.join("\n")}
|
|
992
|
-
|
|
993
|
-
export type TypeResolveFn<TTypes, TParent = {}, TContext = {}> = (
|
|
994
|
-
parent: TParent,
|
|
995
|
-
context: TContext,
|
|
996
|
-
info${optionalSignForInfoArg}: GraphQLResolveInfo
|
|
997
|
-
) => ${namespacedImportPrefix}Maybe<TTypes> | Promise<${namespacedImportPrefix}Maybe<TTypes>>;
|
|
998
|
-
|
|
999
|
-
export type IsTypeOfResolverFn<T = {}, TContext = {}> = (obj: T, context: TContext, info${optionalSignForInfoArg}: GraphQLResolveInfo) => boolean | Promise<boolean>;
|
|
1000
|
-
|
|
1001
|
-
export type NextResolverFn<T> = () => Promise<T>;
|
|
1002
|
-
|
|
1003
|
-
export type DirectiveResolverFn<TResult = {}, TParent = {}, TContext = {}, TArgs = {}> = (
|
|
1004
|
-
next: NextResolverFn<TResult>,
|
|
1005
|
-
parent: TParent,
|
|
1006
|
-
args: TArgs,
|
|
1007
|
-
context: TContext,
|
|
1008
|
-
info${optionalSignForInfoArg}: GraphQLResolveInfo
|
|
1009
|
-
) => TResult | Promise<TResult>;
|
|
1010
|
-
`;
|
|
1011
|
-
const resolversTypeMapping = visitor.buildResolversTypes();
|
|
1012
|
-
const resolversParentTypeMapping = visitor.buildResolversParentTypes();
|
|
1013
|
-
const { getRootResolver, getAllDirectiveResolvers, mappersImports, unusedMappers, hasScalars } = visitor;
|
|
1014
|
-
if (hasScalars()) {
|
|
1015
|
-
imports.push("GraphQLScalarType", "GraphQLScalarTypeConfig");
|
|
1016
|
-
}
|
|
1017
|
-
if (showUnusedMappers && unusedMappers.length) {
|
|
1018
|
-
console.warn(`Unused mappers: ${unusedMappers.join(",")}`);
|
|
1019
|
-
}
|
|
1020
|
-
if (imports.length) {
|
|
1021
|
-
prepend.push(`${importType} { ${imports.join(", ")} } from 'graphql';`);
|
|
1022
|
-
}
|
|
1023
|
-
if (config.customResolveInfo) {
|
|
1024
|
-
const parsedMapper = parseMapper(config.customResolveInfo);
|
|
1025
|
-
if (parsedMapper.isExternal) {
|
|
1026
|
-
if (parsedMapper.default) {
|
|
1027
|
-
prepend.push(`import GraphQLResolveInfo from '${parsedMapper.source}'`);
|
|
1028
|
-
}
|
|
1029
|
-
prepend.push(
|
|
1030
|
-
`import { ${parsedMapper.import} ${parsedMapper.import !== "GraphQLResolveInfo" ? "as GraphQLResolveInfo" : ""} } from '${parsedMapper.source}';`
|
|
1031
|
-
);
|
|
1032
|
-
} else {
|
|
1033
|
-
prepend.push(`type GraphQLResolveInfo = ${parsedMapper.type}`);
|
|
1034
|
-
}
|
|
1035
|
-
}
|
|
1036
|
-
prepend.push(...mappersImports, ...visitor.globalDeclarations);
|
|
1037
|
-
return {
|
|
1038
|
-
prepend,
|
|
1039
|
-
content: [
|
|
1040
|
-
header,
|
|
1041
|
-
resolversTypeMapping,
|
|
1042
|
-
resolversParentTypeMapping,
|
|
1043
|
-
...visitorResult.definitions.filter((d) => typeof d === "string"),
|
|
1044
|
-
getRootResolver(),
|
|
1045
|
-
getAllDirectiveResolvers()
|
|
1046
|
-
].join("\n")
|
|
1047
|
-
};
|
|
1048
|
-
};
|
|
1049
|
-
|
|
1050
|
-
// utils/cache.ts
|
|
1051
|
-
function createCache() {
|
|
1052
|
-
const cache = /* @__PURE__ */ new Map();
|
|
1053
|
-
return function ensure(namespace, key, factory) {
|
|
1054
|
-
const cacheKey = `${namespace}:${key}`;
|
|
1055
|
-
const cachedValue = cache.get(cacheKey);
|
|
1056
|
-
if (cachedValue) {
|
|
1057
|
-
return cachedValue;
|
|
1058
|
-
}
|
|
1059
|
-
const value = factory();
|
|
1060
|
-
cache.set(cacheKey, value);
|
|
1061
|
-
return value;
|
|
1062
|
-
};
|
|
1063
|
-
}
|
|
1064
|
-
|
|
1065
|
-
// utils/load.ts
|
|
1066
|
-
import { getCachedDocumentNodeFromSchema as getCachedDocumentNodeFromSchema3 } from "@graphql-codegen/plugin-helpers";
|
|
1067
|
-
import { ApolloEngineLoader } from "@graphql-tools/apollo-engine-loader";
|
|
1068
|
-
import { CodeFileLoader } from "@graphql-tools/code-file-loader";
|
|
1069
|
-
import { GitLoader } from "@graphql-tools/git-loader";
|
|
1070
|
-
import { GithubLoader } from "@graphql-tools/github-loader";
|
|
1071
|
-
import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader";
|
|
1072
|
-
import { JsonFileLoader } from "@graphql-tools/json-file-loader";
|
|
1073
|
-
import { loadSchema as loadSchemaToolkit } from "@graphql-tools/load";
|
|
1074
|
-
import { PrismaLoader } from "@graphql-tools/prisma-loader";
|
|
1075
|
-
import { UrlLoader } from "@graphql-tools/url-loader";
|
|
1076
|
-
|
|
1077
|
-
// utils/hash.ts
|
|
1078
|
-
import { getCachedDocumentNodeFromSchema as getCachedDocumentNodeFromSchema2 } from "@graphql-codegen/plugin-helpers";
|
|
1079
|
-
import { print } from "graphql";
|
|
1080
|
-
import { createHash } from "node:crypto";
|
|
1081
|
-
function hashContent(content) {
|
|
1082
|
-
return createHash("sha256").update(content).digest("hex");
|
|
1083
|
-
}
|
|
1084
|
-
function hashSchema(schema) {
|
|
1085
|
-
return hashContent(print(getCachedDocumentNodeFromSchema2(schema)));
|
|
1086
|
-
}
|
|
1087
|
-
|
|
1088
|
-
// utils/load.ts
|
|
1089
|
-
async function loadSchema(schemaPointerMap, cwd) {
|
|
1090
|
-
const outputSchemaAst = await loadSchemaToolkit(schemaPointerMap, {
|
|
1091
|
-
loaders: [
|
|
1092
|
-
new CodeFileLoader(),
|
|
1093
|
-
new GitLoader(),
|
|
1094
|
-
new GithubLoader(),
|
|
1095
|
-
new GraphQLFileLoader(),
|
|
1096
|
-
new JsonFileLoader(),
|
|
1097
|
-
new UrlLoader(),
|
|
1098
|
-
new ApolloEngineLoader(),
|
|
1099
|
-
new PrismaLoader()
|
|
1100
|
-
],
|
|
1101
|
-
cwd,
|
|
1102
|
-
includeSources: true
|
|
1103
|
-
});
|
|
1104
|
-
if (!outputSchemaAst.extensions) {
|
|
1105
|
-
outputSchemaAst.extensions = {};
|
|
1106
|
-
}
|
|
1107
|
-
outputSchemaAst.extensions["hash"] = hashSchema(outputSchemaAst);
|
|
1108
|
-
return {
|
|
1109
|
-
outputSchemaAst,
|
|
1110
|
-
outputSchema: getCachedDocumentNodeFromSchema3(outputSchemaAst)
|
|
1111
|
-
};
|
|
1112
|
-
}
|
|
1113
|
-
|
|
1114
800
|
// lib/codegen.ts
|
|
1115
801
|
async function generate(options) {
|
|
1116
802
|
const root = process.cwd();
|
|
@@ -1122,10 +808,10 @@ async function generate(options) {
|
|
|
1122
808
|
contextType: options.contextType,
|
|
1123
809
|
moduleDefinitionName: options.moduleDefinitionName || "typedef.ts",
|
|
1124
810
|
scalars: options.scalars,
|
|
1125
|
-
plugins: normalizeConfig(["typescript", "
|
|
811
|
+
plugins: normalizeConfig(["typescript", "context"]),
|
|
1126
812
|
pluginMap: {
|
|
1127
813
|
typescript: typescriptPlugin,
|
|
1128
|
-
|
|
814
|
+
context: context_exports
|
|
1129
815
|
}
|
|
1130
816
|
};
|
|
1131
817
|
const cache = createCache();
|
|
@@ -1154,17 +840,9 @@ async function generate(options) {
|
|
|
1154
840
|
pluginMap: rootConfig.pluginMap,
|
|
1155
841
|
plugins: rootConfig.plugins,
|
|
1156
842
|
config: {
|
|
1157
|
-
useIndexSignature: true,
|
|
1158
843
|
inputMaybeValue: "T | undefined",
|
|
1159
|
-
mapperTypeSuffix: "Prisma",
|
|
1160
844
|
contextType: rootConfig.contextType,
|
|
1161
|
-
customResolverFn: "@baeta/core#Resolver",
|
|
1162
|
-
customSubscriptionResolver: "@baeta/core#SubscriptionResolver",
|
|
1163
845
|
useTypeImports: true,
|
|
1164
|
-
makeResolverTypeCallable: true,
|
|
1165
|
-
includeDirectives: true,
|
|
1166
|
-
resolverTypeWrapperSignature: "T",
|
|
1167
|
-
emitLegacyCommonJSImports: false,
|
|
1168
846
|
scalars: {
|
|
1169
847
|
BigInt: "number",
|
|
1170
848
|
Bytes: "Buffer",
|