@graphql-codegen/visitor-plugin-common 3.2.0-alpha-20230510113412-eb4264cff → 3.2.0-alpha-20230511145543-742e6af1b

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.
@@ -537,7 +537,7 @@ class BaseResolversVisitor extends base_visitor_js_1.BaseVisitor {
537
537
  return this.wrapWithArray(asString);
538
538
  }
539
539
  _getScalar(name) {
540
- return `${this.config.namespacedImportName ? this.config.namespacedImportName + '.' : ''}Scalars['${name}']['output']`;
540
+ return `${this.config.namespacedImportName ? this.config.namespacedImportName + '.' : ''}Scalars['${name}']`;
541
541
  }
542
542
  NamedType(node) {
543
543
  const nameStr = node.name;
@@ -52,16 +52,15 @@ class BaseTypesVisitor extends base_visitor_js_1.BaseVisitor {
52
52
  return '';
53
53
  }
54
54
  getScalarsImports() {
55
- return Object.keys(this.config.scalars).reduce((res, enumName) => {
55
+ return Object.keys(this.config.scalars)
56
+ .map(enumName => {
56
57
  const mappedValue = this.config.scalars[enumName];
57
- if (mappedValue.input.isExternal) {
58
- res.push(this._buildTypeImport(mappedValue.input.import, mappedValue.input.source, mappedValue.input.default));
59
- }
60
- if (mappedValue.output.isExternal) {
61
- res.push(this._buildTypeImport(mappedValue.output.import, mappedValue.output.source, mappedValue.output.default));
58
+ if (mappedValue.isExternal) {
59
+ return this._buildTypeImport(mappedValue.import, mappedValue.source, mappedValue.default);
62
60
  }
63
- return res;
64
- }, []);
61
+ return null;
62
+ })
63
+ .filter(a => a);
65
64
  }
66
65
  getDirectiveArgumentAndInputFieldMappingsImports() {
67
66
  return Object.keys(this.config.directiveArgumentAndInputFieldMappings)
@@ -78,13 +77,11 @@ class BaseTypesVisitor extends base_visitor_js_1.BaseVisitor {
78
77
  if (this.config.onlyEnums)
79
78
  return '';
80
79
  const allScalars = Object.keys(this.config.scalars).map(scalarName => {
81
- const inputScalarValue = this.config.scalars[scalarName].input.type;
82
- const outputScalarValue = this.config.scalars[scalarName].output.type;
80
+ const scalarValue = this.config.scalars[scalarName].type;
83
81
  const scalarType = this._schema.getType(scalarName);
84
82
  const comment = (scalarType === null || scalarType === void 0 ? void 0 : scalarType.astNode) && scalarType.description ? (0, utils_js_1.transformComment)(scalarType.description, 1) : '';
85
83
  const { scalar } = this._parsedConfig.declarationKind;
86
- return (comment +
87
- (0, utils_js_1.indent)(`${scalarName}: { input: ${inputScalarValue}${this.getPunctuation(scalar)} output: ${outputScalarValue}${this.getPunctuation(scalar)} }`));
84
+ return comment + (0, utils_js_1.indent)(`${scalarName}: ${scalarValue}${this.getPunctuation(scalar)}`);
88
85
  });
89
86
  return new utils_js_1.DeclarationBlock(this._declarationBlockConfig)
90
87
  .export()
@@ -177,7 +174,7 @@ class BaseTypesVisitor extends base_visitor_js_1.BaseVisitor {
177
174
  return '';
178
175
  const originalNode = parent[key];
179
176
  const possibleTypes = originalNode.types
180
- .map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value, 'output') : this.convertName(t)))
177
+ .map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value) : this.convertName(t)))
181
178
  .join(' | ');
182
179
  return new utils_js_1.DeclarationBlock(this._declarationBlockConfig)
183
180
  .export()
@@ -371,8 +368,8 @@ class BaseTypesVisitor extends base_visitor_js_1.BaseVisitor {
371
368
  })
372
369
  .join('\n\n');
373
370
  }
374
- _getScalar(name, type) {
375
- return `Scalars['${name}']['${type}']`;
371
+ _getScalar(name) {
372
+ return `Scalars['${name}']`;
376
373
  }
377
374
  _getDirectiveArgumentNadInputFieldMapping(name) {
378
375
  return `DirectiveArgumentAndInputFieldMappings['${name}']`;
@@ -390,10 +387,10 @@ class BaseTypesVisitor extends base_visitor_js_1.BaseVisitor {
390
387
  .find(a => !!a);
391
388
  return type || null;
392
389
  }
393
- _getTypeForNode(node, isVisitingInputType) {
390
+ _getTypeForNode(node) {
394
391
  const typeAsString = node.name;
395
392
  if (this.scalars[typeAsString]) {
396
- return this._getScalar(typeAsString, isVisitingInputType ? 'input' : 'output');
393
+ return this._getScalar(typeAsString);
397
394
  }
398
395
  if (this.config.enumValues[typeAsString]) {
399
396
  return this.config.enumValues[typeAsString].typeIdentifier;
@@ -407,7 +404,7 @@ class BaseTypesVisitor extends base_visitor_js_1.BaseVisitor {
407
404
  NamedType(node, key, parent, path, ancestors) {
408
405
  const currentVisitContext = this.getVisitorKindContextFromAncestors(ancestors);
409
406
  const isVisitingInputType = currentVisitContext.includes(graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION);
410
- const typeToUse = this._getTypeForNode(node, isVisitingInputType);
407
+ const typeToUse = this._getTypeForNode(node);
411
408
  if (!isVisitingInputType && this.config.fieldWrapperValue && this.config.wrapFieldDefinitions) {
412
409
  return `FieldWrapper<${typeToUse}>`;
413
410
  }
@@ -6,7 +6,7 @@ const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
6
6
  const naming_js_1 = require("./naming.js");
7
7
  class BaseVisitor {
8
8
  constructor(rawConfig, additionalConfig) {
9
- var _a, _b, _c, _d, _e;
9
+ var _a;
10
10
  this._declarationBlockConfig = {};
11
11
  this._parsedConfig = {
12
12
  convert: (0, naming_js_1.convertFactory)(rawConfig),
@@ -25,10 +25,7 @@ class BaseVisitor {
25
25
  };
26
26
  this.scalars = {};
27
27
  for (const key of Object.keys(this.config.scalars || {})) {
28
- this.scalars[key] = {
29
- input: (_c = (_b = this.config.scalars[key]) === null || _b === void 0 ? void 0 : _b.input) === null || _c === void 0 ? void 0 : _c.type,
30
- output: (_e = (_d = this.config.scalars[key]) === null || _d === void 0 ? void 0 : _d.output) === null || _e === void 0 ? void 0 : _e.type,
31
- };
28
+ this.scalars[key] = this.config.scalars[key].type;
32
29
  }
33
30
  (0, auto_bind_1.default)(this);
34
31
  }
package/cjs/scalars.js CHANGED
@@ -2,24 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DEFAULT_SCALARS = void 0;
4
4
  exports.DEFAULT_SCALARS = {
5
- ID: {
6
- input: 'string | number',
7
- output: 'string',
8
- },
9
- String: {
10
- input: 'string',
11
- output: 'string',
12
- },
13
- Boolean: {
14
- input: 'boolean',
15
- output: 'boolean',
16
- },
17
- Int: {
18
- input: 'number',
19
- output: 'number',
20
- },
21
- Float: {
22
- input: 'number',
23
- output: 'number',
24
- },
5
+ ID: 'string',
6
+ String: 'string',
7
+ Boolean: 'boolean',
8
+ Int: 'number',
9
+ Float: 'number',
25
10
  };
@@ -36,7 +36,7 @@ class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
36
36
  this.config.convertName(baseType.name, { useTypesPrefix: this.config.enumPrefix });
37
37
  }
38
38
  else if (this.config.scalars[baseType.name]) {
39
- typeToUse = this.config.scalars[baseType.name].output;
39
+ typeToUse = this.config.scalars[baseType.name];
40
40
  }
41
41
  const wrappedType = this.config.wrapTypeWithModifiers(typeToUse, fieldObj.type);
42
42
  return {
@@ -50,7 +50,6 @@ class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
50
50
  return [];
51
51
  }
52
52
  return fields.map(aliasedField => {
53
- var _a;
54
53
  if (aliasedField.fieldName === '__typename') {
55
54
  const name = this.config.formatNamedField(aliasedField.alias, null);
56
55
  return {
@@ -60,7 +59,7 @@ class PreResolveTypesProcessor extends base_js_1.BaseSelectionSetProcessor {
60
59
  }
61
60
  const fieldObj = schemaType.getFields()[aliasedField.fieldName];
62
61
  const baseType = (0, plugin_helpers_1.getBaseType)(fieldObj.type);
63
- let typeToUse = ((_a = this.config.scalars[baseType.name]) === null || _a === void 0 ? void 0 : _a.output) || baseType.name;
62
+ let typeToUse = this.config.scalars[baseType.name] || baseType.name;
64
63
  if ((0, graphql_1.isEnumType)(baseType)) {
65
64
  typeToUse =
66
65
  (this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '') +
package/cjs/utils.js CHANGED
@@ -200,23 +200,8 @@ function buildScalarsFromConfig(schema, config, defaultScalarsMapping = scalars_
200
200
  exports.buildScalarsFromConfig = buildScalarsFromConfig;
201
201
  function buildScalars(schema, scalarsMapping, defaultScalarsMapping = scalars_js_1.DEFAULT_SCALARS, defaultScalarType = 'any') {
202
202
  const result = {};
203
- function normalizeScalarType(type) {
204
- if (typeof type === 'string') {
205
- return {
206
- input: type,
207
- output: type,
208
- };
209
- }
210
- return {
211
- input: type.input,
212
- output: type.output,
213
- };
214
- }
215
203
  for (const name of Object.keys(defaultScalarsMapping)) {
216
- result[name] = {
217
- input: (0, mappers_js_1.parseMapper)(defaultScalarsMapping[name].input),
218
- output: (0, mappers_js_1.parseMapper)(defaultScalarsMapping[name].output),
219
- };
204
+ result[name] = (0, mappers_js_1.parseMapper)(defaultScalarsMapping[name]);
220
205
  }
221
206
  if (schema) {
222
207
  const typeMap = schema.getTypeMap();
@@ -227,71 +212,23 @@ function buildScalars(schema, scalarsMapping, defaultScalarsMapping = scalars_js
227
212
  var _a;
228
213
  const { name } = scalarType;
229
214
  if (typeof scalarsMapping === 'string') {
230
- const inputMapper = (0, mappers_js_1.parseMapper)(scalarsMapping + '#' + name, name);
231
- if (inputMapper.isExternal) {
232
- inputMapper.type += "['input']";
233
- }
234
- const outputMapper = (0, mappers_js_1.parseMapper)(scalarsMapping + '#' + name, name);
235
- if (outputMapper.isExternal) {
236
- outputMapper.type += "['output']";
237
- }
238
- result[name] = {
239
- input: inputMapper,
240
- output: outputMapper,
241
- };
215
+ const value = (0, mappers_js_1.parseMapper)(scalarsMapping + '#' + name, name);
216
+ result[name] = value;
242
217
  }
243
218
  else if (scalarsMapping && typeof scalarsMapping[name] === 'string') {
244
- const normalizedScalar = normalizeScalarType(scalarsMapping[name]);
245
- const inputMapper = (0, mappers_js_1.parseMapper)(normalizedScalar.input, name);
246
- if (inputMapper.isExternal) {
247
- inputMapper.type += "['input']";
248
- }
249
- const outputMapper = (0, mappers_js_1.parseMapper)(normalizedScalar.output, name);
250
- if (outputMapper.isExternal) {
251
- outputMapper.type += "['output']";
252
- }
253
- result[name] = {
254
- input: inputMapper,
255
- output: outputMapper,
256
- };
219
+ const value = (0, mappers_js_1.parseMapper)(scalarsMapping[name], name);
220
+ result[name] = value;
257
221
  }
258
222
  else if (scalarsMapping === null || scalarsMapping === void 0 ? void 0 : scalarsMapping[name]) {
259
- const mappedScalar = scalarsMapping[name];
260
- if (typeof mappedScalar === 'object' && mappedScalar.input && mappedScalar.output) {
261
- result[name] = {
262
- input: {
263
- isExternal: false,
264
- type: mappedScalar.input,
265
- },
266
- output: {
267
- isExternal: false,
268
- type: mappedScalar.output,
269
- },
270
- };
271
- }
272
- else {
273
- result[name] = {
274
- input: {
275
- isExternal: false,
276
- type: JSON.stringify(mappedScalar),
277
- },
278
- output: {
279
- isExternal: false,
280
- type: JSON.stringify(mappedScalar),
281
- },
282
- };
283
- }
223
+ result[name] = {
224
+ isExternal: false,
225
+ type: JSON.stringify(scalarsMapping[name]),
226
+ };
284
227
  }
285
228
  else if ((_a = scalarType.extensions) === null || _a === void 0 ? void 0 : _a.codegenScalarType) {
286
229
  result[name] = {
287
- input: {
288
- isExternal: false,
289
- type: scalarType.extensions.codegenScalarType,
290
- },
291
- output: {
292
- isExternal: false,
293
- type: scalarType.extensions.codegenScalarType,
294
- },
230
+ isExternal: false,
231
+ type: scalarType.extensions.codegenScalarType,
295
232
  };
296
233
  }
297
234
  else if (!defaultScalarsMapping[name]) {
@@ -299,14 +236,8 @@ function buildScalars(schema, scalarsMapping, defaultScalarsMapping = scalars_js
299
236
  throw new Error(`Unknown scalar type ${name}. Please override it using the "scalars" configuration field!`);
300
237
  }
301
238
  result[name] = {
302
- input: {
303
- isExternal: false,
304
- type: defaultScalarType,
305
- },
306
- output: {
307
- isExternal: false,
308
- type: defaultScalarType,
309
- },
239
+ isExternal: false,
240
+ type: defaultScalarType,
310
241
  };
311
242
  }
312
243
  });
@@ -317,23 +248,13 @@ function buildScalars(schema, scalarsMapping, defaultScalarsMapping = scalars_js
317
248
  }
318
249
  for (const name of Object.keys(scalarsMapping)) {
319
250
  if (typeof scalarsMapping[name] === 'string') {
320
- const normalizedScalar = normalizeScalarType(scalarsMapping[name]);
321
- result[name] = {
322
- input: (0, mappers_js_1.parseMapper)(normalizedScalar.input, name),
323
- output: (0, mappers_js_1.parseMapper)(normalizedScalar.output, name),
324
- };
251
+ const value = (0, mappers_js_1.parseMapper)(scalarsMapping[name], name);
252
+ result[name] = value;
325
253
  }
326
254
  else {
327
- const normalizedScalar = normalizeScalarType(scalarsMapping[name]);
328
255
  result[name] = {
329
- input: {
330
- isExternal: false,
331
- type: JSON.stringify(normalizedScalar.input),
332
- },
333
- output: {
334
- isExternal: false,
335
- type: JSON.stringify(normalizedScalar.output),
336
- },
256
+ isExternal: false,
257
+ type: JSON.stringify(scalarsMapping[name]),
337
258
  };
338
259
  }
339
260
  }
@@ -38,7 +38,7 @@ class OperationVariablesToObject {
38
38
  }
39
39
  getScalar(name) {
40
40
  const prefix = this._namespacedImportName ? `${this._namespacedImportName}.` : '';
41
- return `${prefix}Scalars['${name}']['input']`;
41
+ return `${prefix}Scalars['${name}']`;
42
42
  }
43
43
  getDirectiveMapping(name) {
44
44
  return `DirectiveArgumentAndInputFieldMappings['${name}']`;
@@ -533,7 +533,7 @@ export class BaseResolversVisitor extends BaseVisitor {
533
533
  return this.wrapWithArray(asString);
534
534
  }
535
535
  _getScalar(name) {
536
- return `${this.config.namespacedImportName ? this.config.namespacedImportName + '.' : ''}Scalars['${name}']['output']`;
536
+ return `${this.config.namespacedImportName ? this.config.namespacedImportName + '.' : ''}Scalars['${name}']`;
537
537
  }
538
538
  NamedType(node) {
539
539
  const nameStr = node.name;
@@ -49,16 +49,15 @@ export class BaseTypesVisitor extends BaseVisitor {
49
49
  return '';
50
50
  }
51
51
  getScalarsImports() {
52
- return Object.keys(this.config.scalars).reduce((res, enumName) => {
52
+ return Object.keys(this.config.scalars)
53
+ .map(enumName => {
53
54
  const mappedValue = this.config.scalars[enumName];
54
- if (mappedValue.input.isExternal) {
55
- res.push(this._buildTypeImport(mappedValue.input.import, mappedValue.input.source, mappedValue.input.default));
56
- }
57
- if (mappedValue.output.isExternal) {
58
- res.push(this._buildTypeImport(mappedValue.output.import, mappedValue.output.source, mappedValue.output.default));
55
+ if (mappedValue.isExternal) {
56
+ return this._buildTypeImport(mappedValue.import, mappedValue.source, mappedValue.default);
59
57
  }
60
- return res;
61
- }, []);
58
+ return null;
59
+ })
60
+ .filter(a => a);
62
61
  }
63
62
  getDirectiveArgumentAndInputFieldMappingsImports() {
64
63
  return Object.keys(this.config.directiveArgumentAndInputFieldMappings)
@@ -75,13 +74,11 @@ export class BaseTypesVisitor extends BaseVisitor {
75
74
  if (this.config.onlyEnums)
76
75
  return '';
77
76
  const allScalars = Object.keys(this.config.scalars).map(scalarName => {
78
- const inputScalarValue = this.config.scalars[scalarName].input.type;
79
- const outputScalarValue = this.config.scalars[scalarName].output.type;
77
+ const scalarValue = this.config.scalars[scalarName].type;
80
78
  const scalarType = this._schema.getType(scalarName);
81
79
  const comment = (scalarType === null || scalarType === void 0 ? void 0 : scalarType.astNode) && scalarType.description ? transformComment(scalarType.description, 1) : '';
82
80
  const { scalar } = this._parsedConfig.declarationKind;
83
- return (comment +
84
- indent(`${scalarName}: { input: ${inputScalarValue}${this.getPunctuation(scalar)} output: ${outputScalarValue}${this.getPunctuation(scalar)} }`));
81
+ return comment + indent(`${scalarName}: ${scalarValue}${this.getPunctuation(scalar)}`);
85
82
  });
86
83
  return new DeclarationBlock(this._declarationBlockConfig)
87
84
  .export()
@@ -174,7 +171,7 @@ export class BaseTypesVisitor extends BaseVisitor {
174
171
  return '';
175
172
  const originalNode = parent[key];
176
173
  const possibleTypes = originalNode.types
177
- .map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value, 'output') : this.convertName(t)))
174
+ .map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value) : this.convertName(t)))
178
175
  .join(' | ');
179
176
  return new DeclarationBlock(this._declarationBlockConfig)
180
177
  .export()
@@ -368,8 +365,8 @@ export class BaseTypesVisitor extends BaseVisitor {
368
365
  })
369
366
  .join('\n\n');
370
367
  }
371
- _getScalar(name, type) {
372
- return `Scalars['${name}']['${type}']`;
368
+ _getScalar(name) {
369
+ return `Scalars['${name}']`;
373
370
  }
374
371
  _getDirectiveArgumentNadInputFieldMapping(name) {
375
372
  return `DirectiveArgumentAndInputFieldMappings['${name}']`;
@@ -387,10 +384,10 @@ export class BaseTypesVisitor extends BaseVisitor {
387
384
  .find(a => !!a);
388
385
  return type || null;
389
386
  }
390
- _getTypeForNode(node, isVisitingInputType) {
387
+ _getTypeForNode(node) {
391
388
  const typeAsString = node.name;
392
389
  if (this.scalars[typeAsString]) {
393
- return this._getScalar(typeAsString, isVisitingInputType ? 'input' : 'output');
390
+ return this._getScalar(typeAsString);
394
391
  }
395
392
  if (this.config.enumValues[typeAsString]) {
396
393
  return this.config.enumValues[typeAsString].typeIdentifier;
@@ -404,7 +401,7 @@ export class BaseTypesVisitor extends BaseVisitor {
404
401
  NamedType(node, key, parent, path, ancestors) {
405
402
  const currentVisitContext = this.getVisitorKindContextFromAncestors(ancestors);
406
403
  const isVisitingInputType = currentVisitContext.includes(Kind.INPUT_OBJECT_TYPE_DEFINITION);
407
- const typeToUse = this._getTypeForNode(node, isVisitingInputType);
404
+ const typeToUse = this._getTypeForNode(node);
408
405
  if (!isVisitingInputType && this.config.fieldWrapperValue && this.config.wrapFieldDefinitions) {
409
406
  return `FieldWrapper<${typeToUse}>`;
410
407
  }
@@ -2,7 +2,7 @@ import autoBind from 'auto-bind';
2
2
  import { convertFactory } from './naming.js';
3
3
  export class BaseVisitor {
4
4
  constructor(rawConfig, additionalConfig) {
5
- var _a, _b, _c, _d, _e;
5
+ var _a;
6
6
  this._declarationBlockConfig = {};
7
7
  this._parsedConfig = {
8
8
  convert: convertFactory(rawConfig),
@@ -21,10 +21,7 @@ export class BaseVisitor {
21
21
  };
22
22
  this.scalars = {};
23
23
  for (const key of Object.keys(this.config.scalars || {})) {
24
- this.scalars[key] = {
25
- input: (_c = (_b = this.config.scalars[key]) === null || _b === void 0 ? void 0 : _b.input) === null || _c === void 0 ? void 0 : _c.type,
26
- output: (_e = (_d = this.config.scalars[key]) === null || _d === void 0 ? void 0 : _d.output) === null || _e === void 0 ? void 0 : _e.type,
27
- };
24
+ this.scalars[key] = this.config.scalars[key].type;
28
25
  }
29
26
  autoBind(this);
30
27
  }
package/esm/scalars.js CHANGED
@@ -1,22 +1,7 @@
1
1
  export const DEFAULT_SCALARS = {
2
- ID: {
3
- input: 'string | number',
4
- output: 'string',
5
- },
6
- String: {
7
- input: 'string',
8
- output: 'string',
9
- },
10
- Boolean: {
11
- input: 'boolean',
12
- output: 'boolean',
13
- },
14
- Int: {
15
- input: 'number',
16
- output: 'number',
17
- },
18
- Float: {
19
- input: 'number',
20
- output: 'number',
21
- },
2
+ ID: 'string',
3
+ String: 'string',
4
+ Boolean: 'boolean',
5
+ Int: 'number',
6
+ Float: 'number',
22
7
  };
@@ -33,7 +33,7 @@ export class PreResolveTypesProcessor extends BaseSelectionSetProcessor {
33
33
  this.config.convertName(baseType.name, { useTypesPrefix: this.config.enumPrefix });
34
34
  }
35
35
  else if (this.config.scalars[baseType.name]) {
36
- typeToUse = this.config.scalars[baseType.name].output;
36
+ typeToUse = this.config.scalars[baseType.name];
37
37
  }
38
38
  const wrappedType = this.config.wrapTypeWithModifiers(typeToUse, fieldObj.type);
39
39
  return {
@@ -47,7 +47,6 @@ export class PreResolveTypesProcessor extends BaseSelectionSetProcessor {
47
47
  return [];
48
48
  }
49
49
  return fields.map(aliasedField => {
50
- var _a;
51
50
  if (aliasedField.fieldName === '__typename') {
52
51
  const name = this.config.formatNamedField(aliasedField.alias, null);
53
52
  return {
@@ -57,7 +56,7 @@ export class PreResolveTypesProcessor extends BaseSelectionSetProcessor {
57
56
  }
58
57
  const fieldObj = schemaType.getFields()[aliasedField.fieldName];
59
58
  const baseType = getBaseType(fieldObj.type);
60
- let typeToUse = ((_a = this.config.scalars[baseType.name]) === null || _a === void 0 ? void 0 : _a.output) || baseType.name;
59
+ let typeToUse = this.config.scalars[baseType.name] || baseType.name;
61
60
  if (isEnumType(baseType)) {
62
61
  typeToUse =
63
62
  (this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '') +
package/esm/utils.js CHANGED
@@ -185,23 +185,8 @@ export function buildScalarsFromConfig(schema, config, defaultScalarsMapping = D
185
185
  }
186
186
  export function buildScalars(schema, scalarsMapping, defaultScalarsMapping = DEFAULT_SCALARS, defaultScalarType = 'any') {
187
187
  const result = {};
188
- function normalizeScalarType(type) {
189
- if (typeof type === 'string') {
190
- return {
191
- input: type,
192
- output: type,
193
- };
194
- }
195
- return {
196
- input: type.input,
197
- output: type.output,
198
- };
199
- }
200
188
  for (const name of Object.keys(defaultScalarsMapping)) {
201
- result[name] = {
202
- input: parseMapper(defaultScalarsMapping[name].input),
203
- output: parseMapper(defaultScalarsMapping[name].output),
204
- };
189
+ result[name] = parseMapper(defaultScalarsMapping[name]);
205
190
  }
206
191
  if (schema) {
207
192
  const typeMap = schema.getTypeMap();
@@ -212,71 +197,23 @@ export function buildScalars(schema, scalarsMapping, defaultScalarsMapping = DEF
212
197
  var _a;
213
198
  const { name } = scalarType;
214
199
  if (typeof scalarsMapping === 'string') {
215
- const inputMapper = parseMapper(scalarsMapping + '#' + name, name);
216
- if (inputMapper.isExternal) {
217
- inputMapper.type += "['input']";
218
- }
219
- const outputMapper = parseMapper(scalarsMapping + '#' + name, name);
220
- if (outputMapper.isExternal) {
221
- outputMapper.type += "['output']";
222
- }
223
- result[name] = {
224
- input: inputMapper,
225
- output: outputMapper,
226
- };
200
+ const value = parseMapper(scalarsMapping + '#' + name, name);
201
+ result[name] = value;
227
202
  }
228
203
  else if (scalarsMapping && typeof scalarsMapping[name] === 'string') {
229
- const normalizedScalar = normalizeScalarType(scalarsMapping[name]);
230
- const inputMapper = parseMapper(normalizedScalar.input, name);
231
- if (inputMapper.isExternal) {
232
- inputMapper.type += "['input']";
233
- }
234
- const outputMapper = parseMapper(normalizedScalar.output, name);
235
- if (outputMapper.isExternal) {
236
- outputMapper.type += "['output']";
237
- }
238
- result[name] = {
239
- input: inputMapper,
240
- output: outputMapper,
241
- };
204
+ const value = parseMapper(scalarsMapping[name], name);
205
+ result[name] = value;
242
206
  }
243
207
  else if (scalarsMapping === null || scalarsMapping === void 0 ? void 0 : scalarsMapping[name]) {
244
- const mappedScalar = scalarsMapping[name];
245
- if (typeof mappedScalar === 'object' && mappedScalar.input && mappedScalar.output) {
246
- result[name] = {
247
- input: {
248
- isExternal: false,
249
- type: mappedScalar.input,
250
- },
251
- output: {
252
- isExternal: false,
253
- type: mappedScalar.output,
254
- },
255
- };
256
- }
257
- else {
258
- result[name] = {
259
- input: {
260
- isExternal: false,
261
- type: JSON.stringify(mappedScalar),
262
- },
263
- output: {
264
- isExternal: false,
265
- type: JSON.stringify(mappedScalar),
266
- },
267
- };
268
- }
208
+ result[name] = {
209
+ isExternal: false,
210
+ type: JSON.stringify(scalarsMapping[name]),
211
+ };
269
212
  }
270
213
  else if ((_a = scalarType.extensions) === null || _a === void 0 ? void 0 : _a.codegenScalarType) {
271
214
  result[name] = {
272
- input: {
273
- isExternal: false,
274
- type: scalarType.extensions.codegenScalarType,
275
- },
276
- output: {
277
- isExternal: false,
278
- type: scalarType.extensions.codegenScalarType,
279
- },
215
+ isExternal: false,
216
+ type: scalarType.extensions.codegenScalarType,
280
217
  };
281
218
  }
282
219
  else if (!defaultScalarsMapping[name]) {
@@ -284,14 +221,8 @@ export function buildScalars(schema, scalarsMapping, defaultScalarsMapping = DEF
284
221
  throw new Error(`Unknown scalar type ${name}. Please override it using the "scalars" configuration field!`);
285
222
  }
286
223
  result[name] = {
287
- input: {
288
- isExternal: false,
289
- type: defaultScalarType,
290
- },
291
- output: {
292
- isExternal: false,
293
- type: defaultScalarType,
294
- },
224
+ isExternal: false,
225
+ type: defaultScalarType,
295
226
  };
296
227
  }
297
228
  });
@@ -302,23 +233,13 @@ export function buildScalars(schema, scalarsMapping, defaultScalarsMapping = DEF
302
233
  }
303
234
  for (const name of Object.keys(scalarsMapping)) {
304
235
  if (typeof scalarsMapping[name] === 'string') {
305
- const normalizedScalar = normalizeScalarType(scalarsMapping[name]);
306
- result[name] = {
307
- input: parseMapper(normalizedScalar.input, name),
308
- output: parseMapper(normalizedScalar.output, name),
309
- };
236
+ const value = parseMapper(scalarsMapping[name], name);
237
+ result[name] = value;
310
238
  }
311
239
  else {
312
- const normalizedScalar = normalizeScalarType(scalarsMapping[name]);
313
240
  result[name] = {
314
- input: {
315
- isExternal: false,
316
- type: JSON.stringify(normalizedScalar.input),
317
- },
318
- output: {
319
- isExternal: false,
320
- type: JSON.stringify(normalizedScalar.output),
321
- },
241
+ isExternal: false,
242
+ type: JSON.stringify(scalarsMapping[name]),
322
243
  };
323
244
  }
324
245
  }
@@ -34,7 +34,7 @@ export class OperationVariablesToObject {
34
34
  }
35
35
  getScalar(name) {
36
36
  const prefix = this._namespacedImportName ? `${this._namespacedImportName}.` : '';
37
- return `${prefix}Scalars['${name}']['input']`;
37
+ return `${prefix}Scalars['${name}']`;
38
38
  }
39
39
  getDirectiveMapping(name) {
40
40
  return `DirectiveArgumentAndInputFieldMappings['${name}']`;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@graphql-codegen/visitor-plugin-common",
3
- "version": "3.2.0-alpha-20230510113412-eb4264cff",
3
+ "version": "3.2.0-alpha-20230511145543-742e6af1b",
4
4
  "peerDependencies": {
5
5
  "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"
6
6
  },
7
7
  "dependencies": {
8
8
  "@graphql-tools/optimize": "^1.3.0",
9
- "@graphql-codegen/plugin-helpers": "4.2.1-alpha-20230510113412-eb4264cff",
9
+ "@graphql-codegen/plugin-helpers": "4.2.1-alpha-20230511145543-742e6af1b",
10
10
  "@graphql-tools/relay-operation-optimizer": "^6.5.0",
11
11
  "@graphql-tools/utils": "^9.0.0",
12
12
  "auto-bind": "~4.0.0",
@@ -463,10 +463,10 @@ export declare class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTyp
463
463
  getArgumentsObjectDeclarationBlock(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, name: string, field: FieldDefinitionNode): DeclarationBlock;
464
464
  getArgumentsObjectTypeDefinition(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, name: string, field: FieldDefinitionNode): string;
465
465
  protected buildArgumentsBlock(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode): string;
466
- protected _getScalar(name: string, type: 'input' | 'output'): string;
466
+ protected _getScalar(name: string): string;
467
467
  protected _getDirectiveArgumentNadInputFieldMapping(name: string): string;
468
468
  protected _getDirectiveOverrideType(directives: ReadonlyArray<DirectiveNode>): string | null;
469
- protected _getTypeForNode(node: NamedTypeNode, isVisitingInputType: boolean): string;
469
+ protected _getTypeForNode(node: NamedTypeNode): string;
470
470
  NamedType(node: NamedTypeNode, key: any, parent: any, path: any, ancestors: any): string;
471
471
  ListType(node: ListTypeNode, _key: any, _parent: any, _path: any, _ancestors: any): string;
472
472
  SchemaDefinition(): any;
@@ -463,10 +463,10 @@ export declare class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTyp
463
463
  getArgumentsObjectDeclarationBlock(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, name: string, field: FieldDefinitionNode): DeclarationBlock;
464
464
  getArgumentsObjectTypeDefinition(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, name: string, field: FieldDefinitionNode): string;
465
465
  protected buildArgumentsBlock(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode): string;
466
- protected _getScalar(name: string, type: 'input' | 'output'): string;
466
+ protected _getScalar(name: string): string;
467
467
  protected _getDirectiveArgumentNadInputFieldMapping(name: string): string;
468
468
  protected _getDirectiveOverrideType(directives: ReadonlyArray<DirectiveNode>): string | null;
469
- protected _getTypeForNode(node: NamedTypeNode, isVisitingInputType: boolean): string;
469
+ protected _getTypeForNode(node: NamedTypeNode): string;
470
470
  NamedType(node: NamedTypeNode, key: any, parent: any, path: any, ancestors: any): string;
471
471
  ListType(node: ListTypeNode, _key: any, _parent: any, _path: any, _ancestors: any): string;
472
472
  SchemaDefinition(): any;
@@ -87,10 +87,6 @@ export interface RawConfig {
87
87
  * // plugins...
88
88
  * config: {
89
89
  * scalars: {
90
- * ID: {
91
- * input: 'string',
92
- * output: 'string | number'
93
- * }
94
90
  * DateTime: 'Date',
95
91
  * JSON: '{ [key: string]: any }',
96
92
  * }
@@ -87,10 +87,6 @@ export interface RawConfig {
87
87
  * // plugins...
88
88
  * config: {
89
89
  * scalars: {
90
- * ID: {
91
- * input: 'string',
92
- * output: 'string | number'
93
- * }
94
90
  * DateTime: 'Date',
95
91
  * JSON: '{ [key: string]: any }',
96
92
  * }
@@ -1,5 +1,5 @@
1
1
  import { GraphQLInterfaceType, GraphQLNamedType, GraphQLObjectType, GraphQLOutputType } from 'graphql';
2
- import { AvoidOptionalsConfig, ConvertNameFn, NormalizedScalarsMap } from '../types.cjs';
2
+ import { AvoidOptionalsConfig, ConvertNameFn, ScalarsMap } from '../types.cjs';
3
3
  export type PrimitiveField = {
4
4
  isConditional: boolean;
5
5
  fieldName: string;
@@ -23,7 +23,7 @@ export type SelectionSetProcessorConfig = {
23
23
  namespacedImportName: string | null;
24
24
  convertName: ConvertNameFn<any>;
25
25
  enumPrefix: boolean | null;
26
- scalars: NormalizedScalarsMap;
26
+ scalars: ScalarsMap;
27
27
  formatNamedField(name: string, type?: GraphQLOutputType | GraphQLNamedType | null, isConditional?: boolean, isOptional?: boolean): string;
28
28
  wrapTypeWithModifiers(baseType: string, type: GraphQLOutputType | GraphQLNamedType): string;
29
29
  avoidOptionals?: AvoidOptionalsConfig | boolean;
@@ -1,5 +1,5 @@
1
1
  import { GraphQLInterfaceType, GraphQLNamedType, GraphQLObjectType, GraphQLOutputType } from 'graphql';
2
- import { AvoidOptionalsConfig, ConvertNameFn, NormalizedScalarsMap } from '../types.js';
2
+ import { AvoidOptionalsConfig, ConvertNameFn, ScalarsMap } from '../types.js';
3
3
  export type PrimitiveField = {
4
4
  isConditional: boolean;
5
5
  fieldName: string;
@@ -23,7 +23,7 @@ export type SelectionSetProcessorConfig = {
23
23
  namespacedImportName: string | null;
24
24
  convertName: ConvertNameFn<any>;
25
25
  enumPrefix: boolean | null;
26
- scalars: NormalizedScalarsMap;
26
+ scalars: ScalarsMap;
27
27
  formatNamedField(name: string, type?: GraphQLOutputType | GraphQLNamedType | null, isConditional?: boolean, isOptional?: boolean): string;
28
28
  wrapTypeWithModifiers(baseType: string, type: GraphQLOutputType | GraphQLNamedType): string;
29
29
  avoidOptionals?: AvoidOptionalsConfig | boolean;
@@ -17,29 +17,20 @@ export type ParsedDirectiveArgumentAndInputFieldMappings = {
17
17
  * Scalars map or a string, a map between the GraphQL scalar name and the identifier that should be used
18
18
  */
19
19
  export type ScalarsMap = string | {
20
- [name: string]: string | {
21
- input: string;
22
- output: string;
23
- };
20
+ [name: string]: string;
24
21
  };
25
22
  /**
26
23
  * A normalized map between GraphQL scalar name and the identifier name
27
24
  */
28
25
  export type NormalizedScalarsMap = {
29
- [name: string]: {
30
- input: string;
31
- output: string;
32
- };
26
+ [name: string]: string;
33
27
  };
34
28
  /**
35
29
  * Parsed scalars map - a mapping between GraphQL scalar name and the parsed mapper object,
36
30
  * including all required information for generting code for that mapping.
37
31
  */
38
32
  export type ParsedScalarsMap = {
39
- [name: string]: {
40
- input: ParsedMapper;
41
- output: ParsedMapper;
42
- };
33
+ [name: string]: ParsedMapper;
43
34
  };
44
35
  /**
45
36
  * A raw configuration for enumValues map - can be represented with a single string value for a file path,
@@ -17,29 +17,20 @@ export type ParsedDirectiveArgumentAndInputFieldMappings = {
17
17
  * Scalars map or a string, a map between the GraphQL scalar name and the identifier that should be used
18
18
  */
19
19
  export type ScalarsMap = string | {
20
- [name: string]: string | {
21
- input: string;
22
- output: string;
23
- };
20
+ [name: string]: string;
24
21
  };
25
22
  /**
26
23
  * A normalized map between GraphQL scalar name and the identifier name
27
24
  */
28
25
  export type NormalizedScalarsMap = {
29
- [name: string]: {
30
- input: string;
31
- output: string;
32
- };
26
+ [name: string]: string;
33
27
  };
34
28
  /**
35
29
  * Parsed scalars map - a mapping between GraphQL scalar name and the parsed mapper object,
36
30
  * including all required information for generting code for that mapping.
37
31
  */
38
32
  export type ParsedScalarsMap = {
39
- [name: string]: {
40
- input: ParsedMapper;
41
- output: ParsedMapper;
42
- };
33
+ [name: string]: ParsedMapper;
43
34
  };
44
35
  /**
45
36
  * A raw configuration for enumValues map - can be represented with a single string value for a file path,