diff --git a/.changeset/nice-gifts-sparkle.md b/.changeset/nice-gifts-sparkle.md new file mode 100644 index 00000000000..7b1f609cc31 --- /dev/null +++ b/.changeset/nice-gifts-sparkle.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/visitor-plugin-common': major +--- + +Add `stripIgnoredCharacters` option diff --git a/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts b/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts index 2fcadbfbca8..18e7cddea0a 100644 --- a/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts +++ b/packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts @@ -7,6 +7,7 @@ import { FragmentSpreadNode, GraphQLSchema, Kind, + stripIgnoredCharacters, } from 'graphql'; import { DepGraph } from 'dependency-graph'; import gqlTag from 'graphql-tag'; @@ -208,6 +209,11 @@ export interface RawClientSideBasePluginConfig extends RawConfig { * @description If set to true, it will enable support for parsing variables on fragments. */ experimentalFragmentVariables?: boolean; + /** + * @default false + * @description If set to true, any extraneous whitespace characters, etc. in the strings will be removed — using `graphql-js`'s `stripIgnoredCharacters` function. Has no effect if `documentMode` is set to `documentNode`. + */ + stripIgnoredCharacters?: boolean; } export interface ClientSideBasePluginConfig extends ParsedConfig { @@ -228,6 +234,7 @@ export interface ClientSideBasePluginConfig extends ParsedConfig { pureMagicComment?: boolean; optimizeDocumentNode: boolean; experimentalFragmentVariables?: boolean; + stripIgnoredCharacters?: boolean; } export class ClientSideBaseVisitor< @@ -269,6 +276,7 @@ export class ClientSideBaseVisitor< importDocumentNodeExternallyFrom: getConfigValue(rawConfig.importDocumentNodeExternallyFrom, ''), pureMagicComment: getConfigValue(rawConfig.pureMagicComment, false), experimentalFragmentVariables: getConfigValue(rawConfig.experimentalFragmentVariables, false), + stripIgnoredCharacters: getConfigValue(rawConfig.stripIgnoredCharacters, false), ...additionalConfig, } as any); @@ -381,12 +389,17 @@ export class ClientSideBaseVisitor< return JSON.stringify(gqlObj); } if (this.config.documentMode === DocumentMode.string) { - return '`' + doc + '`'; + return '`' + (this.config.stripIgnoredCharacters ? stripIgnoredCharacters(doc) : doc) + '`'; } const gqlImport = this._parseImport(this.config.gqlImport || 'graphql-tag'); - return (gqlImport.propName || 'gql') + '`' + doc + '`'; + return ( + (gqlImport.propName || 'gql') + + '`' + + (this.config.stripIgnoredCharacters ? stripIgnoredCharacters(doc) : doc) + + '`' + ); } protected _generateFragment(fragmentDocument: FragmentDefinitionNode): string | void { diff --git a/packages/plugins/typescript/operations/tests/ts-documents.spec.ts b/packages/plugins/typescript/operations/tests/ts-documents.spec.ts index 7d43e14e4cd..a9086a5e817 100644 --- a/packages/plugins/typescript/operations/tests/ts-documents.spec.ts +++ b/packages/plugins/typescript/operations/tests/ts-documents.spec.ts @@ -6441,4 +6441,34 @@ function test(q: GetEntityBrandDataQuery): void { `); }); }); + + describe('when using stripIgnoredCharacters option', () => { + it('correctly removes unused chars', async () => { + const ast = parse(/* GraphQL */ ` + query { + me { + ...UserFragment + } + } + fragment UserFragment on User { + id + } + `); + const result = await plugin( + schema, + [{ location: 'test-file.ts', document: ast }], + // @ts-expect-error This is a config option from client side visitor we want to pass in + { stripIgnoredCharacters: true }, + { outputFile: '' } + ); + expect(result.content).toBeSimilarStringTo(` + export type Unnamed_1_QueryVariables = Exact<{ [key: string]: never; }>; + + + export type Unnamed_1_Query = { __typename?: 'Query', me?: { __typename?: 'User', id: string } | null }; + + export type UserFragmentFragment = { __typename?: 'User', id: string }; + `); + }); + }); });