1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11 | }) : function(o, v) {
|
12 | o["default"] = v;
|
13 | });
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18 | __setModuleDefault(result, mod);
|
19 | return result;
|
20 | };
|
21 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
22 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
23 | };
|
24 | Object.defineProperty(exports, "__esModule", { value: true });
|
25 | exports.FlowAPIGenerator = exports.generateSource = void 0;
|
26 | const t = __importStar(require("@babel/types"));
|
27 | const common_tags_1 = require("common-tags");
|
28 | const graphql_1 = require("graphql");
|
29 | const typeCase_1 = require("apollo-codegen-core/lib/compiler/visitors/typeCase");
|
30 | const collectAndMergeFields_1 = require("apollo-codegen-core/lib/compiler/visitors/collectAndMergeFields");
|
31 | const language_1 = __importDefault(require("./language"));
|
32 | const printer_1 = __importDefault(require("./printer"));
|
33 | class FlowGeneratedFile {
|
34 | constructor(fileContents) {
|
35 | this.fileContents = fileContents;
|
36 | }
|
37 | get output() {
|
38 | return this.fileContents;
|
39 | }
|
40 | }
|
41 | function printEnumsAndInputObjects(generator, context) {
|
42 | generator.printer.enqueue(common_tags_1.stripIndent `
|
43 | //==============================================================
|
44 | // START Enums and Input Objects
|
45 | //==============================================================
|
46 | `);
|
47 | context.typesUsed.filter(graphql_1.isEnumType).forEach(enumType => {
|
48 | generator.typeAliasForEnumType(enumType);
|
49 | });
|
50 | context.typesUsed.filter(graphql_1.isInputObjectType).forEach(inputObjectType => {
|
51 | generator.typeAliasForInputObjectType(inputObjectType);
|
52 | });
|
53 | generator.printer.enqueue(common_tags_1.stripIndent `
|
54 | //==============================================================
|
55 | // END Enums and Input Objects
|
56 | //==============================================================
|
57 | `);
|
58 | }
|
59 | function generateSource(context) {
|
60 | const generator = new FlowAPIGenerator(context);
|
61 | const generatedFiles = [];
|
62 | Object.values(context.operations).forEach(operation => {
|
63 | generator.fileHeader();
|
64 | generator.typeAliasesForOperation(operation);
|
65 | const output = generator.printer.printAndClear();
|
66 | generatedFiles.push({
|
67 | sourcePath: operation.filePath,
|
68 | fileName: `${operation.operationName}.js`,
|
69 | content: new FlowGeneratedFile(output)
|
70 | });
|
71 | });
|
72 | Object.values(context.fragments).forEach(fragment => {
|
73 | generator.fileHeader();
|
74 | generator.typeAliasesForFragment(fragment);
|
75 | const output = generator.printer.printAndClear();
|
76 | generatedFiles.push({
|
77 | sourcePath: fragment.filePath,
|
78 | fileName: `${fragment.fragmentName}.js`,
|
79 | content: new FlowGeneratedFile(output)
|
80 | });
|
81 | });
|
82 | generator.fileHeader();
|
83 | printEnumsAndInputObjects(generator, context);
|
84 | const common = generator.printer.printAndClear();
|
85 | return {
|
86 | generatedFiles,
|
87 | common
|
88 | };
|
89 | }
|
90 | exports.generateSource = generateSource;
|
91 | class FlowAPIGenerator extends language_1.default {
|
92 | constructor(context) {
|
93 | super(context.options);
|
94 | this.context = context;
|
95 | this.printer = new printer_1.default();
|
96 | this.scopeStack = [];
|
97 | }
|
98 | fileHeader() {
|
99 | this.printer.enqueue(common_tags_1.stripIndent `
|
100 | /* @flow */
|
101 | /* eslint-disable */
|
102 | // @generated
|
103 | // This file was automatically generated and should not be edited.
|
104 | `);
|
105 | }
|
106 | typeAliasForEnumType(enumType) {
|
107 | this.printer.enqueue(this.enumerationDeclaration(enumType));
|
108 | }
|
109 | typeAliasForInputObjectType(inputObjectType) {
|
110 | const typeAlias = this.inputObjectDeclaration(inputObjectType);
|
111 | const { description } = inputObjectType;
|
112 | const exportDeclarationOptions = description
|
113 | ? { comments: ` ${description.replace("\n", " ")}` }
|
114 | : {};
|
115 | const exportedTypeAlias = this.exportDeclaration(typeAlias, exportDeclarationOptions);
|
116 | this.printer.enqueue(exportedTypeAlias);
|
117 | }
|
118 | typeAliasesForOperation(operation) {
|
119 | const { operationType, operationName, variables, selectionSet } = operation;
|
120 | this.scopeStackPush(operationName);
|
121 | this.printer.enqueue(common_tags_1.stripIndent `
|
122 | // ====================================================
|
123 | // GraphQL ${operationType} operation: ${operationName}
|
124 | // ====================================================
|
125 | `);
|
126 | const variants = this.getVariantsForSelectionSet(selectionSet);
|
127 | const variant = variants[0];
|
128 | const properties = this.getPropertiesForVariant(variant);
|
129 | const exportedTypeAlias = this.exportDeclaration(this.typeAliasObject(operationName, properties));
|
130 | this.printer.enqueue(exportedTypeAlias);
|
131 | this.scopeStackPop();
|
132 | if (variables.length > 0) {
|
133 | const interfaceName = operationName + "Variables";
|
134 | this.scopeStackPush(interfaceName);
|
135 | this.printer.enqueue(this.exportDeclaration(this.typeAliasObject(interfaceName, variables.map(variable => ({
|
136 | name: variable.name,
|
137 | annotation: this.typeAnnotationFromGraphQLType(variable.type)
|
138 | })), { keyInheritsNullability: true })));
|
139 | this.scopeStackPop();
|
140 | }
|
141 | }
|
142 | typeAliasesForFragment(fragment) {
|
143 | const { fragmentName, selectionSet } = fragment;
|
144 | this.scopeStackPush(fragmentName);
|
145 | this.printer.enqueue(common_tags_1.stripIndent `
|
146 | // ====================================================
|
147 | // GraphQL fragment: ${fragmentName}
|
148 | // ====================================================
|
149 | `);
|
150 | const variants = this.getVariantsForSelectionSet(selectionSet);
|
151 | if (variants.length === 1) {
|
152 | const properties = this.getPropertiesForVariant(variants[0]);
|
153 | const name = this.annotationFromScopeStack(this.scopeStack).id.name;
|
154 | const exportedTypeAlias = this.exportDeclaration(this.typeAliasObject(name, properties));
|
155 | this.printer.enqueue(exportedTypeAlias);
|
156 | }
|
157 | else {
|
158 | const unionMembers = [];
|
159 | variants.forEach(variant => {
|
160 | this.scopeStackPush(variant.possibleTypes[0].toString());
|
161 | const properties = this.getPropertiesForVariant(variant);
|
162 | const name = this.annotationFromScopeStack(this.scopeStack).id.name;
|
163 | const exportedTypeAlias = this.exportDeclaration(this.typeAliasObject(name, properties));
|
164 | this.printer.enqueue(exportedTypeAlias);
|
165 | unionMembers.push(this.annotationFromScopeStack(this.scopeStack));
|
166 | this.scopeStackPop();
|
167 | });
|
168 | this.printer.enqueue(this.exportDeclaration(this.typeAliasGenericUnion(this.annotationFromScopeStack(this.scopeStack).id.name, unionMembers)));
|
169 | }
|
170 | this.scopeStackPop();
|
171 | }
|
172 | getVariantsForSelectionSet(selectionSet) {
|
173 | return this.getTypeCasesForSelectionSet(selectionSet).exhaustiveVariants;
|
174 | }
|
175 | getTypeCasesForSelectionSet(selectionSet) {
|
176 | return typeCase_1.typeCaseForSelectionSet(selectionSet, this.context.options.mergeInFieldsFromFragmentSpreads);
|
177 | }
|
178 | getPropertiesForVariant(variant) {
|
179 | const fields = collectAndMergeFields_1.collectAndMergeFields(variant, this.context.options.mergeInFieldsFromFragmentSpreads);
|
180 | return fields.map(field => {
|
181 | const fieldName = field.alias !== undefined ? field.alias : field.name;
|
182 | this.scopeStackPush(fieldName);
|
183 | let res;
|
184 | if (field.selectionSet) {
|
185 | const generatedTypeName = this.annotationFromScopeStack(this.scopeStack);
|
186 | res = this.handleFieldSelectionSetValue(generatedTypeName, field);
|
187 | }
|
188 | else {
|
189 | res = this.handleFieldValue(field, variant);
|
190 | }
|
191 | this.scopeStackPop();
|
192 | return res;
|
193 | });
|
194 | }
|
195 | handleFieldSelectionSetValue(generatedTypeName, field) {
|
196 | const { selectionSet } = field;
|
197 | const annotation = this.typeAnnotationFromGraphQLType(field.type, generatedTypeName.id.name);
|
198 | const typeCase = this.getTypeCasesForSelectionSet(selectionSet);
|
199 | const variants = typeCase.exhaustiveVariants;
|
200 | let exportedTypeAlias;
|
201 | if (variants.length === 1) {
|
202 | const variant = variants[0];
|
203 | const properties = this.getPropertiesForVariant(variant);
|
204 | exportedTypeAlias = this.exportDeclaration(this.typeAliasObject(this.annotationFromScopeStack(this.scopeStack).id.name, properties));
|
205 | }
|
206 | else {
|
207 | const propertySets = variants.map(variant => {
|
208 | this.scopeStackPush(variant.possibleTypes[0].toString());
|
209 | const properties = this.getPropertiesForVariant(variant);
|
210 | this.scopeStackPop();
|
211 | return properties;
|
212 | });
|
213 | exportedTypeAlias = this.exportDeclaration(this.typeAliasObjectUnion(generatedTypeName.id.name, propertySets));
|
214 | }
|
215 | this.printer.enqueue(exportedTypeAlias);
|
216 | return {
|
217 | name: field.alias ? field.alias : field.name,
|
218 | description: field.description,
|
219 | annotation: annotation
|
220 | };
|
221 | }
|
222 | handleFieldValue(field, variant) {
|
223 | let res;
|
224 | if (field.name === "__typename") {
|
225 | const annotations = variant.possibleTypes.map(type => {
|
226 | const annotation = t.stringLiteralTypeAnnotation(type.toString());
|
227 | return annotation;
|
228 | });
|
229 | res = {
|
230 | name: field.alias ? field.alias : field.name,
|
231 | description: field.description,
|
232 | annotation: t.unionTypeAnnotation(annotations)
|
233 | };
|
234 | }
|
235 | else {
|
236 | res = {
|
237 | name: field.alias ? field.alias : field.name,
|
238 | description: field.description,
|
239 | annotation: this.typeAnnotationFromGraphQLType(field.type)
|
240 | };
|
241 | }
|
242 | return res;
|
243 | }
|
244 | get output() {
|
245 | return this.printer.print();
|
246 | }
|
247 | scopeStackPush(name) {
|
248 | this.scopeStack.push(name);
|
249 | }
|
250 | scopeStackPop() {
|
251 | const popped = this.scopeStack.pop();
|
252 | return popped;
|
253 | }
|
254 | }
|
255 | exports.FlowAPIGenerator = FlowAPIGenerator;
|
256 |
|
\ | No newline at end of file |