Skip to content

Commit

Permalink
[TS] Dedup enums and inputs by using global types file (#520)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
danilobuerger authored and shadaj committed Aug 2, 2018
1 parent 5a496e1 commit 6570f72
Show file tree
Hide file tree
Showing 10 changed files with 3,050 additions and 1,314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,11 @@ exports[`successful codegen writes TypeScript types into a __generated__ directo
export interface SimpleQuery {
hello: string;
}
}"
`;

exports[`successful codegen writes TypeScript types into a __generated__ directory next to sources when no output is set 2`] = `
"
/* tslint:disable */
// This file was automatically generated and should not be edited.
Expand All @@ -618,7 +622,11 @@ exports[`successful codegen writes TypeScript types next to sources when output
export interface SimpleQuery {
hello: string;
}
}"
`;

exports[`successful codegen writes TypeScript types next to sources when output is set to empty string 2`] = `
"
/* tslint:disable */
// This file was automatically generated and should not be edited.
Expand All @@ -644,7 +652,11 @@ exports[`successful codegen writes TypeScript types to a custom directory next t
export interface SimpleQuery {
hello: string;
}
}"
`;

exports[`successful codegen writes TypeScript types to a custom directory next to sources when output is set 2`] = `
"
/* tslint:disable */
// This file was automatically generated and should not be edited.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ describe("successful codegen", () => {
expect(
mockFS.readFileSync("directory/__generated__/SimpleQuery.ts").toString()
).toMatchSnapshot();
expect(
mockFS.readFileSync("__generated__/globalTypes.ts").toString()
).toMatchSnapshot();
});

test
Expand Down Expand Up @@ -380,6 +383,9 @@ describe("successful codegen", () => {
.readFileSync("directory/__foo__/SimpleQuery.ts")
.toString()
).toMatchSnapshot();
expect(
mockFS.readFileSync("__foo__/globalTypes.ts").toString()
).toMatchSnapshot();
}
);

Expand Down Expand Up @@ -442,6 +448,9 @@ describe("successful codegen", () => {
.readFileSync("directory/SimpleQuery.ts")
.toString()
).toMatchSnapshot();
expect(
mockFS.readFileSync("globalTypes.ts").toString()
).toMatchSnapshot();
}
);

Expand Down
55 changes: 49 additions & 6 deletions packages/apollo-cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { generateSource as generateSwiftSource } from "apollo-codegen-swift";
import { generateSource as generateTypescriptLegacySource } from "apollo-codegen-typescript-legacy";
import { generateSource as generateFlowLegacySource } from "apollo-codegen-flow-legacy";
import { generateSource as generateFlowSource } from "apollo-codegen-flow";
import { generateSource as generateTypescriptSource } from "apollo-codegen-typescript";
import { generateLocalSource as generateTypescriptLocalSource, generateGlobalSource as generateTypescriptGlobalSource } from "apollo-codegen-typescript";
import { generateSource as generateScalaSource } from "apollo-codegen-scala";
import { GraphQLSchema } from "graphql";
import { FlowCompilerOptions } from '../../apollo-codegen-flow/lib/language';
Expand Down Expand Up @@ -71,12 +71,9 @@ export default function generate(
writeOperationIdsMap(context);
writtenFiles += 1;
}
} else if (target === "flow" || target === "typescript" || target === "ts") {
} else if (target === "flow") {
const context = compileToIR(schema, document, options);
const { generatedFiles, common } =
target === "flow"
? generateFlowSource(context)
: generateTypescriptSource(context);
const { generatedFiles, common } = generateFlowSource(context);

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

writtenFiles += 1;
}
} else if (target === "typescript" || target === "ts") {
const context = compileToIR(schema, document, options);
const generatedFiles = generateTypescriptLocalSource(context);
const generatedGlobalFile = generateTypescriptGlobalSource(context);

const outFiles: {
[fileName: string]: BasicGeneratedFile;
} = {};

if (nextToSources || (fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory())) {
if (nextToSources && !fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath);
}

const globalSourcePath = path.join(outputPath, "globalTypes.ts");
outFiles[globalSourcePath] = {
output: generatedGlobalFile.fileContents,
};

generatedFiles.forEach(({ sourcePath, fileName, content }) => {
let dir = outputPath;
if (nextToSources) {
dir = path.join(path.dirname(sourcePath), dir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}

const outFilePath = path.join(dir, fileName);
outFiles[outFilePath] = {
output: content({ outputPath: outFilePath, globalSourcePath }).fileContents,
};
});

writeGeneratedFiles(outFiles, path.resolve("."));

writtenFiles += Object.keys(outFiles).length;
} else {
fs.writeFileSync(
outputPath,
generatedFiles.map(o => o.content().fileContents).join("\n") +
generatedGlobalFile.fileContents,
);

writtenFiles += 1;
}
} else {
Expand Down
Loading

0 comments on commit 6570f72

Please sign in to comment.