diff --git a/.changeset/@graphql-codegen_typescript-operations-10572-dependencies.md b/.changeset/@graphql-codegen_typescript-operations-10572-dependencies.md new file mode 100644 index 00000000000..81018bf6546 --- /dev/null +++ b/.changeset/@graphql-codegen_typescript-operations-10572-dependencies.md @@ -0,0 +1,5 @@ +--- +"@graphql-codegen/typescript-operations": patch +--- +dependencies updates: + - Removed dependency [`@graphql-codegen/typescript@^5.0.7` ↗︎](https://www.npmjs.com/package/@graphql-codegen/typescript/v/5.0.7) (from `dependencies`) diff --git a/.changeset/khaki-spies-admire.md b/.changeset/khaki-spies-admire.md new file mode 100644 index 00000000000..efda9226e8a --- /dev/null +++ b/.changeset/khaki-spies-admire.md @@ -0,0 +1,9 @@ +--- +'@graphql-codegen/typescript-operations': major +--- + +BREAKING CHANGE: Decouple `typescript-operations` plugin from `typescript` plugin + +Previously, `TypeScriptOperationVariablesToObject` from `typescript-operations` was extending from `typescript` plugin. This made it (1) very hard to read, as we need to jump from base class <-> typescript class <-> typescript-operations class to understand the flow and (2) very hard to evolve the two independently (which is the point of this work). + +Since there's not much shared logic anyways, it's simpler to extend the `typescript-operations` class from the base class directly. diff --git a/packages/plugins/typescript/operations/package.json b/packages/plugins/typescript/operations/package.json index 3c2c9f3555a..dcae7101d81 100644 --- a/packages/plugins/typescript/operations/package.json +++ b/packages/plugins/typescript/operations/package.json @@ -15,7 +15,6 @@ "dependencies": { "@graphql-codegen/plugin-helpers": "^6.1.0", "@graphql-codegen/schema-ast": "^5.0.0", - "@graphql-codegen/typescript": "^5.0.7", "@graphql-codegen/visitor-plugin-common": "6.2.2", "auto-bind": "~4.0.0", "tslib": "~2.6.0" diff --git a/packages/plugins/typescript/operations/src/ts-operation-variables-to-object.ts b/packages/plugins/typescript/operations/src/ts-operation-variables-to-object.ts index 105240bed96..0d5e4c1eb30 100644 --- a/packages/plugins/typescript/operations/src/ts-operation-variables-to-object.ts +++ b/packages/plugins/typescript/operations/src/ts-operation-variables-to-object.ts @@ -1,4 +1,11 @@ -import { TypeScriptOperationVariablesToObject as TSOperationVariablesToObject } from '@graphql-codegen/typescript'; +import { + OperationVariablesToObject, + ConvertNameFn, + NormalizedAvoidOptionalsConfig, + NormalizedScalarsMap, + ParsedEnumValuesMap, +} from '@graphql-codegen/visitor-plugin-common'; +import { Kind, TypeNode } from 'graphql'; export const SCALARS = { ID: 'string | number', @@ -10,7 +17,36 @@ export const SCALARS = { const MAYBE_SUFFIX = ' | null'; -export class TypeScriptOperationVariablesToObject extends TSOperationVariablesToObject { +export class TypeScriptOperationVariablesToObject extends OperationVariablesToObject { + constructor( + _scalars: NormalizedScalarsMap, + _convertName: ConvertNameFn, + private _avoidOptionals: NormalizedAvoidOptionalsConfig, + private _immutableTypes: boolean, + _namespacedImportName: string | null, + _enumNames: string[], + _enumPrefix: boolean, + _enumSuffix: boolean, + _enumValues: ParsedEnumValuesMap, + _applyCoercion: boolean + ) { + super( + _scalars, + _convertName, + _namespacedImportName, + _enumNames, + _enumPrefix, + _enumSuffix, + _enumValues, + _applyCoercion, + {} + ); + } + + protected formatFieldString(fieldName: string, isNonNullType: boolean, hasDefaultValue: boolean): string { + return `${fieldName}${this.getAvoidOption(isNonNullType, hasDefaultValue) ? '?' : ''}`; + } + protected formatTypeString(fieldType: string, _isNonNullType: boolean, _hasDefaultValue: boolean): string { return fieldType; } @@ -23,6 +59,28 @@ export class TypeScriptOperationVariablesToObject extends TSOperationVariablesTo return str; } + protected getAvoidOption(isNonNullType: boolean, hasDefaultValue: boolean) { + const options = this._avoidOptionals; + return ((options.object || !options.defaultValue) && hasDefaultValue) || (!options.object && !isNonNullType); + } + + public wrapAstTypeWithModifiers(baseType: string, typeNode: TypeNode, applyCoercion = false): string { + if (typeNode.kind === Kind.NON_NULL_TYPE) { + const type = this.wrapAstTypeWithModifiers(baseType, typeNode.type, applyCoercion); + + return this.clearOptional(type); + } + if (typeNode.kind === Kind.LIST_TYPE) { + const innerType = this.wrapAstTypeWithModifiers(baseType, typeNode.type, applyCoercion); + const listInputCoercionExtension = applyCoercion ? ` | ${innerType}` : ''; + + return this.wrapMaybe( + `${this._immutableTypes ? 'ReadonlyArray' : 'Array'}<${innerType}>${listInputCoercionExtension}` + ); + } + return this.wrapMaybe(baseType); + } + protected wrapMaybe(type: string): string { return type?.endsWith(MAYBE_SUFFIX) ? type : `${type}${MAYBE_SUFFIX}`; } @@ -30,4 +88,8 @@ export class TypeScriptOperationVariablesToObject extends TSOperationVariablesTo protected getScalar(name: string): string { return this._scalars?.[name]?.input ?? SCALARS[name] ?? 'unknown'; } + + protected getPunctuation(): string { + return ';'; + } } diff --git a/packages/plugins/typescript/operations/src/visitor.ts b/packages/plugins/typescript/operations/src/visitor.ts index 5736bd5dc15..9fa029ea661 100644 --- a/packages/plugins/typescript/operations/src/visitor.ts +++ b/packages/plugins/typescript/operations/src/visitor.ts @@ -169,9 +169,7 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor< this.config.enumPrefix, this.config.enumSuffix, this.config.enumValues, - this.config.arrayInputCoercion, - undefined, - undefined + this.config.arrayInputCoercion ) ); this._declarationBlockConfig = {