@graphql-codegen/typescript-mongodb 2.3.13-alpha-718ff44f4.0 → 2.4.0-alpha-bd464a586.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/config.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Directives = void 0;
4
+ var Directives;
5
+ (function (Directives) {
6
+ Directives["ID"] = "id";
7
+ Directives["ENTITY"] = "entity";
8
+ Directives["ABSTRACT_ENTITY"] = "abstractEntity";
9
+ Directives["UNION"] = "union";
10
+ Directives["LINK"] = "link";
11
+ Directives["COLUMN"] = "column";
12
+ Directives["EMBEDDED"] = "embedded";
13
+ Directives["MAP"] = "map";
14
+ })(Directives = exports.Directives || (exports.Directives = {}));
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FieldsTree = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
6
+ const set_js_1 = tslib_1.__importDefault(require("lodash/set.js"));
7
+ class FieldsTree {
8
+ constructor() {
9
+ this._fields = {};
10
+ }
11
+ addField(path, type) {
12
+ if (type === undefined) {
13
+ throw new Error('Did not expect type to be undefined');
14
+ }
15
+ (0, set_js_1.default)(this._fields, path, type);
16
+ }
17
+ _getInnerField(root, level = 1) {
18
+ if (typeof root === 'string') {
19
+ return root;
20
+ }
21
+ const fields = Object.keys(root).map(fieldName => {
22
+ const fieldValue = root[fieldName];
23
+ return (0, visitor_plugin_common_1.indent)(`${fieldName}: ${this._getInnerField(fieldValue, level + 1)},`, level);
24
+ });
25
+ return level === 1
26
+ ? fields.join('\n')
27
+ : `{
28
+ ${fields.join('\n')}
29
+ ${(0, visitor_plugin_common_1.indent)('}', level - 1)}`;
30
+ }
31
+ get string() {
32
+ return this._getInnerField(this._fields);
33
+ }
34
+ }
35
+ exports.FieldsTree = FieldsTree;
package/cjs/index.js ADDED
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validate = exports.addToSchema = exports.DIRECTIVES = exports.plugin = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
6
+ const path_1 = require("path");
7
+ const graphql_tag_1 = tslib_1.__importDefault(require("graphql-tag"));
8
+ const visitor_js_1 = require("./visitor.js");
9
+ const config_js_1 = require("./config.js");
10
+ const plugin = (schema, documents, config) => {
11
+ const visitor = new visitor_js_1.TsMongoVisitor(schema, config);
12
+ const astNode = (0, plugin_helpers_1.getCachedDocumentNodeFromSchema)(schema);
13
+ const visitorResult = (0, plugin_helpers_1.oldVisit)(astNode, { leave: visitor });
14
+ const header = visitor.objectIdImport;
15
+ return [header, ...visitorResult.definitions.filter(d => typeof d === 'string')].join('\n');
16
+ };
17
+ exports.plugin = plugin;
18
+ exports.DIRECTIVES = (0, graphql_tag_1.default) `
19
+ directive @${config_js_1.Directives.UNION}(discriminatorField: String, additionalFields: [AdditionalEntityFields]) on UNION
20
+ directive @${config_js_1.Directives.ABSTRACT_ENTITY}(discriminatorField: String!, additionalFields: [AdditionalEntityFields]) on INTERFACE
21
+ directive @${config_js_1.Directives.ENTITY}(embedded: Boolean, additionalFields: [AdditionalEntityFields]) on OBJECT
22
+ directive @${config_js_1.Directives.COLUMN}(overrideType: String) on FIELD_DEFINITION
23
+ directive @${config_js_1.Directives.ID} on FIELD_DEFINITION
24
+ directive @${config_js_1.Directives.LINK}(overrideType: String) on FIELD_DEFINITION
25
+ directive @${config_js_1.Directives.EMBEDDED} on FIELD_DEFINITION
26
+ directive @${config_js_1.Directives.MAP}(path: String!) on FIELD_DEFINITION
27
+ # Inputs
28
+ input AdditionalEntityFields {
29
+ path: String
30
+ type: String
31
+ }
32
+ `;
33
+ exports.addToSchema = exports.DIRECTIVES;
34
+ const validate = async (schema, documents, config, outputFile) => {
35
+ if ((0, path_1.extname)(outputFile) !== '.ts' && (0, path_1.extname)(outputFile) !== '.tsx') {
36
+ throw new Error(`Plugin "typescript-mongodb" requires extension to be ".ts" or ".tsx"!`);
37
+ }
38
+ };
39
+ exports.validate = validate;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -1,58 +1,12 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
-
7
- const pluginHelpers = require('@graphql-codegen/plugin-helpers');
8
- const path = require('path');
9
- const gql = _interopDefault(require('graphql-tag'));
10
- const visitorPluginCommon = require('@graphql-codegen/visitor-plugin-common');
11
- const set = _interopDefault(require('lodash/set.js'));
12
- const autoBind = _interopDefault(require('auto-bind'));
13
- const graphql = require('graphql');
14
-
15
- class FieldsTree {
16
- constructor() {
17
- this._fields = {};
18
- }
19
- addField(path, type) {
20
- if (type === undefined) {
21
- throw new Error('Did not expect type to be undefined');
22
- }
23
- set(this._fields, path, type);
24
- }
25
- _getInnerField(root, level = 1) {
26
- if (typeof root === 'string') {
27
- return root;
28
- }
29
- const fields = Object.keys(root).map(fieldName => {
30
- const fieldValue = root[fieldName];
31
- return visitorPluginCommon.indent(`${fieldName}: ${this._getInnerField(fieldValue, level + 1)},`, level);
32
- });
33
- return level === 1
34
- ? fields.join('\n')
35
- : `{
36
- ${fields.join('\n')}
37
- ${visitorPluginCommon.indent('}', level - 1)}`;
38
- }
39
- get string() {
40
- return this._getInnerField(this._fields);
41
- }
42
- }
43
-
44
- var Directives;
45
- (function (Directives) {
46
- Directives["ID"] = "id";
47
- Directives["ENTITY"] = "entity";
48
- Directives["ABSTRACT_ENTITY"] = "abstractEntity";
49
- Directives["UNION"] = "union";
50
- Directives["LINK"] = "link";
51
- Directives["COLUMN"] = "column";
52
- Directives["EMBEDDED"] = "embedded";
53
- Directives["MAP"] = "map";
54
- })(Directives || (Directives = {}));
55
-
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TsMongoVisitor = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const fields_tree_js_1 = require("./fields-tree.js");
6
+ const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
7
+ const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
8
+ const config_js_1 = require("./config.js");
9
+ const graphql_1 = require("graphql");
56
10
  function resolveObjectId(pointer) {
57
11
  if (!pointer) {
58
12
  return { identifier: 'ObjectId', module: 'mongodb' };
@@ -66,7 +20,7 @@ function resolveObjectId(pointer) {
66
20
  module: null,
67
21
  };
68
22
  }
69
- class TsMongoVisitor extends visitorPluginCommon.BaseVisitor {
23
+ class TsMongoVisitor extends visitor_plugin_common_1.BaseVisitor {
70
24
  constructor(_schema, pluginConfig) {
71
25
  super(pluginConfig, {
72
26
  dbTypeSuffix: pluginConfig.dbTypeSuffix || 'DbObject',
@@ -74,12 +28,12 @@ class TsMongoVisitor extends visitorPluginCommon.BaseVisitor {
74
28
  objectIdType: resolveObjectId(pluginConfig.objectIdType).identifier,
75
29
  objectIdImport: resolveObjectId(pluginConfig.objectIdType).module,
76
30
  idFieldName: pluginConfig.idFieldName || '_id',
77
- enumsAsString: visitorPluginCommon.getConfigValue(pluginConfig.enumsAsString, true),
78
- avoidOptionals: visitorPluginCommon.getConfigValue(pluginConfig.avoidOptionals, false),
79
- scalars: visitorPluginCommon.buildScalarsFromConfig(_schema, pluginConfig),
31
+ enumsAsString: (0, visitor_plugin_common_1.getConfigValue)(pluginConfig.enumsAsString, true),
32
+ avoidOptionals: (0, visitor_plugin_common_1.getConfigValue)(pluginConfig.avoidOptionals, false),
33
+ scalars: (0, visitor_plugin_common_1.buildScalarsFromConfig)(_schema, pluginConfig),
80
34
  });
81
35
  this._schema = _schema;
82
- autoBind(this);
36
+ (0, auto_bind_1.default)(this);
83
37
  }
84
38
  get objectIdImport() {
85
39
  if (this.config.objectIdImport === null) {
@@ -89,17 +43,17 @@ class TsMongoVisitor extends visitorPluginCommon.BaseVisitor {
89
43
  }
90
44
  _resolveDirectiveValue(valueNode) {
91
45
  switch (valueNode.kind) {
92
- case graphql.Kind.INT:
93
- case graphql.Kind.STRING:
94
- case graphql.Kind.FLOAT:
95
- case graphql.Kind.BOOLEAN:
96
- case graphql.Kind.ENUM:
46
+ case graphql_1.Kind.INT:
47
+ case graphql_1.Kind.STRING:
48
+ case graphql_1.Kind.FLOAT:
49
+ case graphql_1.Kind.BOOLEAN:
50
+ case graphql_1.Kind.ENUM:
97
51
  return valueNode.value;
98
- case graphql.Kind.LIST:
52
+ case graphql_1.Kind.LIST:
99
53
  return valueNode.values.map(v => this._resolveDirectiveValue(v));
100
- case graphql.Kind.NULL:
54
+ case graphql_1.Kind.NULL:
101
55
  return null;
102
- case graphql.Kind.OBJECT:
56
+ case graphql_1.Kind.OBJECT:
103
57
  return valueNode.fields.reduce((prev, f) => {
104
58
  return {
105
59
  ...prev,
@@ -134,7 +88,7 @@ class TsMongoVisitor extends visitorPluginCommon.BaseVisitor {
134
88
  return (interfaces || [])
135
89
  .map(namedType => {
136
90
  const schemaType = this._schema.getType(namedType.name.value);
137
- const abstractEntityDirective = this._getDirectiveFromAstNode(schemaType.astNode, Directives.ABSTRACT_ENTITY);
91
+ const abstractEntityDirective = this._getDirectiveFromAstNode(schemaType.astNode, config_js_1.Directives.ABSTRACT_ENTITY);
138
92
  if (!abstractEntityDirective) {
139
93
  return null;
140
94
  }
@@ -143,47 +97,47 @@ class TsMongoVisitor extends visitorPluginCommon.BaseVisitor {
143
97
  .filter(a => a);
144
98
  }
145
99
  _handleIdField(fieldNode, tree, addOptionalSign) {
146
- tree.addField(`${this.config.idFieldName}${addOptionalSign ? '?' : ''}`, visitorPluginCommon.wrapTypeNodeWithModifiers(this.config.objectIdType, fieldNode.type));
100
+ tree.addField(`${this.config.idFieldName}${addOptionalSign ? '?' : ''}`, (0, visitor_plugin_common_1.wrapTypeNodeWithModifiers)(this.config.objectIdType, fieldNode.type));
147
101
  }
148
102
  _handleLinkField(fieldNode, tree, linkDirective, mapPath, addOptionalSign) {
149
103
  const overrideType = this._getDirectiveArgValue(linkDirective, 'overrideType');
150
- const coreType = overrideType || visitorPluginCommon.getBaseTypeNode(fieldNode.type);
104
+ const coreType = overrideType || (0, visitor_plugin_common_1.getBaseTypeNode)(fieldNode.type);
151
105
  const type = this.convertName(coreType, { suffix: this.config.dbTypeSuffix });
152
- tree.addField(`${mapPath || fieldNode.name.value}${addOptionalSign ? '?' : ''}`, visitorPluginCommon.wrapTypeNodeWithModifiers(`${type}['${this.config.idFieldName}']`, fieldNode.type));
106
+ tree.addField(`${mapPath || fieldNode.name.value}${addOptionalSign ? '?' : ''}`, (0, visitor_plugin_common_1.wrapTypeNodeWithModifiers)(`${type}['${this.config.idFieldName}']`, fieldNode.type));
153
107
  }
154
108
  _handleColumnField(fieldNode, tree, columnDirective, mapPath, addOptionalSign) {
155
109
  const overrideType = this._getDirectiveArgValue(columnDirective, 'overrideType');
156
- const coreType = visitorPluginCommon.getBaseTypeNode(fieldNode.type);
110
+ const coreType = (0, visitor_plugin_common_1.getBaseTypeNode)(fieldNode.type);
157
111
  let type = null;
158
112
  if (this.scalars[coreType.name.value]) {
159
113
  type = this.scalars[coreType.name.value];
160
114
  }
161
115
  else {
162
116
  const schemaType = this._schema.getType(coreType.name.value);
163
- if (graphql.isEnumType(schemaType) && this.config.enumsAsString) {
117
+ if ((0, graphql_1.isEnumType)(schemaType) && this.config.enumsAsString) {
164
118
  type = this.scalars.String;
165
119
  }
166
120
  else {
167
121
  type = coreType.name.value;
168
122
  }
169
123
  }
170
- tree.addField(`${mapPath || fieldNode.name.value}${addOptionalSign ? '?' : ''}`, overrideType || visitorPluginCommon.wrapTypeNodeWithModifiers(type, fieldNode.type));
124
+ tree.addField(`${mapPath || fieldNode.name.value}${addOptionalSign ? '?' : ''}`, overrideType || (0, visitor_plugin_common_1.wrapTypeNodeWithModifiers)(type, fieldNode.type));
171
125
  }
172
126
  _handleEmbeddedField(fieldNode, tree, mapPath, addOptionalSign) {
173
- const coreType = visitorPluginCommon.getBaseTypeNode(fieldNode.type);
127
+ const coreType = (0, visitor_plugin_common_1.getBaseTypeNode)(fieldNode.type);
174
128
  const type = this.convertName(coreType, { suffix: this.config.dbTypeSuffix });
175
- tree.addField(`${mapPath || fieldNode.name.value}${addOptionalSign ? '?' : ''}`, visitorPluginCommon.wrapTypeNodeWithModifiers(type, fieldNode.type));
129
+ tree.addField(`${mapPath || fieldNode.name.value}${addOptionalSign ? '?' : ''}`, (0, visitor_plugin_common_1.wrapTypeNodeWithModifiers)(type, fieldNode.type));
176
130
  }
177
131
  _buildFieldsTree(fields) {
178
- const tree = new FieldsTree();
132
+ const tree = new fields_tree_js_1.FieldsTree();
179
133
  fields.forEach(field => {
180
- const idDirective = this._getDirectiveFromAstNode(field, Directives.ID);
181
- const linkDirective = this._getDirectiveFromAstNode(field, Directives.LINK);
182
- const columnDirective = this._getDirectiveFromAstNode(field, Directives.COLUMN);
183
- const embeddedDirective = this._getDirectiveFromAstNode(field, Directives.EMBEDDED);
184
- const mapDirective = this._getDirectiveFromAstNode(field, Directives.MAP);
134
+ const idDirective = this._getDirectiveFromAstNode(field, config_js_1.Directives.ID);
135
+ const linkDirective = this._getDirectiveFromAstNode(field, config_js_1.Directives.LINK);
136
+ const columnDirective = this._getDirectiveFromAstNode(field, config_js_1.Directives.COLUMN);
137
+ const embeddedDirective = this._getDirectiveFromAstNode(field, config_js_1.Directives.EMBEDDED);
138
+ const mapDirective = this._getDirectiveFromAstNode(field, config_js_1.Directives.MAP);
185
139
  const mapPath = this._getDirectiveArgValue(mapDirective, 'path');
186
- const addOptionalSign = !this.config.avoidOptionals && field.type.kind !== graphql.Kind.NON_NULL_TYPE;
140
+ const addOptionalSign = !this.config.avoidOptionals && field.type.kind !== graphql_1.Kind.NON_NULL_TYPE;
187
141
  if (idDirective) {
188
142
  this._handleIdField(field, tree, addOptionalSign);
189
143
  }
@@ -210,7 +164,7 @@ class TsMongoVisitor extends visitorPluginCommon.BaseVisitor {
210
164
  }
211
165
  }
212
166
  InterfaceTypeDefinition(node) {
213
- const abstractEntityDirective = this._getDirectiveFromAstNode(node, Directives.ABSTRACT_ENTITY);
167
+ const abstractEntityDirective = this._getDirectiveFromAstNode(node, config_js_1.Directives.ABSTRACT_ENTITY);
214
168
  if (abstractEntityDirective === null) {
215
169
  return null;
216
170
  }
@@ -219,14 +173,14 @@ class TsMongoVisitor extends visitorPluginCommon.BaseVisitor {
219
173
  const fields = this._buildFieldsTree(node.fields);
220
174
  fields.addField(discriminatorField, this.scalars.String);
221
175
  this._addAdditionalFields(fields, additionalFields);
222
- return new visitorPluginCommon.DeclarationBlock(this._declarationBlockConfig)
176
+ return new visitor_plugin_common_1.DeclarationBlock(this._declarationBlockConfig)
223
177
  .export()
224
178
  .asKind('type')
225
179
  .withName(this.convertName(node, { suffix: this.config.dbInterfaceSuffix }))
226
180
  .withBlock(fields.string).string;
227
181
  }
228
182
  UnionTypeDefinition(node) {
229
- const unionDirective = this._getDirectiveFromAstNode(node, Directives.UNION);
183
+ const unionDirective = this._getDirectiveFromAstNode(node, config_js_1.Directives.UNION);
230
184
  if (unionDirective === null) {
231
185
  return null;
232
186
  }
@@ -234,8 +188,8 @@ class TsMongoVisitor extends visitorPluginCommon.BaseVisitor {
234
188
  const possibleTypes = node.types
235
189
  .map(namedType => {
236
190
  const schemaType = this._schema.getType(namedType.name.value);
237
- const entityDirective = this._getDirectiveFromAstNode(schemaType.astNode, Directives.ENTITY);
238
- const abstractEntityDirective = this._getDirectiveFromAstNode(schemaType.astNode, Directives.ABSTRACT_ENTITY);
191
+ const entityDirective = this._getDirectiveFromAstNode(schemaType.astNode, config_js_1.Directives.ENTITY);
192
+ const abstractEntityDirective = this._getDirectiveFromAstNode(schemaType.astNode, config_js_1.Directives.ABSTRACT_ENTITY);
239
193
  if (entityDirective) {
240
194
  return this.convertName(namedType, { suffix: this.config.dbTypeSuffix });
241
195
  }
@@ -249,10 +203,10 @@ class TsMongoVisitor extends visitorPluginCommon.BaseVisitor {
249
203
  return null;
250
204
  }
251
205
  const additionalFields = this._getDirectiveArgValue(unionDirective, 'additionalFields');
252
- const fields = new FieldsTree();
206
+ const fields = new fields_tree_js_1.FieldsTree();
253
207
  fields.addField(discriminatorField, this.scalars.String);
254
208
  this._addAdditionalFields(fields, additionalFields);
255
- return new visitorPluginCommon.DeclarationBlock(this._declarationBlockConfig)
209
+ return new visitor_plugin_common_1.DeclarationBlock(this._declarationBlockConfig)
256
210
  .export()
257
211
  .asKind('type')
258
212
  .withName(this.convertName(node, { suffix: this.config.dbTypeSuffix }))
@@ -260,7 +214,7 @@ class TsMongoVisitor extends visitorPluginCommon.BaseVisitor {
260
214
  .withBlock(fields.string).string;
261
215
  }
262
216
  ObjectTypeDefinition(node) {
263
- const entityDirective = this._getDirectiveFromAstNode(node, Directives.ENTITY);
217
+ const entityDirective = this._getDirectiveFromAstNode(node, config_js_1.Directives.ENTITY);
264
218
  if (entityDirective === null) {
265
219
  return null;
266
220
  }
@@ -268,7 +222,7 @@ class TsMongoVisitor extends visitorPluginCommon.BaseVisitor {
268
222
  const fields = this._buildFieldsTree(node.fields);
269
223
  const additionalFields = this._getDirectiveArgValue(entityDirective, 'additionalFields');
270
224
  this._addAdditionalFields(fields, additionalFields);
271
- return new visitorPluginCommon.DeclarationBlock(this._declarationBlockConfig)
225
+ return new visitor_plugin_common_1.DeclarationBlock(this._declarationBlockConfig)
272
226
  .export()
273
227
  .asKind('type')
274
228
  .withName(this.convertName(node, { suffix: this.config.dbTypeSuffix }))
@@ -276,37 +230,4 @@ class TsMongoVisitor extends visitorPluginCommon.BaseVisitor {
276
230
  .withBlock(fields.string).string;
277
231
  }
278
232
  }
279
-
280
- const plugin = (schema, documents, config) => {
281
- const visitor = new TsMongoVisitor(schema, config);
282
- const astNode = pluginHelpers.getCachedDocumentNodeFromSchema(schema);
283
- const visitorResult = pluginHelpers.oldVisit(astNode, { leave: visitor });
284
- const header = visitor.objectIdImport;
285
- return [header, ...visitorResult.definitions.filter(d => typeof d === 'string')].join('\n');
286
- };
287
- const DIRECTIVES = gql `
288
- directive @${Directives.UNION}(discriminatorField: String, additionalFields: [AdditionalEntityFields]) on UNION
289
- directive @${Directives.ABSTRACT_ENTITY}(discriminatorField: String!, additionalFields: [AdditionalEntityFields]) on INTERFACE
290
- directive @${Directives.ENTITY}(embedded: Boolean, additionalFields: [AdditionalEntityFields]) on OBJECT
291
- directive @${Directives.COLUMN}(overrideType: String) on FIELD_DEFINITION
292
- directive @${Directives.ID} on FIELD_DEFINITION
293
- directive @${Directives.LINK}(overrideType: String) on FIELD_DEFINITION
294
- directive @${Directives.EMBEDDED} on FIELD_DEFINITION
295
- directive @${Directives.MAP}(path: String!) on FIELD_DEFINITION
296
- # Inputs
297
- input AdditionalEntityFields {
298
- path: String
299
- type: String
300
- }
301
- `;
302
- const addToSchema = DIRECTIVES;
303
- const validate = async (schema, documents, config, outputFile) => {
304
- if (path.extname(outputFile) !== '.ts' && path.extname(outputFile) !== '.tsx') {
305
- throw new Error(`Plugin "typescript-mongodb" requires extension to be ".ts" or ".tsx"!`);
306
- }
307
- };
308
-
309
- exports.DIRECTIVES = DIRECTIVES;
310
- exports.addToSchema = addToSchema;
311
- exports.plugin = plugin;
312
- exports.validate = validate;
233
+ exports.TsMongoVisitor = TsMongoVisitor;
package/esm/config.js ADDED
@@ -0,0 +1,11 @@
1
+ export var Directives;
2
+ (function (Directives) {
3
+ Directives["ID"] = "id";
4
+ Directives["ENTITY"] = "entity";
5
+ Directives["ABSTRACT_ENTITY"] = "abstractEntity";
6
+ Directives["UNION"] = "union";
7
+ Directives["LINK"] = "link";
8
+ Directives["COLUMN"] = "column";
9
+ Directives["EMBEDDED"] = "embedded";
10
+ Directives["MAP"] = "map";
11
+ })(Directives || (Directives = {}));
@@ -0,0 +1,30 @@
1
+ import { indent } from '@graphql-codegen/visitor-plugin-common';
2
+ import set from 'lodash/set.js';
3
+ export class FieldsTree {
4
+ constructor() {
5
+ this._fields = {};
6
+ }
7
+ addField(path, type) {
8
+ if (type === undefined) {
9
+ throw new Error('Did not expect type to be undefined');
10
+ }
11
+ set(this._fields, path, type);
12
+ }
13
+ _getInnerField(root, level = 1) {
14
+ if (typeof root === 'string') {
15
+ return root;
16
+ }
17
+ const fields = Object.keys(root).map(fieldName => {
18
+ const fieldValue = root[fieldName];
19
+ return indent(`${fieldName}: ${this._getInnerField(fieldValue, level + 1)},`, level);
20
+ });
21
+ return level === 1
22
+ ? fields.join('\n')
23
+ : `{
24
+ ${fields.join('\n')}
25
+ ${indent('}', level - 1)}`;
26
+ }
27
+ get string() {
28
+ return this._getInnerField(this._fields);
29
+ }
30
+ }
package/esm/index.js ADDED
@@ -0,0 +1,33 @@
1
+ import { getCachedDocumentNodeFromSchema, oldVisit, } from '@graphql-codegen/plugin-helpers';
2
+ import { extname } from 'path';
3
+ import gql from 'graphql-tag';
4
+ import { TsMongoVisitor } from './visitor.js';
5
+ import { Directives } from './config.js';
6
+ export const plugin = (schema, documents, config) => {
7
+ const visitor = new TsMongoVisitor(schema, config);
8
+ const astNode = getCachedDocumentNodeFromSchema(schema);
9
+ const visitorResult = oldVisit(astNode, { leave: visitor });
10
+ const header = visitor.objectIdImport;
11
+ return [header, ...visitorResult.definitions.filter(d => typeof d === 'string')].join('\n');
12
+ };
13
+ export const DIRECTIVES = gql `
14
+ directive @${Directives.UNION}(discriminatorField: String, additionalFields: [AdditionalEntityFields]) on UNION
15
+ directive @${Directives.ABSTRACT_ENTITY}(discriminatorField: String!, additionalFields: [AdditionalEntityFields]) on INTERFACE
16
+ directive @${Directives.ENTITY}(embedded: Boolean, additionalFields: [AdditionalEntityFields]) on OBJECT
17
+ directive @${Directives.COLUMN}(overrideType: String) on FIELD_DEFINITION
18
+ directive @${Directives.ID} on FIELD_DEFINITION
19
+ directive @${Directives.LINK}(overrideType: String) on FIELD_DEFINITION
20
+ directive @${Directives.EMBEDDED} on FIELD_DEFINITION
21
+ directive @${Directives.MAP}(path: String!) on FIELD_DEFINITION
22
+ # Inputs
23
+ input AdditionalEntityFields {
24
+ path: String
25
+ type: String
26
+ }
27
+ `;
28
+ export const addToSchema = DIRECTIVES;
29
+ export const validate = async (schema, documents, config, outputFile) => {
30
+ if (extname(outputFile) !== '.ts' && extname(outputFile) !== '.tsx') {
31
+ throw new Error(`Plugin "typescript-mongodb" requires extension to be ".ts" or ".tsx"!`);
32
+ }
33
+ };
@@ -1,52 +1,8 @@
1
- import { getCachedDocumentNodeFromSchema, oldVisit } from '@graphql-codegen/plugin-helpers';
2
- import { extname } from 'path';
3
- import gql from 'graphql-tag';
4
- import { indent, BaseVisitor, getConfigValue, buildScalarsFromConfig, wrapTypeNodeWithModifiers, getBaseTypeNode, DeclarationBlock } from '@graphql-codegen/visitor-plugin-common';
5
- import set from 'lodash/set.js';
1
+ import { FieldsTree } from './fields-tree.js';
2
+ import { getBaseTypeNode, DeclarationBlock, getConfigValue, BaseVisitor, buildScalarsFromConfig, wrapTypeNodeWithModifiers, } from '@graphql-codegen/visitor-plugin-common';
6
3
  import autoBind from 'auto-bind';
7
- import { Kind, isEnumType } from 'graphql';
8
-
9
- class FieldsTree {
10
- constructor() {
11
- this._fields = {};
12
- }
13
- addField(path, type) {
14
- if (type === undefined) {
15
- throw new Error('Did not expect type to be undefined');
16
- }
17
- set(this._fields, path, type);
18
- }
19
- _getInnerField(root, level = 1) {
20
- if (typeof root === 'string') {
21
- return root;
22
- }
23
- const fields = Object.keys(root).map(fieldName => {
24
- const fieldValue = root[fieldName];
25
- return indent(`${fieldName}: ${this._getInnerField(fieldValue, level + 1)},`, level);
26
- });
27
- return level === 1
28
- ? fields.join('\n')
29
- : `{
30
- ${fields.join('\n')}
31
- ${indent('}', level - 1)}`;
32
- }
33
- get string() {
34
- return this._getInnerField(this._fields);
35
- }
36
- }
37
-
38
- var Directives;
39
- (function (Directives) {
40
- Directives["ID"] = "id";
41
- Directives["ENTITY"] = "entity";
42
- Directives["ABSTRACT_ENTITY"] = "abstractEntity";
43
- Directives["UNION"] = "union";
44
- Directives["LINK"] = "link";
45
- Directives["COLUMN"] = "column";
46
- Directives["EMBEDDED"] = "embedded";
47
- Directives["MAP"] = "map";
48
- })(Directives || (Directives = {}));
49
-
4
+ import { Directives } from './config.js';
5
+ import { Kind, isEnumType, } from 'graphql';
50
6
  function resolveObjectId(pointer) {
51
7
  if (!pointer) {
52
8
  return { identifier: 'ObjectId', module: 'mongodb' };
@@ -60,7 +16,7 @@ function resolveObjectId(pointer) {
60
16
  module: null,
61
17
  };
62
18
  }
63
- class TsMongoVisitor extends BaseVisitor {
19
+ export class TsMongoVisitor extends BaseVisitor {
64
20
  constructor(_schema, pluginConfig) {
65
21
  super(pluginConfig, {
66
22
  dbTypeSuffix: pluginConfig.dbTypeSuffix || 'DbObject',
@@ -270,34 +226,3 @@ class TsMongoVisitor extends BaseVisitor {
270
226
  .withBlock(fields.string).string;
271
227
  }
272
228
  }
273
-
274
- const plugin = (schema, documents, config) => {
275
- const visitor = new TsMongoVisitor(schema, config);
276
- const astNode = getCachedDocumentNodeFromSchema(schema);
277
- const visitorResult = oldVisit(astNode, { leave: visitor });
278
- const header = visitor.objectIdImport;
279
- return [header, ...visitorResult.definitions.filter(d => typeof d === 'string')].join('\n');
280
- };
281
- const DIRECTIVES = gql `
282
- directive @${Directives.UNION}(discriminatorField: String, additionalFields: [AdditionalEntityFields]) on UNION
283
- directive @${Directives.ABSTRACT_ENTITY}(discriminatorField: String!, additionalFields: [AdditionalEntityFields]) on INTERFACE
284
- directive @${Directives.ENTITY}(embedded: Boolean, additionalFields: [AdditionalEntityFields]) on OBJECT
285
- directive @${Directives.COLUMN}(overrideType: String) on FIELD_DEFINITION
286
- directive @${Directives.ID} on FIELD_DEFINITION
287
- directive @${Directives.LINK}(overrideType: String) on FIELD_DEFINITION
288
- directive @${Directives.EMBEDDED} on FIELD_DEFINITION
289
- directive @${Directives.MAP}(path: String!) on FIELD_DEFINITION
290
- # Inputs
291
- input AdditionalEntityFields {
292
- path: String
293
- type: String
294
- }
295
- `;
296
- const addToSchema = DIRECTIVES;
297
- const validate = async (schema, documents, config, outputFile) => {
298
- if (extname(outputFile) !== '.ts' && extname(outputFile) !== '.tsx') {
299
- throw new Error(`Plugin "typescript-mongodb" requires extension to be ".ts" or ".tsx"!`);
300
- }
301
- };
302
-
303
- export { DIRECTIVES, addToSchema, plugin, validate };
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@graphql-codegen/typescript-mongodb",
3
- "version": "2.3.13-alpha-718ff44f4.0",
3
+ "version": "2.4.0-alpha-bd464a586.0",
4
4
  "description": "GraphQL Code Generator plugin for generting a ready-to-use ORM types for MongoDB",
5
5
  "peerDependencies": {
6
6
  "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0",
7
7
  "graphql-tag": "^2.0.0"
8
8
  },
9
9
  "dependencies": {
10
- "@graphql-codegen/plugin-helpers": "^2.4.0",
11
- "@graphql-codegen/typescript": "^2.7.0-alpha-718ff44f4.0",
12
- "@graphql-codegen/visitor-plugin-common": "2.10.0",
13
- "@graphql-tools/utils": "^8.1.1",
10
+ "@graphql-codegen/plugin-helpers": "^2.5.0-alpha-bd464a586.0",
11
+ "@graphql-codegen/typescript": "^2.7.0-alpha-bd464a586.0",
12
+ "@graphql-codegen/visitor-plugin-common": "2.11.0-alpha-bd464a586.0",
13
+ "@graphql-tools/utils": "^8.8.0",
14
14
  "auto-bind": "~4.0.0",
15
15
  "lodash": "~4.17.0",
16
16
  "tslib": "~2.4.0"
@@ -21,21 +21,28 @@
21
21
  "directory": "packages/plugins/typescript/mongodb"
22
22
  },
23
23
  "license": "MIT",
24
- "main": "index.js",
25
- "module": "index.mjs",
26
- "typings": "index.d.ts",
24
+ "main": "cjs/index.js",
25
+ "module": "esm/index.js",
26
+ "typings": "typings/index.d.ts",
27
27
  "typescript": {
28
- "definition": "index.d.ts"
28
+ "definition": "typings/index.d.ts"
29
29
  },
30
+ "type": "module",
30
31
  "exports": {
31
- "./package.json": "./package.json",
32
32
  ".": {
33
- "require": "./index.js",
34
- "import": "./index.mjs"
33
+ "require": {
34
+ "types": "./typings/index.d.ts",
35
+ "default": "./cjs/index.js"
36
+ },
37
+ "import": {
38
+ "types": "./typings/index.d.ts",
39
+ "default": "./esm/index.js"
40
+ },
41
+ "default": {
42
+ "types": "./typings/index.d.ts",
43
+ "default": "./esm/index.js"
44
+ }
35
45
  },
36
- "./*": {
37
- "require": "./*.js",
38
- "import": "./*.mjs"
39
- }
46
+ "./package.json": "./package.json"
40
47
  }
41
- }
48
+ }
File without changes
File without changes
@@ -1,5 +1,5 @@
1
1
  import { PluginFunction, PluginValidateFn } from '@graphql-codegen/plugin-helpers';
2
- import { TypeScriptMongoPluginConfig } from './config';
2
+ import { TypeScriptMongoPluginConfig } from './config.js';
3
3
  export declare const plugin: PluginFunction<TypeScriptMongoPluginConfig>;
4
4
  export declare const DIRECTIVES: import("graphql").DocumentNode;
5
5
  export declare const addToSchema: import("graphql").DocumentNode;
@@ -1,5 +1,5 @@
1
1
  import { ParsedConfig, BaseVisitor } from '@graphql-codegen/visitor-plugin-common';
2
- import { TypeScriptMongoPluginConfig } from './config';
2
+ import { TypeScriptMongoPluginConfig } from './config.js';
3
3
  import { GraphQLSchema, ObjectTypeDefinitionNode, InterfaceTypeDefinitionNode, UnionTypeDefinitionNode } from 'graphql';
4
4
  export interface TypeScriptMongoPluginParsedConfig extends ParsedConfig {
5
5
  dbTypeSuffix: string;