Skip to content

Commit

Permalink
fix(typescript-operations): Import type definitions of dependent frag…
Browse files Browse the repository at this point in the history
…ments when `inlineFragmentType` is `mask` (#7799)

* Import type definitions of dependent fragments when `inlineFragmentType` is `mask`

* Add changeset
  • Loading branch information
izumin5210 authored May 4, 2022
1 parent 2966686 commit 9312920
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fifty-students-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript-operations': patch
---

Import type definitions of dependent fragments when `inlineFragmentType` is `mask`
3 changes: 2 additions & 1 deletion packages/plugins/typescript/operations/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
}

public getImports(): Array<string> {
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'))
: [];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const UserNameFragment = /* GraphQL */ `
fragment UserName on User {
name
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { UserNameFragment } from './issue-7798-child';

export const UserFragment = /* GraphQL */ `
fragment User on User {
...UserName
}
${UserNameFragment}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

1 comment on commit 9312920

@vercel
Copy link

@vercel vercel bot commented on 9312920 May 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.