Skip to content

Commit

Permalink
fix: Don't generate invalid type names with mergeFragmentTypes (#9369)
Browse files Browse the repository at this point in the history
* fix: Don't generate invalid type names with mergeFragmentTypes

The behavior was depentent on the default pascalCase
namingConvention. We now apply similar removal of quotes and special
characters out of the box, ensuring we keep functioning even if "keep"
is selected as the naming convention.

* changes
  • Loading branch information
asmundg committed May 12, 2023
1 parent a7dda35 commit 5950f5a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-berries-deny.md
@@ -0,0 +1,5 @@
---
'@graphql-codegen/visitor-plugin-common': patch
---

Output valid type names with mergeFragmentTypes
Expand Up @@ -459,10 +459,13 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
// then don't have a typename for naming the fragment.
acc[
selectedTypes.length <= 3
? selectedTypes.join('_')
? // Remove quote marks to produce a valid type name
selectedTypes.map(t => t.replace(/'/g, '')).join('_')
: createHash('sha256')
.update(selectedTypes.join() || transformedSet || '')
// Remove invalid characters to produce a valid type name
.digest('base64')
.replace(/[=+/]/g, '')
] = [transformedSet];
}
return acc;
Expand Down
24 changes: 12 additions & 12 deletions packages/plugins/typescript/operations/tests/ts-documents.spec.ts
Expand Up @@ -2227,15 +2227,15 @@ describe('TypeScript Operations Plugin', () => {
id
}
`);
const config = { preResolveTypes: true, mergeFragmentTypes: true };
const config = { preResolveTypes: true, mergeFragmentTypes: true, namingConvention: 'keep' };
const { content } = await plugin(schema, [{ location: 'test-file.ts', document: ast }], config, {
outputFile: '',
});

expect(content).toBeSimilarStringTo(`
export type TestQueryVariables = Exact<{ [key: string]: never; }>;
export type testQueryVariables = Exact<{ [key: string]: never; }>;
export type TestQuery = (
export type testQuery = (
{ notifications: Array<(
{ id: string }
& { __typename?: 'TextNotification' | 'ImageNotification' }
Expand All @@ -2260,7 +2260,7 @@ describe('TypeScript Operations Plugin', () => {
}
}
`);
const config = { preResolveTypes: true, mergeFragmentTypes: true };
const config = { preResolveTypes: true, mergeFragmentTypes: true, namingConvention: 'keep' };
const { content } = await plugin(schema, [{ location: 'test-file.ts', document: ast }], config, {
outputFile: '',
});
Expand Down Expand Up @@ -2321,7 +2321,7 @@ describe('TypeScript Operations Plugin', () => {
}
}
`);
const config = { preResolveTypes: true, mergeFragmentTypes: true };
const config = { preResolveTypes: true, mergeFragmentTypes: true, namingConvention: 'keep' };
const { content } = await plugin(testSchema, [{ location: 'test-file.ts', document: ast }], config, {
outputFile: '',
});
Expand All @@ -2332,12 +2332,12 @@ describe('TypeScript Operations Plugin', () => {
& { __typename?: 'A' }
);
type N_ZhJjUzpMTyh98zugnx0IKwiLetPNjV8KYbSlmpAeuu_Fragment = (
type N_zhJJUzpMTyh98zugnx0IKwiLetPNjV8KybSlmpAEUU_Fragment = (
{ id: string }
& { __typename?: 'B' | 'C' | 'D' | 'E' }
);
export type NFragment = N_A_Fragment | N_ZhJjUzpMTyh98zugnx0IKwiLetPNjV8KYbSlmpAeuu_Fragment;
export type NFragment = N_A_Fragment | N_zhJJUzpMTyh98zugnx0IKwiLetPNjV8KybSlmpAEUU_Fragment;
`);
await validate(content, config);
});
Expand All @@ -2352,7 +2352,7 @@ describe('TypeScript Operations Plugin', () => {
}
}
`);
const config = { preResolveTypes: true, mergeFragmentTypes: true };
const config = { preResolveTypes: true, mergeFragmentTypes: true, namingConvention: 'keep' };
const { content } = await plugin(schema, [{ location: 'test-file.ts', document: ast }], config, {
outputFile: '',
});
Expand Down Expand Up @@ -2381,15 +2381,15 @@ describe('TypeScript Operations Plugin', () => {
id
}
`);
const config = { preResolveTypes: true, skipTypename: true, mergeFragmentTypes: true };
const config = { preResolveTypes: true, skipTypename: true, mergeFragmentTypes: true, namingConvention: 'keep' };
const { content } = await plugin(schema, [{ location: 'test-file.ts', document: ast }], config, {
outputFile: '',
});

expect(content).toBeSimilarStringTo(`
export type TestQueryVariables = Exact<{ [key: string]: never; }>;
export type testQueryVariables = Exact<{ [key: string]: never; }>;
export type TestQuery = { notifications: Array<{ id: string }> };
export type testQuery = { notifications: Array<{ id: string }> };
`);
await validate(content, config);
});
Expand All @@ -2403,7 +2403,7 @@ describe('TypeScript Operations Plugin', () => {
}
}
`);
const config = { preResolveTypes: true, skipTypename: true, mergeFragmentTypes: true };
const config = { preResolveTypes: true, skipTypename: true, mergeFragmentTypes: true, namingConvention: 'keep' };
const { content } = await plugin(schema, [{ location: 'test-file.ts', document: ast }], config, {
outputFile: '',
});
Expand Down

0 comments on commit 5950f5a

Please sign in to comment.