Skip to content
Merged
1 change: 1 addition & 0 deletions packages/apollo-codegen-core/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface CompilerOptions {
namespace?: string;
generateOperationIds?: boolean;
operationIdsPath?: string;
fileExtension?: string;
}

export interface CompilerContext {
Expand Down
15 changes: 11 additions & 4 deletions packages/apollo-codegen-typescript/src/codeGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import TypescriptGenerator, {
TypescriptCompilerOptions
} from "./language";
import Printer from "./printer";
import { DEFAULT_FILE_EXTENSION } from "./helpers";
import { GraphQLType, isListType } from "graphql/type/definition";
import {
GraphQLNonNull,
Expand Down Expand Up @@ -84,14 +85,15 @@ function printGlobalImport(
generator: TypescriptAPIGenerator,
typesUsed: GraphQLType[],
outputPath: string,
fileExtension: string,
globalSourcePath: string
) {
if (typesUsed.length > 0) {
const relative = path.relative(
path.dirname(outputPath),
path.join(
path.dirname(globalSourcePath),
path.basename(globalSourcePath, ".ts")
path.basename(globalSourcePath, fileExtension)
)
);

Expand All @@ -118,7 +120,8 @@ export function generateSource(context: CompilerContext) {

generatedFiles.push({
sourcePath: operation.filePath,
fileName: `${operation.operationName}.ts`,
fileName: `${operation.operationName}${context.options.fileExtension ||
DEFAULT_FILE_EXTENSION}`,
content: new TypescriptGeneratedFile(output)
});
});
Expand Down Expand Up @@ -164,14 +167,16 @@ export function generateLocalSource(

const operations = Object.values(context.operations).map(operation => ({
sourcePath: operation.filePath,
fileName: `${operation.operationName}.ts`,
fileName: `${operation.operationName}${context.options.fileExtension ||
DEFAULT_FILE_EXTENSION}`,
content: (options?: IGeneratedFileOptions) => {
generator.fileHeader();
if (options && options.outputPath && options.globalSourcePath) {
printGlobalImport(
generator,
generator.getGlobalTypesUsedForOperation(operation),
options.outputPath,
context.options.fileExtension || DEFAULT_FILE_EXTENSION,
options.globalSourcePath
);
}
Expand All @@ -183,14 +188,16 @@ export function generateLocalSource(

const fragments = Object.values(context.fragments).map(fragment => ({
sourcePath: fragment.filePath,
fileName: `${fragment.fragmentName}.ts`,
fileName: `${fragment.fragmentName}${context.options.fileExtension ||
DEFAULT_FILE_EXTENSION}`,
content: (options?: IGeneratedFileOptions) => {
generator.fileHeader();
if (options && options.outputPath && options.globalSourcePath) {
printGlobalImport(
generator,
generator.getGlobalTypesUsedForFragment(fragment),
options.outputPath,
context.options.fileExtension || DEFAULT_FILE_EXTENSION,
options.globalSourcePath
);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/apollo-codegen-typescript/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import * as t from "@babel/types";

import { CompilerOptions } from "apollo-codegen-core/lib/compiler";

const DEFAULT_FILE_EXTENSION = ".ts";

const builtInScalarMap = {
[GraphQLString.name]: t.TSStringKeyword(),
[GraphQLInt.name]: t.TSNumberKeyword(),
Expand Down Expand Up @@ -71,3 +73,5 @@ export function createTypeFromGraphQLTypeFunction(

return typeFromGraphQLType;
}

export { DEFAULT_FILE_EXTENSION };
20 changes: 20 additions & 0 deletions packages/apollo/src/commands/client/__tests__/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,26 @@ describe("client:codegen", () => {
expect(fs.readFileSync("API.ts").toString()).toMatchSnapshot();
});

test
Comment thread
JakeDawkins marked this conversation as resolved.
.fs({
"schema.json": fullSchemaJsonString,
"queryOne.graphql": simpleQuery.toString(),
"my.config.js": defaultConfig
})
.command([
"client:codegen",
"--config=my.config.js",
"--target=typescript",
"--fileExtension=.d.ts",
Comment thread
adrienharnay marked this conversation as resolved.
Outdated
"outDirectory"
])
.it("writes types for typescript with a custom extension", () => {
const [filePath] = fs.readdirSync("./outDirectory");
const file = fs.readFileSync(`./outDirectory/${filePath}`).toString();
Comment thread
adrienharnay marked this conversation as resolved.

expect(file).toMatchSnapshot();
});

test
.fs({
"schema.json": fullSchemaJsonString,
Expand Down
6 changes: 5 additions & 1 deletion packages/apollo/src/commands/client/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ export default class Generate extends ClientCommand {
// typescript
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.'
'By default, TypeScript will put a file named "globalTypes.ts" inside the "output" directory. Set "globalTypesFile" to specify a different path. Alternatively, set "fileExtension" to modify the extension of the file'
}),
fileExtension: flags.string({
description:
'By default, TypeScript will output ".ts" files. Set "fileExtension" to specify a different file extension'
Comment thread
adrienharnay marked this conversation as resolved.
Outdated
})
};

Expand Down
9 changes: 8 additions & 1 deletion packages/apollo/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { generateSource as generateScalaSource } from "apollo-codegen-scala";

import { FlowCompilerOptions } from "../../apollo-codegen-flow/lib/language";
import { validateQueryDocument } from "apollo-language-server/lib/errors/validation";
import { DEFAULT_FILE_EXTENSION as TYPESCRIPT_DEFAULT_FILE_EXTENSION } from "apollo-codegen-typescript/lib/helpers";

export type TargetType =
| "json"
Expand All @@ -38,6 +39,7 @@ export type GenerationOptions = CompilerOptions &
LegacyCompilerOptions &
FlowCompilerOptions & {
globalTypesFile?: string;
fileExtension?: string;
rootPath?: string;
};

Expand Down Expand Up @@ -154,7 +156,12 @@ export default function generate(
}

const globalSourcePath =
options.globalTypesFile || path.join(outputPath, "globalTypes.ts");
options.globalTypesFile ||
path.join(
outputPath,
`globalTypes${options.fileExtension ||
TYPESCRIPT_DEFAULT_FILE_EXTENSION}`
);
outFiles[globalSourcePath] = {
output: generatedGlobalFile.fileContents
};
Expand Down