Skip to content

Commit 9312920

Browse files
authored
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
1 parent 2966686 commit 9312920

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

‎.changeset/fifty-students-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/typescript-operations': patch
3+
---
4+
5+
Import type definitions of dependent fragments when `inlineFragmentType` is `mask`

‎packages/plugins/typescript/operations/src/visitor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
118118
}
119119

120120
public getImports(): Array<string> {
121-
return !this.config.globalNamespace && this.config.inlineFragmentTypes === 'combine'
121+
return !this.config.globalNamespace &&
122+
(this.config.inlineFragmentTypes === 'combine' || this.config.inlineFragmentTypes === 'mask')
122123
? this.config.fragmentImports.map(fragmentImport => generateFragmentImportStatement(fragmentImport, 'type'))
123124
: [];
124125
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const UserNameFragment = /* GraphQL */ `
2+
fragment UserName on User {
3+
name
4+
}
5+
`;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { UserNameFragment } from './issue-7798-child';
2+
3+
export const UserFragment = /* GraphQL */ `
4+
fragment User on User {
5+
...UserName
6+
}
7+
${UserNameFragment}
8+
`;

‎packages/presets/near-operation-file/tests/near-operation-file.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,41 @@ describe('near-operation-file preset', () => {
544544
);
545545
}
546546
});
547+
548+
it('#7798 - importing type definitions of dependent fragments when `inlineFragmentType` is `mask`', async () => {
549+
const result = await executeCodegen({
550+
schema: [
551+
/* GraphQL */ `
552+
type User {
553+
id: ID!
554+
name: String!
555+
}
556+
557+
type Query {
558+
user(id: ID!): User!
559+
}
560+
`,
561+
],
562+
documents: [
563+
path.join(__dirname, 'fixtures/issue-7798-parent.ts'),
564+
path.join(__dirname, 'fixtures/issue-7798-child.ts'),
565+
],
566+
generates: {
567+
'out1.ts': {
568+
preset,
569+
presetConfig: {
570+
baseTypesPath: 'types.ts',
571+
},
572+
plugins: ['typescript-operations'],
573+
config: { inlineFragmentTypes: 'mask' },
574+
},
575+
},
576+
});
577+
578+
const parentContent = result.find(generatedDoc => generatedDoc.filename.match(/issue-7798-parent/)).content;
579+
const imports = parentContent.match(/import.*UserNameFragment/g);
580+
expect(imports).toHaveLength(1);
581+
});
547582
});
548583

549584
it('Should build the correct operation files paths', async () => {

0 commit comments

Comments
 (0)