@graphql-codegen/visitor-plugin-common 3.2.0-alpha-20230509132251-2cf02ebb6 → 3.2.0-alpha-20230509133142-063e9cefb

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