@graphql-mesh/thrift 1.0.0-alpha-3fc47d119.0 → 1.0.0-alpha-20230420181317-a95037648
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.js +502 -0
- package/cjs/package.json +1 -0
- package/{index.mjs → esm/index.js} +156 -135
- package/package.json +29 -22
- package/typings/index.d.cts +12 -0
- package/typings/index.d.ts +12 -0
- package/index.d.ts +0 -15
- package/index.js +0 -480
package/cjs/index.js
ADDED
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const graphql_1 = require("graphql");
|
|
4
|
+
const graphql_scalars_1 = require("graphql-scalars");
|
|
5
|
+
const pascal_case_1 = require("pascal-case");
|
|
6
|
+
const thrift_client_1 = require("@creditkarma/thrift-client");
|
|
7
|
+
const thrift_parser_1 = require("@creditkarma/thrift-parser");
|
|
8
|
+
const thrift_server_core_1 = require("@creditkarma/thrift-server-core");
|
|
9
|
+
const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
|
|
10
|
+
const store_1 = require("@graphql-mesh/store");
|
|
11
|
+
const string_interpolation_1 = require("@graphql-mesh/string-interpolation");
|
|
12
|
+
const utils_1 = require("@graphql-mesh/utils");
|
|
13
|
+
const utils_2 = require("@graphql-tools/utils");
|
|
14
|
+
class ThriftHandler {
|
|
15
|
+
constructor({ config, baseDir, store, importFn, logger, }) {
|
|
16
|
+
this.config = config;
|
|
17
|
+
this.baseDir = baseDir;
|
|
18
|
+
this.idl = store.proxy('idl.json', store_1.PredefinedProxyOptions.JsonWithoutValidation);
|
|
19
|
+
this.importFn = importFn;
|
|
20
|
+
this.logger = logger;
|
|
21
|
+
}
|
|
22
|
+
async parseWithIncludes(idlFilePath, includesMap) {
|
|
23
|
+
const rawThrift = await (0, utils_1.readFileOrUrl)(idlFilePath, {
|
|
24
|
+
allowUnknownExtensions: true,
|
|
25
|
+
cwd: this.baseDir,
|
|
26
|
+
headers: this.config.schemaHeaders,
|
|
27
|
+
fetch: this.fetchFn,
|
|
28
|
+
logger: this.logger,
|
|
29
|
+
importFn: this.importFn,
|
|
30
|
+
});
|
|
31
|
+
const parseResult = (0, thrift_parser_1.parse)(rawThrift, { organize: false });
|
|
32
|
+
const idlNamespace = cross_helpers_1.path.basename(idlFilePath).split('.')[0];
|
|
33
|
+
if (parseResult.type === thrift_parser_1.SyntaxType.ThriftErrors) {
|
|
34
|
+
if (parseResult.errors.length === 1) {
|
|
35
|
+
throw parseResult.errors[0];
|
|
36
|
+
}
|
|
37
|
+
throw new utils_2.AggregateError(parseResult.errors);
|
|
38
|
+
}
|
|
39
|
+
includesMap[idlNamespace] = parseResult;
|
|
40
|
+
const includes = parseResult.body.filter((statement) => statement.type === thrift_parser_1.SyntaxType.IncludeDefinition);
|
|
41
|
+
await Promise.all(includes.map(async (include) => {
|
|
42
|
+
const includePath = cross_helpers_1.path.resolve(cross_helpers_1.path.dirname(idlFilePath), include.path.value);
|
|
43
|
+
await this.parseWithIncludes(includePath, includesMap);
|
|
44
|
+
}));
|
|
45
|
+
return includesMap;
|
|
46
|
+
}
|
|
47
|
+
async getMeshSource({ fetchFn }) {
|
|
48
|
+
var _a, _b;
|
|
49
|
+
this.fetchFn = fetchFn;
|
|
50
|
+
const { serviceName, operationHeaders } = this.config;
|
|
51
|
+
const namespaceASTMap = await this.idl.getWithSet(async () => {
|
|
52
|
+
const includeMap = {};
|
|
53
|
+
await this.parseWithIncludes(this.config.idl, includeMap);
|
|
54
|
+
return includeMap;
|
|
55
|
+
});
|
|
56
|
+
const enumTypeMap = new Map();
|
|
57
|
+
const outputTypeMap = new Map();
|
|
58
|
+
const inputTypeMap = new Map();
|
|
59
|
+
const rootFields = {};
|
|
60
|
+
const annotations = {};
|
|
61
|
+
const methodAnnotations = {};
|
|
62
|
+
const methodNames = [];
|
|
63
|
+
const methodParameters = {};
|
|
64
|
+
const topTypeMap = {};
|
|
65
|
+
class MeshThriftClient extends thrift_server_core_1.ThriftClient {
|
|
66
|
+
constructor() {
|
|
67
|
+
super(...arguments);
|
|
68
|
+
this._serviceName = serviceName;
|
|
69
|
+
this._annotations = annotations;
|
|
70
|
+
this._methodAnnotations = methodAnnotations;
|
|
71
|
+
this._methodNames = methodNames;
|
|
72
|
+
this._methodParameters = methodParameters;
|
|
73
|
+
}
|
|
74
|
+
writeType(typeVal, value, output) {
|
|
75
|
+
switch (typeVal.type) {
|
|
76
|
+
case thrift_server_core_1.TType.BOOL:
|
|
77
|
+
output.writeBool(value);
|
|
78
|
+
break;
|
|
79
|
+
case thrift_server_core_1.TType.BYTE:
|
|
80
|
+
output.writeByte(value);
|
|
81
|
+
break;
|
|
82
|
+
case thrift_server_core_1.TType.DOUBLE:
|
|
83
|
+
output.writeDouble(value);
|
|
84
|
+
break;
|
|
85
|
+
case thrift_server_core_1.TType.I16:
|
|
86
|
+
output.writeI16(value);
|
|
87
|
+
break;
|
|
88
|
+
case thrift_server_core_1.TType.I32:
|
|
89
|
+
output.writeI32(value);
|
|
90
|
+
break;
|
|
91
|
+
case thrift_server_core_1.TType.I64:
|
|
92
|
+
output.writeI64(value.toString());
|
|
93
|
+
break;
|
|
94
|
+
case thrift_server_core_1.TType.STRING:
|
|
95
|
+
output.writeString(value);
|
|
96
|
+
break;
|
|
97
|
+
case thrift_server_core_1.TType.STRUCT: {
|
|
98
|
+
output.writeStructBegin(typeVal.name);
|
|
99
|
+
const typeMap = typeVal.fields;
|
|
100
|
+
for (const argName in value) {
|
|
101
|
+
const argType = typeMap[argName];
|
|
102
|
+
const argVal = value[argName];
|
|
103
|
+
if (argType) {
|
|
104
|
+
output.writeFieldBegin(argName, argType.type, argType.id);
|
|
105
|
+
this.writeType(argType, argVal, output);
|
|
106
|
+
output.writeFieldEnd();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
output.writeFieldStop();
|
|
110
|
+
output.writeStructEnd();
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case thrift_server_core_1.TType.ENUM:
|
|
114
|
+
// TODO: A
|
|
115
|
+
break;
|
|
116
|
+
case thrift_server_core_1.TType.MAP: {
|
|
117
|
+
const keys = Object.keys(value);
|
|
118
|
+
output.writeMapBegin(typeVal.keyType.type, typeVal.valType.type, keys.length);
|
|
119
|
+
for (const key of keys) {
|
|
120
|
+
this.writeType(typeVal.keyType, key, output);
|
|
121
|
+
const val = value[key];
|
|
122
|
+
this.writeType(typeVal.valType, val, output);
|
|
123
|
+
}
|
|
124
|
+
output.writeMapEnd();
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
case thrift_server_core_1.TType.LIST:
|
|
128
|
+
output.writeListBegin(typeVal.elementType.type, value.length);
|
|
129
|
+
for (const element of value) {
|
|
130
|
+
this.writeType(typeVal.elementType, element, output);
|
|
131
|
+
}
|
|
132
|
+
output.writeListEnd();
|
|
133
|
+
break;
|
|
134
|
+
case thrift_server_core_1.TType.SET:
|
|
135
|
+
output.writeSetBegin(typeVal.elementType.type, value.length);
|
|
136
|
+
for (const element of value) {
|
|
137
|
+
this.writeType(typeVal.elementType, element, output);
|
|
138
|
+
}
|
|
139
|
+
output.writeSetEnd();
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
readType(type, input) {
|
|
144
|
+
switch (type) {
|
|
145
|
+
case thrift_server_core_1.TType.BOOL:
|
|
146
|
+
return input.readBool();
|
|
147
|
+
case thrift_server_core_1.TType.BYTE:
|
|
148
|
+
return input.readByte();
|
|
149
|
+
case thrift_server_core_1.TType.DOUBLE:
|
|
150
|
+
return input.readDouble();
|
|
151
|
+
case thrift_server_core_1.TType.I16:
|
|
152
|
+
return input.readI16();
|
|
153
|
+
case thrift_server_core_1.TType.I32:
|
|
154
|
+
return input.readI32();
|
|
155
|
+
case thrift_server_core_1.TType.I64:
|
|
156
|
+
return BigInt(input.readI64().toString());
|
|
157
|
+
case thrift_server_core_1.TType.STRING:
|
|
158
|
+
return input.readString();
|
|
159
|
+
case thrift_server_core_1.TType.STRUCT: {
|
|
160
|
+
const result = {};
|
|
161
|
+
input.readStructBegin();
|
|
162
|
+
while (true) {
|
|
163
|
+
const field = input.readFieldBegin();
|
|
164
|
+
const fieldType = field.fieldType;
|
|
165
|
+
const fieldName = field.fieldName || 'success';
|
|
166
|
+
if (fieldType === thrift_server_core_1.TType.STOP) {
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
result[fieldName] = this.readType(fieldType, input);
|
|
170
|
+
input.readFieldEnd();
|
|
171
|
+
}
|
|
172
|
+
input.readStructEnd();
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
case thrift_server_core_1.TType.ENUM:
|
|
176
|
+
// TODO: A
|
|
177
|
+
break;
|
|
178
|
+
case thrift_server_core_1.TType.MAP: {
|
|
179
|
+
const result = {};
|
|
180
|
+
const map = input.readMapBegin();
|
|
181
|
+
for (let i = 0; i < map.size; i++) {
|
|
182
|
+
const key = this.readType(map.keyType, input);
|
|
183
|
+
const value = this.readType(map.valueType, input);
|
|
184
|
+
result[key] = value;
|
|
185
|
+
}
|
|
186
|
+
input.readMapEnd();
|
|
187
|
+
return result;
|
|
188
|
+
}
|
|
189
|
+
case thrift_server_core_1.TType.LIST: {
|
|
190
|
+
const result = [];
|
|
191
|
+
const list = input.readListBegin();
|
|
192
|
+
for (let i = 0; i < list.size; i++) {
|
|
193
|
+
const element = this.readType(list.elementType, input);
|
|
194
|
+
result.push(element);
|
|
195
|
+
}
|
|
196
|
+
input.readListEnd();
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
case thrift_server_core_1.TType.SET: {
|
|
200
|
+
const result = [];
|
|
201
|
+
const list = input.readSetBegin();
|
|
202
|
+
for (let i = 0; i < list.size; i++) {
|
|
203
|
+
const element = this.readType(list.elementType, input);
|
|
204
|
+
result.push(element);
|
|
205
|
+
}
|
|
206
|
+
input.readSetEnd();
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
async doRequest(methodName, args, fields, context) {
|
|
212
|
+
const Transport = this.transport;
|
|
213
|
+
const Protocol = this.protocol;
|
|
214
|
+
const writer = new Transport();
|
|
215
|
+
const output = new Protocol(writer);
|
|
216
|
+
const id = this.incrementRequestId();
|
|
217
|
+
output.writeMessageBegin(methodName, thrift_server_core_1.MessageType.CALL, id);
|
|
218
|
+
this.writeType({
|
|
219
|
+
name: (0, pascal_case_1.pascalCase)(methodName) + '__Args',
|
|
220
|
+
type: thrift_server_core_1.TType.STRUCT,
|
|
221
|
+
fields,
|
|
222
|
+
id,
|
|
223
|
+
}, args, output);
|
|
224
|
+
output.writeMessageEnd();
|
|
225
|
+
const data = await this.connection.send(writer.flush(), context);
|
|
226
|
+
const reader = this.transport.receiver(data);
|
|
227
|
+
const input = new Protocol(reader);
|
|
228
|
+
const { fieldName, messageType } = input.readMessageBegin();
|
|
229
|
+
if (fieldName === methodName) {
|
|
230
|
+
if (messageType === thrift_server_core_1.MessageType.EXCEPTION) {
|
|
231
|
+
const err = thrift_server_core_1.TApplicationExceptionCodec.decode(input);
|
|
232
|
+
input.readMessageEnd();
|
|
233
|
+
return Promise.reject(err);
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
const result = this.readType(thrift_server_core_1.TType.STRUCT, input);
|
|
237
|
+
input.readMessageEnd();
|
|
238
|
+
if (result.success != null) {
|
|
239
|
+
return result.success;
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
throw new thrift_server_core_1.TApplicationException(thrift_server_core_1.TApplicationExceptionType.UNKNOWN, methodName + ' failed: unknown result');
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
throw new thrift_server_core_1.TApplicationException(thrift_server_core_1.TApplicationExceptionType.WRONG_METHOD_NAME, 'Received a response to an unknown RPC function: ' + fieldName);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
MeshThriftClient.serviceName = serviceName;
|
|
252
|
+
MeshThriftClient.annotations = annotations;
|
|
253
|
+
MeshThriftClient.methodAnnotations = methodAnnotations;
|
|
254
|
+
MeshThriftClient.methodNames = methodNames;
|
|
255
|
+
const thriftHttpClient = (0, thrift_client_1.createHttpClient)(MeshThriftClient, {
|
|
256
|
+
...this.config,
|
|
257
|
+
requestOptions: {
|
|
258
|
+
headers: operationHeaders,
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
function processComments(comments) {
|
|
262
|
+
return comments.map(comment => comment.value).join('\n');
|
|
263
|
+
}
|
|
264
|
+
function getGraphQLFunctionType(functionType, id = Math.random()) {
|
|
265
|
+
let inputType;
|
|
266
|
+
let outputType;
|
|
267
|
+
let typeVal;
|
|
268
|
+
switch (functionType.type) {
|
|
269
|
+
case thrift_parser_1.SyntaxType.BinaryKeyword:
|
|
270
|
+
case thrift_parser_1.SyntaxType.StringKeyword:
|
|
271
|
+
inputType = graphql_1.GraphQLString;
|
|
272
|
+
outputType = graphql_1.GraphQLString;
|
|
273
|
+
break;
|
|
274
|
+
case thrift_parser_1.SyntaxType.DoubleKeyword:
|
|
275
|
+
inputType = graphql_1.GraphQLFloat;
|
|
276
|
+
outputType = graphql_1.GraphQLFloat;
|
|
277
|
+
typeVal = typeVal || { type: thrift_server_core_1.TType.DOUBLE };
|
|
278
|
+
break;
|
|
279
|
+
case thrift_parser_1.SyntaxType.VoidKeyword:
|
|
280
|
+
typeVal = typeVal || { type: thrift_server_core_1.TType.VOID };
|
|
281
|
+
inputType = graphql_scalars_1.GraphQLVoid;
|
|
282
|
+
outputType = graphql_scalars_1.GraphQLVoid;
|
|
283
|
+
break;
|
|
284
|
+
case thrift_parser_1.SyntaxType.BoolKeyword:
|
|
285
|
+
typeVal = typeVal || { type: thrift_server_core_1.TType.BOOL };
|
|
286
|
+
inputType = graphql_1.GraphQLBoolean;
|
|
287
|
+
outputType = graphql_1.GraphQLBoolean;
|
|
288
|
+
break;
|
|
289
|
+
case thrift_parser_1.SyntaxType.I8Keyword:
|
|
290
|
+
inputType = graphql_1.GraphQLInt;
|
|
291
|
+
outputType = graphql_1.GraphQLInt;
|
|
292
|
+
typeVal = typeVal || { type: thrift_server_core_1.TType.I08 };
|
|
293
|
+
break;
|
|
294
|
+
case thrift_parser_1.SyntaxType.I16Keyword:
|
|
295
|
+
inputType = graphql_1.GraphQLInt;
|
|
296
|
+
outputType = graphql_1.GraphQLInt;
|
|
297
|
+
typeVal = typeVal || { type: thrift_server_core_1.TType.I16 };
|
|
298
|
+
break;
|
|
299
|
+
case thrift_parser_1.SyntaxType.I32Keyword:
|
|
300
|
+
inputType = graphql_1.GraphQLInt;
|
|
301
|
+
outputType = graphql_1.GraphQLInt;
|
|
302
|
+
typeVal = typeVal || { type: thrift_server_core_1.TType.I32 };
|
|
303
|
+
break;
|
|
304
|
+
case thrift_parser_1.SyntaxType.ByteKeyword:
|
|
305
|
+
inputType = graphql_scalars_1.GraphQLByte;
|
|
306
|
+
outputType = graphql_scalars_1.GraphQLByte;
|
|
307
|
+
typeVal = typeVal || { type: thrift_server_core_1.TType.BYTE };
|
|
308
|
+
break;
|
|
309
|
+
case thrift_parser_1.SyntaxType.I64Keyword:
|
|
310
|
+
inputType = graphql_scalars_1.GraphQLBigInt;
|
|
311
|
+
outputType = graphql_scalars_1.GraphQLBigInt;
|
|
312
|
+
typeVal = typeVal || { type: thrift_server_core_1.TType.I64 };
|
|
313
|
+
break;
|
|
314
|
+
case thrift_parser_1.SyntaxType.ListType: {
|
|
315
|
+
const ofTypeList = getGraphQLFunctionType(functionType.valueType, id);
|
|
316
|
+
inputType = new graphql_1.GraphQLList(ofTypeList.inputType);
|
|
317
|
+
outputType = new graphql_1.GraphQLList(ofTypeList.outputType);
|
|
318
|
+
typeVal = typeVal || { type: thrift_server_core_1.TType.LIST, elementType: ofTypeList.typeVal };
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
case thrift_parser_1.SyntaxType.SetType: {
|
|
322
|
+
const ofSetType = getGraphQLFunctionType(functionType.valueType, id);
|
|
323
|
+
inputType = new graphql_1.GraphQLList(ofSetType.inputType);
|
|
324
|
+
outputType = new graphql_1.GraphQLList(ofSetType.outputType);
|
|
325
|
+
typeVal = typeVal || { type: thrift_server_core_1.TType.SET, elementType: ofSetType.typeVal };
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
case thrift_parser_1.SyntaxType.MapType: {
|
|
329
|
+
inputType = graphql_scalars_1.GraphQLJSON;
|
|
330
|
+
outputType = graphql_scalars_1.GraphQLJSON;
|
|
331
|
+
const ofTypeKey = getGraphQLFunctionType(functionType.keyType, id);
|
|
332
|
+
const ofTypeValue = getGraphQLFunctionType(functionType.valueType, id);
|
|
333
|
+
typeVal = typeVal || {
|
|
334
|
+
type: thrift_server_core_1.TType.MAP,
|
|
335
|
+
keyType: ofTypeKey.typeVal,
|
|
336
|
+
valType: ofTypeValue.typeVal,
|
|
337
|
+
};
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
case thrift_parser_1.SyntaxType.Identifier: {
|
|
341
|
+
const typeName = functionType.value.replace('.', '_');
|
|
342
|
+
if (enumTypeMap.has(typeName)) {
|
|
343
|
+
const enumType = enumTypeMap.get(typeName);
|
|
344
|
+
inputType = enumType;
|
|
345
|
+
outputType = enumType;
|
|
346
|
+
}
|
|
347
|
+
if (inputTypeMap.has(typeName)) {
|
|
348
|
+
inputType = inputTypeMap.get(typeName);
|
|
349
|
+
}
|
|
350
|
+
if (outputTypeMap.has(typeName)) {
|
|
351
|
+
outputType = outputTypeMap.get(typeName);
|
|
352
|
+
}
|
|
353
|
+
typeVal = topTypeMap[typeName];
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
default:
|
|
357
|
+
throw new Error(`Unknown function type: ${cross_helpers_1.util.inspect(functionType)}!`);
|
|
358
|
+
}
|
|
359
|
+
return {
|
|
360
|
+
inputType: inputType,
|
|
361
|
+
outputType: outputType,
|
|
362
|
+
typeVal: {
|
|
363
|
+
...typeVal,
|
|
364
|
+
id,
|
|
365
|
+
},
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
const { args: commonArgs, contextVariables } = (0, string_interpolation_1.parseInterpolationStrings)(Object.values(operationHeaders || {}));
|
|
369
|
+
const headersFactory = (0, string_interpolation_1.getInterpolatedHeadersFactory)(operationHeaders);
|
|
370
|
+
const baseNamespace = cross_helpers_1.path.basename(this.config.idl, '.thrift');
|
|
371
|
+
for (const namespace of Object.keys(namespaceASTMap).reverse()) {
|
|
372
|
+
const thriftAST = namespaceASTMap[namespace];
|
|
373
|
+
for (const statement of thriftAST.body) {
|
|
374
|
+
let typeName = 'name' in statement ? statement.name.value : undefined;
|
|
375
|
+
if (namespace !== baseNamespace) {
|
|
376
|
+
typeName = `${namespace}_${typeName}`;
|
|
377
|
+
}
|
|
378
|
+
switch (statement.type) {
|
|
379
|
+
case thrift_parser_1.SyntaxType.EnumDefinition:
|
|
380
|
+
enumTypeMap.set(typeName, new graphql_1.GraphQLEnumType({
|
|
381
|
+
name: typeName,
|
|
382
|
+
description: processComments(statement.comments),
|
|
383
|
+
values: statement.members.reduce((prev, curr) => ({
|
|
384
|
+
...prev,
|
|
385
|
+
[curr.name.value]: {
|
|
386
|
+
description: processComments(curr.comments),
|
|
387
|
+
value: curr.name.value,
|
|
388
|
+
},
|
|
389
|
+
}), {}),
|
|
390
|
+
}));
|
|
391
|
+
break;
|
|
392
|
+
case thrift_parser_1.SyntaxType.StructDefinition: {
|
|
393
|
+
const description = processComments(statement.comments);
|
|
394
|
+
const objectFields = {};
|
|
395
|
+
const inputObjectFields = {};
|
|
396
|
+
const structTypeVal = {
|
|
397
|
+
id: Math.random(),
|
|
398
|
+
name: typeName,
|
|
399
|
+
type: thrift_server_core_1.TType.STRUCT,
|
|
400
|
+
fields: {},
|
|
401
|
+
};
|
|
402
|
+
topTypeMap[typeName] = structTypeVal;
|
|
403
|
+
const structFieldTypeMap = structTypeVal.fields;
|
|
404
|
+
for (const field of statement.fields) {
|
|
405
|
+
const fieldName = field.name.value;
|
|
406
|
+
let fieldOutputType;
|
|
407
|
+
let fieldInputType;
|
|
408
|
+
const description = processComments(field.comments);
|
|
409
|
+
const processedFieldTypes = getGraphQLFunctionType(field.fieldType, (_a = field.fieldID) === null || _a === void 0 ? void 0 : _a.value);
|
|
410
|
+
fieldOutputType = processedFieldTypes.outputType;
|
|
411
|
+
fieldInputType = processedFieldTypes.inputType;
|
|
412
|
+
if (field.requiredness === 'required') {
|
|
413
|
+
fieldOutputType = new graphql_1.GraphQLNonNull(fieldOutputType);
|
|
414
|
+
fieldInputType = new graphql_1.GraphQLNonNull(fieldInputType);
|
|
415
|
+
}
|
|
416
|
+
objectFields[fieldName] = {
|
|
417
|
+
type: fieldOutputType,
|
|
418
|
+
description,
|
|
419
|
+
};
|
|
420
|
+
inputObjectFields[fieldName] = {
|
|
421
|
+
type: fieldInputType,
|
|
422
|
+
description,
|
|
423
|
+
};
|
|
424
|
+
structFieldTypeMap[fieldName] = processedFieldTypes.typeVal;
|
|
425
|
+
}
|
|
426
|
+
outputTypeMap.set(typeName, new graphql_1.GraphQLObjectType({
|
|
427
|
+
name: typeName,
|
|
428
|
+
description,
|
|
429
|
+
fields: objectFields,
|
|
430
|
+
}));
|
|
431
|
+
inputTypeMap.set(typeName, new graphql_1.GraphQLInputObjectType({
|
|
432
|
+
name: typeName + 'Input',
|
|
433
|
+
description,
|
|
434
|
+
fields: inputObjectFields,
|
|
435
|
+
}));
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
case thrift_parser_1.SyntaxType.ServiceDefinition:
|
|
439
|
+
for (const fnIndex in statement.functions) {
|
|
440
|
+
const fn = statement.functions[fnIndex];
|
|
441
|
+
const fnName = fn.name.value;
|
|
442
|
+
const description = processComments(fn.comments);
|
|
443
|
+
const { outputType: returnType } = getGraphQLFunctionType(fn.returnType, Number(fnIndex) + 1);
|
|
444
|
+
const args = {};
|
|
445
|
+
for (const argName in commonArgs) {
|
|
446
|
+
const typeNameOrType = commonArgs[argName].type;
|
|
447
|
+
args[argName] = {
|
|
448
|
+
type: typeof typeNameOrType === 'string'
|
|
449
|
+
? inputTypeMap.get(typeNameOrType)
|
|
450
|
+
: typeNameOrType || graphql_1.GraphQLID,
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
const fieldTypeMap = {};
|
|
454
|
+
for (const field of fn.fields) {
|
|
455
|
+
const fieldName = field.name.value;
|
|
456
|
+
const fieldDescription = processComments(field.comments);
|
|
457
|
+
let { inputType: fieldType, typeVal } = getGraphQLFunctionType(field.fieldType, (_b = field.fieldID) === null || _b === void 0 ? void 0 : _b.value);
|
|
458
|
+
if (field.requiredness === 'required') {
|
|
459
|
+
fieldType = new graphql_1.GraphQLNonNull(fieldType);
|
|
460
|
+
}
|
|
461
|
+
args[fieldName] = {
|
|
462
|
+
type: fieldType,
|
|
463
|
+
description: fieldDescription,
|
|
464
|
+
};
|
|
465
|
+
fieldTypeMap[fieldName] = typeVal;
|
|
466
|
+
}
|
|
467
|
+
rootFields[fnName] = {
|
|
468
|
+
type: returnType,
|
|
469
|
+
description,
|
|
470
|
+
args,
|
|
471
|
+
resolve: async (root, args, context, info) => thriftHttpClient.doRequest(fnName, args, fieldTypeMap, {
|
|
472
|
+
headers: headersFactory({ root, args, context, info, env: cross_helpers_1.process.env }),
|
|
473
|
+
}),
|
|
474
|
+
};
|
|
475
|
+
methodNames.push(fnName);
|
|
476
|
+
methodAnnotations[fnName] = { annotations: {}, fieldAnnotations: {} };
|
|
477
|
+
methodParameters[fnName] = fn.fields.length + 1;
|
|
478
|
+
}
|
|
479
|
+
break;
|
|
480
|
+
case thrift_parser_1.SyntaxType.TypedefDefinition: {
|
|
481
|
+
const { inputType, outputType } = getGraphQLFunctionType(statement.definitionType, Math.random());
|
|
482
|
+
inputTypeMap.set(typeName, inputType);
|
|
483
|
+
outputTypeMap.set(typeName, outputType);
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
const queryObjectType = new graphql_1.GraphQLObjectType({
|
|
490
|
+
name: 'Query',
|
|
491
|
+
fields: rootFields,
|
|
492
|
+
});
|
|
493
|
+
const schema = new graphql_1.GraphQLSchema({
|
|
494
|
+
query: queryObjectType,
|
|
495
|
+
});
|
|
496
|
+
return {
|
|
497
|
+
schema,
|
|
498
|
+
contextVariables,
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
exports.default = ThriftHandler;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|