Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add stripIgnoredCharacters option (visitor-plugin-common) - continued #8489

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nice-gifts-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/visitor-plugin-common': major
---

Add `stripIgnoredCharacters` option
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
FragmentSpreadNode,
GraphQLSchema,
Kind,
stripIgnoredCharacters,
} from 'graphql';
import { DepGraph } from 'dependency-graph';
import gqlTag from 'graphql-tag';
Expand Down Expand Up @@ -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 {
Expand All @@ -228,6 +234,7 @@ export interface ClientSideBasePluginConfig extends ParsedConfig {
pureMagicComment?: boolean;
optimizeDocumentNode: boolean;
experimentalFragmentVariables?: boolean;
stripIgnoredCharacters?: boolean;
}

export class ClientSideBaseVisitor<
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions packages/plugins/typescript/operations/tests/ts-documents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
`);
});
});
});