@graphql-mesh/transport-rest 0.0.0

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.
Files changed (78) hide show
  1. package/cjs/directives/dictionary.js +19 -0
  2. package/cjs/directives/discriminator.js +9 -0
  3. package/cjs/directives/flatten.js +16 -0
  4. package/cjs/directives/getTypeResolverForAbstractType.js +102 -0
  5. package/cjs/directives/httpOperation.js +404 -0
  6. package/cjs/directives/isFileUpload.js +7 -0
  7. package/cjs/directives/jsonApiFields.js +23 -0
  8. package/cjs/directives/length.js +26 -0
  9. package/cjs/directives/link.js +67 -0
  10. package/cjs/directives/process.js +181 -0
  11. package/cjs/directives/pubsubOperation.js +31 -0
  12. package/cjs/directives/regexp.js +24 -0
  13. package/cjs/directives/resolveDataByUnionInputType.js +39 -0
  14. package/cjs/directives/resolveRoot.js +10 -0
  15. package/cjs/directives/resolveRootField.js +29 -0
  16. package/cjs/directives/responseMetadata.js +16 -0
  17. package/cjs/directives/scalars.js +53 -0
  18. package/cjs/directives/typescriptAnnotations.js +8 -0
  19. package/cjs/index.js +20 -0
  20. package/cjs/package.json +1 -0
  21. package/esm/directives/dictionary.js +15 -0
  22. package/esm/directives/discriminator.js +5 -0
  23. package/esm/directives/flatten.js +12 -0
  24. package/esm/directives/getTypeResolverForAbstractType.js +98 -0
  25. package/esm/directives/httpOperation.js +399 -0
  26. package/esm/directives/isFileUpload.js +3 -0
  27. package/esm/directives/jsonApiFields.js +18 -0
  28. package/esm/directives/length.js +22 -0
  29. package/esm/directives/link.js +63 -0
  30. package/esm/directives/process.js +177 -0
  31. package/esm/directives/pubsubOperation.js +27 -0
  32. package/esm/directives/regexp.js +20 -0
  33. package/esm/directives/resolveDataByUnionInputType.js +35 -0
  34. package/esm/directives/resolveRoot.js +6 -0
  35. package/esm/directives/resolveRootField.js +25 -0
  36. package/esm/directives/responseMetadata.js +12 -0
  37. package/esm/directives/scalars.js +48 -0
  38. package/esm/directives/typescriptAnnotations.js +4 -0
  39. package/esm/index.js +15 -0
  40. package/package.json +56 -0
  41. package/typings/directives/dictionary.d.cts +2 -0
  42. package/typings/directives/dictionary.d.ts +2 -0
  43. package/typings/directives/discriminator.d.cts +5 -0
  44. package/typings/directives/discriminator.d.ts +5 -0
  45. package/typings/directives/flatten.d.cts +2 -0
  46. package/typings/directives/flatten.d.ts +2 -0
  47. package/typings/directives/getTypeResolverForAbstractType.d.cts +7 -0
  48. package/typings/directives/getTypeResolverForAbstractType.d.ts +7 -0
  49. package/typings/directives/httpOperation.d.cts +28 -0
  50. package/typings/directives/httpOperation.d.ts +28 -0
  51. package/typings/directives/isFileUpload.d.cts +4 -0
  52. package/typings/directives/isFileUpload.d.ts +4 -0
  53. package/typings/directives/jsonApiFields.d.cts +2 -0
  54. package/typings/directives/jsonApiFields.d.ts +2 -0
  55. package/typings/directives/length.d.cts +5 -0
  56. package/typings/directives/length.d.ts +5 -0
  57. package/typings/directives/link.d.cts +2 -0
  58. package/typings/directives/link.d.ts +2 -0
  59. package/typings/directives/process.d.cts +12 -0
  60. package/typings/directives/process.d.ts +12 -0
  61. package/typings/directives/pubsubOperation.d.cts +10 -0
  62. package/typings/directives/pubsubOperation.d.ts +10 -0
  63. package/typings/directives/regexp.d.cts +2 -0
  64. package/typings/directives/regexp.d.ts +2 -0
  65. package/typings/directives/resolveDataByUnionInputType.d.cts +2 -0
  66. package/typings/directives/resolveDataByUnionInputType.d.ts +2 -0
  67. package/typings/directives/resolveRoot.d.cts +2 -0
  68. package/typings/directives/resolveRoot.d.ts +2 -0
  69. package/typings/directives/resolveRootField.d.cts +2 -0
  70. package/typings/directives/resolveRootField.d.ts +2 -0
  71. package/typings/directives/responseMetadata.d.cts +2 -0
  72. package/typings/directives/responseMetadata.d.ts +2 -0
  73. package/typings/directives/scalars.d.cts +3 -0
  74. package/typings/directives/scalars.d.ts +3 -0
  75. package/typings/directives/typescriptAnnotations.d.cts +2 -0
  76. package/typings/directives/typescriptAnnotations.d.ts +2 -0
  77. package/typings/index.d.cts +7 -0
  78. package/typings/index.d.ts +7 -0
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processDirectives = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const transport_common_1 = require("@graphql-mesh/transport-common");
6
+ const utils_1 = require("@graphql-tools/utils");
7
+ const dictionary_1 = require("./dictionary");
8
+ const discriminator_1 = require("./discriminator");
9
+ const flatten_1 = require("./flatten");
10
+ const getTypeResolverForAbstractType_1 = require("./getTypeResolverForAbstractType");
11
+ const httpOperation_1 = require("./httpOperation");
12
+ const link_1 = require("./link");
13
+ const pubsubOperation_1 = require("./pubsubOperation");
14
+ const resolveRoot_1 = require("./resolveRoot");
15
+ const resolveRootField_1 = require("./resolveRootField");
16
+ const responseMetadata_1 = require("./responseMetadata");
17
+ const scalars_1 = require("./scalars");
18
+ const typescriptAnnotations_1 = require("./typescriptAnnotations");
19
+ function processDirectives(schema, { globalFetch, logger, pubsub, ...extraGlobalOptions } = {}) {
20
+ const nonExecutableObjMapScalar = schema.getType('ObjMap');
21
+ if (nonExecutableObjMapScalar && (0, graphql_1.isScalarType)(nonExecutableObjMapScalar)) {
22
+ (0, scalars_1.addExecutionLogicToScalar)(nonExecutableObjMapScalar, transport_common_1.ObjMapScalar);
23
+ }
24
+ const transportDirectives = (0, utils_1.getDirective)(schema, schema, 'transport');
25
+ const currDirective = transportDirectives?.[0];
26
+ const globalOptions = {
27
+ endpoint: currDirective?.location,
28
+ operationHeaders: currDirective?.headers,
29
+ queryParams: currDirective?.queryParams,
30
+ queryStringOptions: currDirective?.queryStringOptions,
31
+ ...extraGlobalOptions,
32
+ };
33
+ if (typeof globalOptions.operationHeaders === 'string') {
34
+ globalOptions.operationHeaders = JSON.parse(globalOptions.operationHeaders);
35
+ }
36
+ if (typeof globalOptions.queryParams === 'string') {
37
+ globalOptions.queryParams = JSON.parse(globalOptions.queryParams);
38
+ }
39
+ const typeMap = schema.getTypeMap();
40
+ for (const typeName in typeMap) {
41
+ const type = typeMap[typeName];
42
+ const exampleAnnotations = (0, utils_1.getDirective)(schema, type, 'example');
43
+ if (exampleAnnotations?.length) {
44
+ const examples = [];
45
+ for (const exampleAnnotation of exampleAnnotations) {
46
+ if (exampleAnnotation?.value) {
47
+ examples.push(exampleAnnotation.value);
48
+ }
49
+ }
50
+ type.extensions = type.extensions || {};
51
+ type.extensions.examples = examples;
52
+ }
53
+ if ((0, graphql_1.isScalarType)(type)) {
54
+ (0, scalars_1.processScalarType)(schema, type);
55
+ }
56
+ if ((0, graphql_1.isInterfaceType)(type)) {
57
+ const directiveAnnotations = (0, utils_1.getDirectives)(schema, type);
58
+ for (const directiveAnnotation of directiveAnnotations) {
59
+ switch (directiveAnnotation.name) {
60
+ case 'discriminator':
61
+ (0, discriminator_1.processDiscriminatorAnnotations)({
62
+ interfaceType: type,
63
+ discriminatorFieldName: directiveAnnotation.args.field,
64
+ });
65
+ break;
66
+ }
67
+ }
68
+ }
69
+ if ((0, graphql_1.isUnionType)(type)) {
70
+ const directiveAnnotations = (0, utils_1.getDirectives)(schema, type);
71
+ let statusCodeTypeNameIndexMap;
72
+ let discriminatorField;
73
+ let discriminatorMapping;
74
+ for (const directiveAnnotation of directiveAnnotations) {
75
+ switch (directiveAnnotation.name) {
76
+ case 'statusCodeTypeName':
77
+ statusCodeTypeNameIndexMap = statusCodeTypeNameIndexMap || {};
78
+ statusCodeTypeNameIndexMap[directiveAnnotation.args.statusCode] =
79
+ directiveAnnotation.args.typeName;
80
+ break;
81
+ case 'discriminator':
82
+ discriminatorField = directiveAnnotation.args.field;
83
+ discriminatorMapping = directiveAnnotation.args.mapping;
84
+ break;
85
+ }
86
+ }
87
+ type.resolveType = (0, getTypeResolverForAbstractType_1.getTypeResolverForAbstractType)({
88
+ possibleTypes: type.getTypes(),
89
+ discriminatorField,
90
+ discriminatorMapping,
91
+ statusCodeTypeNameMap: statusCodeTypeNameIndexMap,
92
+ });
93
+ }
94
+ if ((0, graphql_1.isEnumType)(type)) {
95
+ const directiveAnnotations = (0, utils_1.getDirectives)(schema, type);
96
+ for (const directiveAnnotation of directiveAnnotations) {
97
+ switch (directiveAnnotation.name) {
98
+ case 'typescript':
99
+ (0, typescriptAnnotations_1.processTypeScriptAnnotations)(type, directiveAnnotation.args.type);
100
+ break;
101
+ }
102
+ }
103
+ const enumValues = type.getValues();
104
+ for (const enumValue of enumValues) {
105
+ const directiveAnnotations = (0, utils_1.getDirectives)(schema, enumValue);
106
+ for (const directiveAnnotation of directiveAnnotations) {
107
+ switch (directiveAnnotation.name) {
108
+ case 'enum': {
109
+ const realValue = JSON.parse(directiveAnnotation.args.value);
110
+ enumValue.value = realValue;
111
+ type._valueLookup.set(realValue, enumValue);
112
+ break;
113
+ }
114
+ }
115
+ }
116
+ }
117
+ }
118
+ if ('getFields' in type) {
119
+ const fields = type.getFields();
120
+ for (const fieldName in fields) {
121
+ const field = fields[fieldName];
122
+ const directiveAnnotations = (0, utils_1.getDirectives)(schema, field);
123
+ for (const directiveAnnotation of directiveAnnotations) {
124
+ switch (directiveAnnotation.name) {
125
+ case 'resolveRoot':
126
+ (0, resolveRoot_1.processResolveRootAnnotations)(field);
127
+ break;
128
+ case 'resolveRootField':
129
+ (0, resolveRootField_1.processResolveRootFieldAnnotations)(field, directiveAnnotation.args.field);
130
+ break;
131
+ case 'flatten':
132
+ (0, flatten_1.processFlattenAnnotations)(field);
133
+ break;
134
+ case 'pubsubOperation':
135
+ (0, pubsubOperation_1.processPubSubOperationAnnotations)({
136
+ field: field,
137
+ pubsubTopic: directiveAnnotation.args.pubsubTopic,
138
+ globalPubsub: pubsub,
139
+ logger,
140
+ });
141
+ break;
142
+ case 'httpOperation':
143
+ (0, httpOperation_1.addHTTPRootFieldResolver)(schema, field, logger, globalFetch,
144
+ // TODO: Fix JSON parsing here for queryParams and headers
145
+ {
146
+ sourceName: directiveAnnotation.args.sourceName,
147
+ endpoint: directiveAnnotation.args.endpoint,
148
+ path: directiveAnnotation.args.path,
149
+ httpMethod: directiveAnnotation.args.httpMethod,
150
+ operationSpecificHeaders: typeof directiveAnnotation.args.operationSpecificHeaders === 'string'
151
+ ? JSON.parse(directiveAnnotation.args.operationSpecificHeaders)
152
+ : directiveAnnotation.args.operationSpecificHeaders,
153
+ isBinary: directiveAnnotation.args.isBinary,
154
+ requestBaseBody: typeof directiveAnnotation.args.requestBaseBody === 'string'
155
+ ? JSON.parse(directiveAnnotation.args.requestBaseBody)
156
+ : directiveAnnotation.args.requestBaseBody,
157
+ queryParamArgMap: typeof directiveAnnotation.args.queryParamArgMap === 'string'
158
+ ? JSON.parse(directiveAnnotation.args.queryParamArgMap)
159
+ : directiveAnnotation.args.queryParamArgMap,
160
+ queryStringOptionsByParam: typeof directiveAnnotation.args.queryStringOptionsByParam === 'string'
161
+ ? JSON.parse(directiveAnnotation.args.queryStringOptionsByParam)
162
+ : directiveAnnotation.args.queryStringOptionsByParam,
163
+ jsonApiFields: directiveAnnotation.args.jsonApiFields,
164
+ }, globalOptions);
165
+ break;
166
+ case 'responseMetadata':
167
+ (0, responseMetadata_1.processResponseMetadataAnnotations)(field);
168
+ break;
169
+ case 'link':
170
+ (0, link_1.processLinkFieldAnnotations)(field, directiveAnnotation.args.defaultRootType, directiveAnnotation.args.defaultField);
171
+ break;
172
+ case 'dictionary':
173
+ (0, dictionary_1.processDictionaryDirective)(fields, field);
174
+ }
175
+ }
176
+ }
177
+ }
178
+ }
179
+ return schema;
180
+ }
181
+ exports.processDirectives = processDirectives;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processPubSubOperationAnnotations = void 0;
4
+ const string_interpolation_1 = require("@graphql-mesh/string-interpolation");
5
+ const utils_1 = require("@graphql-tools/utils");
6
+ function processPubSubOperationAnnotations({ field, globalPubsub, pubsubTopic, logger: globalLogger, }) {
7
+ field.subscribe = function pubSubSubscribeFn(root, args, context, info) {
8
+ const logger = context?.logger || globalLogger;
9
+ const operationLogger = logger.child(`${info.parentType.name}.${field.name}`);
10
+ const pubsub = context?.pubsub || globalPubsub;
11
+ if (!pubsub) {
12
+ return (0, utils_1.createGraphQLError)(`You should have PubSub defined in either the config or the context!`);
13
+ }
14
+ const interpolationData = { root, args, context, info, env: process.env };
15
+ let interpolatedPubSubTopic = string_interpolation_1.stringInterpolator.parse(pubsubTopic, interpolationData);
16
+ if (interpolatedPubSubTopic.startsWith('webhook:')) {
17
+ const [, expectedMethod, expectedUrl] = interpolatedPubSubTopic.split(':');
18
+ const expectedPath = new URL(expectedUrl, 'http://localhost').pathname;
19
+ interpolatedPubSubTopic = `webhook:${expectedMethod}:${expectedPath}`;
20
+ }
21
+ operationLogger.debug(`${info.parentType.name}.${field.name} => Subscribing to pubSubTopic: ${interpolatedPubSubTopic}`);
22
+ return pubsub.asyncIterator(interpolatedPubSubTopic);
23
+ };
24
+ field.resolve = function pubSubResolver(root, args, context, info) {
25
+ const logger = context?.logger || globalLogger;
26
+ const operationLogger = logger.child(`${info.parentType.name}.${field.name}`);
27
+ operationLogger.debug('Received ', root, ' from ', pubsubTopic);
28
+ return root;
29
+ };
30
+ }
31
+ exports.processPubSubOperationAnnotations = processPubSubOperationAnnotations;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processRegExpAnnotations = void 0;
4
+ function processRegExpAnnotations(scalar, pattern) {
5
+ function coerceString(value) {
6
+ if (value != null) {
7
+ const vStr = value.toString();
8
+ const regexp = new RegExp(pattern);
9
+ if (!regexp.test(vStr)) {
10
+ throw new Error(`${scalar.name} must match ${pattern} but given ${vStr}`);
11
+ }
12
+ return vStr;
13
+ }
14
+ }
15
+ scalar.serialize = coerceString;
16
+ scalar.parseValue = coerceString;
17
+ scalar.parseLiteral = ast => {
18
+ if ('value' in ast) {
19
+ return coerceString(ast.value);
20
+ }
21
+ return null;
22
+ };
23
+ }
24
+ exports.processRegExpAnnotations = processRegExpAnnotations;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveDataByUnionInputType = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const utils_1 = require("@graphql-mesh/utils");
6
+ const utils_2 = require("@graphql-tools/utils");
7
+ function resolveDataByUnionInputType(data, type, schema) {
8
+ if (data) {
9
+ if ((0, graphql_1.isListType)(type)) {
10
+ return (0, utils_2.asArray)(data).map(elem => resolveDataByUnionInputType(elem, type.ofType, schema));
11
+ }
12
+ if ((0, graphql_1.isNonNullType)(type)) {
13
+ return resolveDataByUnionInputType(data, type.ofType, schema);
14
+ }
15
+ if ((0, graphql_1.isInputObjectType)(type)) {
16
+ const typeOneOfDirectives = (0, utils_2.getDirective)(schema, type, 'oneOf');
17
+ const isOneOf = typeOneOfDirectives?.length;
18
+ const fieldMap = type.getFields();
19
+ data = (0, utils_2.asArray)(data)[0];
20
+ for (const propertyName in data) {
21
+ const fieldName = (0, utils_1.sanitizeNameForGraphQL)(propertyName);
22
+ const field = fieldMap[fieldName];
23
+ if (field) {
24
+ if (isOneOf) {
25
+ const resolvedData = resolveDataByUnionInputType(data[fieldName], field.type, schema);
26
+ return resolvedData;
27
+ }
28
+ const fieldData = data[fieldName];
29
+ data[fieldName] = undefined;
30
+ const fieldResolveRootFieldDirectives = (0, utils_2.getDirective)(schema, field, 'resolveRootField');
31
+ const realFieldName = fieldResolveRootFieldDirectives?.[0]?.field || fieldName;
32
+ data[realFieldName] = resolveDataByUnionInputType(fieldData, field.type, schema);
33
+ }
34
+ }
35
+ }
36
+ }
37
+ return data;
38
+ }
39
+ exports.resolveDataByUnionInputType = resolveDataByUnionInputType;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processResolveRootAnnotations = void 0;
4
+ function rootResolver(root) {
5
+ return root;
6
+ }
7
+ function processResolveRootAnnotations(field) {
8
+ field.resolve = rootResolver;
9
+ }
10
+ exports.processResolveRootAnnotations = processResolveRootAnnotations;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processResolveRootFieldAnnotations = void 0;
4
+ const graphql_1 = require("graphql");
5
+ function isOriginallyListType(type) {
6
+ if ((0, graphql_1.isNonNullType)(type)) {
7
+ return isOriginallyListType(type.ofType);
8
+ }
9
+ return (0, graphql_1.isListType)(type);
10
+ }
11
+ function processResolveRootFieldAnnotations(field, propertyName) {
12
+ if (!field.resolve || field.resolve.name === 'defaultFieldResolver') {
13
+ field.resolve = (root, args, context, info) => {
14
+ const actualFieldObj = root[propertyName];
15
+ if (actualFieldObj != null) {
16
+ const isArray = Array.isArray(actualFieldObj);
17
+ const isListType = isOriginallyListType(info.returnType);
18
+ if (isListType && !isArray) {
19
+ return [actualFieldObj];
20
+ }
21
+ else if (!isListType && isArray) {
22
+ return actualFieldObj[0];
23
+ }
24
+ }
25
+ return actualFieldObj;
26
+ };
27
+ }
28
+ }
29
+ exports.processResolveRootFieldAnnotations = processResolveRootFieldAnnotations;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processResponseMetadataAnnotations = void 0;
4
+ function processResponseMetadataAnnotations(field) {
5
+ field.resolve = function responseMetadataResolver(root) {
6
+ return {
7
+ url: root.$url,
8
+ headers: root.$response.header,
9
+ method: root.$method,
10
+ status: root.$statusCode,
11
+ statusText: root.$statusText,
12
+ body: root.$response.body,
13
+ };
14
+ };
15
+ }
16
+ exports.processResponseMetadataAnnotations = processResponseMetadataAnnotations;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addExecutionLogicToScalar = exports.processScalarType = void 0;
4
+ const graphql_scalars_1 = require("graphql-scalars");
5
+ const transport_common_1 = require("@graphql-mesh/transport-common");
6
+ const utils_1 = require("@graphql-tools/utils");
7
+ const length_js_1 = require("./length.js");
8
+ const regexp_js_1 = require("./regexp.js");
9
+ const typescriptAnnotations_js_1 = require("./typescriptAnnotations.js");
10
+ function processScalarType(schema, type) {
11
+ if (type.name in graphql_scalars_1.resolvers) {
12
+ const actualScalar = graphql_scalars_1.resolvers[type.name];
13
+ addExecutionLogicToScalar(type, actualScalar);
14
+ }
15
+ if (type.name === 'ObjMap') {
16
+ addExecutionLogicToScalar(type, transport_common_1.ObjMapScalar);
17
+ }
18
+ const directiveAnnotations = (0, utils_1.getDirectives)(schema, type);
19
+ for (const directiveAnnotation of directiveAnnotations) {
20
+ switch (directiveAnnotation.name) {
21
+ case 'length':
22
+ (0, length_js_1.processLengthAnnotations)(type, directiveAnnotation.args);
23
+ break;
24
+ case 'regexp':
25
+ (0, regexp_js_1.processRegExpAnnotations)(type, directiveAnnotation.args.pattern);
26
+ break;
27
+ case 'typescript':
28
+ (0, typescriptAnnotations_js_1.processTypeScriptAnnotations)(type, directiveAnnotation.args.type);
29
+ break;
30
+ }
31
+ }
32
+ }
33
+ exports.processScalarType = processScalarType;
34
+ function addExecutionLogicToScalar(nonExecutableScalar, actualScalar) {
35
+ Object.defineProperties(nonExecutableScalar, {
36
+ serialize: {
37
+ value: actualScalar.serialize,
38
+ },
39
+ parseValue: {
40
+ value: actualScalar.parseValue,
41
+ },
42
+ parseLiteral: {
43
+ value: actualScalar.parseLiteral,
44
+ },
45
+ extensions: {
46
+ value: {
47
+ ...actualScalar.extensions,
48
+ ...nonExecutableScalar.extensions,
49
+ },
50
+ },
51
+ });
52
+ }
53
+ exports.addExecutionLogicToScalar = addExecutionLogicToScalar;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processTypeScriptAnnotations = void 0;
4
+ function processTypeScriptAnnotations(type, typeDefinition) {
5
+ type.extensions = type.extensions || {};
6
+ type.extensions.codegenScalarType = typeDefinition;
7
+ }
8
+ exports.processTypeScriptAnnotations = processTypeScriptAnnotations;
package/cjs/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processDirectives = exports.getSubgraphExecutor = void 0;
4
+ const transport_common_1 = require("@graphql-mesh/transport-common");
5
+ const process_1 = require("./directives/process");
6
+ const getSubgraphExecutor = function getRESTSubgraphExecutor({ transportEntry, getSubgraph, fetch, pubsub, logger }) {
7
+ const preProcessedSchema = getSubgraph();
8
+ const processDirectiveOpts = {
9
+ globalFetch: fetch,
10
+ pubsub,
11
+ logger,
12
+ ...transportEntry.options,
13
+ };
14
+ const processedSchema = (0, process_1.processDirectives)(preProcessedSchema, processDirectiveOpts);
15
+ const executor = (0, transport_common_1.createDefaultExecutor)(processedSchema);
16
+ return executor;
17
+ };
18
+ exports.getSubgraphExecutor = getSubgraphExecutor;
19
+ var process_2 = require("./directives/process");
20
+ Object.defineProperty(exports, "processDirectives", { enumerable: true, get: function () { return process_2.processDirectives; } });
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,15 @@
1
+ export function processDictionaryDirective(fieldMap, field) {
2
+ field.resolve = function dictionaryDirectiveHandler(root) {
3
+ const result = [];
4
+ for (const key in root) {
5
+ if (key in fieldMap) {
6
+ continue;
7
+ }
8
+ result.push({
9
+ key,
10
+ value: root[key],
11
+ });
12
+ }
13
+ return result;
14
+ };
15
+ }
@@ -0,0 +1,5 @@
1
+ export function processDiscriminatorAnnotations({ interfaceType, discriminatorFieldName, }) {
2
+ interfaceType.resolveType = function discriminatorDirectiveHandler(root) {
3
+ return root[discriminatorFieldName];
4
+ };
5
+ }
@@ -0,0 +1,12 @@
1
+ export function processFlattenAnnotations(field) {
2
+ if (!field.resolve || field.resolve.name === 'defaultFieldResolver') {
3
+ const fieldName = field.name;
4
+ field.resolve = function flattenDirectiveHandler(root) {
5
+ let result = root[fieldName];
6
+ if (!Array.isArray(root)) {
7
+ result = [result];
8
+ }
9
+ return result.flat(Infinity);
10
+ };
11
+ }
12
+ }
@@ -0,0 +1,98 @@
1
+ import { createGraphQLError, getDirective } from '@graphql-tools/utils';
2
+ export function getTypeResolverForAbstractType({ possibleTypes, discriminatorField, discriminatorMapping, statusCodeTypeNameMap, }) {
3
+ return function resolveType(data, _ctx, info) {
4
+ if (data.__typename) {
5
+ return data.__typename;
6
+ }
7
+ else if (discriminatorField != null && data[discriminatorField]) {
8
+ const discriminatorValue = data[discriminatorField];
9
+ return discriminatorMapping?.[discriminatorValue] || discriminatorValue;
10
+ }
11
+ if (data.$statusCode && statusCodeTypeNameMap) {
12
+ const typeName = statusCodeTypeNameMap[data.$statusCode.toString()] || statusCodeTypeNameMap.default;
13
+ if (typeName) {
14
+ return typeName;
15
+ }
16
+ }
17
+ const dataTypeOf = typeof data;
18
+ if (dataTypeOf !== 'object') {
19
+ for (const possibleType of possibleTypes) {
20
+ const fieldMap = possibleType.getFields();
21
+ const fields = Object.values(fieldMap);
22
+ if (fields.length === 1) {
23
+ const field = fields[0];
24
+ const directiveObjs = getDirective(info.schema, field, 'resolveRoot');
25
+ if (directiveObjs?.length) {
26
+ const fieldType = field.type;
27
+ if ('parseValue' in fieldType) {
28
+ try {
29
+ fieldType.parseValue(data);
30
+ return possibleType.name;
31
+ }
32
+ catch (e) { }
33
+ }
34
+ }
35
+ }
36
+ }
37
+ }
38
+ // const validationErrors: Record<string, ErrorObject[]> = {};
39
+ const dataKeys = dataTypeOf
40
+ ? Object.keys(data)
41
+ // Remove metadata fields used to pass data
42
+ .filter(property => !property.toString().startsWith('$'))
43
+ : null;
44
+ const scoreTypeNameMap = new Map();
45
+ for (const possibleType of possibleTypes) {
46
+ const typeName = possibleType.name;
47
+ if (dataKeys != null) {
48
+ const typeFields = Object.keys(possibleType.getFields());
49
+ if (dataKeys.length <= typeFields.length &&
50
+ dataKeys.every(property => typeFields.includes(property.toString()))) {
51
+ return typeName;
52
+ }
53
+ else {
54
+ const score = dataKeys.filter(property => typeFields.includes(property.toString())).length;
55
+ if (score || typeFields.includes('additionalProperties')) {
56
+ scoreTypeNameMap.set(score, typeName);
57
+ }
58
+ }
59
+ } /* else {
60
+ const validateFn = possibleType.extensions.validateWithJSONSchema as ValidateFunction;
61
+ if (validateFn) {
62
+ const isValid = validateFn(data);
63
+ if (isValid) {
64
+ return typeName;
65
+ }
66
+ validationErrors[typeName] = ajv.errors || validateFn.errors;
67
+ }
68
+ } */
69
+ }
70
+ const maxScore = Math.max(...scoreTypeNameMap.keys());
71
+ const typeName = scoreTypeNameMap.get(maxScore);
72
+ if (typeName) {
73
+ return typeName;
74
+ }
75
+ if (data.$response) {
76
+ const error = createGraphQLError(`HTTP Error: ${data.$statusCode}`, {
77
+ extensions: {
78
+ http: {
79
+ status: data.$statusCode,
80
+ headers: data.$response.header,
81
+ },
82
+ request: {
83
+ url: data.$url,
84
+ method: data.$method,
85
+ },
86
+ responseJson: data.$response,
87
+ },
88
+ });
89
+ return error;
90
+ }
91
+ /*
92
+ const error = new GraphQLError(`Received data doesn't met the union`, null, null, null, null, null, {
93
+ validationErrors,
94
+ });
95
+ return error;
96
+ */
97
+ };
98
+ }