Skip to content

Commit

Permalink
Rename transform toggle ignore list (#5342)
Browse files Browse the repository at this point in the history
* feat: Make rename transform enable/disable the ignore list with a flag

* fix: Correct usage of useIgnoreList flag for rename transform

* refactor: Rename useIgnoreList to includeDefaults
  • Loading branch information
cweckesser authored May 24, 2023
1 parent bc438f8 commit 01fb0cc
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/metal-lamps-shake.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 4 additions & 2 deletions packages/transforms/rename/src/wrapRename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,6 +28,7 @@ export default class WrapRename implements Transform {
useRegExpForFields,
useRegExpForArguments,
} = change;
const includeDefaults = change.includeDefaults === true;

const regExpFlags = change.regExpFlags || undefined;

Expand All @@ -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,
}),
);
}

Expand Down
60 changes: 60 additions & 0 deletions packages/transforms/rename/test/wrapRename.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
6 changes: 6 additions & 0 deletions packages/transforms/rename/yaml-config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion website/src/generated-markdown/RenameTransform.generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
* `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
Original file line number Diff line number Diff line change
Expand Up @@ -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
* `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

0 comments on commit 01fb0cc

Please sign in to comment.