1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const printer_1 = require("./printer");
|
4 | const types_1 = require("./types");
|
5 | const constructors_1 = require("./constructors");
|
6 | const graphql_1 = require("graphql");
|
7 | const operationFile_1 = require("./operationFile");
|
8 | const fragmentFile_1 = require("./fragmentFile");
|
9 | const normalizedDataDeclaration_1 = require("./normalizedDataDeclaration");
|
10 | class TypescriptGeneratedFile {
|
11 | constructor(fileContents) {
|
12 | this.fileContents = fileContents;
|
13 | }
|
14 | get output() {
|
15 | return this.fileContents;
|
16 | }
|
17 | }
|
18 | function 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 | }
|
45 | exports.generateLocalSource = generateLocalSource;
|
46 | const globalTypes = `export type Maybe<T> = T | null;
|
47 | export type Optional<T> = Maybe<T> | undefined;
|
48 | export type If<T, V> = { __typename: T } & V;
|
49 | export type Operation<Data> = { query: string; variables?: any };
|
50 | export type ById<T> = { [id: string]: T | undefined }`;
|
51 | function 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 | }
|
69 | exports.generateGlobalSource = generateGlobalSource;
|
70 |
|
\ | No newline at end of file |