From 9312920a489c0504dc57ff90d603d80aa383ce6e Mon Sep 17 00:00:00 2001 From: Masayuki Izumi Date: Wed, 4 May 2022 20:41:44 +0900 Subject: [PATCH] fix(typescript-operations): Import type definitions of dependent fragments when `inlineFragmentType` is `mask` (#7799) * Import type definitions of dependent fragments when `inlineFragmentType` is `mask` * Add changeset --- .changeset/fifty-students-fail.md | 5 +++ .../typescript/operations/src/visitor.ts | 3 +- .../tests/fixtures/issue-7798-child.ts | 5 +++ .../tests/fixtures/issue-7798-parent.ts | 8 +++++ .../tests/near-operation-file.spec.ts | 35 +++++++++++++++++++ 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 .changeset/fifty-students-fail.md create mode 100644 packages/presets/near-operation-file/tests/fixtures/issue-7798-child.ts create mode 100644 packages/presets/near-operation-file/tests/fixtures/issue-7798-parent.ts diff --git a/.changeset/fifty-students-fail.md b/.changeset/fifty-students-fail.md new file mode 100644 index 00000000000..e9b2584c7be --- /dev/null +++ b/.changeset/fifty-students-fail.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/typescript-operations': patch +--- + +Import type definitions of dependent fragments when `inlineFragmentType` is `mask` diff --git a/packages/plugins/typescript/operations/src/visitor.ts b/packages/plugins/typescript/operations/src/visitor.ts index 05f3da1929e..a9e40b99651 100644 --- a/packages/plugins/typescript/operations/src/visitor.ts +++ b/packages/plugins/typescript/operations/src/visitor.ts @@ -118,7 +118,8 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor< } public getImports(): Array { - return !this.config.globalNamespace && this.config.inlineFragmentTypes === 'combine' + return !this.config.globalNamespace && + (this.config.inlineFragmentTypes === 'combine' || this.config.inlineFragmentTypes === 'mask') ? this.config.fragmentImports.map(fragmentImport => generateFragmentImportStatement(fragmentImport, 'type')) : []; } diff --git a/packages/presets/near-operation-file/tests/fixtures/issue-7798-child.ts b/packages/presets/near-operation-file/tests/fixtures/issue-7798-child.ts new file mode 100644 index 00000000000..c01c45eddbb --- /dev/null +++ b/packages/presets/near-operation-file/tests/fixtures/issue-7798-child.ts @@ -0,0 +1,5 @@ +export const UserNameFragment = /* GraphQL */ ` + fragment UserName on User { + name + } +`; diff --git a/packages/presets/near-operation-file/tests/fixtures/issue-7798-parent.ts b/packages/presets/near-operation-file/tests/fixtures/issue-7798-parent.ts new file mode 100644 index 00000000000..daa14d347f7 --- /dev/null +++ b/packages/presets/near-operation-file/tests/fixtures/issue-7798-parent.ts @@ -0,0 +1,8 @@ +import { UserNameFragment } from './issue-7798-child'; + +export const UserFragment = /* GraphQL */ ` + fragment User on User { + ...UserName + } + ${UserNameFragment} +`; diff --git a/packages/presets/near-operation-file/tests/near-operation-file.spec.ts b/packages/presets/near-operation-file/tests/near-operation-file.spec.ts index e08d42ba018..d0e422a015f 100644 --- a/packages/presets/near-operation-file/tests/near-operation-file.spec.ts +++ b/packages/presets/near-operation-file/tests/near-operation-file.spec.ts @@ -544,6 +544,41 @@ describe('near-operation-file preset', () => { ); } }); + + it('#7798 - importing type definitions of dependent fragments when `inlineFragmentType` is `mask`', async () => { + const result = await executeCodegen({ + schema: [ + /* GraphQL */ ` + type User { + id: ID! + name: String! + } + + type Query { + user(id: ID!): User! + } + `, + ], + documents: [ + path.join(__dirname, 'fixtures/issue-7798-parent.ts'), + path.join(__dirname, 'fixtures/issue-7798-child.ts'), + ], + generates: { + 'out1.ts': { + preset, + presetConfig: { + baseTypesPath: 'types.ts', + }, + plugins: ['typescript-operations'], + config: { inlineFragmentTypes: 'mask' }, + }, + }, + }); + + const parentContent = result.find(generatedDoc => generatedDoc.filename.match(/issue-7798-parent/)).content; + const imports = parentContent.match(/import.*UserNameFragment/g); + expect(imports).toHaveLength(1); + }); }); it('Should build the correct operation files paths', async () => {