From ed9c205d15d7f14ed73e54aecf40e4fad5664e9d Mon Sep 17 00:00:00 2001 From: "Henry Q. Dineen" Date: Tue, 20 Feb 2024 15:25:29 -0500 Subject: [PATCH] fix(typescript-operations): properly handle aliased conditionals (#9842) * fix(typescript-operations): properly handle aliased fields with conditional directives * add changeset --- .changeset/mighty-doors-stare.md | 6 ++ .../src/selection-set-processor/base.ts | 2 +- .../pre-resolve-types.ts | 7 ++- .../src/selection-set-to-object.ts | 5 +- .../operations/tests/ts-documents.spec.ts | 56 +++++++++++++++++++ 5 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 .changeset/mighty-doors-stare.md diff --git a/.changeset/mighty-doors-stare.md b/.changeset/mighty-doors-stare.md new file mode 100644 index 00000000000..f8f31d0f113 --- /dev/null +++ b/.changeset/mighty-doors-stare.md @@ -0,0 +1,6 @@ +--- +'@graphql-codegen/visitor-plugin-common': patch +'@graphql-codegen/typescript-operations': patch +--- + +properly handle aliased conditionals diff --git a/packages/plugins/other/visitor-plugin-common/src/selection-set-processor/base.ts b/packages/plugins/other/visitor-plugin-common/src/selection-set-processor/base.ts index 619fd1bfdc9..8f2edb4a5aa 100644 --- a/packages/plugins/other/visitor-plugin-common/src/selection-set-processor/base.ts +++ b/packages/plugins/other/visitor-plugin-common/src/selection-set-processor/base.ts @@ -2,7 +2,7 @@ import { GraphQLInterfaceType, GraphQLNamedType, GraphQLObjectType, GraphQLOutpu import { AvoidOptionalsConfig, ConvertNameFn, NormalizedScalarsMap } from '../types.js'; export type PrimitiveField = { isConditional: boolean; fieldName: string }; -export type PrimitiveAliasedFields = { alias: string; fieldName: string }; +export type PrimitiveAliasedFields = { isConditional: boolean; alias: string; fieldName: string }; export type LinkField = { alias: string; name: string; type: string; selectionSet: string }; export type NameAndType = { name: string; type: string }; export type ProcessResult = null | Array; diff --git a/packages/plugins/other/visitor-plugin-common/src/selection-set-processor/pre-resolve-types.ts b/packages/plugins/other/visitor-plugin-common/src/selection-set-processor/pre-resolve-types.ts index ece06d6c54a..2be42bfa053 100644 --- a/packages/plugins/other/visitor-plugin-common/src/selection-set-processor/pre-resolve-types.ts +++ b/packages/plugins/other/visitor-plugin-common/src/selection-set-processor/pre-resolve-types.ts @@ -101,7 +101,12 @@ export class PreResolveTypesProcessor extends BaseSelectionSetProcessor ({ alias: field.alias.value, fieldName: field.name.value, + isConditional: hasConditionalDirectives(field), })), options.unsetTypes ), diff --git a/packages/plugins/typescript/operations/tests/ts-documents.spec.ts b/packages/plugins/typescript/operations/tests/ts-documents.spec.ts index edb7603924f..65ca1a8a0fe 100644 --- a/packages/plugins/typescript/operations/tests/ts-documents.spec.ts +++ b/packages/plugins/typescript/operations/tests/ts-documents.spec.ts @@ -6051,6 +6051,62 @@ function test(q: GetEntityBrandDataQuery): void { expect(content).toMatchSnapshot(); }); + + it('#8461 - conditional directives are ignored on fields with alias', async () => { + const testSchema = buildSchema(/* GraphQL */ ` + type User { + firstName: String! + lastName: Int! + address: Address! + } + + type Address { + postalCode: String! + } + + type Query { + viewer: User! + } + `); + + const query = parse(/* GraphQL */ ` + query UserQuery($skipFirstName: Boolean!, $skipAddress: Boolean!) { + viewer { + givenName: firstName @skip(if: $skipFirstName) + lastName + mailingAddress: address @skip(if: $skipAddress) { + postalCode + } + } + } + `); + + const config = { preResolveTypes: true }; + + const { content } = await plugin(testSchema, [{ location: '', document: query }], config, { + outputFile: 'graphql.ts', + }); + + expect(content).toBeSimilarStringTo(` + export type UserQueryQueryVariables = Exact<{ + skipFirstName: Scalars['Boolean']['input']; + skipAddress: Scalars['Boolean']['input']; + }>; + + export type UserQueryQuery = { + __typename?: 'Query', + viewer: { + __typename?: 'User', + lastName: number, + givenName?: string, + mailingAddress?: { + __typename?: 'Address', + postalCode: string + } + } + }; + `); + }); }); describe('conditional directives handling', () => {