UNPKG

4.89 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.TypeScriptDocumentsVisitor = void 0;
4const tslib_1 = require("tslib");
5const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
6const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
7const graphql_1 = require("graphql");
8const ts_operation_variables_to_object_js_1 = require("./ts-operation-variables-to-object.js");
9const ts_selection_set_processor_js_1 = require("./ts-selection-set-processor.js");
10class TypeScriptDocumentsVisitor extends visitor_plugin_common_1.BaseDocumentsVisitor {
11 constructor(schema, config, allFragments) {
12 super(config, {
13 arrayInputCoercion: (0, visitor_plugin_common_1.getConfigValue)(config.arrayInputCoercion, true),
14 noExport: (0, visitor_plugin_common_1.getConfigValue)(config.noExport, false),
15 avoidOptionals: (0, visitor_plugin_common_1.normalizeAvoidOptionals)((0, visitor_plugin_common_1.getConfigValue)(config.avoidOptionals, false)),
16 immutableTypes: (0, visitor_plugin_common_1.getConfigValue)(config.immutableTypes, false),
17 nonOptionalTypename: (0, visitor_plugin_common_1.getConfigValue)(config.nonOptionalTypename, false),
18 preResolveTypes: (0, visitor_plugin_common_1.getConfigValue)(config.preResolveTypes, true),
19 mergeFragmentTypes: (0, visitor_plugin_common_1.getConfigValue)(config.mergeFragmentTypes, false),
20 }, schema);
21 (0, auto_bind_1.default)(this);
22 const preResolveTypes = (0, visitor_plugin_common_1.getConfigValue)(config.preResolveTypes, true);
23 const defaultMaybeValue = 'T | null';
24 const maybeValue = (0, visitor_plugin_common_1.getConfigValue)(config.maybeValue, defaultMaybeValue);
25 const wrapOptional = (type) => {
26 if (preResolveTypes === true) {
27 return maybeValue.replace('T', type);
28 }
29 const prefix = this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '';
30 return `${prefix}Maybe<${type}>`;
31 };
32 const wrapArray = (type) => {
33 const listModifier = this.config.immutableTypes ? 'ReadonlyArray' : 'Array';
34 return `${listModifier}<${type}>`;
35 };
36 const formatNamedField = (name, type, isConditional = false) => {
37 const optional = isConditional || (!this.config.avoidOptionals.field && !!type && !(0, graphql_1.isNonNullType)(type));
38 return (this.config.immutableTypes ? `readonly ${name}` : name) + (optional ? '?' : '');
39 };
40 const processorConfig = {
41 namespacedImportName: this.config.namespacedImportName,
42 convertName: this.convertName.bind(this),
43 enumPrefix: this.config.enumPrefix,
44 scalars: this.scalars,
45 formatNamedField,
46 wrapTypeWithModifiers(baseType, type) {
47 return (0, visitor_plugin_common_1.wrapTypeWithModifiers)(baseType, type, { wrapOptional, wrapArray });
48 },
49 avoidOptionals: this.config.avoidOptionals,
50 };
51 const processor = new (preResolveTypes ? visitor_plugin_common_1.PreResolveTypesProcessor : ts_selection_set_processor_js_1.TypeScriptSelectionSetProcessor)(processorConfig);
52 this.setSelectionSetHandler(new visitor_plugin_common_1.SelectionSetToObject(processor, this.scalars, this.schema, this.convertName.bind(this), this.getFragmentSuffix.bind(this), allFragments, this.config));
53 const enumsNames = Object.keys(schema.getTypeMap()).filter(typeName => (0, graphql_1.isEnumType)(schema.getType(typeName)));
54 this.setVariablesTransformer(new ts_operation_variables_to_object_js_1.TypeScriptOperationVariablesToObject(this.scalars, this.convertName.bind(this), this.config.avoidOptionals.object, this.config.immutableTypes, this.config.namespacedImportName, enumsNames, this.config.enumPrefix, this.config.enumValues, this.config.arrayInputCoercion, undefined, 'InputMaybe'));
55 this._declarationBlockConfig = {
56 ignoreExport: this.config.noExport,
57 };
58 }
59 getImports() {
60 return !this.config.globalNamespace &&
61 (this.config.inlineFragmentTypes === 'combine' || this.config.inlineFragmentTypes === 'mask')
62 ? this.config.fragmentImports.map(fragmentImport => (0, visitor_plugin_common_1.generateFragmentImportStatement)(fragmentImport, 'type'))
63 : [];
64 }
65 getPunctuation(_declarationKind) {
66 return ';';
67 }
68 applyVariablesWrapper(variablesBlock) {
69 const prefix = this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '';
70 return `${prefix}Exact<${variablesBlock === '{}' ? `{ [key: string]: never; }` : variablesBlock}>`;
71 }
72}
73exports.TypeScriptDocumentsVisitor = TypeScriptDocumentsVisitor;