@baeta/plugin-graphql 0.0.34 → 0.1.1
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 +38 -0
- package/dist/index.cjs +64 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +90 -57
- package/dist/index.js.map +1 -1
- package/package.json +24 -21
package/dist/index.js
CHANGED
|
@@ -29,21 +29,16 @@ function createCache() {
|
|
|
29
29
|
|
|
30
30
|
// utils/load.ts
|
|
31
31
|
import { getCachedDocumentNodeFromSchema as getCachedDocumentNodeFromSchema2 } from "@graphql-codegen/plugin-helpers";
|
|
32
|
-
import { ApolloEngineLoader } from "@graphql-tools/apollo-engine-loader";
|
|
33
|
-
import { CodeFileLoader } from "@graphql-tools/code-file-loader";
|
|
34
|
-
import { GitLoader } from "@graphql-tools/git-loader";
|
|
35
|
-
import { GithubLoader } from "@graphql-tools/github-loader";
|
|
36
32
|
import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader";
|
|
37
|
-
import {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
import { UrlLoader } from "@graphql-tools/url-loader";
|
|
33
|
+
import {
|
|
34
|
+
loadSchema as loadSchemaToolkit
|
|
35
|
+
} from "@graphql-tools/load";
|
|
41
36
|
import { validateSchema } from "graphql";
|
|
42
37
|
|
|
43
38
|
// utils/hash.ts
|
|
39
|
+
import { createHash } from "node:crypto";
|
|
44
40
|
import { getCachedDocumentNodeFromSchema } from "@graphql-codegen/plugin-helpers";
|
|
45
41
|
import { print } from "graphql";
|
|
46
|
-
import { createHash } from "node:crypto";
|
|
47
42
|
function hashContent(content) {
|
|
48
43
|
return createHash("sha256").update(content).digest("hex");
|
|
49
44
|
}
|
|
@@ -52,18 +47,9 @@ function hashSchema(schema) {
|
|
|
52
47
|
}
|
|
53
48
|
|
|
54
49
|
// utils/load.ts
|
|
55
|
-
async function loadSchema(schemaPointerMap, cwd) {
|
|
50
|
+
async function loadSchema(schemaPointerMap, cwd, extraLoaders = []) {
|
|
56
51
|
const outputSchemaAst = await loadSchemaToolkit(schemaPointerMap, {
|
|
57
|
-
loaders: [
|
|
58
|
-
new CodeFileLoader(),
|
|
59
|
-
new GitLoader(),
|
|
60
|
-
new GithubLoader(),
|
|
61
|
-
new GraphQLFileLoader(),
|
|
62
|
-
new JsonFileLoader(),
|
|
63
|
-
new UrlLoader(),
|
|
64
|
-
new ApolloEngineLoader(),
|
|
65
|
-
new PrismaLoader()
|
|
66
|
-
],
|
|
52
|
+
loaders: [new GraphQLFileLoader(), ...extraLoaders],
|
|
67
53
|
cwd,
|
|
68
54
|
includeSources: true
|
|
69
55
|
});
|
|
@@ -78,7 +64,7 @@ ${messages}`);
|
|
|
78
64
|
if (!outputSchemaAst.extensions) {
|
|
79
65
|
outputSchemaAst.extensions = {};
|
|
80
66
|
}
|
|
81
|
-
outputSchemaAst.extensions
|
|
67
|
+
outputSchemaAst.extensions.hash = hashSchema(outputSchemaAst);
|
|
82
68
|
return {
|
|
83
69
|
outputSchemaAst,
|
|
84
70
|
outputSchema: getCachedDocumentNodeFromSchema2(outputSchemaAst)
|
|
@@ -132,20 +118,62 @@ import {
|
|
|
132
118
|
// lib/modules/builder.ts
|
|
133
119
|
import { pascalCase } from "change-case-all";
|
|
134
120
|
import {
|
|
121
|
+
Kind as Kind3,
|
|
135
122
|
isInterfaceType,
|
|
136
123
|
isScalarType,
|
|
137
124
|
isUnionType,
|
|
138
|
-
Kind as Kind2,
|
|
139
125
|
visit
|
|
140
126
|
} from "graphql";
|
|
141
127
|
|
|
128
|
+
// lib/modules/hashes.ts
|
|
129
|
+
import {
|
|
130
|
+
Kind
|
|
131
|
+
} from "graphql";
|
|
132
|
+
import hash from "murmurhash";
|
|
133
|
+
function getObjectTypeHash(node) {
|
|
134
|
+
const fieldNames = buildFieldsNames(node.fields);
|
|
135
|
+
const typeFields = fieldNames.map(([fieldName, fieldType]) => `${fieldName}:${fieldType}`).join(",");
|
|
136
|
+
const fieldMap = {};
|
|
137
|
+
for (const [fieldName, fieldType] of fieldNames) {
|
|
138
|
+
fieldMap[fieldName] = hash.v3(fieldType).toString(36);
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
hash: hash.v3(typeFields).toString(36),
|
|
142
|
+
fieldsHashes: fieldMap
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function buildFieldsNames(fields = []) {
|
|
146
|
+
if (fields.length === 0) {
|
|
147
|
+
return [];
|
|
148
|
+
}
|
|
149
|
+
const fieldsNames = [];
|
|
150
|
+
for (const field of fields) {
|
|
151
|
+
const fieldName = field.name.value;
|
|
152
|
+
const fieldType = buildTypeName(field.type);
|
|
153
|
+
fieldsNames.push([fieldName, fieldType]);
|
|
154
|
+
}
|
|
155
|
+
return fieldsNames.sort(([a], [b]) => a.localeCompare(b));
|
|
156
|
+
}
|
|
157
|
+
function buildTypeName(node) {
|
|
158
|
+
if (node.kind === Kind.NAMED_TYPE) {
|
|
159
|
+
return node.name.value;
|
|
160
|
+
}
|
|
161
|
+
if (node.kind === Kind.LIST_TYPE) {
|
|
162
|
+
return `[${buildTypeName(node.type)}]`;
|
|
163
|
+
}
|
|
164
|
+
if (node.kind === Kind.NON_NULL_TYPE) {
|
|
165
|
+
return `${buildTypeName(node.type)}!`;
|
|
166
|
+
}
|
|
167
|
+
return "";
|
|
168
|
+
}
|
|
169
|
+
|
|
142
170
|
// lib/modules/utils.ts
|
|
171
|
+
import { platform } from "node:os";
|
|
143
172
|
import { getBaseType } from "@graphql-codegen/plugin-helpers";
|
|
144
173
|
import { DEFAULT_SCALARS, wrapTypeWithModifiers } from "@graphql-codegen/visitor-plugin-common";
|
|
145
174
|
import {
|
|
146
|
-
Kind
|
|
175
|
+
Kind as Kind2
|
|
147
176
|
} from "graphql";
|
|
148
|
-
import { platform } from "node:os";
|
|
149
177
|
import parse from "parse-filepath";
|
|
150
178
|
var sep = "/";
|
|
151
179
|
function collectUsedTypes(doc) {
|
|
@@ -157,7 +185,7 @@ function collectUsedTypes(doc) {
|
|
|
157
185
|
pushUnique(used, type);
|
|
158
186
|
}
|
|
159
187
|
function findRelated(node) {
|
|
160
|
-
if (node.kind ===
|
|
188
|
+
if (node.kind === Kind2.OBJECT_TYPE_DEFINITION || node.kind === Kind2.OBJECT_TYPE_EXTENSION) {
|
|
161
189
|
markAsUsed(node.name.value);
|
|
162
190
|
if (node.fields) {
|
|
163
191
|
for (const field of node.fields) {
|
|
@@ -169,14 +197,14 @@ function collectUsedTypes(doc) {
|
|
|
169
197
|
findRelated(_interface);
|
|
170
198
|
}
|
|
171
199
|
}
|
|
172
|
-
} else if (node.kind ===
|
|
200
|
+
} else if (node.kind === Kind2.INPUT_OBJECT_TYPE_DEFINITION || node.kind === Kind2.INPUT_OBJECT_TYPE_EXTENSION) {
|
|
173
201
|
markAsUsed(node.name.value);
|
|
174
202
|
if (node.fields) {
|
|
175
203
|
for (const field of node.fields) {
|
|
176
204
|
findRelated(field);
|
|
177
205
|
}
|
|
178
206
|
}
|
|
179
|
-
} else if (node.kind ===
|
|
207
|
+
} else if (node.kind === Kind2.INTERFACE_TYPE_DEFINITION || node.kind === Kind2.INTERFACE_TYPE_EXTENSION) {
|
|
180
208
|
markAsUsed(node.name.value);
|
|
181
209
|
if (node.fields) {
|
|
182
210
|
for (const field of node.fields) {
|
|
@@ -188,29 +216,29 @@ function collectUsedTypes(doc) {
|
|
|
188
216
|
findRelated(_interface);
|
|
189
217
|
}
|
|
190
218
|
}
|
|
191
|
-
} else if (node.kind ===
|
|
219
|
+
} else if (node.kind === Kind2.UNION_TYPE_DEFINITION || node.kind === Kind2.UNION_TYPE_EXTENSION) {
|
|
192
220
|
markAsUsed(node.name.value);
|
|
193
221
|
if (node.types) {
|
|
194
222
|
for (const type of node.types) {
|
|
195
223
|
findRelated(type);
|
|
196
224
|
}
|
|
197
225
|
}
|
|
198
|
-
} else if (node.kind ===
|
|
226
|
+
} else if (node.kind === Kind2.ENUM_TYPE_DEFINITION || node.kind === Kind2.ENUM_TYPE_EXTENSION) {
|
|
199
227
|
markAsUsed(node.name.value);
|
|
200
|
-
} else if (node.kind ===
|
|
228
|
+
} else if (node.kind === Kind2.SCALAR_TYPE_DEFINITION || node.kind === Kind2.SCALAR_TYPE_EXTENSION) {
|
|
201
229
|
if (!isGraphQLPrimitive(node.name.value)) {
|
|
202
230
|
markAsUsed(node.name.value);
|
|
203
231
|
}
|
|
204
|
-
} else if (node.kind ===
|
|
232
|
+
} else if (node.kind === Kind2.INPUT_VALUE_DEFINITION) {
|
|
205
233
|
findRelated(resolveTypeNode(node.type));
|
|
206
|
-
} else if (node.kind ===
|
|
234
|
+
} else if (node.kind === Kind2.FIELD_DEFINITION) {
|
|
207
235
|
findRelated(resolveTypeNode(node.type));
|
|
208
236
|
if (node.arguments) {
|
|
209
237
|
for (const argument of node.arguments) {
|
|
210
238
|
findRelated(argument);
|
|
211
239
|
}
|
|
212
240
|
}
|
|
213
|
-
} else if (node.kind ===
|
|
241
|
+
} else if (node.kind === Kind2.NAMED_TYPE && // Named type
|
|
214
242
|
!isGraphQLPrimitive(node.name.value)) {
|
|
215
243
|
markAsUsed(node.name.value);
|
|
216
244
|
}
|
|
@@ -254,7 +282,7 @@ function collectObjectFieldTypesAndArguments(schema) {
|
|
|
254
282
|
}
|
|
255
283
|
const schemaTypes = schema.getTypeMap();
|
|
256
284
|
for (const type of Object.values(schemaTypes)) {
|
|
257
|
-
if (type.astNode?.kind !==
|
|
285
|
+
if (type.astNode?.kind !== Kind2.OBJECT_TYPE_DEFINITION) {
|
|
258
286
|
continue;
|
|
259
287
|
}
|
|
260
288
|
const schemaType = schemaTypes[type.name];
|
|
@@ -267,10 +295,10 @@ function collectObjectFieldTypesAndArguments(schema) {
|
|
|
267
295
|
return { fieldTypes, fieldArguments };
|
|
268
296
|
}
|
|
269
297
|
function resolveTypeNode(node) {
|
|
270
|
-
if (node.kind ===
|
|
298
|
+
if (node.kind === Kind2.LIST_TYPE) {
|
|
271
299
|
return resolveTypeNode(node.type);
|
|
272
300
|
}
|
|
273
|
-
if (node.kind ===
|
|
301
|
+
if (node.kind === Kind2.NON_NULL_TYPE) {
|
|
274
302
|
return resolveTypeNode(node.type);
|
|
275
303
|
}
|
|
276
304
|
return node;
|
|
@@ -390,6 +418,7 @@ function buildModule(name, doc, {
|
|
|
390
418
|
);
|
|
391
419
|
const defined = createObject(registryKeys, () => []);
|
|
392
420
|
const extended = createObject(registryKeys, () => []);
|
|
421
|
+
const hashes = {};
|
|
393
422
|
const usedTypes = collectUsedTypes(doc);
|
|
394
423
|
visit(doc, {
|
|
395
424
|
ObjectTypeDefinition(node) {
|
|
@@ -436,7 +465,7 @@ function buildModule(name, doc, {
|
|
|
436
465
|
);
|
|
437
466
|
const imports = [
|
|
438
467
|
`import * as ${importNamespace} from "${importPath}";`,
|
|
439
|
-
'import { DocumentNode } from "graphql";',
|
|
468
|
+
'import type { DocumentNode } from "graphql";',
|
|
440
469
|
'import * as Baeta from "@baeta/core/sdk";'
|
|
441
470
|
];
|
|
442
471
|
if (extensionsPath) {
|
|
@@ -470,6 +499,7 @@ ${shouldDeclare ? `${indent(2)(imports.join("\n"))}
|
|
|
470
499
|
return `export namespace ModuleMetadata {
|
|
471
500
|
export const id = '${name}';
|
|
472
501
|
export const dirname = './${name}';
|
|
502
|
+
export const hashes = ${JSON.stringify(hashes)};
|
|
473
503
|
export const typedef = ${JSON.stringify(doc)} as unknown as DocumentNode;
|
|
474
504
|
${printBaetaManager()}
|
|
475
505
|
}`;
|
|
@@ -714,31 +744,32 @@ ${body}
|
|
|
714
744
|
function collectTypeDefinition(node) {
|
|
715
745
|
const name2 = node.name.value;
|
|
716
746
|
switch (node.kind) {
|
|
717
|
-
case
|
|
747
|
+
case Kind3.OBJECT_TYPE_DEFINITION: {
|
|
718
748
|
defined.objects.push(name2);
|
|
719
749
|
collectFields(node, picks.objects);
|
|
750
|
+
hashes[name2] = getObjectTypeHash(node);
|
|
720
751
|
break;
|
|
721
752
|
}
|
|
722
|
-
case
|
|
753
|
+
case Kind3.ENUM_TYPE_DEFINITION: {
|
|
723
754
|
defined.enums.push(name2);
|
|
724
755
|
collectValuesFromEnum(node);
|
|
725
756
|
break;
|
|
726
757
|
}
|
|
727
|
-
case
|
|
758
|
+
case Kind3.INPUT_OBJECT_TYPE_DEFINITION: {
|
|
728
759
|
defined.inputs.push(name2);
|
|
729
760
|
collectFields(node, picks.inputs);
|
|
730
761
|
break;
|
|
731
762
|
}
|
|
732
|
-
case
|
|
763
|
+
case Kind3.SCALAR_TYPE_DEFINITION: {
|
|
733
764
|
defined.scalars.push(name2);
|
|
734
765
|
break;
|
|
735
766
|
}
|
|
736
|
-
case
|
|
767
|
+
case Kind3.INTERFACE_TYPE_DEFINITION: {
|
|
737
768
|
defined.interfaces.push(name2);
|
|
738
769
|
collectFields(node, picks.interfaces);
|
|
739
770
|
break;
|
|
740
771
|
}
|
|
741
|
-
case
|
|
772
|
+
case Kind3.UNION_TYPE_DEFINITION: {
|
|
742
773
|
defined.unions.push(name2);
|
|
743
774
|
collectUnionTypes(node);
|
|
744
775
|
break;
|
|
@@ -748,8 +779,9 @@ ${body}
|
|
|
748
779
|
function collectTypeExtension(node) {
|
|
749
780
|
const name2 = node.name.value;
|
|
750
781
|
switch (node.kind) {
|
|
751
|
-
case
|
|
782
|
+
case Kind3.OBJECT_TYPE_EXTENSION: {
|
|
752
783
|
collectFields(node, picks.objects);
|
|
784
|
+
hashes[name2] = getObjectTypeHash(node);
|
|
753
785
|
if (rootTypes.includes(name2)) {
|
|
754
786
|
pushUnique(defined.objects, name2);
|
|
755
787
|
return;
|
|
@@ -757,22 +789,22 @@ ${body}
|
|
|
757
789
|
pushUnique(extended.objects, name2);
|
|
758
790
|
break;
|
|
759
791
|
}
|
|
760
|
-
case
|
|
792
|
+
case Kind3.ENUM_TYPE_EXTENSION: {
|
|
761
793
|
collectValuesFromEnum(node);
|
|
762
794
|
pushUnique(extended.enums, name2);
|
|
763
795
|
break;
|
|
764
796
|
}
|
|
765
|
-
case
|
|
797
|
+
case Kind3.INPUT_OBJECT_TYPE_EXTENSION: {
|
|
766
798
|
collectFields(node, picks.inputs);
|
|
767
799
|
pushUnique(extended.inputs, name2);
|
|
768
800
|
break;
|
|
769
801
|
}
|
|
770
|
-
case
|
|
802
|
+
case Kind3.INTERFACE_TYPE_EXTENSION: {
|
|
771
803
|
collectFields(node, picks.interfaces);
|
|
772
804
|
pushUnique(extended.interfaces, name2);
|
|
773
805
|
break;
|
|
774
806
|
}
|
|
775
|
-
case
|
|
807
|
+
case Kind3.UNION_TYPE_EXTENSION: {
|
|
776
808
|
pushUnique(extended.unions, name2);
|
|
777
809
|
break;
|
|
778
810
|
}
|
|
@@ -780,17 +812,17 @@ ${body}
|
|
|
780
812
|
}
|
|
781
813
|
function getParentType(type) {
|
|
782
814
|
if (["Query", "Mutation", "Subscription"].includes(type)) {
|
|
783
|
-
return "{}";
|
|
815
|
+
return "{ }";
|
|
784
816
|
}
|
|
785
817
|
return type;
|
|
786
818
|
}
|
|
787
819
|
function getResultType(type, field) {
|
|
788
|
-
return fieldTypes?.[type]?.[field] || "{}";
|
|
820
|
+
return fieldTypes?.[type]?.[field] || "{ }";
|
|
789
821
|
}
|
|
790
822
|
function getArgsType(type, field) {
|
|
791
823
|
const hasArgs = fieldArguments?.[type]?.[field] ?? false;
|
|
792
824
|
if (!hasArgs) {
|
|
793
|
-
return "{}";
|
|
825
|
+
return "{ }";
|
|
794
826
|
}
|
|
795
827
|
const fieldUpper = field[0].toUpperCase() + field.slice(1);
|
|
796
828
|
return `Types.${type}${fieldUpper}Args`;
|
|
@@ -804,7 +836,7 @@ ${body}
|
|
|
804
836
|
var preset = {
|
|
805
837
|
buildGeneratesSection: (options) => {
|
|
806
838
|
const { baseOutputDir } = options;
|
|
807
|
-
const { baseTypesPath, extensionsPath, encapsulateModuleTypes } = options.presetConfig;
|
|
839
|
+
const { baseTypesPath, extensionsPath, encapsulateModuleTypes, importExtension } = options.presetConfig;
|
|
808
840
|
const requireRootResolvers = getConfigValue(options?.presetConfig.requireRootResolvers, false);
|
|
809
841
|
const cwd = resolve(options.presetConfig.cwd || process.cwd());
|
|
810
842
|
const importTypesNamespace = options.presetConfig.importTypesNamespace || "Types";
|
|
@@ -971,7 +1003,7 @@ ${result}
|
|
|
971
1003
|
},
|
|
972
1004
|
schemaAst: options.schemaAst
|
|
973
1005
|
};
|
|
974
|
-
const baseTypesFilename = baseTypesPath.replace(/\.(js|ts|d.ts)$/, "");
|
|
1006
|
+
const baseTypesFilename = baseTypesPath.replace(/\.(js|ts|d.ts)$/, "") + (importExtension || "");
|
|
975
1007
|
const baseTypesDir = stripFilename(baseOutput.filename);
|
|
976
1008
|
const outputs = modules.map((moduleName) => {
|
|
977
1009
|
const filename = resolve(cwd, baseOutputDir, moduleName, options.presetConfig.filename);
|
|
@@ -1047,9 +1079,9 @@ async function generate(options) {
|
|
|
1047
1079
|
Object.assign(schemaPointerMap, ptr);
|
|
1048
1080
|
}
|
|
1049
1081
|
}
|
|
1050
|
-
const
|
|
1051
|
-
const result = await cache("schema",
|
|
1052
|
-
return loadSchema(schemaPointerMap, options.cwd);
|
|
1082
|
+
const hash2 = JSON.stringify(schemaPointerMap);
|
|
1083
|
+
const result = await cache("schema", hash2, async () => {
|
|
1084
|
+
return loadSchema(schemaPointerMap, options.cwd, options.loaders);
|
|
1053
1085
|
});
|
|
1054
1086
|
const outputs = await preset.buildGeneratesSection({
|
|
1055
1087
|
baseOutputDir: options.modulesDir,
|
|
@@ -1057,7 +1089,8 @@ async function generate(options) {
|
|
|
1057
1089
|
baseTypesPath: rootConfig.baseTypesPath,
|
|
1058
1090
|
filename: rootConfig.moduleDefinitionName,
|
|
1059
1091
|
encapsulateModuleTypes: "none",
|
|
1060
|
-
extensionsPath: options.extensions
|
|
1092
|
+
extensionsPath: options.extensions,
|
|
1093
|
+
importExtension: options.importExtension
|
|
1061
1094
|
},
|
|
1062
1095
|
schema: result.outputSchema,
|
|
1063
1096
|
schemaAst: result.outputSchemaAst,
|