@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.
@@ -1,44 +1,55 @@
1
- import { parse, SyntaxType } from '@creditkarma/thrift-parser';
2
- import { readFileOrUrl } from '@graphql-mesh/utils';
3
- import { GraphQLID, GraphQLNonNull, GraphQLObjectType, GraphQLInputObjectType, GraphQLEnumType, GraphQLSchema, GraphQLList, GraphQLInt, GraphQLBoolean, GraphQLFloat, GraphQLString } from 'graphql';
4
- import { GraphQLJSON, GraphQLBigInt, GraphQLByte, GraphQLVoid } from 'graphql-scalars';
5
- import { createHttpClient } from '@creditkarma/thrift-client';
6
- import { TType, ThriftClient, MessageType, TApplicationExceptionCodec, TApplicationException, TApplicationExceptionType } from '@creditkarma/thrift-server-core';
1
+ import { GraphQLBoolean, GraphQLEnumType, GraphQLFloat, GraphQLID, GraphQLInputObjectType, GraphQLInt, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString, } from 'graphql';
2
+ import { GraphQLBigInt, GraphQLByte, GraphQLJSON, GraphQLVoid } from 'graphql-scalars';
7
3
  import { pascalCase } from 'pascal-case';
4
+ import { createHttpClient } from '@creditkarma/thrift-client';
5
+ import { parse, SyntaxType, } from '@creditkarma/thrift-parser';
6
+ import { MessageType, TApplicationException, TApplicationExceptionCodec, TApplicationExceptionType, ThriftClient, TType, } from '@creditkarma/thrift-server-core';
7
+ import { path, process, util } from '@graphql-mesh/cross-helpers';
8
8
  import { PredefinedProxyOptions } from '@graphql-mesh/store';
9
+ import { getInterpolatedHeadersFactory, parseInterpolationStrings, } from '@graphql-mesh/string-interpolation';
10
+ import { readFileOrUrl } from '@graphql-mesh/utils';
9
11
  import { AggregateError } from '@graphql-tools/utils';
10
- import { parseInterpolationStrings, getInterpolatedHeadersFactory } from '@graphql-mesh/string-interpolation';
11
- import { util, process } from '@graphql-mesh/cross-helpers';
12
-
13
- class ThriftHandler {
14
- constructor({ config, baseDir, store, fetchFn, importFn, logger }) {
12
+ export default class ThriftHandler {
13
+ constructor({ config, baseDir, store, importFn, logger, }) {
15
14
  this.config = config;
16
15
  this.baseDir = baseDir;
17
16
  this.idl = store.proxy('idl.json', PredefinedProxyOptions.JsonWithoutValidation);
18
- this.fetchFn = fetchFn;
19
17
  this.importFn = importFn;
20
18
  this.logger = logger;
21
19
  }
22
- async getMeshSource() {
23
- var _a, _b;
24
- const { schemaHeaders, serviceName, operationHeaders } = this.config;
25
- const thriftAST = await this.idl.getWithSet(async () => {
26
- const rawThrift = await readFileOrUrl(this.config.idl, {
27
- allowUnknownExtensions: true,
28
- cwd: this.baseDir,
29
- headers: schemaHeaders,
30
- fetch: this.fetchFn,
31
- logger: this.logger,
32
- importFn: this.importFn,
33
- });
34
- const parseResult = parse(rawThrift, { organize: false });
35
- if (parseResult.type === SyntaxType.ThriftErrors) {
36
- if (parseResult.errors.length === 1) {
37
- throw parseResult.errors[0];
38
- }
39
- throw new AggregateError(parseResult.errors);
20
+ async parseWithIncludes(idlFilePath, includesMap) {
21
+ const rawThrift = await readFileOrUrl(idlFilePath, {
22
+ allowUnknownExtensions: true,
23
+ cwd: this.baseDir,
24
+ headers: this.config.schemaHeaders,
25
+ fetch: this.fetchFn,
26
+ logger: this.logger,
27
+ importFn: this.importFn,
28
+ });
29
+ const parseResult = parse(rawThrift, { organize: false });
30
+ const idlNamespace = path.basename(idlFilePath).split('.')[0];
31
+ if (parseResult.type === SyntaxType.ThriftErrors) {
32
+ if (parseResult.errors.length === 1) {
33
+ throw parseResult.errors[0];
40
34
  }
41
- return parseResult;
35
+ throw new AggregateError(parseResult.errors);
36
+ }
37
+ includesMap[idlNamespace] = parseResult;
38
+ const includes = parseResult.body.filter((statement) => statement.type === SyntaxType.IncludeDefinition);
39
+ await Promise.all(includes.map(async (include) => {
40
+ const includePath = path.resolve(path.dirname(idlFilePath), include.path.value);
41
+ await this.parseWithIncludes(includePath, includesMap);
42
+ }));
43
+ return includesMap;
44
+ }
45
+ async getMeshSource({ fetchFn }) {
46
+ var _a, _b;
47
+ this.fetchFn = fetchFn;
48
+ const { serviceName, operationHeaders } = this.config;
49
+ const namespaceASTMap = await this.idl.getWithSet(async () => {
50
+ const includeMap = {};
51
+ await this.parseWithIncludes(this.config.idl, includeMap);
52
+ return includeMap;
42
53
  });
43
54
  const enumTypeMap = new Map();
44
55
  const outputTypeMap = new Map();
@@ -317,11 +328,15 @@ class ThriftHandler {
317
328
  outputType = GraphQLJSON;
318
329
  const ofTypeKey = getGraphQLFunctionType(functionType.keyType, id);
319
330
  const ofTypeValue = getGraphQLFunctionType(functionType.valueType, id);
320
- typeVal = typeVal || { type: TType.MAP, keyType: ofTypeKey.typeVal, valType: ofTypeValue.typeVal };
331
+ typeVal = typeVal || {
332
+ type: TType.MAP,
333
+ keyType: ofTypeKey.typeVal,
334
+ valType: ofTypeValue.typeVal,
335
+ };
321
336
  break;
322
337
  }
323
338
  case SyntaxType.Identifier: {
324
- const typeName = functionType.value;
339
+ const typeName = functionType.value.replace('.', '_');
325
340
  if (enumTypeMap.has(typeName)) {
326
341
  const enumType = enumTypeMap.get(typeName);
327
342
  inputType = enumType;
@@ -350,114 +365,122 @@ class ThriftHandler {
350
365
  }
351
366
  const { args: commonArgs, contextVariables } = parseInterpolationStrings(Object.values(operationHeaders || {}));
352
367
  const headersFactory = getInterpolatedHeadersFactory(operationHeaders);
353
- for (const statement of thriftAST.body) {
354
- switch (statement.type) {
355
- case SyntaxType.EnumDefinition:
356
- enumTypeMap.set(statement.name.value, new GraphQLEnumType({
357
- name: statement.name.value,
358
- description: processComments(statement.comments),
359
- values: statement.members.reduce((prev, curr) => ({
360
- ...prev,
361
- [curr.name.value]: {
362
- description: processComments(curr.comments),
363
- value: curr.name.value,
364
- },
365
- }), {}),
366
- }));
367
- break;
368
- case SyntaxType.StructDefinition: {
369
- const structName = statement.name.value;
370
- const description = processComments(statement.comments);
371
- const objectFields = {};
372
- const inputObjectFields = {};
373
- const structTypeVal = {
374
- id: Math.random(),
375
- name: structName,
376
- type: TType.STRUCT,
377
- fields: {},
378
- };
379
- topTypeMap[structName] = structTypeVal;
380
- const structFieldTypeMap = structTypeVal.fields;
381
- for (const field of statement.fields) {
382
- const fieldName = field.name.value;
383
- let fieldOutputType;
384
- let fieldInputType;
385
- const description = processComments(field.comments);
386
- const processedFieldTypes = getGraphQLFunctionType(field.fieldType, (_a = field.fieldID) === null || _a === void 0 ? void 0 : _a.value);
387
- fieldOutputType = processedFieldTypes.outputType;
388
- fieldInputType = processedFieldTypes.inputType;
389
- if (field.requiredness === 'required') {
390
- fieldOutputType = new GraphQLNonNull(fieldOutputType);
391
- fieldInputType = new GraphQLNonNull(fieldInputType);
392
- }
393
- objectFields[fieldName] = {
394
- type: fieldOutputType,
395
- description,
396
- };
397
- inputObjectFields[fieldName] = {
398
- type: fieldInputType,
399
- description,
400
- };
401
- structFieldTypeMap[fieldName] = processedFieldTypes.typeVal;
402
- }
403
- outputTypeMap.set(structName, new GraphQLObjectType({
404
- name: structName,
405
- description,
406
- fields: objectFields,
407
- }));
408
- inputTypeMap.set(structName, new GraphQLInputObjectType({
409
- name: structName + 'Input',
410
- description,
411
- fields: inputObjectFields,
412
- }));
413
- break;
368
+ const baseNamespace = path.basename(this.config.idl, '.thrift');
369
+ for (const namespace of Object.keys(namespaceASTMap).reverse()) {
370
+ const thriftAST = namespaceASTMap[namespace];
371
+ for (const statement of thriftAST.body) {
372
+ let typeName = 'name' in statement ? statement.name.value : undefined;
373
+ if (namespace !== baseNamespace) {
374
+ typeName = `${namespace}_${typeName}`;
414
375
  }
415
- case SyntaxType.ServiceDefinition:
416
- for (const fnIndex in statement.functions) {
417
- const fn = statement.functions[fnIndex];
418
- const fnName = fn.name.value;
419
- const description = processComments(fn.comments);
420
- const { outputType: returnType } = getGraphQLFunctionType(fn.returnType, Number(fnIndex) + 1);
421
- const args = {};
422
- for (const argName in commonArgs) {
423
- const typeNameOrType = commonArgs[argName].type;
424
- args[argName] = {
425
- type: typeof typeNameOrType === 'string' ? inputTypeMap.get(typeNameOrType) : typeNameOrType || GraphQLID,
426
- };
427
- }
428
- const fieldTypeMap = {};
429
- for (const field of fn.fields) {
376
+ switch (statement.type) {
377
+ case SyntaxType.EnumDefinition:
378
+ enumTypeMap.set(typeName, new GraphQLEnumType({
379
+ name: typeName,
380
+ description: processComments(statement.comments),
381
+ values: statement.members.reduce((prev, curr) => ({
382
+ ...prev,
383
+ [curr.name.value]: {
384
+ description: processComments(curr.comments),
385
+ value: curr.name.value,
386
+ },
387
+ }), {}),
388
+ }));
389
+ break;
390
+ case SyntaxType.StructDefinition: {
391
+ const description = processComments(statement.comments);
392
+ const objectFields = {};
393
+ const inputObjectFields = {};
394
+ const structTypeVal = {
395
+ id: Math.random(),
396
+ name: typeName,
397
+ type: TType.STRUCT,
398
+ fields: {},
399
+ };
400
+ topTypeMap[typeName] = structTypeVal;
401
+ const structFieldTypeMap = structTypeVal.fields;
402
+ for (const field of statement.fields) {
430
403
  const fieldName = field.name.value;
431
- const fieldDescription = processComments(field.comments);
432
- let { inputType: fieldType, typeVal } = getGraphQLFunctionType(field.fieldType, (_b = field.fieldID) === null || _b === void 0 ? void 0 : _b.value);
404
+ let fieldOutputType;
405
+ let fieldInputType;
406
+ const description = processComments(field.comments);
407
+ const processedFieldTypes = getGraphQLFunctionType(field.fieldType, (_a = field.fieldID) === null || _a === void 0 ? void 0 : _a.value);
408
+ fieldOutputType = processedFieldTypes.outputType;
409
+ fieldInputType = processedFieldTypes.inputType;
433
410
  if (field.requiredness === 'required') {
434
- fieldType = new GraphQLNonNull(fieldType);
411
+ fieldOutputType = new GraphQLNonNull(fieldOutputType);
412
+ fieldInputType = new GraphQLNonNull(fieldInputType);
435
413
  }
436
- args[fieldName] = {
437
- type: fieldType,
438
- description: fieldDescription,
414
+ objectFields[fieldName] = {
415
+ type: fieldOutputType,
416
+ description,
417
+ };
418
+ inputObjectFields[fieldName] = {
419
+ type: fieldInputType,
420
+ description,
439
421
  };
440
- fieldTypeMap[fieldName] = typeVal;
422
+ structFieldTypeMap[fieldName] = processedFieldTypes.typeVal;
441
423
  }
442
- rootFields[fnName] = {
443
- type: returnType,
424
+ outputTypeMap.set(typeName, new GraphQLObjectType({
425
+ name: typeName,
444
426
  description,
445
- args,
446
- resolve: async (root, args, context, info) => thriftHttpClient.doRequest(fnName, args, fieldTypeMap, {
447
- headers: headersFactory({ root, args, context, info, env: process.env }),
448
- }),
449
- };
450
- methodNames.push(fnName);
451
- methodAnnotations[fnName] = { annotations: {}, fieldAnnotations: {} };
452
- methodParameters[fnName] = fn.fields.length + 1;
427
+ fields: objectFields,
428
+ }));
429
+ inputTypeMap.set(typeName, new GraphQLInputObjectType({
430
+ name: typeName + 'Input',
431
+ description,
432
+ fields: inputObjectFields,
433
+ }));
434
+ break;
435
+ }
436
+ case SyntaxType.ServiceDefinition:
437
+ for (const fnIndex in statement.functions) {
438
+ const fn = statement.functions[fnIndex];
439
+ const fnName = fn.name.value;
440
+ const description = processComments(fn.comments);
441
+ const { outputType: returnType } = getGraphQLFunctionType(fn.returnType, Number(fnIndex) + 1);
442
+ const args = {};
443
+ for (const argName in commonArgs) {
444
+ const typeNameOrType = commonArgs[argName].type;
445
+ args[argName] = {
446
+ type: typeof typeNameOrType === 'string'
447
+ ? inputTypeMap.get(typeNameOrType)
448
+ : typeNameOrType || GraphQLID,
449
+ };
450
+ }
451
+ const fieldTypeMap = {};
452
+ for (const field of fn.fields) {
453
+ const fieldName = field.name.value;
454
+ const fieldDescription = processComments(field.comments);
455
+ let { inputType: fieldType, typeVal } = getGraphQLFunctionType(field.fieldType, (_b = field.fieldID) === null || _b === void 0 ? void 0 : _b.value);
456
+ if (field.requiredness === 'required') {
457
+ fieldType = new GraphQLNonNull(fieldType);
458
+ }
459
+ args[fieldName] = {
460
+ type: fieldType,
461
+ description: fieldDescription,
462
+ };
463
+ fieldTypeMap[fieldName] = typeVal;
464
+ }
465
+ rootFields[fnName] = {
466
+ type: returnType,
467
+ description,
468
+ args,
469
+ resolve: async (root, args, context, info) => thriftHttpClient.doRequest(fnName, args, fieldTypeMap, {
470
+ headers: headersFactory({ root, args, context, info, env: process.env }),
471
+ }),
472
+ };
473
+ methodNames.push(fnName);
474
+ methodAnnotations[fnName] = { annotations: {}, fieldAnnotations: {} };
475
+ methodParameters[fnName] = fn.fields.length + 1;
476
+ }
477
+ break;
478
+ case SyntaxType.TypedefDefinition: {
479
+ const { inputType, outputType } = getGraphQLFunctionType(statement.definitionType, Math.random());
480
+ inputTypeMap.set(typeName, inputType);
481
+ outputTypeMap.set(typeName, outputType);
482
+ break;
453
483
  }
454
- break;
455
- case SyntaxType.TypedefDefinition: {
456
- const { inputType, outputType } = getGraphQLFunctionType(statement.definitionType, Math.random());
457
- const typeName = statement.name.value;
458
- inputTypeMap.set(typeName, inputType);
459
- outputTypeMap.set(typeName, outputType);
460
- break;
461
484
  }
462
485
  }
463
486
  }
@@ -474,5 +497,3 @@ class ThriftHandler {
474
497
  };
475
498
  }
476
499
  }
477
-
478
- export default ThriftHandler;
package/package.json CHANGED
@@ -1,24 +1,24 @@
1
1
  {
2
2
  "name": "@graphql-mesh/thrift",
3
- "version": "1.0.0-alpha-3fc47d119.0",
3
+ "version": "1.0.0-alpha-20230420181317-a95037648",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@graphql-mesh/types": "0.79.0-alpha-3fc47d119.0",
7
- "@graphql-mesh/utils": "1.0.0-alpha-3fc47d119.0",
8
- "graphql": "*"
6
+ "@graphql-mesh/cross-helpers": "^0.3.4",
7
+ "@graphql-mesh/store": "1.0.0-alpha-20230420181317-a95037648",
8
+ "@graphql-mesh/types": "1.0.0-alpha-20230420181317-a95037648",
9
+ "@graphql-mesh/utils": "1.0.0-alpha-20230420181317-a95037648",
10
+ "@graphql-tools/utils": "^9.2.1",
11
+ "graphql": "*",
12
+ "tslib": "^2.4.0"
9
13
  },
10
14
  "dependencies": {
11
15
  "@creditkarma/thrift-client": "1.0.4",
12
16
  "@creditkarma/thrift-parser": "2.0.0",
13
17
  "@creditkarma/thrift-server-core": "1.0.4",
14
- "@graphql-mesh/cross-helpers": "0.2.0",
15
- "@graphql-mesh/store": "1.0.0-alpha-3fc47d119.0",
16
- "@graphql-mesh/string-interpolation": "0.3.0",
17
- "@graphql-tools/utils": "8.8.0",
18
- "graphql-scalars": "1.17.0",
18
+ "@graphql-mesh/string-interpolation": "0.4.4",
19
+ "graphql-scalars": "^1.20.4",
19
20
  "pascal-case": "3.1.2",
20
- "thrift": "0.16.0",
21
- "tslib": "^2.4.0",
21
+ "thrift": "0.18.1",
22
22
  "url-join": "4.0.1"
23
23
  },
24
24
  "repository": {
@@ -27,21 +27,28 @@
27
27
  "directory": "packages/handlers/thrift"
28
28
  },
29
29
  "license": "MIT",
30
- "main": "index.js",
31
- "module": "index.mjs",
32
- "typings": "index.d.ts",
30
+ "main": "cjs/index.js",
31
+ "module": "esm/index.js",
32
+ "typings": "typings/index.d.ts",
33
33
  "typescript": {
34
- "definition": "index.d.ts"
34
+ "definition": "typings/index.d.ts"
35
35
  },
36
+ "type": "module",
36
37
  "exports": {
37
38
  ".": {
38
- "require": "./index.js",
39
- "import": "./index.mjs"
40
- },
41
- "./*": {
42
- "require": "./*.js",
43
- "import": "./*.mjs"
39
+ "require": {
40
+ "types": "./typings/index.d.cts",
41
+ "default": "./cjs/index.js"
42
+ },
43
+ "import": {
44
+ "types": "./typings/index.d.ts",
45
+ "default": "./esm/index.js"
46
+ },
47
+ "default": {
48
+ "types": "./typings/index.d.ts",
49
+ "default": "./esm/index.js"
50
+ }
44
51
  },
45
52
  "./package.json": "./package.json"
46
53
  }
47
- }
54
+ }
@@ -0,0 +1,12 @@
1
+ import { GetMeshSourcePayload, MeshHandler, MeshHandlerOptions, MeshSource, YamlConfig } from '@graphql-mesh/types';
2
+ export default class ThriftHandler implements MeshHandler {
3
+ private config;
4
+ private baseDir;
5
+ private idl;
6
+ private fetchFn;
7
+ private importFn;
8
+ private logger;
9
+ constructor({ config, baseDir, store, importFn, logger, }: MeshHandlerOptions<YamlConfig.ThriftHandler>);
10
+ private parseWithIncludes;
11
+ getMeshSource({ fetchFn }: GetMeshSourcePayload): Promise<MeshSource>;
12
+ }
@@ -0,0 +1,12 @@
1
+ import { GetMeshSourcePayload, MeshHandler, MeshHandlerOptions, MeshSource, YamlConfig } from '@graphql-mesh/types';
2
+ export default class ThriftHandler implements MeshHandler {
3
+ private config;
4
+ private baseDir;
5
+ private idl;
6
+ private fetchFn;
7
+ private importFn;
8
+ private logger;
9
+ constructor({ config, baseDir, store, importFn, logger, }: MeshHandlerOptions<YamlConfig.ThriftHandler>);
10
+ private parseWithIncludes;
11
+ getMeshSource({ fetchFn }: GetMeshSourcePayload): Promise<MeshSource>;
12
+ }
package/index.d.ts DELETED
@@ -1,15 +0,0 @@
1
- import { GetMeshSourceOptions, MeshHandler, YamlConfig } from '@graphql-mesh/types';
2
- import { GraphQLSchema } from 'graphql';
3
- export default class ThriftHandler implements MeshHandler {
4
- private config;
5
- private baseDir;
6
- private idl;
7
- private fetchFn;
8
- private importFn;
9
- private logger;
10
- constructor({ config, baseDir, store, fetchFn, importFn, logger }: GetMeshSourceOptions<YamlConfig.ThriftHandler>);
11
- getMeshSource(): Promise<{
12
- schema: GraphQLSchema;
13
- contextVariables: Record<string, string>;
14
- }>;
15
- }