Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS] Added outputGlobalTypes option to specify path for global types #546

Merged
merged 4 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,41 @@ export type SimpleQuery = {
//=============================================================="
`;

exports[`successful codegen writes TypeScript global types to a custom path when globalTypesFile is set 1`] = `
"/* tslint:disable */
// This file was automatically generated and should not be edited.

import { SomeEnum } from \\"./../../__foo__/bar\\";

// ====================================================
// GraphQL query operation: SimpleQuery
// ====================================================

export interface SimpleQuery {
someEnum: SomeEnum;
}
"
`;

exports[`successful codegen writes TypeScript global types to a custom path when globalTypesFile is set 2`] = `
"/* tslint:disable */
// This file was automatically generated and should not be edited.

//==============================================================
// START Enums and Input Objects
//==============================================================

export enum SomeEnum {
bar = \\"bar\\",
foo = \\"foo\\",
}

//==============================================================
// END Enums and Input Objects
//==============================================================
"
`;

exports[`successful codegen writes TypeScript types into a __generated__ directory next to sources when no output is set 1`] = `
"/* 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 @@ -467,6 +467,40 @@ describe("successful codegen", () => {
}
);

test
.do(() => {
vol.fromJSON({
"schema.json": JSON.stringify(fullSchema.__schema),
"directory/component.tsx": `
gql\`
query SimpleQuery {
someEnum
}
\`;
`
});
})
.command([
"codegen:generate",
"--schema=schema.json",
"--queries=**/*.tsx",
"--target=typescript",
"--globalTypesFile=__foo__/bar.ts"
])
.it(
"writes TypeScript global types to a custom path when globalTypesFile is set",
() => {
expect(
mockFS
.readFileSync("directory/__generated__/SimpleQuery.ts")
.toString()
).toMatchSnapshot();
expect(
mockFS.readFileSync("__foo__/bar.ts").toString()
).toMatchSnapshot();
}
);

test
.do(() => {
vol.fromJSON({
Expand Down
7 changes: 6 additions & 1 deletion packages/apollo-cli/src/commands/codegen/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export default class Generate extends Command {
description:
'By default, TypeScript/Flow will put each generated file in a directory next to its source file using the value of the "output" as the directory name. Set "outputFlat" to put all generated files in the directory relative to the current working directory defined by "output".'
}),
globalTypesFile: flags.string({
description:
'By default, TypeScript will put a file named "globalTypes.ts" inside the "output" directory. Set "globalTypesFile" to specify a different path.'
}),
watch: flags.boolean({
description: "Watch the query files to auto-generate on changes."
})
Expand Down Expand Up @@ -227,7 +231,8 @@ export default class Generate extends Command {
mergeInFieldsFromFragmentSpreads:
flags.mergeInFieldsFromFragmentSpreads,
useFlowExactObjects: flags.useFlowExactObjects,
useFlowReadOnlyTypes: flags.useFlowReadOnlyTypes
useFlowReadOnlyTypes: flags.useFlowReadOnlyTypes,
globalTypesFile: flags.globalTypesFile
}
);

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type Query {
hello: String!
serverSideField: ServerField!
someEnum: SomeEnum!
}

type ServerField {
Expand All @@ -15,3 +16,8 @@ type RemovedField {
type RemovedType {
fieldName: String
}

enum SomeEnum {
foo
bar
}
14 changes: 11 additions & 3 deletions packages/apollo-cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export type TargetType =

export type GenerationOptions = CompilerOptions &
LegacyCompilerOptions &
FlowCompilerOptions;
FlowCompilerOptions & {
globalTypesFile?: string;
};

export default function generate(
inputPaths: string[],
Expand Down Expand Up @@ -141,11 +143,17 @@ export default function generate(
nextToSources ||
(fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory())
) {
if (nextToSources && !fs.existsSync(outputPath)) {
if (options.globalTypesFile) {
const globalTypesDir = path.dirname(options.globalTypesFile);
if (!fs.existsSync(globalTypesDir)) {
fs.mkdirSync(globalTypesDir);
}
} else if (nextToSources && !fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath);
}

const globalSourcePath = path.join(outputPath, "globalTypes.ts");
const globalSourcePath =
options.globalTypesFile || path.join(outputPath, "globalTypes.ts");
outFiles[globalSourcePath] = {
output: generatedGlobalFile.fileContents
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ type Query {
# ⚡ Warning ⚡
hello: String!
serverSideField: ServerField!
someEnum: SomeEnum!

# ✔️ Notice ✔️
me: User
Expand All @@ -176,6 +177,8 @@ type RemovedField {

}

enum SomeEnum { }

type ServerField { }

"
Expand Down