1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const util_1 = require("util");
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | class IngesterBuilder {
|
9 | constructor(rootType) {
|
10 | this.rootType = rootType;
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 | this.types = [];
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 | this.enums = [];
|
23 | if (util_1.isString(rootType)) {
|
24 | this.name = rootType;
|
25 | }
|
26 | else {
|
27 | this.name = rootType.name;
|
28 | this.types.push(rootType.build(this.types));
|
29 | }
|
30 | }
|
31 | withType(builder) {
|
32 | this.types.push(builder.build(this.types));
|
33 | return this;
|
34 | }
|
35 | withEnum(builder) {
|
36 | this.enums.push(builder.build());
|
37 | return this;
|
38 | }
|
39 | build() {
|
40 | return {
|
41 | root_type: this.name,
|
42 | types: [...this.enums, ...this.types],
|
43 | };
|
44 | }
|
45 | }
|
46 | exports.IngesterBuilder = IngesterBuilder;
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | class TypeBuilder {
|
52 | constructor(name, description) {
|
53 | this.name = name;
|
54 | this.description = description;
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | this.fields = [];
|
63 | }
|
64 | withScalarField(name, kind, description, directives = []) {
|
65 | const field = {
|
66 | name,
|
67 | type: {
|
68 | kind: "SCALAR",
|
69 | name: kind,
|
70 | },
|
71 | };
|
72 | if (description) {
|
73 | field.description = description;
|
74 | }
|
75 | if (directives.length > 0) {
|
76 | field.directives = directives.map(d => ({ name: d }));
|
77 | }
|
78 | this.fields.push(field);
|
79 | return this;
|
80 | }
|
81 | withObjectField(name, object, description, args = [], directives = []) {
|
82 | const field = {
|
83 | name,
|
84 | args: args,
|
85 | type: {
|
86 | kind: "OBJECT",
|
87 | name: util_1.isString(object) ? object : object.name,
|
88 | },
|
89 | };
|
90 | if (description) {
|
91 | field.description = description;
|
92 | }
|
93 | if (directives.length > 0) {
|
94 | field.directives = directives.map(d => ({ name: d }));
|
95 | }
|
96 | this.fields.push(field);
|
97 | return this;
|
98 | }
|
99 | withEnumField(name, object, description, directives = []) {
|
100 | const field = {
|
101 | name,
|
102 | type: {
|
103 | kind: "ENUM",
|
104 | name: util_1.isString(object) ? object : object.name,
|
105 | },
|
106 | };
|
107 | if (description) {
|
108 | field.description = description;
|
109 | }
|
110 | if (directives.length > 0) {
|
111 | field.directives = directives.map(d => ({ name: d }));
|
112 | }
|
113 | this.fields.push(field);
|
114 | return this;
|
115 | }
|
116 | withStringField(name, description, directives = []) {
|
117 | return this.withScalarField(name, "String", description, directives);
|
118 | }
|
119 | withBooleanField(name, description, directives = []) {
|
120 | return this.withScalarField(name, "Boolean", description, directives);
|
121 | }
|
122 | withFloatField(name, description, directives = []) {
|
123 | return this.withScalarField(name, "Float", description, directives);
|
124 | }
|
125 | withIntField(name, description, directives = []) {
|
126 | return this.withScalarField(name, "Int", description, directives);
|
127 | }
|
128 | withListScalarField(name, kind, description) {
|
129 | const field = {
|
130 | name,
|
131 | type: {
|
132 | kind: "LIST",
|
133 | ofType: {
|
134 | kind: "SCALAR",
|
135 | name: kind,
|
136 | },
|
137 | },
|
138 | };
|
139 | if (description) {
|
140 | field.description = description;
|
141 | }
|
142 | this.fields.push(field);
|
143 | return this;
|
144 | }
|
145 | withListObjectField(name, object, description = null, args = []) {
|
146 | const field = {
|
147 | name,
|
148 | args: args,
|
149 | type: {
|
150 | kind: "LIST",
|
151 | ofType: {
|
152 | kind: "OBJECT",
|
153 | name: util_1.isString(object) ? object : object.name,
|
154 | },
|
155 | },
|
156 | };
|
157 | if (description) {
|
158 | field.description = description;
|
159 | }
|
160 | this.fields.push(field);
|
161 | return this;
|
162 | }
|
163 | build(types) {
|
164 | this.fields.filter(f => f.type.kind === "OBJECT" || (f.type.kind === "LIST" && f.type.ofType.kind === "OBJECT")).forEach(f => {
|
165 | f.args = (f.args || []).map(a => {
|
166 | const refType = types.find(t => t.name === (f.type.name || (f.type.ofType && f.type.ofType.name)));
|
167 | if (refType) {
|
168 | const refFieldType = refType.fields.find(fi => fi.name === a);
|
169 | if (refFieldType) {
|
170 | if (refFieldType.type.kind === "OBJECT") {
|
171 | throw new Error(`Referenced type '${f.type.name}' in arg '${a}' is of type OBJECT. Only SCALAR is supports as args`);
|
172 | }
|
173 | const argsType = Object.assign({}, refFieldType, {
|
174 |
|
175 | defaultValue: null, type: {
|
176 | kind: "LIST",
|
177 | ofType: {
|
178 | kind: "SCALAR",
|
179 | name: refFieldType.type.name,
|
180 | },
|
181 | } });
|
182 | delete argsType.args;
|
183 | delete argsType.directives;
|
184 | return argsType;
|
185 | }
|
186 | }
|
187 | throw new Error(`Referenced type '${f.type.name}' in arg '${a}' of field '${f.name}' in type '${this.name}' could not be found`);
|
188 | });
|
189 | if (!f.args || f.args.length === 0) {
|
190 | delete f.args;
|
191 | }
|
192 | });
|
193 | const object = {
|
194 | kind: "OBJECT",
|
195 | name: this.name,
|
196 | fields: this.fields,
|
197 | };
|
198 | if (this.description) {
|
199 | object.description = this.description;
|
200 | }
|
201 | return object;
|
202 | }
|
203 | }
|
204 | exports.TypeBuilder = TypeBuilder;
|
205 |
|
206 |
|
207 |
|
208 | class EnumBuilder {
|
209 | constructor(name, values, description) {
|
210 | this.name = name;
|
211 | this.values = values;
|
212 | this.description = description;
|
213 | }
|
214 | build() {
|
215 | const enu = {
|
216 | kind: "ENUM",
|
217 | name: this.name,
|
218 | enumValues: this.values.map(v => ({ name: v })),
|
219 | };
|
220 | if (this.description) {
|
221 | enu.description = this.description;
|
222 | }
|
223 | return enu;
|
224 | }
|
225 | }
|
226 | exports.EnumBuilder = EnumBuilder;
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | function buildIngester(rootType) {
|
236 | return new IngesterBuilder(rootType);
|
237 | }
|
238 | exports.buildIngester = buildIngester;
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 | function buildType(name) {
|
245 | return new TypeBuilder(name);
|
246 | }
|
247 | exports.buildType = buildType;
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | function buildEnum(name, values, description) {
|
256 | return new EnumBuilder(name, values, description);
|
257 | }
|
258 | exports.buildEnum = buildEnum;
|
259 |
|
\ | No newline at end of file |