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