1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.TypeScriptDocumentsVisitor = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
6 | const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
7 | const graphql_1 = require("graphql");
|
8 | const ts_operation_variables_to_object_js_1 = require("./ts-operation-variables-to-object.js");
|
9 | const ts_selection_set_processor_js_1 = require("./ts-selection-set-processor.js");
|
10 | class 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, isOptional = false) => {
|
37 | const optional = isOptional || 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 | enumSuffix: this.config.enumSuffix,
|
45 | scalars: this.scalars,
|
46 | formatNamedField,
|
47 | wrapTypeWithModifiers(baseType, type) {
|
48 | return (0, visitor_plugin_common_1.wrapTypeWithModifiers)(baseType, type, { wrapOptional, wrapArray });
|
49 | },
|
50 | avoidOptionals: this.config.avoidOptionals,
|
51 | };
|
52 | const processor = new (preResolveTypes ? visitor_plugin_common_1.PreResolveTypesProcessor : ts_selection_set_processor_js_1.TypeScriptSelectionSetProcessor)(processorConfig);
|
53 | this.setSelectionSetHandler(new visitor_plugin_common_1.SelectionSetToObject(processor, this.scalars, this.schema, this.convertName.bind(this), this.getFragmentSuffix.bind(this), allFragments, this.config));
|
54 | const enumsNames = Object.keys(schema.getTypeMap()).filter(typeName => (0, graphql_1.isEnumType)(schema.getType(typeName)));
|
55 | 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.enumSuffix, this.config.enumValues, this.config.arrayInputCoercion, undefined, 'InputMaybe'));
|
56 | this._declarationBlockConfig = {
|
57 | ignoreExport: this.config.noExport,
|
58 | };
|
59 | }
|
60 | getImports() {
|
61 | return !this.config.globalNamespace &&
|
62 | (this.config.inlineFragmentTypes === 'combine' || this.config.inlineFragmentTypes === 'mask')
|
63 | ? this.config.fragmentImports.map(fragmentImport => (0, visitor_plugin_common_1.generateFragmentImportStatement)(fragmentImport, 'type'))
|
64 | : [];
|
65 | }
|
66 | getPunctuation(_declarationKind) {
|
67 | return ';';
|
68 | }
|
69 | applyVariablesWrapper(variablesBlock) {
|
70 | const prefix = this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '';
|
71 | return `${prefix}Exact<${variablesBlock === '{}' ? `{ [key: string]: never; }` : variablesBlock}>`;
|
72 | }
|
73 | }
|
74 | exports.TypeScriptDocumentsVisitor = TypeScriptDocumentsVisitor;
|