UNPKG

3.17 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const printer_1 = require("./printer");
4const types_1 = require("./types");
5const constructors_1 = require("./constructors");
6const graphql_1 = require("graphql");
7const operationFile_1 = require("./operationFile");
8const fragmentFile_1 = require("./fragmentFile");
9const normalizedDataDeclaration_1 = require("./normalizedDataDeclaration");
10class TypescriptGeneratedFile {
11 constructor(fileContents) {
12 this.fileContents = fileContents;
13 }
14 get output() {
15 return this.fileContents;
16 }
17}
18function generateLocalSource(context) {
19 return Object.values(context.fragments)
20 .map(fragment => ({
21 sourcePath: fragment.filePath,
22 fileName: `${fragment.fragmentName}.ts`,
23 content: (options) => {
24 const printer = new printer_1.default();
25 if (options && options.outputPath && options.globalSourcePath) {
26 fragmentFile_1.fragmentFile(fragment, options.outputPath, options.globalSourcePath, context).forEach(printable => printer.enqueue(printable));
27 }
28 const result = printer.print();
29 return new TypescriptGeneratedFile(result);
30 }
31 }))
32 .concat(Object.values(context.operations).map(operation => ({
33 sourcePath: operation.filePath,
34 fileName: `${operation.operationName}.ts`,
35 content: (options) => {
36 const printer = new printer_1.default();
37 if (options && options.outputPath && options.globalSourcePath) {
38 operationFile_1.operationFile(operation, options.outputPath, options.globalSourcePath, context).forEach(printable => printer.enqueue(printable));
39 }
40 const result = printer.print();
41 return new TypescriptGeneratedFile(result);
42 }
43 })));
44}
45exports.generateLocalSource = generateLocalSource;
46const globalTypes = `export type Maybe<T> = T | null;
47export type Optional<T> = Maybe<T> | undefined;
48export type If<T, V> = { __typename: T } & V;
49export type Operation<Data> = { query: string; variables?: any };
50export type ById<T> = { [id: string]: T | undefined }`;
51function generateGlobalSource(context) {
52 const printer = new printer_1.default();
53 printer.enqueue(globalTypes);
54 context.typesUsed.forEach(type => {
55 if (graphql_1.isEnumType(type)) {
56 printer.enqueue(types_1.exportDeclaration(types_1.enumDeclarationForGraphQLEnumType(type)));
57 }
58 else if (graphql_1.isInputObjectType(type)) {
59 printer.enqueue(types_1.exportDeclaration(types_1.typeAliasDeclarationForGraphQLInputObjectType(type)));
60 printer.enqueue(types_1.exportDeclaration(constructors_1.constructorDeclarationForGraphQLInputObjectType(type)));
61 }
62 });
63 normalizedDataDeclaration_1.normalizedDataDeclarations(context).forEach(declaration => {
64 printer.enqueue(types_1.exportDeclaration(declaration));
65 });
66 const result = printer.print();
67 return new TypescriptGeneratedFile(result);
68}
69exports.generateGlobalSource = generateGlobalSource;
70//# sourceMappingURL=newCodeGeneration.js.map
\No newline at end of file