UNPKG

3.48 kBPlain TextView Raw
1import { BasicGeneratedFile } from "apollo-codegen-core/lib/utilities/CodeGenerator";
2
3import { CompilerContext } from "apollo-codegen-core/lib/compiler";
4
5import Printer from "./printer";
6
7import {
8 typeAliasDeclarationForGraphQLInputObjectType,
9 enumDeclarationForGraphQLEnumType,
10 exportDeclaration
11} from "./types";
12import { constructorDeclarationForGraphQLInputObjectType } from "./constructors";
13import { isEnumType, isInputObjectType } from "graphql";
14import { operationFile } from "./operationFile";
15import { fragmentFile } from "./fragmentFile";
16import { normalizedDataDeclarations } from "./normalizedDataDeclaration";
17
18class TypescriptGeneratedFile implements BasicGeneratedFile {
19 fileContents: string;
20
21 constructor(fileContents: string) {
22 this.fileContents = fileContents;
23 }
24
25 get output() {
26 return this.fileContents;
27 }
28}
29
30interface IGeneratedFileOptions {
31 outputPath?: string;
32 globalSourcePath?: string;
33}
34
35interface IGeneratedFile {
36 sourcePath: string;
37 fileName: string;
38 content: (options?: IGeneratedFileOptions) => TypescriptGeneratedFile;
39}
40
41export function generateLocalSource(
42 context: CompilerContext
43): IGeneratedFile[] {
44 return Object.values(context.fragments)
45 .map(fragment => ({
46 sourcePath: fragment.filePath,
47 fileName: `${fragment.fragmentName}.ts`,
48 content: (options?: IGeneratedFileOptions) => {
49 const printer = new Printer();
50 if (options && options.outputPath && options.globalSourcePath) {
51 fragmentFile(
52 fragment,
53 options.outputPath,
54 options.globalSourcePath,
55 context
56 ).forEach(printable => printer.enqueue(printable));
57 }
58 return new TypescriptGeneratedFile(printer.print());
59 }
60 }))
61 .concat(
62 Object.values(context.operations).map(operation => ({
63 sourcePath: operation.filePath,
64 fileName: `${operation.operationName}.ts`,
65 content: (options?: IGeneratedFileOptions) => {
66 const printer = new Printer();
67 if (options && options.outputPath && options.globalSourcePath) {
68 operationFile(
69 operation,
70 options.outputPath,
71 options.globalSourcePath,
72 context
73 ).forEach(printable => printer.enqueue(printable));
74 }
75 return new TypescriptGeneratedFile(printer.print());
76 }
77 }))
78 );
79}
80
81const globalTypes = `export type Maybe<T> = T | null;
82export type Optional<T> = Maybe<T> | undefined;
83export type If<T, V> = { __typename: T } & V;
84export type Operation<Data> = { query: string; variables?: any };
85export type ById<T> = { [id: string]: T | undefined }`;
86
87export function generateGlobalSource(
88 context: CompilerContext
89): TypescriptGeneratedFile {
90 const printer = new Printer();
91 printer.enqueue(globalTypes);
92 context.typesUsed.forEach(type => {
93 if (isEnumType(type)) {
94 printer.enqueue(
95 exportDeclaration(enumDeclarationForGraphQLEnumType(type))
96 );
97 } else if (isInputObjectType(type)) {
98 printer.enqueue(
99 exportDeclaration(typeAliasDeclarationForGraphQLInputObjectType(type))
100 );
101 printer.enqueue(
102 exportDeclaration(constructorDeclarationForGraphQLInputObjectType(type))
103 );
104 }
105 });
106 normalizedDataDeclarations(context).forEach(declaration => {
107 printer.enqueue(exportDeclaration(declaration));
108 });
109 return new TypescriptGeneratedFile(printer.print());
110}