UNPKG

8.31 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const util_1 = require("util");
4/**
5 * Builder to construct Ingester instances fluently
6 * @Deprecated use the SDL (GraphQL Schema definition language) ingester definition.
7 */
8class IngesterBuilder {
9 constructor(rootType) {
10 this.rootType = rootType;
11 /**
12 * @Deprecated do not use
13 * Public to prevent type errors. This whole class will be removed in the future
14 * @type {any[]}
15 */
16 this.types = [];
17 /**
18 * @Deprecated do not use
19 * Public to prevent type errors. This whole class will be removed in the future
20 * @type {any[]}
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}
46exports.IngesterBuilder = IngesterBuilder;
47/**
48 * Builder to construct TypeBuilder instances fluently
49 * @Deprecated use the SDL (GraphQL Schema definition language) ingester definition.
50 */
51class TypeBuilder {
52 constructor(name, description) {
53 this.name = name;
54 this.description = description;
55 /**
56 * do not use! This is public only to prevent compiler errors when the location of automation-client is not
57 * identical for dependencies within an SDM.
58 *
59 * @Deprecated
60 * @type {any[]}
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 // TODO what are those default values
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}
204exports.TypeBuilder = TypeBuilder;
205/**
206 * Builder to construct EnumType instances fluently
207 */
208class 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}
226exports.EnumBuilder = EnumBuilder;
227/**
228 * Create an IngesterBuilder for the provided rootType
229 *
230 * If rootType is TypeBuilder instance, it is added to the types collection.
231 * Therefore there is no need to call withType on the rootType.
232 * @param {string | TypeBuilder} rootType
233 * @returns {IngesterBuilder}
234 */
235function buildIngester(rootType) {
236 return new IngesterBuilder(rootType);
237}
238exports.buildIngester = buildIngester;
239/**
240 * Create a TypeBuilder for the provided name
241 * @param {string} name
242 * @returns {TypeBuilder}
243 */
244function buildType(name) {
245 return new TypeBuilder(name);
246}
247exports.buildType = buildType;
248/**
249 * Create a EnumBuilder for the provided name, description and values
250 * @param {string} name
251 * @param {string[]} values
252 * @param {string} description
253 * @returns {EnumBuilder}
254 */
255function buildEnum(name, values, description) {
256 return new EnumBuilder(name, values, description);
257}
258exports.buildEnum = buildEnum;
259//# sourceMappingURL=ingesters.js.map
\No newline at end of file