diff --git a/.changeset/metal-lamps-shake.md b/.changeset/metal-lamps-shake.md new file mode 100644 index 0000000000000..767aea943eb6f --- /dev/null +++ b/.changeset/metal-lamps-shake.md @@ -0,0 +1,7 @@ +--- +'@graphql-mesh/transform-rename': minor +'@graphql-mesh/types': minor +--- + +The rename transform uses an ignore list to skip renaming certain types in a schema. It should be +possible to manually enable/disable the usage of the exclusion list via configuration. diff --git a/packages/transforms/rename/src/wrapRename.ts b/packages/transforms/rename/src/wrapRename.ts index 806acbb744f6b..7a850a0014ec1 100644 --- a/packages/transforms/rename/src/wrapRename.ts +++ b/packages/transforms/rename/src/wrapRename.ts @@ -7,6 +7,7 @@ import { } from '@graphql-mesh/utils'; import { DelegationContext, SubschemaConfig, Transform } from '@graphql-tools/delegate'; import { ExecutionRequest, ExecutionResult } from '@graphql-tools/utils'; +// RenameTypesOptions import { RenameInputObjectFields, RenameObjectFieldArguments, @@ -27,6 +28,7 @@ export default class WrapRename implements Transform { useRegExpForFields, useRegExpForArguments, } = change; + const includeDefaults = change.includeDefaults === true; const regExpFlags = change.regExpFlags || undefined; @@ -40,11 +42,11 @@ export default class WrapRename implements Transform { } this.transforms.push( new RenameTypes(typeName => { - if (ignoreList.includes(typeName)) { + if (!includeDefaults && ignoreList.includes(typeName)) { return typeName; } return replaceTypeNameFn(typeName); - }) as any, + }), ); } diff --git a/packages/transforms/rename/test/wrapRename.spec.ts b/packages/transforms/rename/test/wrapRename.spec.ts index 09472212a2131..ddb742f96235d 100644 --- a/packages/transforms/rename/test/wrapRename.spec.ts +++ b/packages/transforms/rename/test/wrapRename.spec.ts @@ -808,4 +808,64 @@ describe('rename', () => { expect(queryFieldMap.bar).toBeDefined(); expect(printSchema(newSchema)).toMatchSnapshot(); }); + + it('should be possible to rename scalars', () => { + const newSchema = wrapSchema({ + schema: makeExecutableSchema({ + typeDefs: /* GraphQL */ ` + scalar UUID + + type SomeEntity { + id: UUID! + name: String! + } + + type Query { + getSomeEntityById(id: UUID!): SomeEntity + } + `, + resolvers: {}, + }), + transforms: [ + new RenameTransform({ + config: { + mode: 'wrap', + renames: [ + { + from: { + argument: '(.*)', + field: '(.*)', + type: '(.*)', + }, + to: { + argument: 'MyPrefix_$1', + field: 'MyPrefix_$1', + type: 'MyPrefix_$1', + }, + useRegExpForArguments: true, + useRegExpForFields: true, + useRegExpForTypes: true, + includeDefaults: true, + }, + ], + }, + }), + ], + }); + + // Check types + expect(newSchema.getType('SomeEntity')).toBeUndefined(); + expect(newSchema.getType('MyPrefix_SomeEntity')).toBeDefined(); + expect(newSchema.getType('getSomeEntityById')).toBeUndefined(); + expect(newSchema.getType('MyPrefix_UUID')).toBeDefined(); + // Check fields + expect( + (newSchema.getType('MyPrefix_SomeEntity') as any)._fields.id.type.ofType.toString(), + ).toBe('MyPrefix_UUID'); + // Check arguments + expect( + (newSchema.getQueryType() as any)._fields.getSomeEntityById.args[0].type.ofType.toString(), + ).toBe('MyPrefix_UUID'); + expect(newSchema.getType('MyPrefix_UUID')).toBeDefined(); + }); }); diff --git a/packages/transforms/rename/yaml-config.graphql b/packages/transforms/rename/yaml-config.graphql index a35abf17b6f14..048896a97108c 100644 --- a/packages/transforms/rename/yaml-config.graphql +++ b/packages/transforms/rename/yaml-config.graphql @@ -37,6 +37,12 @@ type RenameTransformObject @md { Flags to use in the Regular Expression """ regExpFlags: String + """ + Flag to indicate whether certain default types (built-ins, scalars and other types specified an exclusion list) should be renamed or not. + + @default: false + """ + includeDefaults: Boolean } type RenameConfig { diff --git a/packages/types/src/config-schema.json b/packages/types/src/config-schema.json index e7ed813d0f847..3cb37b6d5d2b2 100644 --- a/packages/types/src/config-schema.json +++ b/packages/types/src/config-schema.json @@ -3679,6 +3679,10 @@ "regExpFlags": { "type": "string", "description": "Flags to use in the Regular Expression" + }, + "includeDefaults": { + "type": "boolean", + "description": "Flag to indicate whether certain default types (built-ins, scalars and other types specified an exclusion list) should be renamed or not.\n\n@default: false" } }, "required": ["from", "to"] diff --git a/packages/types/src/config.ts b/packages/types/src/config.ts index 1eb8bb3ff9135..b02f83cba41f7 100644 --- a/packages/types/src/config.ts +++ b/packages/types/src/config.ts @@ -1401,6 +1401,12 @@ export interface RenameTransformObject { * Flags to use in the Regular Expression */ regExpFlags?: string; + /** + * Flag to indicate whether certain default types (built-ins, scalars and other types specified an exclusion list) should be renamed or not. + * + * @default: false + */ + includeDefaults?: boolean; } export interface RenameConfig { type?: string; diff --git a/website/src/generated-markdown/RenameTransform.generated.md b/website/src/generated-markdown/RenameTransform.generated.md index f6d8b492763d0..dc92c2b3e47a3 100644 --- a/website/src/generated-markdown/RenameTransform.generated.md +++ b/website/src/generated-markdown/RenameTransform.generated.md @@ -12,4 +12,7 @@ * `useRegExpForTypes` (type: `Boolean`) - Use Regular Expression for type names * `useRegExpForFields` (type: `Boolean`) - Use Regular Expression for field names * `useRegExpForArguments` (type: `Boolean`) - Use Regular Expression for field names - * `regExpFlags` (type: `String`) - Flags to use in the Regular Expression \ No newline at end of file + * `regExpFlags` (type: `String`) - Flags to use in the Regular Expression + * `includeDefaults` (type: `Boolean`) - Flag to indicate whether certain default types (built-ins, scalars and other types specified an exclusion list) should be renamed or not. + +@default: false \ No newline at end of file diff --git a/website/src/generated-markdown/RenameTransformObject.generated.md b/website/src/generated-markdown/RenameTransformObject.generated.md index 508a94ee3ab5b..a8580b4cc9f57 100644 --- a/website/src/generated-markdown/RenameTransformObject.generated.md +++ b/website/src/generated-markdown/RenameTransformObject.generated.md @@ -10,4 +10,7 @@ * `useRegExpForTypes` (type: `Boolean`) - Use Regular Expression for type names * `useRegExpForFields` (type: `Boolean`) - Use Regular Expression for field names * `useRegExpForArguments` (type: `Boolean`) - Use Regular Expression for field names -* `regExpFlags` (type: `String`) - Flags to use in the Regular Expression \ No newline at end of file +* `regExpFlags` (type: `String`) - Flags to use in the Regular Expression +* `includeDefaults` (type: `Boolean`) - Flag to indicate whether certain default types (built-ins, scalars and other types specified an exclusion list) should be renamed or not. + +@default: false \ No newline at end of file