Skip to content

Commit 8b509ec

Browse files
authored
fix: dedupe module imports in codegen (#1504)
1 parent cbbb0fc commit 8b509ec

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

.changeset/stupid-clouds-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': patch
3+
---
4+
5+
dedupe imports in codegen

packages/cli/src/type-generator.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import prettier from 'prettier';
77
// @ts-expect-error TODO: type out if necessary
88
import uncrashable from '@float-capital/float-subgraph-uncrashable/src/Index.bs.js';
99
import DataSourceTemplateCodeGenerator from './codegen/template';
10-
import { GENERATED_FILE_NOTE } from './codegen/typescript';
10+
import { GENERATED_FILE_NOTE, ModuleImports } from './codegen/typescript';
1111
import { displayPath } from './command-helpers/fs';
1212
import { Spinner, step, withSpinner } from './command-helpers/spinner';
1313
import debug from './debug';
@@ -190,6 +190,7 @@ export default class TypeGenerator {
190190
}
191191

192192
async generateTypesForDataSourceTemplates(subgraph: immutable.Map<any, any>) {
193+
const moduleImports: ModuleImports[] = [];
193194
return await withSpinner(
194195
`Generate types for data source templates`,
195196
`Failed to generate types for data source templates`,
@@ -200,22 +201,42 @@ export default class TypeGenerator {
200201
.get('templates', immutable.List())
201202
.reduce((codeSegments: any, template: any) => {
202203
step(spinner, 'Generate types for data source template', String(template.get('name')));
203-
204204
const codeGenerator = new DataSourceTemplateCodeGenerator(template, this.protocol);
205205

206-
// Only generate module imports once, because they are identical for
207-
// all types generated for data source templates.
208-
if (codeSegments.isEmpty()) {
209-
codeSegments = codeSegments.concat(codeGenerator.generateModuleImports());
210-
}
206+
// we want to get all the imports from the templates
207+
moduleImports.push(...codeGenerator.generateModuleImports());
211208

212209
return codeSegments.concat(codeGenerator.generateTypes());
213210
}, immutable.List());
214211

212+
// we want to dedupe the imports from the templates
213+
const dedupeModulesImports = moduleImports.reduce(
214+
(acc: ModuleImports[], curr: ModuleImports) => {
215+
const found = acc.find(item => item.module === curr.module);
216+
if (found) {
217+
const foundNames = Array.isArray(found.nameOrNames)
218+
? found.nameOrNames
219+
: [found.nameOrNames];
220+
const currNames = Array.isArray(curr.nameOrNames)
221+
? curr.nameOrNames
222+
: [curr.nameOrNames];
223+
const names = new Set([...foundNames, ...currNames]);
224+
found.nameOrNames = Array.from(names);
225+
} else {
226+
acc.push(curr);
227+
}
228+
return acc;
229+
},
230+
[],
231+
);
232+
215233
if (!codeSegments.isEmpty()) {
216-
const code = prettier.format([GENERATED_FILE_NOTE, ...codeSegments].join('\n'), {
217-
parser: 'typescript',
218-
});
234+
const code = prettier.format(
235+
[GENERATED_FILE_NOTE, ...dedupeModulesImports, ...codeSegments].join('\n'),
236+
{
237+
parser: 'typescript',
238+
},
239+
);
219240

220241
const outputFile = path.join(this.options.outputDir, 'templates.ts');
221242
step(spinner, `Write types for templates to`, displayPath(outputFile));

0 commit comments

Comments
 (0)