diff --git a/packages/graphql-codegen-typescript-operations/src/index.ts b/packages/graphql-codegen-typescript-operations/src/index.ts index 6a4fa9a17..32f8a05c2 100644 --- a/packages/graphql-codegen-typescript-operations/src/index.ts +++ b/packages/graphql-codegen-typescript-operations/src/index.ts @@ -74,10 +74,16 @@ export const plugin: PluginFunction< }`; } + const commonTypes = []; + if (config.inlineCommonTypes && content.includes("Maybe<")) { + commonTypes.push(`type Maybe = T | null;`); + } + return { prepend: [ ...visitor.getImports(), ...visitor.getGlobalDeclarations(visitor.config.noExport), + ...commonTypes, ], content, }; diff --git a/packages/graphql-codegen-typescript-operations/src/ts-operation-variables-to-object.ts b/packages/graphql-codegen-typescript-operations/src/ts-operation-variables-to-object.ts index a006d6f9a..ce03bc884 100644 --- a/packages/graphql-codegen-typescript-operations/src/ts-operation-variables-to-object.ts +++ b/packages/graphql-codegen-typescript-operations/src/ts-operation-variables-to-object.ts @@ -9,7 +9,6 @@ import { Kind, TypeNode } from "graphql"; const BASIC_TYPES = ["string", "number", "boolean", "any"]; export class TypeScriptOperationVariablesToObject extends TSOperationVariablesToObject { - public isMaybeUsed = false; public immutableTypes: boolean; constructor( @@ -23,7 +22,6 @@ export class TypeScriptOperationVariablesToObject extends TSOperationVariablesTo _enumValues: ParsedEnumValuesMap = {}, _applyCoercion = false, protected inlineCommonTypes = false, - protected setIsMaybeUsed: () => void, ) { super( _scalars, @@ -50,7 +48,6 @@ export class TypeScriptOperationVariablesToObject extends TSOperationVariablesTo protected wrapMaybe(type?: string) { if (this.inlineCommonTypes) { - this.setIsMaybeUsed(); return `Maybe${type ? `<${type}>` : ""}`; } diff --git a/packages/graphql-codegen-typescript-operations/src/visitor.ts b/packages/graphql-codegen-typescript-operations/src/visitor.ts index 06eb60cb6..742979745 100644 --- a/packages/graphql-codegen-typescript-operations/src/visitor.ts +++ b/packages/graphql-codegen-typescript-operations/src/visitor.ts @@ -45,7 +45,6 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor< TypeScriptDocumentsParsedConfig > { private usedTypes: Set; - private isMaybeUsed = false; private isExactUsed = false; constructor( @@ -73,7 +72,6 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor< this.usedTypes = new Set(); const wrapOptional = (type: string) => { - this.isMaybeUsed = true; const prefix = !this.config.inlineCommonTypes && this.config.namespacedImportName ? `${this.config.namespacedImportName}.` @@ -144,7 +142,6 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor< this.config.enumValues, true, this.config.inlineCommonTypes, - this.setIsMaybeUsed.bind(this), ), ); this._declarationBlockConfig = { @@ -152,10 +149,6 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor< }; } - public setIsMaybeUsed() { - this.isMaybeUsed = true; - } - public getImports(): Array { const imports = !this.config.globalNamespace ? this.config.fragmentImports @@ -171,9 +164,6 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor< ) : []; - if (this.config.inlineCommonTypes && this.isMaybeUsed) { - imports.push(`type Maybe = T | null;`); - } if (this.config.inlineCommonTypes && this.isExactUsed) { imports.push(`type Exact = { [K in keyof T]: T[K];