Skip to content

Commit 632c6fa

Browse files
Decouple typescript-operations plugin from typescript plugin (#10572)
* Decouple TypeScriptOperationVariablesToObject from typescript plugin * Add changeset * Remove typescript plugin dep * chore(dependencies): updated changesets for modified dependencies --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 02c16ca commit 632c6fa

5 files changed

Lines changed: 79 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-codegen/typescript-operations": patch
3+
---
4+
dependencies updates:
5+
- Removed dependency [`@graphql-codegen/typescript@^5.0.7` ↗︎](https://www.npmjs.com/package/@graphql-codegen/typescript/v/5.0.7) (from `dependencies`)

.changeset/khaki-spies-admire.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@graphql-codegen/typescript-operations': major
3+
---
4+
5+
BREAKING CHANGE: Decouple `typescript-operations` plugin from `typescript` plugin
6+
7+
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).
8+
9+
Since there's not much shared logic anyways, it's simpler to extend the `typescript-operations` class from the base class directly.

packages/plugins/typescript/operations/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"dependencies": {
1616
"@graphql-codegen/plugin-helpers": "^6.1.0",
1717
"@graphql-codegen/schema-ast": "^5.0.0",
18-
"@graphql-codegen/typescript": "^5.0.7",
1918
"@graphql-codegen/visitor-plugin-common": "6.2.2",
2019
"auto-bind": "~4.0.0",
2120
"tslib": "~2.6.0"

packages/plugins/typescript/operations/src/ts-operation-variables-to-object.ts

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { TypeScriptOperationVariablesToObject as TSOperationVariablesToObject } from '@graphql-codegen/typescript';
1+
import {
2+
OperationVariablesToObject,
3+
ConvertNameFn,
4+
NormalizedAvoidOptionalsConfig,
5+
NormalizedScalarsMap,
6+
ParsedEnumValuesMap,
7+
} from '@graphql-codegen/visitor-plugin-common';
8+
import { Kind, TypeNode } from 'graphql';
29

310
export const SCALARS = {
411
ID: 'string | number',
@@ -10,7 +17,36 @@ export const SCALARS = {
1017

1118
const MAYBE_SUFFIX = ' | null';
1219

13-
export class TypeScriptOperationVariablesToObject extends TSOperationVariablesToObject {
20+
export class TypeScriptOperationVariablesToObject extends OperationVariablesToObject {
21+
constructor(
22+
_scalars: NormalizedScalarsMap,
23+
_convertName: ConvertNameFn,
24+
private _avoidOptionals: NormalizedAvoidOptionalsConfig,
25+
private _immutableTypes: boolean,
26+
_namespacedImportName: string | null,
27+
_enumNames: string[],
28+
_enumPrefix: boolean,
29+
_enumSuffix: boolean,
30+
_enumValues: ParsedEnumValuesMap,
31+
_applyCoercion: boolean
32+
) {
33+
super(
34+
_scalars,
35+
_convertName,
36+
_namespacedImportName,
37+
_enumNames,
38+
_enumPrefix,
39+
_enumSuffix,
40+
_enumValues,
41+
_applyCoercion,
42+
{}
43+
);
44+
}
45+
46+
protected formatFieldString(fieldName: string, isNonNullType: boolean, hasDefaultValue: boolean): string {
47+
return `${fieldName}${this.getAvoidOption(isNonNullType, hasDefaultValue) ? '?' : ''}`;
48+
}
49+
1450
protected formatTypeString(fieldType: string, _isNonNullType: boolean, _hasDefaultValue: boolean): string {
1551
return fieldType;
1652
}
@@ -23,11 +59,37 @@ export class TypeScriptOperationVariablesToObject extends TSOperationVariablesTo
2359
return str;
2460
}
2561

62+
protected getAvoidOption(isNonNullType: boolean, hasDefaultValue: boolean) {
63+
const options = this._avoidOptionals;
64+
return ((options.object || !options.defaultValue) && hasDefaultValue) || (!options.object && !isNonNullType);
65+
}
66+
67+
public wrapAstTypeWithModifiers(baseType: string, typeNode: TypeNode, applyCoercion = false): string {
68+
if (typeNode.kind === Kind.NON_NULL_TYPE) {
69+
const type = this.wrapAstTypeWithModifiers(baseType, typeNode.type, applyCoercion);
70+
71+
return this.clearOptional(type);
72+
}
73+
if (typeNode.kind === Kind.LIST_TYPE) {
74+
const innerType = this.wrapAstTypeWithModifiers(baseType, typeNode.type, applyCoercion);
75+
const listInputCoercionExtension = applyCoercion ? ` | ${innerType}` : '';
76+
77+
return this.wrapMaybe(
78+
`${this._immutableTypes ? 'ReadonlyArray' : 'Array'}<${innerType}>${listInputCoercionExtension}`
79+
);
80+
}
81+
return this.wrapMaybe(baseType);
82+
}
83+
2684
protected wrapMaybe(type: string): string {
2785
return type?.endsWith(MAYBE_SUFFIX) ? type : `${type}${MAYBE_SUFFIX}`;
2886
}
2987

3088
protected getScalar(name: string): string {
3189
return this._scalars?.[name]?.input ?? SCALARS[name] ?? 'unknown';
3290
}
91+
92+
protected getPunctuation(): string {
93+
return ';';
94+
}
3395
}

packages/plugins/typescript/operations/src/visitor.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,7 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
169169
this.config.enumPrefix,
170170
this.config.enumSuffix,
171171
this.config.enumValues,
172-
this.config.arrayInputCoercion,
173-
undefined,
174-
undefined
172+
this.config.arrayInputCoercion
175173
)
176174
);
177175
this._declarationBlockConfig = {

0 commit comments

Comments
 (0)