@graphitation/supermassive 1.0.0 → 1.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.
Files changed (67) hide show
  1. package/.eslintcache +1 -0
  2. package/CHANGELOG.md +18 -2
  3. package/lib/bin/supermassive.d.ts.map +1 -1
  4. package/lib/bin/supermassive.js +48 -4
  5. package/lib/bin/supermassive.mjs +48 -4
  6. package/lib/codegen/context/expect.d.ts +1 -0
  7. package/lib/codegen/context/expect.d.ts.map +1 -0
  8. package/lib/codegen/context/expect.js +0 -0
  9. package/lib/codegen/context/expect.mjs +0 -0
  10. package/lib/codegen/context/import.d.ts +5 -0
  11. package/lib/codegen/context/import.d.ts.map +1 -0
  12. package/lib/codegen/context/import.js +55 -0
  13. package/lib/codegen/context/import.mjs +37 -0
  14. package/lib/codegen/context/index.d.ts +52 -0
  15. package/lib/codegen/context/index.d.ts.map +1 -0
  16. package/lib/codegen/context/index.js +253 -0
  17. package/lib/codegen/context/index.mjs +242 -0
  18. package/lib/codegen/context/model.d.ts +5 -0
  19. package/lib/codegen/context/model.d.ts.map +1 -0
  20. package/lib/codegen/context/model.js +53 -0
  21. package/lib/codegen/context/model.mjs +37 -0
  22. package/lib/codegen/context/utilities.d.ts +2 -0
  23. package/lib/codegen/context/utilities.d.ts.map +1 -0
  24. package/lib/codegen/context/utilities.js +26 -0
  25. package/lib/codegen/context/utilities.mjs +8 -0
  26. package/lib/codegen/index.d.ts +7 -0
  27. package/lib/codegen/index.d.ts.map +1 -0
  28. package/lib/codegen/index.js +41 -0
  29. package/lib/codegen/index.mjs +23 -0
  30. package/lib/codegen/models.d.ts +5 -0
  31. package/lib/codegen/models.d.ts.map +1 -0
  32. package/lib/codegen/models.js +159 -0
  33. package/lib/codegen/models.mjs +142 -0
  34. package/lib/codegen/resolvers.d.ts +5 -0
  35. package/lib/codegen/resolvers.d.ts.map +1 -0
  36. package/lib/codegen/resolvers.js +155 -0
  37. package/lib/codegen/resolvers.mjs +143 -0
  38. package/lib/codegen/typedVisitor.d.ts +57 -0
  39. package/lib/codegen/typedVisitor.d.ts.map +1 -0
  40. package/lib/codegen/typedVisitor.js +26 -0
  41. package/lib/codegen/typedVisitor.mjs +8 -0
  42. package/lib/codegen/types.d.ts +18 -0
  43. package/lib/codegen/types.d.ts.map +1 -0
  44. package/lib/codegen/types.js +15 -0
  45. package/lib/codegen/types.mjs +0 -0
  46. package/lib/codegen/utilities.d.ts +50 -0
  47. package/lib/codegen/utilities.d.ts.map +1 -0
  48. package/lib/codegen/utilities.js +192 -0
  49. package/lib/codegen/utilities.mjs +174 -0
  50. package/lib/{extractImplicitTypesToTypescript.d.ts → extractors/extractImplicitTypesToTypescript.d.ts} +0 -0
  51. package/lib/extractors/extractImplicitTypesToTypescript.d.ts.map +1 -0
  52. package/lib/{extractImplicitTypesToTypescript.js → extractors/extractImplicitTypesToTypescript.js} +0 -0
  53. package/lib/{extractImplicitTypesToTypescript.mjs → extractors/extractImplicitTypesToTypescript.mjs} +1 -1
  54. package/lib/extractors/index.d.ts +1 -1
  55. package/lib/extractors/index.d.ts.map +1 -1
  56. package/lib/extractors/index.js +1 -1
  57. package/lib/extractors/index.mjs +1 -1
  58. package/lib/index.d.ts +3 -1
  59. package/lib/index.d.ts.map +1 -1
  60. package/lib/index.js +2 -0
  61. package/lib/index.mjs +2 -0
  62. package/lib/utilities/blankGraphQLTag.d.ts +2 -0
  63. package/lib/utilities/blankGraphQLTag.d.ts.map +1 -0
  64. package/lib/utilities/blankGraphQLTag.js +25 -0
  65. package/lib/utilities/blankGraphQLTag.mjs +7 -0
  66. package/package.json +1 -1
  67. package/lib/extractImplicitTypesToTypescript.d.ts.map +0 -1
@@ -0,0 +1,253 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
9
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10
+ var __spreadValues = (a, b) => {
11
+ for (var prop in b || (b = {}))
12
+ if (__hasOwnProp.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ if (__getOwnPropSymbols)
15
+ for (var prop of __getOwnPropSymbols(b)) {
16
+ if (__propIsEnum.call(b, prop))
17
+ __defNormalProp(a, prop, b[prop]);
18
+ }
19
+ return a;
20
+ };
21
+ var __export = (target, all) => {
22
+ for (var name in all)
23
+ __defProp(target, name, { get: all[name], enumerable: true });
24
+ };
25
+ var __copyProps = (to, from, except, desc) => {
26
+ if (from && typeof from === "object" || typeof from === "function") {
27
+ for (let key of __getOwnPropNames(from))
28
+ if (!__hasOwnProp.call(to, key) && key !== except)
29
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
30
+ }
31
+ return to;
32
+ };
33
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
34
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
35
+ var context_exports = {};
36
+ __export(context_exports, {
37
+ BUILT_IN_SCALARS: () => BUILT_IN_SCALARS,
38
+ TsCodegenContext: () => TsCodegenContext,
39
+ TypeLocation: () => TypeLocation,
40
+ extractContext: () => extractContext
41
+ });
42
+ module.exports = __toCommonJS(context_exports);
43
+ var import_graphql = require("graphql");
44
+ var import_typescript = __toESM(require("typescript"));
45
+ var import_utilities = require("./utilities");
46
+ var import_utilities2 = require("../utilities");
47
+ var import_import = require("./import");
48
+ var import_model = require("./model");
49
+ const DEFAULT_SCALAR_TYPE = "unknown";
50
+ const BUILT_IN_SCALARS = {
51
+ ID: "string",
52
+ Int: "number",
53
+ Float: "number",
54
+ String: "string",
55
+ Boolean: "boolean"
56
+ };
57
+ const TsCodegenContextDefault = {
58
+ moduleRoot: "",
59
+ moduleModelsPath: "./__generated__/models.interface.ts",
60
+ moduleResolversPath: "./__generated__/resolvers.interface.ts",
61
+ baseModel: {
62
+ name: "BaseModel",
63
+ from: null
64
+ },
65
+ context: {
66
+ name: "unknown",
67
+ from: null
68
+ },
69
+ resolveInfo: {
70
+ name: "ResolveInfo",
71
+ from: "@graphitation/supermassive"
72
+ }
73
+ };
74
+ class TsCodegenContext {
75
+ constructor(options) {
76
+ this.options = options;
77
+ this.imports = [];
78
+ this.typeNameToImports = /* @__PURE__ */ new Map();
79
+ this.typeNameToModels = /* @__PURE__ */ new Map();
80
+ this.allModelNames = /* @__PURE__ */ new Set();
81
+ this.allPossibleModels = /* @__PURE__ */ new Set();
82
+ }
83
+ addImport(imp, node) {
84
+ var _a;
85
+ for (const { typeName } of imp.defs) {
86
+ const { imp: existingImport } = this.typeNameToImports.get(typeName) || {};
87
+ if (existingImport) {
88
+ throw new import_graphql.GraphQLError(`Definition ${typeName} is imported multiple times: ${existingImport.from}`, [
89
+ (_a = getArgumentValue(existingImport.directive.arguments, "from")) != null ? _a : existingImport.directive,
90
+ imp.directive,
91
+ node
92
+ ]);
93
+ }
94
+ this.typeNameToImports.set(typeName, {
95
+ modelName: (0, import_utilities2.addModelSuffix)(typeName),
96
+ imp
97
+ });
98
+ }
99
+ this.imports.push(imp);
100
+ }
101
+ addPossibleModel(possibleModel) {
102
+ this.allPossibleModels.add(possibleModel);
103
+ }
104
+ addModel(model, node) {
105
+ const existingModel = this.typeNameToModels.get(model.typeName);
106
+ if (existingModel) {
107
+ throw new import_graphql.GraphQLError(`Model for type ${model.typeName} is defined multiple times`, [existingModel.directive, node]);
108
+ }
109
+ this.typeNameToModels.set(model.typeName, model);
110
+ }
111
+ getAllImportDeclarations() {
112
+ return this.imports.map(({ defs, from }) => (0, import_utilities.createImportDeclaration)(defs.map(({ typeName }) => (0, import_utilities2.addModelSuffix)(typeName)), from));
113
+ }
114
+ getAllModelImportDeclarations() {
115
+ const imports = this.getAllImportDeclarations();
116
+ const models = Array.from(this.typeNameToModels.values()).map((model) => {
117
+ if (!model.from) {
118
+ return;
119
+ }
120
+ return (0, import_utilities.createImportDeclaration)([model.modelName], model.from, model.tsType !== model.modelName ? model.tsType : void 0);
121
+ }).filter(Boolean);
122
+ return imports.concat(models);
123
+ }
124
+ getScalarDeclaration(scalarName) {
125
+ if (!scalarName || BUILT_IN_SCALARS.hasOwnProperty(scalarName)) {
126
+ return;
127
+ }
128
+ let model;
129
+ if (this.typeNameToModels.has(scalarName)) {
130
+ const { from, modelName, tsType } = this.typeNameToModels.get(scalarName);
131
+ model = from ? modelName : tsType;
132
+ } else {
133
+ model = DEFAULT_SCALAR_TYPE;
134
+ }
135
+ return import_typescript.factory.createTypeAliasDeclaration(void 0, [import_typescript.factory.createModifier(import_typescript.default.SyntaxKind.ExportKeyword)], import_typescript.factory.createIdentifier((0, import_utilities2.addModelSuffix)(scalarName)), void 0, import_typescript.factory.createTypeReferenceNode(import_typescript.factory.createIdentifier(model), void 0));
136
+ }
137
+ getAllResolverImportDeclarations() {
138
+ const imports = [];
139
+ imports.push((0, import_utilities.createImportDeclaration)(["PromiseOrValue"], "@graphitation/supermassive"));
140
+ if (this.options.resolveInfo.from) {
141
+ imports.push((0, import_utilities.createImportDeclaration)([this.options.resolveInfo.name], this.options.resolveInfo.from));
142
+ }
143
+ if (this.options.context.from && this.options.context.name) {
144
+ imports.push((0, import_utilities.createImportDeclaration)([this.options.context.name], this.options.context.from));
145
+ }
146
+ imports.push((0, import_utilities.createImportDeclaration)(Array.from(this.allModelNames), "./models.interface"));
147
+ imports.push(...this.getAllImportDeclarations());
148
+ return imports;
149
+ }
150
+ getBaseModelType() {
151
+ return new TypeLocation(null, this.options.baseModel.name);
152
+ }
153
+ getContextType() {
154
+ return new TypeLocation(null, this.options.context.name || TsCodegenContextDefault.context.name);
155
+ }
156
+ getResolveInfoType() {
157
+ return new TypeLocation(null, this.options.resolveInfo.name);
158
+ }
159
+ getDefaultTypes() {
160
+ return [
161
+ (0, import_typescript.addSyntheticLeadingComment)(import_typescript.factory.createInterfaceDeclaration(void 0, [import_typescript.factory.createModifier(import_typescript.default.SyntaxKind.ExportKeyword)], import_typescript.factory.createIdentifier("BaseModel"), void 0, void 0, [
162
+ import_typescript.factory.createPropertySignature(void 0, import_typescript.factory.createIdentifier("__typename"), void 0, import_typescript.factory.createKeywordTypeNode(import_typescript.default.SyntaxKind.StringKeyword))
163
+ ]), import_typescript.SyntaxKind.SingleLineCommentTrivia, " Base type for all models. Enables automatic resolution of abstract GraphQL types (interfaces, unions)", true)
164
+ ];
165
+ }
166
+ getModelType(typeName, useScalars = false) {
167
+ if (BUILT_IN_SCALARS.hasOwnProperty(typeName)) {
168
+ return new TypeLocation(null, BUILT_IN_SCALARS[typeName]);
169
+ } else if (this.typeNameToImports.has(typeName)) {
170
+ let { modelName } = this.typeNameToImports.get(typeName);
171
+ return new TypeLocation(null, modelName);
172
+ } else {
173
+ const modelName = this.allPossibleModels.has(typeName) ? (0, import_utilities2.addModelSuffix)(typeName) : typeName;
174
+ if (this.allPossibleModels.has(typeName)) {
175
+ this.allModelNames.add(modelName);
176
+ }
177
+ return new TypeLocation(null, modelName);
178
+ }
179
+ }
180
+ getDefinedModelType(typeName) {
181
+ if (this.typeNameToModels.has(typeName)) {
182
+ let imp = this.typeNameToModels.get(typeName);
183
+ return new TypeLocation(null, imp.modelName);
184
+ } else {
185
+ return null;
186
+ }
187
+ }
188
+ }
189
+ function extractContext(options, document) {
190
+ const fullOptions = __spreadValues(__spreadValues({}, TsCodegenContextDefault), options);
191
+ let context = new TsCodegenContext(fullOptions);
192
+ (0, import_graphql.visit)(document, {
193
+ Directive: {
194
+ enter(node, _key, _parent, _path, ancestors) {
195
+ if (node.name.value === import_import.IMPORT_DIRECTIVE_NAME) {
196
+ context.addImport((0, import_import.processImportDirective)(node), node);
197
+ } else if (node.name.value === import_model.MODEL_DIRECTIVE_NAME) {
198
+ context.addModel((0, import_model.processModelDirective)(node, ancestors), node);
199
+ }
200
+ }
201
+ },
202
+ EnumTypeDefinition: {
203
+ enter(node) {
204
+ context.addPossibleModel(node.name.value);
205
+ }
206
+ },
207
+ ObjectTypeDefinition: {
208
+ enter(node) {
209
+ context.addPossibleModel(node.name.value);
210
+ }
211
+ },
212
+ UnionTypeDefinition: {
213
+ enter(node) {
214
+ context.addPossibleModel(node.name.value);
215
+ }
216
+ },
217
+ ScalarTypeDefinition: {
218
+ enter(node) {
219
+ context.addPossibleModel(node.name.value);
220
+ }
221
+ },
222
+ InterfaceTypeDefinition: {
223
+ enter(node) {
224
+ context.addPossibleModel(node.name.value);
225
+ }
226
+ }
227
+ });
228
+ return context;
229
+ }
230
+ class TypeLocation {
231
+ constructor(from, name) {
232
+ this.from = from;
233
+ this.name = name;
234
+ }
235
+ toTypeReference() {
236
+ if (this.from != null) {
237
+ return import_typescript.factory.createTypeReferenceNode(import_typescript.factory.createQualifiedName(import_typescript.factory.createIdentifier(this.from), import_typescript.factory.createIdentifier(this.name)));
238
+ } else {
239
+ return import_typescript.factory.createTypeReferenceNode(this.name);
240
+ }
241
+ }
242
+ toExpression() {
243
+ if (this.from != null) {
244
+ return import_typescript.factory.createPropertyAccessExpression(import_typescript.factory.createIdentifier(this.from), import_typescript.factory.createIdentifier(this.name));
245
+ } else {
246
+ return import_typescript.factory.createIdentifier(this.name);
247
+ }
248
+ }
249
+ }
250
+ const getArgumentValue = (args = [], name) => {
251
+ var _a;
252
+ return (_a = args.find((arg) => arg.name.value === name)) == null ? void 0 : _a.value;
253
+ };
@@ -0,0 +1,242 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __spreadValues = (a, b) => {
7
+ for (var prop in b || (b = {}))
8
+ if (__hasOwnProp.call(b, prop))
9
+ __defNormalProp(a, prop, b[prop]);
10
+ if (__getOwnPropSymbols)
11
+ for (var prop of __getOwnPropSymbols(b)) {
12
+ if (__propIsEnum.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ }
15
+ return a;
16
+ };
17
+
18
+ // src/codegen/context/index.ts
19
+ import {
20
+ GraphQLError,
21
+ visit
22
+ } from "graphql";
23
+ import ts, {
24
+ factory,
25
+ addSyntheticLeadingComment,
26
+ SyntaxKind
27
+ } from "typescript";
28
+ import { createImportDeclaration } from "./utilities.mjs";
29
+ import { addModelSuffix } from "../utilities.mjs";
30
+ import { IMPORT_DIRECTIVE_NAME, processImportDirective } from "./import.mjs";
31
+ import { MODEL_DIRECTIVE_NAME, processModelDirective } from "./model.mjs";
32
+ var DEFAULT_SCALAR_TYPE = "unknown";
33
+ var BUILT_IN_SCALARS = {
34
+ ID: "string",
35
+ Int: "number",
36
+ Float: "number",
37
+ String: "string",
38
+ Boolean: "boolean"
39
+ };
40
+ var TsCodegenContextDefault = {
41
+ moduleRoot: "",
42
+ moduleModelsPath: "./__generated__/models.interface.ts",
43
+ moduleResolversPath: "./__generated__/resolvers.interface.ts",
44
+ baseModel: {
45
+ name: "BaseModel",
46
+ from: null
47
+ },
48
+ context: {
49
+ name: "unknown",
50
+ from: null
51
+ },
52
+ resolveInfo: {
53
+ name: "ResolveInfo",
54
+ from: "@graphitation/supermassive"
55
+ }
56
+ };
57
+ var TsCodegenContext = class {
58
+ constructor(options) {
59
+ this.options = options;
60
+ this.imports = [];
61
+ this.typeNameToImports = /* @__PURE__ */ new Map();
62
+ this.typeNameToModels = /* @__PURE__ */ new Map();
63
+ this.allModelNames = /* @__PURE__ */ new Set();
64
+ this.allPossibleModels = /* @__PURE__ */ new Set();
65
+ }
66
+ addImport(imp, node) {
67
+ var _a;
68
+ for (const { typeName } of imp.defs) {
69
+ const { imp: existingImport } = this.typeNameToImports.get(typeName) || {};
70
+ if (existingImport) {
71
+ throw new GraphQLError(`Definition ${typeName} is imported multiple times: ${existingImport.from}`, [
72
+ (_a = getArgumentValue(existingImport.directive.arguments, "from")) != null ? _a : existingImport.directive,
73
+ imp.directive,
74
+ node
75
+ ]);
76
+ }
77
+ this.typeNameToImports.set(typeName, {
78
+ modelName: addModelSuffix(typeName),
79
+ imp
80
+ });
81
+ }
82
+ this.imports.push(imp);
83
+ }
84
+ addPossibleModel(possibleModel) {
85
+ this.allPossibleModels.add(possibleModel);
86
+ }
87
+ addModel(model, node) {
88
+ const existingModel = this.typeNameToModels.get(model.typeName);
89
+ if (existingModel) {
90
+ throw new GraphQLError(`Model for type ${model.typeName} is defined multiple times`, [existingModel.directive, node]);
91
+ }
92
+ this.typeNameToModels.set(model.typeName, model);
93
+ }
94
+ getAllImportDeclarations() {
95
+ return this.imports.map(({ defs, from }) => createImportDeclaration(defs.map(({ typeName }) => addModelSuffix(typeName)), from));
96
+ }
97
+ getAllModelImportDeclarations() {
98
+ const imports = this.getAllImportDeclarations();
99
+ const models = Array.from(this.typeNameToModels.values()).map((model) => {
100
+ if (!model.from) {
101
+ return;
102
+ }
103
+ return createImportDeclaration([model.modelName], model.from, model.tsType !== model.modelName ? model.tsType : void 0);
104
+ }).filter(Boolean);
105
+ return imports.concat(models);
106
+ }
107
+ getScalarDeclaration(scalarName) {
108
+ if (!scalarName || BUILT_IN_SCALARS.hasOwnProperty(scalarName)) {
109
+ return;
110
+ }
111
+ let model;
112
+ if (this.typeNameToModels.has(scalarName)) {
113
+ const { from, modelName, tsType } = this.typeNameToModels.get(scalarName);
114
+ model = from ? modelName : tsType;
115
+ } else {
116
+ model = DEFAULT_SCALAR_TYPE;
117
+ }
118
+ return factory.createTypeAliasDeclaration(void 0, [factory.createModifier(ts.SyntaxKind.ExportKeyword)], factory.createIdentifier(addModelSuffix(scalarName)), void 0, factory.createTypeReferenceNode(factory.createIdentifier(model), void 0));
119
+ }
120
+ getAllResolverImportDeclarations() {
121
+ const imports = [];
122
+ imports.push(createImportDeclaration(["PromiseOrValue"], "@graphitation/supermassive"));
123
+ if (this.options.resolveInfo.from) {
124
+ imports.push(createImportDeclaration([this.options.resolveInfo.name], this.options.resolveInfo.from));
125
+ }
126
+ if (this.options.context.from && this.options.context.name) {
127
+ imports.push(createImportDeclaration([this.options.context.name], this.options.context.from));
128
+ }
129
+ imports.push(createImportDeclaration(Array.from(this.allModelNames), "./models.interface"));
130
+ imports.push(...this.getAllImportDeclarations());
131
+ return imports;
132
+ }
133
+ getBaseModelType() {
134
+ return new TypeLocation(null, this.options.baseModel.name);
135
+ }
136
+ getContextType() {
137
+ return new TypeLocation(null, this.options.context.name || TsCodegenContextDefault.context.name);
138
+ }
139
+ getResolveInfoType() {
140
+ return new TypeLocation(null, this.options.resolveInfo.name);
141
+ }
142
+ getDefaultTypes() {
143
+ return [
144
+ addSyntheticLeadingComment(factory.createInterfaceDeclaration(void 0, [factory.createModifier(ts.SyntaxKind.ExportKeyword)], factory.createIdentifier("BaseModel"), void 0, void 0, [
145
+ factory.createPropertySignature(void 0, factory.createIdentifier("__typename"), void 0, factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword))
146
+ ]), SyntaxKind.SingleLineCommentTrivia, " Base type for all models. Enables automatic resolution of abstract GraphQL types (interfaces, unions)", true)
147
+ ];
148
+ }
149
+ getModelType(typeName, useScalars = false) {
150
+ if (BUILT_IN_SCALARS.hasOwnProperty(typeName)) {
151
+ return new TypeLocation(null, BUILT_IN_SCALARS[typeName]);
152
+ } else if (this.typeNameToImports.has(typeName)) {
153
+ let { modelName } = this.typeNameToImports.get(typeName);
154
+ return new TypeLocation(null, modelName);
155
+ } else {
156
+ const modelName = this.allPossibleModels.has(typeName) ? addModelSuffix(typeName) : typeName;
157
+ if (this.allPossibleModels.has(typeName)) {
158
+ this.allModelNames.add(modelName);
159
+ }
160
+ return new TypeLocation(null, modelName);
161
+ }
162
+ }
163
+ getDefinedModelType(typeName) {
164
+ if (this.typeNameToModels.has(typeName)) {
165
+ let imp = this.typeNameToModels.get(typeName);
166
+ return new TypeLocation(null, imp.modelName);
167
+ } else {
168
+ return null;
169
+ }
170
+ }
171
+ };
172
+ function extractContext(options, document) {
173
+ const fullOptions = __spreadValues(__spreadValues({}, TsCodegenContextDefault), options);
174
+ let context = new TsCodegenContext(fullOptions);
175
+ visit(document, {
176
+ Directive: {
177
+ enter(node, _key, _parent, _path, ancestors) {
178
+ if (node.name.value === IMPORT_DIRECTIVE_NAME) {
179
+ context.addImport(processImportDirective(node), node);
180
+ } else if (node.name.value === MODEL_DIRECTIVE_NAME) {
181
+ context.addModel(processModelDirective(node, ancestors), node);
182
+ }
183
+ }
184
+ },
185
+ EnumTypeDefinition: {
186
+ enter(node) {
187
+ context.addPossibleModel(node.name.value);
188
+ }
189
+ },
190
+ ObjectTypeDefinition: {
191
+ enter(node) {
192
+ context.addPossibleModel(node.name.value);
193
+ }
194
+ },
195
+ UnionTypeDefinition: {
196
+ enter(node) {
197
+ context.addPossibleModel(node.name.value);
198
+ }
199
+ },
200
+ ScalarTypeDefinition: {
201
+ enter(node) {
202
+ context.addPossibleModel(node.name.value);
203
+ }
204
+ },
205
+ InterfaceTypeDefinition: {
206
+ enter(node) {
207
+ context.addPossibleModel(node.name.value);
208
+ }
209
+ }
210
+ });
211
+ return context;
212
+ }
213
+ var TypeLocation = class {
214
+ constructor(from, name) {
215
+ this.from = from;
216
+ this.name = name;
217
+ }
218
+ toTypeReference() {
219
+ if (this.from != null) {
220
+ return factory.createTypeReferenceNode(factory.createQualifiedName(factory.createIdentifier(this.from), factory.createIdentifier(this.name)));
221
+ } else {
222
+ return factory.createTypeReferenceNode(this.name);
223
+ }
224
+ }
225
+ toExpression() {
226
+ if (this.from != null) {
227
+ return factory.createPropertyAccessExpression(factory.createIdentifier(this.from), factory.createIdentifier(this.name));
228
+ } else {
229
+ return factory.createIdentifier(this.name);
230
+ }
231
+ }
232
+ };
233
+ var getArgumentValue = (args = [], name) => {
234
+ var _a;
235
+ return (_a = args.find((arg) => arg.name.value === name)) == null ? void 0 : _a.value;
236
+ };
237
+ export {
238
+ BUILT_IN_SCALARS,
239
+ TsCodegenContext,
240
+ TypeLocation,
241
+ extractContext
242
+ };
@@ -0,0 +1,5 @@
1
+ import { ASTNode, DirectiveNode } from "graphql";
2
+ import { DefinitionModel } from "../types";
3
+ export declare const MODEL_DIRECTIVE_NAME = "model";
4
+ export declare function processModelDirective(node: DirectiveNode, ancestors: ReadonlyArray<ASTNode | ReadonlyArray<ASTNode>>): DefinitionModel;
5
+ //# sourceMappingURL=model.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../../src/codegen/context/model.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,OAAO,EACP,aAAa,EAGd,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAG3C,eAAO,MAAM,oBAAoB,UAAU,CAAC;AAE5C,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,aAAa,EACnB,SAAS,EAAE,aAAa,CAAC,OAAO,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC,GACzD,eAAe,CAwCjB"}
@@ -0,0 +1,53 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var model_exports = {};
19
+ __export(model_exports, {
20
+ MODEL_DIRECTIVE_NAME: () => MODEL_DIRECTIVE_NAME,
21
+ processModelDirective: () => processModelDirective
22
+ });
23
+ module.exports = __toCommonJS(model_exports);
24
+ var import_graphql = require("graphql");
25
+ var import_utilities = require("../utilities");
26
+ const MODEL_DIRECTIVE_NAME = "model";
27
+ function processModelDirective(node, ancestors) {
28
+ const from = getArgumentValue(node.arguments, "from");
29
+ const tsType = getArgumentValue(node.arguments, "tsType");
30
+ if (from && from.kind !== "StringValue") {
31
+ throw new import_graphql.GraphQLError(`Directive @module requires "from" argument to exist and be a path to a typescript file.`, [from != null ? from : node]);
32
+ }
33
+ if ((tsType == null ? void 0 : tsType.kind) !== "StringValue") {
34
+ throw new import_graphql.GraphQLError(`Directive @model requires "tsType" argument to exist and be a name of the exported typescript type`, [tsType != null ? tsType : node]);
35
+ }
36
+ const typeDef = ancestors[ancestors.length - 1];
37
+ if (!typeDef || Array.isArray(typeDef) || typeDef.kind !== "ObjectTypeDefinition" && typeDef.kind !== "ScalarTypeDefinition") {
38
+ throw new import_graphql.GraphQLError("Directive @model must be defined on Object or Scalar type", [node]);
39
+ }
40
+ const typeName = typeDef.name.value;
41
+ return {
42
+ typeName,
43
+ modelName: `_${(0, import_utilities.addModelSuffix)(typeName)}`,
44
+ tsType: tsType.value,
45
+ importName: from ? (0, import_utilities.createVariableNameFromImport)(from.value) : null,
46
+ from: (from == null ? void 0 : from.value) || null,
47
+ directive: node
48
+ };
49
+ }
50
+ const getArgumentValue = (args = [], name) => {
51
+ var _a;
52
+ return (_a = args.find((arg) => arg.name.value === name)) == null ? void 0 : _a.value;
53
+ };
@@ -0,0 +1,37 @@
1
+ // src/codegen/context/model.ts
2
+ import {
3
+ GraphQLError
4
+ } from "graphql";
5
+ import { createVariableNameFromImport, addModelSuffix } from "../utilities.mjs";
6
+ var MODEL_DIRECTIVE_NAME = "model";
7
+ function processModelDirective(node, ancestors) {
8
+ const from = getArgumentValue(node.arguments, "from");
9
+ const tsType = getArgumentValue(node.arguments, "tsType");
10
+ if (from && from.kind !== "StringValue") {
11
+ throw new GraphQLError(`Directive @module requires "from" argument to exist and be a path to a typescript file.`, [from != null ? from : node]);
12
+ }
13
+ if ((tsType == null ? void 0 : tsType.kind) !== "StringValue") {
14
+ throw new GraphQLError(`Directive @model requires "tsType" argument to exist and be a name of the exported typescript type`, [tsType != null ? tsType : node]);
15
+ }
16
+ const typeDef = ancestors[ancestors.length - 1];
17
+ if (!typeDef || Array.isArray(typeDef) || typeDef.kind !== "ObjectTypeDefinition" && typeDef.kind !== "ScalarTypeDefinition") {
18
+ throw new GraphQLError("Directive @model must be defined on Object or Scalar type", [node]);
19
+ }
20
+ const typeName = typeDef.name.value;
21
+ return {
22
+ typeName,
23
+ modelName: `_${addModelSuffix(typeName)}`,
24
+ tsType: tsType.value,
25
+ importName: from ? createVariableNameFromImport(from.value) : null,
26
+ from: (from == null ? void 0 : from.value) || null,
27
+ directive: node
28
+ };
29
+ }
30
+ var getArgumentValue = (args = [], name) => {
31
+ var _a;
32
+ return (_a = args.find((arg) => arg.name.value === name)) == null ? void 0 : _a.value;
33
+ };
34
+ export {
35
+ MODEL_DIRECTIVE_NAME,
36
+ processModelDirective
37
+ };
@@ -0,0 +1,2 @@
1
+ export declare function createImportDeclaration(importNames: string[], from: string, importAlias?: string): import("typescript").ImportDeclaration;
2
+ //# sourceMappingURL=utilities.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../../../src/codegen/context/utilities.ts"],"names":[],"mappings":"AAEA,wBAAgB,uBAAuB,CACrC,WAAW,EAAE,MAAM,EAAE,EACrB,IAAI,EAAE,MAAM,EACZ,WAAW,CAAC,EAAE,MAAM,0CAmBrB"}
@@ -0,0 +1,26 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var utilities_exports = {};
19
+ __export(utilities_exports, {
20
+ createImportDeclaration: () => createImportDeclaration
21
+ });
22
+ module.exports = __toCommonJS(utilities_exports);
23
+ var import_typescript = require("typescript");
24
+ function createImportDeclaration(importNames, from, importAlias) {
25
+ return import_typescript.factory.createImportDeclaration(void 0, void 0, import_typescript.factory.createImportClause(true, void 0, import_typescript.factory.createNamedImports(importNames.map((importName) => import_typescript.factory.createImportSpecifier(importAlias ? import_typescript.factory.createIdentifier(importAlias) : void 0, import_typescript.factory.createIdentifier(importName))))), import_typescript.factory.createStringLiteral(from));
26
+ }
@@ -0,0 +1,8 @@
1
+ // src/codegen/context/utilities.ts
2
+ import { factory } from "typescript";
3
+ function createImportDeclaration(importNames, from, importAlias) {
4
+ return factory.createImportDeclaration(void 0, void 0, factory.createImportClause(true, void 0, factory.createNamedImports(importNames.map((importName) => factory.createImportSpecifier(importAlias ? factory.createIdentifier(importAlias) : void 0, factory.createIdentifier(importName))))), factory.createStringLiteral(from));
5
+ }
6
+ export {
7
+ createImportDeclaration
8
+ };
@@ -0,0 +1,7 @@
1
+ import ts from "typescript";
2
+ import { DocumentNode } from "graphql";
3
+ export declare function generateTS(document: DocumentNode, contextImport?: string, contextName?: string): {
4
+ models: ts.SourceFile;
5
+ resolvers: ts.SourceFile;
6
+ };
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/codegen/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAC5B,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAKvC,wBAAgB,UAAU,CACxB,QAAQ,EAAE,YAAY,EACtB,aAAa,CAAC,EAAE,MAAM,EACtB,WAAW,CAAC,EAAE,MAAM,GACnB;IAAE,MAAM,EAAE,EAAE,CAAC,UAAU,CAAC;IAAC,SAAS,EAAE,EAAE,CAAC,UAAU,CAAA;CAAE,CAkBrD"}