Skip to content

Commit 6570f72

Browse files
danilobuergershadaj
authored andcommitted
[TS] Dedup enums and inputs by using global types file (#520)
* [TS] Dedup enums and inputs by using global types file * [TS] Only import global types that are being used * Sync text schema.graphql and .json * [TS] Refactored getGlobalTypesUsedForOperation
1 parent 5a496e1 commit 6570f72

10 files changed

Lines changed: 3050 additions & 1314 deletions

File tree

packages/apollo-cli/src/commands/codegen/__tests__/__snapshots__/generate.test.ts.snap

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,11 @@ exports[`successful codegen writes TypeScript types into a __generated__ directo
592592
593593
export interface SimpleQuery {
594594
hello: string;
595-
}
595+
}"
596+
`;
597+
598+
exports[`successful codegen writes TypeScript types into a __generated__ directory next to sources when no output is set 2`] = `
599+
"
596600
597601
/* tslint:disable */
598602
// This file was automatically generated and should not be edited.
@@ -618,7 +622,11 @@ exports[`successful codegen writes TypeScript types next to sources when output
618622
619623
export interface SimpleQuery {
620624
hello: string;
621-
}
625+
}"
626+
`;
627+
628+
exports[`successful codegen writes TypeScript types next to sources when output is set to empty string 2`] = `
629+
"
622630
623631
/* tslint:disable */
624632
// This file was automatically generated and should not be edited.
@@ -644,7 +652,11 @@ exports[`successful codegen writes TypeScript types to a custom directory next t
644652
645653
export interface SimpleQuery {
646654
hello: string;
647-
}
655+
}"
656+
`;
657+
658+
exports[`successful codegen writes TypeScript types to a custom directory next to sources when output is set 2`] = `
659+
"
648660
649661
/* tslint:disable */
650662
// This file was automatically generated and should not be edited.

packages/apollo-cli/src/commands/codegen/__tests__/generate.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ describe("successful codegen", () => {
325325
expect(
326326
mockFS.readFileSync("directory/__generated__/SimpleQuery.ts").toString()
327327
).toMatchSnapshot();
328+
expect(
329+
mockFS.readFileSync("__generated__/globalTypes.ts").toString()
330+
).toMatchSnapshot();
328331
});
329332

330333
test
@@ -380,6 +383,9 @@ describe("successful codegen", () => {
380383
.readFileSync("directory/__foo__/SimpleQuery.ts")
381384
.toString()
382385
).toMatchSnapshot();
386+
expect(
387+
mockFS.readFileSync("__foo__/globalTypes.ts").toString()
388+
).toMatchSnapshot();
383389
}
384390
);
385391

@@ -442,6 +448,9 @@ describe("successful codegen", () => {
442448
.readFileSync("directory/SimpleQuery.ts")
443449
.toString()
444450
).toMatchSnapshot();
451+
expect(
452+
mockFS.readFileSync("globalTypes.ts").toString()
453+
).toMatchSnapshot();
445454
}
446455
);
447456

packages/apollo-cli/src/generate.ts

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { generateSource as generateSwiftSource } from "apollo-codegen-swift";
1212
import { generateSource as generateTypescriptLegacySource } from "apollo-codegen-typescript-legacy";
1313
import { generateSource as generateFlowLegacySource } from "apollo-codegen-flow-legacy";
1414
import { generateSource as generateFlowSource } from "apollo-codegen-flow";
15-
import { generateSource as generateTypescriptSource } from "apollo-codegen-typescript";
15+
import { generateLocalSource as generateTypescriptLocalSource, generateGlobalSource as generateTypescriptGlobalSource } from "apollo-codegen-typescript";
1616
import { generateSource as generateScalaSource } from "apollo-codegen-scala";
1717
import { GraphQLSchema } from "graphql";
1818
import { FlowCompilerOptions } from '../../apollo-codegen-flow/lib/language';
@@ -71,12 +71,9 @@ export default function generate(
7171
writeOperationIdsMap(context);
7272
writtenFiles += 1;
7373
}
74-
} else if (target === "flow" || target === "typescript" || target === "ts") {
74+
} else if (target === "flow") {
7575
const context = compileToIR(schema, document, options);
76-
const { generatedFiles, common } =
77-
target === "flow"
78-
? generateFlowSource(context)
79-
: generateTypescriptSource(context);
76+
const { generatedFiles, common } = generateFlowSource(context);
8077

8178
const outFiles: {
8279
[fileName: string]: BasicGeneratedFile;
@@ -118,6 +115,52 @@ export default function generate(
118115
generatedFiles.map(o => o.content.fileContents).join("\n") + common
119116
);
120117

118+
writtenFiles += 1;
119+
}
120+
} else if (target === "typescript" || target === "ts") {
121+
const context = compileToIR(schema, document, options);
122+
const generatedFiles = generateTypescriptLocalSource(context);
123+
const generatedGlobalFile = generateTypescriptGlobalSource(context);
124+
125+
const outFiles: {
126+
[fileName: string]: BasicGeneratedFile;
127+
} = {};
128+
129+
if (nextToSources || (fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory())) {
130+
if (nextToSources && !fs.existsSync(outputPath)) {
131+
fs.mkdirSync(outputPath);
132+
}
133+
134+
const globalSourcePath = path.join(outputPath, "globalTypes.ts");
135+
outFiles[globalSourcePath] = {
136+
output: generatedGlobalFile.fileContents,
137+
};
138+
139+
generatedFiles.forEach(({ sourcePath, fileName, content }) => {
140+
let dir = outputPath;
141+
if (nextToSources) {
142+
dir = path.join(path.dirname(sourcePath), dir);
143+
if (!fs.existsSync(dir)) {
144+
fs.mkdirSync(dir);
145+
}
146+
}
147+
148+
const outFilePath = path.join(dir, fileName);
149+
outFiles[outFilePath] = {
150+
output: content({ outputPath: outFilePath, globalSourcePath }).fileContents,
151+
};
152+
});
153+
154+
writeGeneratedFiles(outFiles, path.resolve("."));
155+
156+
writtenFiles += Object.keys(outFiles).length;
157+
} else {
158+
fs.writeFileSync(
159+
outputPath,
160+
generatedFiles.map(o => o.content().fileContents).join("\n") +
161+
generatedGlobalFile.fileContents,
162+
);
163+
121164
writtenFiles += 1;
122165
}
123166
} else {

0 commit comments

Comments
 (0)