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