From 943370d8fe93cd4553bd06cf2303c66431d7c7d6 Mon Sep 17 00:00:00 2001 From: Jan Stevens Date: Mon, 17 Oct 2022 07:29:15 +0000 Subject: [PATCH 1/3] fix: add spec and align generic-sdk with graphql-request sdk --- .../src/client-side-base-visitor.ts | 17 ++++++++-- .../typescript/generic-sdk/src/visitor.ts | 6 +++- .../operations/tests/ts-documents.spec.ts | 31 +++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) 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/generic-sdk/src/visitor.ts b/packages/plugins/typescript/generic-sdk/src/visitor.ts index 1834a40de22..5911c98b726 100644 --- a/packages/plugins/typescript/generic-sdk/src/visitor.ts +++ b/packages/plugins/typescript/generic-sdk/src/visitor.ts @@ -29,6 +29,7 @@ function isStreamOperation(operationAST: OperationDefinitionNode) { } export class GenericSdkVisitor extends ClientSideBaseVisitor { + private _externalImportPrefix: string; private _operationsToInclude: { node: OperationDefinitionNode; documentVariableName: string; @@ -56,6 +57,7 @@ export class GenericSdkVisitor extends ClientSideBaseVisitor { + const operationName = o.node.name.value; const optionalVariables = !o.node.variableDefinitions || o.node.variableDefinitions.length === 0 || @@ -92,7 +95,7 @@ export class GenericSdkVisitor extends ClientSideBaseVisitor` : o.operationResultType; - return `${o.node.name.value}(variables${optionalVariables ? '?' : ''}: ${ + return `${operationName}(variables${optionalVariables ? '?' : ''}: ${ o.operationVariablesTypes }, options?: C): ${returnType}<${resultData}> { return requester<${o.operationResultType}, ${o.operationVariablesTypes}>(${ @@ -100,6 +103,7 @@ export class GenericSdkVisitor extends ClientSideBaseVisitor; }`; }) + .filter(Boolean) .map(s => indentMultiline(s, 2)); const documentNodeType = this.config.documentMode === DocumentMode.string ? 'string' : 'DocumentNode'; diff --git a/packages/plugins/typescript/operations/tests/ts-documents.spec.ts b/packages/plugins/typescript/operations/tests/ts-documents.spec.ts index 7d43e14e4cd..f6a0ed6e265 100644 --- a/packages/plugins/typescript/operations/tests/ts-documents.spec.ts +++ b/packages/plugins/typescript/operations/tests/ts-documents.spec.ts @@ -3,6 +3,7 @@ import { parse, buildClientSchema, buildSchema } from 'graphql'; import { plugin } from '../src/index.js'; import { plugin as tsPlugin } from '../../typescript/src/index.js'; import { mergeOutputs, Types } from '@graphql-codegen/plugin-helpers'; +import { DocumentMode } from '@graphql-codegen/visitor-plugin-common'; describe('TypeScript Operations Plugin', () => { const gitHuntSchema = buildClientSchema(require('../../../../../dev-test/githunt/schema.json')); @@ -6441,4 +6442,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 }; + `); + }); + }); }); From f546b6a54c9cf5dc8971cfca8e1f665bd385d9ee Mon Sep 17 00:00:00 2001 From: Jan Stevens Date: Mon, 17 Oct 2022 07:40:01 +0000 Subject: [PATCH 2/3] fix: revert changes to generic-sdk --- packages/plugins/typescript/generic-sdk/src/visitor.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/plugins/typescript/generic-sdk/src/visitor.ts b/packages/plugins/typescript/generic-sdk/src/visitor.ts index 5911c98b726..1834a40de22 100644 --- a/packages/plugins/typescript/generic-sdk/src/visitor.ts +++ b/packages/plugins/typescript/generic-sdk/src/visitor.ts @@ -29,7 +29,6 @@ function isStreamOperation(operationAST: OperationDefinitionNode) { } export class GenericSdkVisitor extends ClientSideBaseVisitor { - private _externalImportPrefix: string; private _operationsToInclude: { node: OperationDefinitionNode; documentVariableName: string; @@ -57,7 +56,6 @@ export class GenericSdkVisitor extends ClientSideBaseVisitor { - const operationName = o.node.name.value; const optionalVariables = !o.node.variableDefinitions || o.node.variableDefinitions.length === 0 || @@ -95,7 +92,7 @@ export class GenericSdkVisitor extends ClientSideBaseVisitor` : o.operationResultType; - return `${operationName}(variables${optionalVariables ? '?' : ''}: ${ + return `${o.node.name.value}(variables${optionalVariables ? '?' : ''}: ${ o.operationVariablesTypes }, options?: C): ${returnType}<${resultData}> { return requester<${o.operationResultType}, ${o.operationVariablesTypes}>(${ @@ -103,7 +100,6 @@ export class GenericSdkVisitor extends ClientSideBaseVisitor; }`; }) - .filter(Boolean) .map(s => indentMultiline(s, 2)); const documentNodeType = this.config.documentMode === DocumentMode.string ? 'string' : 'DocumentNode'; From 37d7dba699eadcb2a152136e77699d9f2275a6a8 Mon Sep 17 00:00:00 2001 From: Jan Stevens Date: Mon, 17 Oct 2022 07:43:43 +0000 Subject: [PATCH 3/3] fix: add changeset, remove unused include --- .changeset/nice-gifts-sparkle.md | 5 +++++ .../plugins/typescript/operations/tests/ts-documents.spec.ts | 1 - 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 .changeset/nice-gifts-sparkle.md 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/typescript/operations/tests/ts-documents.spec.ts b/packages/plugins/typescript/operations/tests/ts-documents.spec.ts index f6a0ed6e265..a9086a5e817 100644 --- a/packages/plugins/typescript/operations/tests/ts-documents.spec.ts +++ b/packages/plugins/typescript/operations/tests/ts-documents.spec.ts @@ -3,7 +3,6 @@ import { parse, buildClientSchema, buildSchema } from 'graphql'; import { plugin } from '../src/index.js'; import { plugin as tsPlugin } from '../../typescript/src/index.js'; import { mergeOutputs, Types } from '@graphql-codegen/plugin-helpers'; -import { DocumentMode } from '@graphql-codegen/visitor-plugin-common'; describe('TypeScript Operations Plugin', () => { const gitHuntSchema = buildClientSchema(require('../../../../../dev-test/githunt/schema.json'));