Skip to content

Commit b94a187

Browse files
committed
Do not create globalTypes.ts when it is empty
Fix #2080 Alternative to #2055
1 parent dc4fe35 commit b94a187

4 files changed

Lines changed: 170 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
- `apollo-codegen-swift`
1212
- <First `apollo-codegen-swift` related entry goes here>
1313
- `apollo-codegen-typescript`
14-
- <First `apollo-codegen-typescript` related entry goes here>
14+
- Don't generate globalTypes.ts when it is empty.
1515
- `apollo-codegen-core`
1616
- <First `apollo-codegen-core` related entry goes here>
1717
- `apollo-env`

packages/apollo/src/commands/client/__tests__/__snapshots__/generate.test.ts.snap

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,108 @@ exports[`client:codegen generates operation IDs for swift files when flag is set
4141
}"
4242
`;
4343

44+
exports[`client:codegen writes Flow types to a custom directory next to sources when output is set 1`] = `
45+
"/* @flow */
46+
/* eslint-disable */
47+
// @generated
48+
// This file was automatically generated and should not be edited.
49+
50+
// ====================================================
51+
// GraphQL query operation: SimpleQuery
52+
// ====================================================
53+
54+
export type SimpleQuery = {
55+
hello: string
56+
};/* @flow */
57+
/* eslint-disable */
58+
// @generated
59+
// This file was automatically generated and should not be edited.
60+
61+
//==============================================================
62+
// START Enums and Input Objects
63+
//==============================================================
64+
65+
//==============================================================
66+
// END Enums and Input Objects
67+
//=============================================================="
68+
`;
69+
70+
exports[`client:codegen writes TypeScript global types to __generated__/globalTypes.ts when globalTypesFile is not set 1`] = `
71+
"/* tslint:disable */
72+
/* eslint-disable */
73+
// @generated
74+
// This file was automatically generated and should not be edited.
75+
76+
//==============================================================
77+
// START Enums and Input Objects
78+
//==============================================================
79+
80+
export enum SomeEnum {
81+
bar = \\"bar\\",
82+
foo = \\"foo\\",
83+
}
84+
85+
//==============================================================
86+
// END Enums and Input Objects
87+
//==============================================================
88+
"
89+
`;
90+
91+
exports[`client:codegen writes TypeScript global types to a custom path when globalTypesFile is set 1`] = `
92+
"/* tslint:disable */
93+
/* eslint-disable */
94+
// @generated
95+
// This file was automatically generated and should not be edited.
96+
97+
import { SomeEnum } from \\"./../../__foo__/bar\\";
98+
99+
// ====================================================
100+
// GraphQL query operation: SimpleQuery
101+
// ====================================================
102+
103+
export interface SimpleQuery {
104+
someEnum: SomeEnum;
105+
}
106+
"
107+
`;
108+
109+
exports[`client:codegen writes TypeScript global types to a custom path when globalTypesFile is set 2`] = `
110+
"/* tslint:disable */
111+
/* eslint-disable */
112+
// @generated
113+
// This file was automatically generated and should not be edited.
114+
115+
//==============================================================
116+
// START Enums and Input Objects
117+
//==============================================================
118+
119+
export enum SomeEnum {
120+
bar = \\"bar\\",
121+
foo = \\"foo\\",
122+
}
123+
124+
//==============================================================
125+
// END Enums and Input Objects
126+
//==============================================================
127+
"
128+
`;
129+
130+
exports[`client:codegen writes TypeScript types into a __generated__ directory next to sources when no output is set 1`] = `
131+
"/* tslint:disable */
132+
/* eslint-disable */
133+
// @generated
134+
// This file was automatically generated and should not be edited.
135+
136+
// ====================================================
137+
// GraphQL query operation: SimpleQuery
138+
// ====================================================
139+
140+
export interface SimpleQuery {
141+
hello: string;
142+
}
143+
"
144+
`;
145+
44146
exports[`client:codegen writes exact Flow types when the useFlowExactObjects flag is set 1`] = `
45147
"/* @flow */
46148
/* eslint-disable */

packages/apollo/src/commands/client/__tests__/generate.test.ts

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,7 @@ describe("client:codegen", () => {
441441
expect(output).toMatchSnapshot();
442442
});
443443

444-
// TODO: fix UnhandledPromiseRejection
445444
test
446-
.skip()
447445
.fs({
448446
"schema.json": fullSchemaJsonString,
449447
"components/component.tsx": `
@@ -471,12 +469,65 @@ describe("client:codegen", () => {
471469
.readFileSync("./components/__generated__/SimpleQuery.ts")
472470
.toString()
473471
).toMatchSnapshot();
472+
}
473+
);
474+
475+
test
476+
.fs({
477+
"schema.json": fullSchemaJsonString,
478+
"components/component.tsx": `
479+
const query = gql\`
480+
query SimpleQuery {
481+
someEnum
482+
}
483+
\`;
484+
`,
485+
"my.config.js": `
486+
module.exports = {
487+
client: {
488+
includes: ["./**.graphql", "./**/*.tsx"],
489+
service: { name: "my-service-name", localSchemaFile: "./schema.json" }
490+
}
491+
}
492+
`
493+
})
494+
.command(["client:codegen", "--target=typescript", "--config=my.config.js"])
495+
.it(
496+
"writes TypeScript global types to __generated__/globalTypes.ts when globalTypesFile is not set",
497+
() => {
474498
expect(
475499
fs.readFileSync("./__generated__/globalTypes.ts").toString()
476500
).toMatchSnapshot();
477501
}
478502
);
479503

504+
test
505+
.fs({
506+
"schema.json": fullSchemaJsonString,
507+
"components/component.tsx": `
508+
const query = gql\`
509+
query SimpleQuery {
510+
hello
511+
}
512+
\`;
513+
`,
514+
"my.config.js": `
515+
module.exports = {
516+
client: {
517+
includes: ["./**.graphql", "./**/*.tsx"],
518+
service: { name: "my-service-name", localSchemaFile: "./schema.json" }
519+
}
520+
}
521+
`
522+
})
523+
.command(["client:codegen", "--target=typescript", "--config=my.config.js"])
524+
.it(
525+
"doesn't write an empty TypeScript global types file when it is empty",
526+
() => {
527+
expect(fs.existsSync("./__generated__/globalTypes.ts")).toBe(false);
528+
}
529+
);
530+
480531
// TODO: fix
481532
test
482533
.skip()
@@ -547,9 +598,7 @@ describe("client:codegen", () => {
547598
}
548599
);
549600

550-
// TODO: fix unhandled rejection
551601
test
552-
.skip()
553602
.fs({
554603
"schema.json": fullSchemaJsonString,
555604
"components/component.tsx": `
@@ -584,9 +633,7 @@ describe("client:codegen", () => {
584633
}
585634
);
586635

587-
// fix
588636
test
589-
.skip()
590637
.fs({
591638
"schema.json": fullSchemaJsonString,
592639
"components/component.jsx": `

packages/apollo/src/generate.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,25 +156,27 @@ export default function generate(
156156
nextToSources ||
157157
(fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory())
158158
) {
159-
if (options.globalTypesFile) {
160-
const globalTypesDir = path.dirname(options.globalTypesFile);
161-
if (!fs.existsSync(globalTypesDir)) {
162-
fs.mkdirSync(globalTypesDir);
163-
}
164-
} else if (nextToSources && !fs.existsSync(outputPath)) {
165-
fs.mkdirSync(outputPath);
166-
}
167-
168159
const globalSourcePath =
169160
options.globalTypesFile ||
170161
path.join(
171162
outputPath,
172163
`globalTypes.${options.tsFileExtension ||
173164
TYPESCRIPT_DEFAULT_FILE_EXTENSION}`
174165
);
175-
outFiles[globalSourcePath] = {
176-
output: generatedGlobalFile.fileContents
177-
};
166+
167+
if (context.typesUsed.length > 0) {
168+
if (options.globalTypesFile) {
169+
const globalTypesDir = path.dirname(options.globalTypesFile);
170+
if (!fs.existsSync(globalTypesDir)) {
171+
fs.mkdirSync(globalTypesDir);
172+
}
173+
} else if (nextToSources && !fs.existsSync(outputPath)) {
174+
fs.mkdirSync(outputPath);
175+
}
176+
outFiles[globalSourcePath] = {
177+
output: generatedGlobalFile.fileContents
178+
};
179+
}
178180

179181
generatedFiles.forEach(({ sourcePath, fileName, content }) => {
180182
let dir = outputPath;

0 commit comments

Comments
 (0)