Skip to content

Commit 03e2824

Browse files
danilobuergermartijnwalraven
authored andcommitted
[TS] Add outputGlobalTypes option to specify path for global types (#546)
1 parent 41b2198 commit 03e2824

7 files changed

Lines changed: 97 additions & 6 deletions

File tree

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,41 @@ export type SimpleQuery = {
575575
//=============================================================="
576576
`;
577577

578+
exports[`successful codegen writes TypeScript global types to a custom path when globalTypesFile is set 1`] = `
579+
"/* tslint:disable */
580+
// This file was automatically generated and should not be edited.
581+
582+
import { SomeEnum } from \\"./../../__foo__/bar\\";
583+
584+
// ====================================================
585+
// GraphQL query operation: SimpleQuery
586+
// ====================================================
587+
588+
export interface SimpleQuery {
589+
someEnum: SomeEnum;
590+
}
591+
"
592+
`;
593+
594+
exports[`successful codegen writes TypeScript global types to a custom path when globalTypesFile is set 2`] = `
595+
"/* tslint:disable */
596+
// This file was automatically generated and should not be edited.
597+
598+
//==============================================================
599+
// START Enums and Input Objects
600+
//==============================================================
601+
602+
export enum SomeEnum {
603+
bar = \\"bar\\",
604+
foo = \\"foo\\",
605+
}
606+
607+
//==============================================================
608+
// END Enums and Input Objects
609+
//==============================================================
610+
"
611+
`;
612+
578613
exports[`successful codegen writes TypeScript types into a __generated__ directory next to sources when no output is set 1`] = `
579614
"/* tslint:disable */
580615
// This file was automatically generated and should not be edited.

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,40 @@ describe("successful codegen", () => {
467467
}
468468
);
469469

470+
test
471+
.do(() => {
472+
vol.fromJSON({
473+
"schema.json": JSON.stringify(fullSchema.__schema),
474+
"directory/component.tsx": `
475+
gql\`
476+
query SimpleQuery {
477+
someEnum
478+
}
479+
\`;
480+
`
481+
});
482+
})
483+
.command([
484+
"codegen:generate",
485+
"--schema=schema.json",
486+
"--queries=**/*.tsx",
487+
"--target=typescript",
488+
"--globalTypesFile=__foo__/bar.ts"
489+
])
490+
.it(
491+
"writes TypeScript global types to a custom path when globalTypesFile is set",
492+
() => {
493+
expect(
494+
mockFS
495+
.readFileSync("directory/__generated__/SimpleQuery.ts")
496+
.toString()
497+
).toMatchSnapshot();
498+
expect(
499+
mockFS.readFileSync("__foo__/bar.ts").toString()
500+
).toMatchSnapshot();
501+
}
502+
);
503+
470504
test
471505
.do(() => {
472506
vol.fromJSON({

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ export default class Generate extends Command {
9595
description:
9696
'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".'
9797
}),
98+
globalTypesFile: flags.string({
99+
description:
100+
'By default, TypeScript will put a file named "globalTypes.ts" inside the "output" directory. Set "globalTypesFile" to specify a different path.'
101+
}),
98102
watch: flags.boolean({
99103
description: "Watch the query files to auto-generate on changes."
100104
})
@@ -227,7 +231,8 @@ export default class Generate extends Command {
227231
mergeInFieldsFromFragmentSpreads:
228232
flags.mergeInFieldsFromFragmentSpreads,
229233
useFlowExactObjects: flags.useFlowExactObjects,
230-
useFlowReadOnlyTypes: flags.useFlowReadOnlyTypes
234+
useFlowReadOnlyTypes: flags.useFlowReadOnlyTypes,
235+
globalTypesFile: flags.globalTypesFile
231236
}
232237
);
233238

packages/apollo-cli/src/commands/schema/__tests__/__snapshots__/download.test.ts.snap

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

packages/apollo-cli/src/commands/schema/__tests__/fixtures/schema.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
type Query {
22
hello: String!
33
serverSideField: ServerField!
4+
someEnum: SomeEnum!
45
}
56

67
type ServerField {
@@ -15,3 +16,8 @@ type RemovedField {
1516
type RemovedType {
1617
fieldName: String
1718
}
19+
20+
enum SomeEnum {
21+
foo
22+
bar
23+
}

packages/apollo-cli/src/generate.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ export type TargetType =
4040

4141
export type GenerationOptions = CompilerOptions &
4242
LegacyCompilerOptions &
43-
FlowCompilerOptions;
43+
FlowCompilerOptions & {
44+
globalTypesFile?: string;
45+
};
4446

4547
export default function generate(
4648
inputPaths: string[],
@@ -141,11 +143,17 @@ export default function generate(
141143
nextToSources ||
142144
(fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory())
143145
) {
144-
if (nextToSources && !fs.existsSync(outputPath)) {
146+
if (options.globalTypesFile) {
147+
const globalTypesDir = path.dirname(options.globalTypesFile);
148+
if (!fs.existsSync(globalTypesDir)) {
149+
fs.mkdirSync(globalTypesDir);
150+
}
151+
} else if (nextToSources && !fs.existsSync(outputPath)) {
145152
fs.mkdirSync(outputPath);
146153
}
147154

148-
const globalSourcePath = path.join(outputPath, "globalTypes.ts");
155+
const globalSourcePath =
156+
options.globalTypesFile || path.join(outputPath, "globalTypes.ts");
149157
outFiles[globalSourcePath] = {
150158
output: generatedGlobalFile.fileContents
151159
};

packages/apollo-cli/src/printer/__tests__/__snapshots__/print.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ type Query {
161161
# ⚡ Warning
162162
hello: String!
163163
serverSideField: ServerField!
164+
someEnum: SomeEnum!
164165
165166
# ✔️ Notice ✔️
166167
me: User
@@ -176,6 +177,8 @@ type RemovedField {
176177
177178
}
178179
180+
enum SomeEnum { }
181+
179182
type ServerField { }
180183
181184
"

0 commit comments

Comments
 (0)