UNPKG

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