Skip to content

Commit

Permalink
avoid printing imports when there are no operations (#5035)
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha committed Nov 4, 2020
1 parent 2e6f20a commit 3e3941b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/olive-owls-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typed-document-node': patch
---

Avoid printing imports when there are no operations
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const plugin: PluginFunction<RawClientSideBasePluginConfig> = (
const visitorResult = visit(allAst, { leave: visitor });

return {
prepend: visitor.getImports(),
prepend: allAst.definitions.length === 0 ? [] : visitor.getImports(),
content: [visitor.fragments, ...visitorResult.definitions.filter(t => typeof t === 'string')].join('\n'),
};
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Types } from '@graphql-codegen/plugin-helpers';
import { plugin } from '../src';

describe('TypeDocumentNode', () => {
it('Should not output imports when there are no operations at all', async () => {
const result = (await plugin(null as any, [], {})) as Types.ComplexPluginOutput;
expect(result.content).toBe('');
expect(result.prepend.length).toBe(0);
});
});

0 comments on commit 3e3941b

Please sign in to comment.