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 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | const printing_1 = require("apollo-codegen-core/lib/utilities/printing");
|
23 | const graphql_1 = require("apollo-codegen-core/lib/utilities/graphql");
|
24 | const helpers_1 = require("./helpers");
|
25 | const t = __importStar(require("@babel/types"));
|
26 | class FlowGenerator {
|
27 | constructor(compilerOptions) {
|
28 | this.options = compilerOptions;
|
29 | this.typeAnnotationFromGraphQLType = helpers_1.createTypeAnnotationFromGraphQLTypeFunction(compilerOptions);
|
30 | }
|
31 | enumerationDeclaration(type) {
|
32 | const { name, description } = type;
|
33 | const unionValues = graphql_1.sortEnumValues(type.getValues()).map(({ value }) => {
|
34 | const type = t.stringLiteralTypeAnnotation(value);
|
35 | return type;
|
36 | });
|
37 | const typeAlias = t.exportNamedDeclaration(t.typeAlias(t.identifier(name), undefined, t.unionTypeAnnotation(unionValues)), []);
|
38 | typeAlias.leadingComments = [
|
39 | {
|
40 | type: "CommentBlock",
|
41 | value: printing_1.commentBlockContent(description || "")
|
42 | }
|
43 | ];
|
44 | return typeAlias;
|
45 | }
|
46 | inputObjectDeclaration(inputObjectType) {
|
47 | const { name } = inputObjectType;
|
48 | const fieldMap = inputObjectType.getFields();
|
49 | const fields = Object.keys(inputObjectType.getFields()).map((fieldName) => {
|
50 | const field = fieldMap[fieldName];
|
51 | return {
|
52 | name: fieldName,
|
53 | annotation: this.typeAnnotationFromGraphQLType(field.type)
|
54 | };
|
55 | });
|
56 | const typeAlias = this.typeAliasObject(name, fields, {
|
57 | keyInheritsNullability: true,
|
58 | exact: true
|
59 | });
|
60 | return typeAlias;
|
61 | }
|
62 | objectTypeAnnotation(fields, { keyInheritsNullability = false } = {}) {
|
63 | const objectTypeAnnotation = t.objectTypeAnnotation(fields.map(({ name, description, annotation }) => {
|
64 | const objectTypeProperty = t.objectTypeProperty(t.identifier(name), annotation);
|
65 | objectTypeProperty.optional =
|
66 | keyInheritsNullability &&
|
67 | annotation.type === "NullableTypeAnnotation";
|
68 | if (this.options.useReadOnlyTypes) {
|
69 | objectTypeProperty.variance = { kind: "plus" };
|
70 | }
|
71 | if (description) {
|
72 | objectTypeProperty.leadingComments = [
|
73 | {
|
74 | type: "CommentBlock",
|
75 | value: printing_1.commentBlockContent(description)
|
76 | }
|
77 | ];
|
78 | }
|
79 | return objectTypeProperty;
|
80 | }));
|
81 | if (this.options.useFlowExactObjects) {
|
82 | objectTypeAnnotation.exact = true;
|
83 | }
|
84 | return objectTypeAnnotation;
|
85 | }
|
86 | typeAliasObject(name, fields, { keyInheritsNullability = false, exact = false } = {}) {
|
87 | const objectTypeAnnotation = this.objectTypeAnnotation(fields, {
|
88 | keyInheritsNullability
|
89 | });
|
90 | if (exact) {
|
91 | objectTypeAnnotation.exact = true;
|
92 | }
|
93 | return t.typeAlias(t.identifier(name), undefined, objectTypeAnnotation);
|
94 | }
|
95 | typeAliasObjectUnion(name, members) {
|
96 | return t.typeAlias(t.identifier(name), undefined, t.unionTypeAnnotation(members.map(member => {
|
97 | return this.objectTypeAnnotation(member);
|
98 | })));
|
99 | }
|
100 | typeAliasGenericUnion(name, members) {
|
101 | return t.typeAlias(t.identifier(name), undefined, t.unionTypeAnnotation(members));
|
102 | }
|
103 | exportDeclaration(declaration, options = {}) {
|
104 | const exportedDeclaration = t.exportNamedDeclaration(declaration, []);
|
105 | if (options.comments) {
|
106 | exportedDeclaration.trailingComments = [
|
107 | {
|
108 | type: "CommentBlock",
|
109 | value: printing_1.commentBlockContent(options.comments)
|
110 | }
|
111 | ];
|
112 | }
|
113 | return exportedDeclaration;
|
114 | }
|
115 | annotationFromScopeStack(scope) {
|
116 | return t.genericTypeAnnotation(t.identifier(scope.join("_")));
|
117 | }
|
118 | }
|
119 | exports.default = FlowGenerator;
|
120 |
|
\ | No newline at end of file |