@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.
- package/cjs/directives/dictionary.js +19 -0
- package/cjs/directives/discriminator.js +9 -0
- package/cjs/directives/flatten.js +16 -0
- package/cjs/directives/getTypeResolverForAbstractType.js +102 -0
- package/cjs/directives/httpOperation.js +404 -0
- package/cjs/directives/isFileUpload.js +7 -0
- package/cjs/directives/jsonApiFields.js +23 -0
- package/cjs/directives/length.js +26 -0
- package/cjs/directives/link.js +67 -0
- package/cjs/directives/process.js +181 -0
- package/cjs/directives/pubsubOperation.js +31 -0
- package/cjs/directives/regexp.js +24 -0
- package/cjs/directives/resolveDataByUnionInputType.js +39 -0
- package/cjs/directives/resolveRoot.js +10 -0
- package/cjs/directives/resolveRootField.js +29 -0
- package/cjs/directives/responseMetadata.js +16 -0
- package/cjs/directives/scalars.js +53 -0
- package/cjs/directives/typescriptAnnotations.js +8 -0
- package/cjs/index.js +20 -0
- package/cjs/package.json +1 -0
- package/esm/directives/dictionary.js +15 -0
- package/esm/directives/discriminator.js +5 -0
- package/esm/directives/flatten.js +12 -0
- package/esm/directives/getTypeResolverForAbstractType.js +98 -0
- package/esm/directives/httpOperation.js +399 -0
- package/esm/directives/isFileUpload.js +3 -0
- package/esm/directives/jsonApiFields.js +18 -0
- package/esm/directives/length.js +22 -0
- package/esm/directives/link.js +63 -0
- package/esm/directives/process.js +177 -0
- package/esm/directives/pubsubOperation.js +27 -0
- package/esm/directives/regexp.js +20 -0
- package/esm/directives/resolveDataByUnionInputType.js +35 -0
- package/esm/directives/resolveRoot.js +6 -0
- package/esm/directives/resolveRootField.js +25 -0
- package/esm/directives/responseMetadata.js +12 -0
- package/esm/directives/scalars.js +48 -0
- package/esm/directives/typescriptAnnotations.js +4 -0
- package/esm/index.js +15 -0
- package/package.json +56 -0
- package/typings/directives/dictionary.d.cts +2 -0
- package/typings/directives/dictionary.d.ts +2 -0
- package/typings/directives/discriminator.d.cts +5 -0
- package/typings/directives/discriminator.d.ts +5 -0
- package/typings/directives/flatten.d.cts +2 -0
- package/typings/directives/flatten.d.ts +2 -0
- package/typings/directives/getTypeResolverForAbstractType.d.cts +7 -0
- package/typings/directives/getTypeResolverForAbstractType.d.ts +7 -0
- package/typings/directives/httpOperation.d.cts +28 -0
- package/typings/directives/httpOperation.d.ts +28 -0
- package/typings/directives/isFileUpload.d.cts +4 -0
- package/typings/directives/isFileUpload.d.ts +4 -0
- package/typings/directives/jsonApiFields.d.cts +2 -0
- package/typings/directives/jsonApiFields.d.ts +2 -0
- package/typings/directives/length.d.cts +5 -0
- package/typings/directives/length.d.ts +5 -0
- package/typings/directives/link.d.cts +2 -0
- package/typings/directives/link.d.ts +2 -0
- package/typings/directives/process.d.cts +12 -0
- package/typings/directives/process.d.ts +12 -0
- package/typings/directives/pubsubOperation.d.cts +10 -0
- package/typings/directives/pubsubOperation.d.ts +10 -0
- package/typings/directives/regexp.d.cts +2 -0
- package/typings/directives/regexp.d.ts +2 -0
- package/typings/directives/resolveDataByUnionInputType.d.cts +2 -0
- package/typings/directives/resolveDataByUnionInputType.d.ts +2 -0
- package/typings/directives/resolveRoot.d.cts +2 -0
- package/typings/directives/resolveRoot.d.ts +2 -0
- package/typings/directives/resolveRootField.d.cts +2 -0
- package/typings/directives/resolveRootField.d.ts +2 -0
- package/typings/directives/responseMetadata.d.cts +2 -0
- package/typings/directives/responseMetadata.d.ts +2 -0
- package/typings/directives/scalars.d.cts +3 -0
- package/typings/directives/scalars.d.ts +3 -0
- package/typings/directives/typescriptAnnotations.d.cts +2 -0
- package/typings/directives/typescriptAnnotations.d.ts +2 -0
- package/typings/index.d.cts +7 -0
- package/typings/index.d.ts +7 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processDictionaryDirective = void 0;
|
|
4
|
+
function processDictionaryDirective(fieldMap, field) {
|
|
5
|
+
field.resolve = function dictionaryDirectiveHandler(root) {
|
|
6
|
+
const result = [];
|
|
7
|
+
for (const key in root) {
|
|
8
|
+
if (key in fieldMap) {
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
result.push({
|
|
12
|
+
key,
|
|
13
|
+
value: root[key],
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
exports.processDictionaryDirective = processDictionaryDirective;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processDiscriminatorAnnotations = void 0;
|
|
4
|
+
function processDiscriminatorAnnotations({ interfaceType, discriminatorFieldName, }) {
|
|
5
|
+
interfaceType.resolveType = function discriminatorDirectiveHandler(root) {
|
|
6
|
+
return root[discriminatorFieldName];
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
exports.processDiscriminatorAnnotations = processDiscriminatorAnnotations;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processFlattenAnnotations = void 0;
|
|
4
|
+
function processFlattenAnnotations(field) {
|
|
5
|
+
if (!field.resolve || field.resolve.name === 'defaultFieldResolver') {
|
|
6
|
+
const fieldName = field.name;
|
|
7
|
+
field.resolve = function flattenDirectiveHandler(root) {
|
|
8
|
+
let result = root[fieldName];
|
|
9
|
+
if (!Array.isArray(root)) {
|
|
10
|
+
result = [result];
|
|
11
|
+
}
|
|
12
|
+
return result.flat(Infinity);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.processFlattenAnnotations = processFlattenAnnotations;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTypeResolverForAbstractType = void 0;
|
|
4
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
5
|
+
function getTypeResolverForAbstractType({ possibleTypes, discriminatorField, discriminatorMapping, statusCodeTypeNameMap, }) {
|
|
6
|
+
return function resolveType(data, _ctx, info) {
|
|
7
|
+
if (data.__typename) {
|
|
8
|
+
return data.__typename;
|
|
9
|
+
}
|
|
10
|
+
else if (discriminatorField != null && data[discriminatorField]) {
|
|
11
|
+
const discriminatorValue = data[discriminatorField];
|
|
12
|
+
return discriminatorMapping?.[discriminatorValue] || discriminatorValue;
|
|
13
|
+
}
|
|
14
|
+
if (data.$statusCode && statusCodeTypeNameMap) {
|
|
15
|
+
const typeName = statusCodeTypeNameMap[data.$statusCode.toString()] || statusCodeTypeNameMap.default;
|
|
16
|
+
if (typeName) {
|
|
17
|
+
return typeName;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const dataTypeOf = typeof data;
|
|
21
|
+
if (dataTypeOf !== 'object') {
|
|
22
|
+
for (const possibleType of possibleTypes) {
|
|
23
|
+
const fieldMap = possibleType.getFields();
|
|
24
|
+
const fields = Object.values(fieldMap);
|
|
25
|
+
if (fields.length === 1) {
|
|
26
|
+
const field = fields[0];
|
|
27
|
+
const directiveObjs = (0, utils_1.getDirective)(info.schema, field, 'resolveRoot');
|
|
28
|
+
if (directiveObjs?.length) {
|
|
29
|
+
const fieldType = field.type;
|
|
30
|
+
if ('parseValue' in fieldType) {
|
|
31
|
+
try {
|
|
32
|
+
fieldType.parseValue(data);
|
|
33
|
+
return possibleType.name;
|
|
34
|
+
}
|
|
35
|
+
catch (e) { }
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// const validationErrors: Record<string, ErrorObject[]> = {};
|
|
42
|
+
const dataKeys = dataTypeOf
|
|
43
|
+
? Object.keys(data)
|
|
44
|
+
// Remove metadata fields used to pass data
|
|
45
|
+
.filter(property => !property.toString().startsWith('$'))
|
|
46
|
+
: null;
|
|
47
|
+
const scoreTypeNameMap = new Map();
|
|
48
|
+
for (const possibleType of possibleTypes) {
|
|
49
|
+
const typeName = possibleType.name;
|
|
50
|
+
if (dataKeys != null) {
|
|
51
|
+
const typeFields = Object.keys(possibleType.getFields());
|
|
52
|
+
if (dataKeys.length <= typeFields.length &&
|
|
53
|
+
dataKeys.every(property => typeFields.includes(property.toString()))) {
|
|
54
|
+
return typeName;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
const score = dataKeys.filter(property => typeFields.includes(property.toString())).length;
|
|
58
|
+
if (score || typeFields.includes('additionalProperties')) {
|
|
59
|
+
scoreTypeNameMap.set(score, typeName);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
} /* else {
|
|
63
|
+
const validateFn = possibleType.extensions.validateWithJSONSchema as ValidateFunction;
|
|
64
|
+
if (validateFn) {
|
|
65
|
+
const isValid = validateFn(data);
|
|
66
|
+
if (isValid) {
|
|
67
|
+
return typeName;
|
|
68
|
+
}
|
|
69
|
+
validationErrors[typeName] = ajv.errors || validateFn.errors;
|
|
70
|
+
}
|
|
71
|
+
} */
|
|
72
|
+
}
|
|
73
|
+
const maxScore = Math.max(...scoreTypeNameMap.keys());
|
|
74
|
+
const typeName = scoreTypeNameMap.get(maxScore);
|
|
75
|
+
if (typeName) {
|
|
76
|
+
return typeName;
|
|
77
|
+
}
|
|
78
|
+
if (data.$response) {
|
|
79
|
+
const error = (0, utils_1.createGraphQLError)(`HTTP Error: ${data.$statusCode}`, {
|
|
80
|
+
extensions: {
|
|
81
|
+
http: {
|
|
82
|
+
status: data.$statusCode,
|
|
83
|
+
headers: data.$response.header,
|
|
84
|
+
},
|
|
85
|
+
request: {
|
|
86
|
+
url: data.$url,
|
|
87
|
+
method: data.$method,
|
|
88
|
+
},
|
|
89
|
+
responseJson: data.$response,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
return error;
|
|
93
|
+
}
|
|
94
|
+
/*
|
|
95
|
+
const error = new GraphQLError(`Received data doesn't met the union`, null, null, null, null, null, {
|
|
96
|
+
validationErrors,
|
|
97
|
+
});
|
|
98
|
+
return error;
|
|
99
|
+
*/
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
exports.getTypeResolverForAbstractType = getTypeResolverForAbstractType;
|
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.addHTTPRootFieldResolver = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const dset_1 = require("dset");
|
|
6
|
+
const graphql_1 = require("graphql");
|
|
7
|
+
const qs_1 = require("qs");
|
|
8
|
+
const url_join_1 = tslib_1.__importDefault(require("url-join"));
|
|
9
|
+
const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
|
|
10
|
+
const string_interpolation_1 = require("@graphql-mesh/string-interpolation");
|
|
11
|
+
const utils_1 = require("@graphql-mesh/utils");
|
|
12
|
+
const utils_2 = require("@graphql-tools/utils");
|
|
13
|
+
const fetch_1 = require("@whatwg-node/fetch");
|
|
14
|
+
const isFileUpload_js_1 = require("./isFileUpload.js");
|
|
15
|
+
const jsonApiFields_js_1 = require("./jsonApiFields.js");
|
|
16
|
+
const resolveDataByUnionInputType_js_1 = require("./resolveDataByUnionInputType.js");
|
|
17
|
+
const isListTypeOrNonNullListType = (0, utils_2.memoize1)(function isListTypeOrNonNullListType(type) {
|
|
18
|
+
if ((0, graphql_1.isNonNullType)(type)) {
|
|
19
|
+
return (0, graphql_1.isListType)(type.ofType);
|
|
20
|
+
}
|
|
21
|
+
return (0, graphql_1.isListType)(type);
|
|
22
|
+
});
|
|
23
|
+
const defaultQsOptions = {
|
|
24
|
+
indices: false,
|
|
25
|
+
};
|
|
26
|
+
function addHTTPRootFieldResolver(schema, field, globalLogger = new utils_1.DefaultLogger('HTTP'), globalFetch, { path, operationSpecificHeaders, httpMethod, isBinary, requestBaseBody, queryParamArgMap, queryStringOptionsByParam, jsonApiFields, }, { sourceName, endpoint, timeout, operationHeaders: globalOperationHeaders, queryStringOptions: globalQueryStringOptions = {}, queryParams: globalQueryParams, }) {
|
|
27
|
+
globalQueryStringOptions = {
|
|
28
|
+
...defaultQsOptions,
|
|
29
|
+
...globalQueryStringOptions,
|
|
30
|
+
};
|
|
31
|
+
const returnNamedGraphQLType = (0, graphql_1.getNamedType)(field.type);
|
|
32
|
+
field.resolve = async (root, args, context, info) => {
|
|
33
|
+
if (jsonApiFields) {
|
|
34
|
+
args.fields = undefined;
|
|
35
|
+
}
|
|
36
|
+
const logger = context?.logger || globalLogger;
|
|
37
|
+
const operationLogger = logger.child(`${info.parentType.name}.${info.fieldName}`);
|
|
38
|
+
operationLogger.debug(`=> Resolving`);
|
|
39
|
+
const interpolationData = { root, args, context, env: cross_helpers_1.process.env };
|
|
40
|
+
const interpolatedBaseUrl = string_interpolation_1.stringInterpolator.parse(endpoint, interpolationData);
|
|
41
|
+
const interpolatedPath = string_interpolation_1.stringInterpolator.parse(path, interpolationData);
|
|
42
|
+
let fullPath = (0, url_join_1.default)(interpolatedBaseUrl, interpolatedPath);
|
|
43
|
+
const headers = {};
|
|
44
|
+
for (const headerName in globalOperationHeaders) {
|
|
45
|
+
const nonInterpolatedValue = globalOperationHeaders[headerName];
|
|
46
|
+
const interpolatedValue = string_interpolation_1.stringInterpolator.parse(nonInterpolatedValue, interpolationData);
|
|
47
|
+
if (interpolatedValue) {
|
|
48
|
+
headers[headerName.toLowerCase()] = interpolatedValue;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (operationSpecificHeaders) {
|
|
52
|
+
for (const headerName in operationSpecificHeaders) {
|
|
53
|
+
const nonInterpolatedValue = operationSpecificHeaders[headerName];
|
|
54
|
+
const interpolatedValue = string_interpolation_1.stringInterpolator.parse(nonInterpolatedValue, interpolationData);
|
|
55
|
+
if (interpolatedValue) {
|
|
56
|
+
headers[headerName.toLowerCase()] = interpolatedValue;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const requestInit = {
|
|
61
|
+
method: httpMethod,
|
|
62
|
+
headers,
|
|
63
|
+
};
|
|
64
|
+
if (timeout) {
|
|
65
|
+
requestInit.signal = AbortSignal.timeout(timeout);
|
|
66
|
+
}
|
|
67
|
+
// Handle binary data
|
|
68
|
+
if (isBinary) {
|
|
69
|
+
const binaryUpload = await args.input;
|
|
70
|
+
if ((0, isFileUpload_js_1.isFileUpload)(binaryUpload)) {
|
|
71
|
+
const readable = binaryUpload.createReadStream();
|
|
72
|
+
const chunks = [];
|
|
73
|
+
for await (const chunk of readable) {
|
|
74
|
+
for (const byte of chunk) {
|
|
75
|
+
chunks.push(byte);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
requestInit.body = new Uint8Array(chunks);
|
|
79
|
+
const [, contentType] = Object.entries(headers).find(([key]) => key.toLowerCase() === 'content-type') || [];
|
|
80
|
+
if (!contentType) {
|
|
81
|
+
headers['content-type'] = binaryUpload.mimetype;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
requestInit.body = binaryUpload;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
if (requestBaseBody != null) {
|
|
88
|
+
args.input = args.input || {};
|
|
89
|
+
for (const key in requestBaseBody) {
|
|
90
|
+
const configValue = requestBaseBody[key];
|
|
91
|
+
if (typeof configValue === 'string') {
|
|
92
|
+
const value = string_interpolation_1.stringInterpolator.parse(configValue, interpolationData);
|
|
93
|
+
(0, dset_1.dset)(args.input, key, value);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
args.input[key] = configValue;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// Resolve union input
|
|
101
|
+
const input = (args.input = (0, resolveDataByUnionInputType_js_1.resolveDataByUnionInputType)(args.input, field.args?.find(arg => arg.name === 'input')?.type, schema));
|
|
102
|
+
if (input != null) {
|
|
103
|
+
const [, contentType] = Object.entries(headers).find(([key]) => key.toLowerCase() === 'content-type') || [];
|
|
104
|
+
if (contentType?.startsWith('application/x-www-form-urlencoded')) {
|
|
105
|
+
requestInit.body = (0, qs_1.stringify)(input, globalQueryStringOptions);
|
|
106
|
+
}
|
|
107
|
+
else if (contentType?.startsWith('multipart/form-data')) {
|
|
108
|
+
delete headers['content-type'];
|
|
109
|
+
delete headers['Content-Type'];
|
|
110
|
+
const formData = new fetch_1.FormData();
|
|
111
|
+
for (const key in input) {
|
|
112
|
+
const inputValue = input[key];
|
|
113
|
+
if (inputValue != null) {
|
|
114
|
+
let formDataValue;
|
|
115
|
+
if (typeof inputValue === 'object') {
|
|
116
|
+
if (inputValue instanceof fetch_1.File) {
|
|
117
|
+
formDataValue = inputValue;
|
|
118
|
+
}
|
|
119
|
+
else if (inputValue.name && inputValue instanceof fetch_1.Blob) {
|
|
120
|
+
formDataValue = new fetch_1.File([inputValue], inputValue.name, {
|
|
121
|
+
type: inputValue.type,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
else if (inputValue.arrayBuffer) {
|
|
125
|
+
const arrayBuffer = await inputValue.arrayBuffer();
|
|
126
|
+
if (inputValue.name) {
|
|
127
|
+
formDataValue = new fetch_1.File([arrayBuffer], inputValue.name, {
|
|
128
|
+
type: inputValue.type,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
formDataValue = new fetch_1.Blob([arrayBuffer], { type: inputValue.type });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
formDataValue = JSON.stringify(inputValue);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
formDataValue = inputValue.toString();
|
|
141
|
+
}
|
|
142
|
+
formData.append(key, formDataValue);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
requestInit.body = formData;
|
|
146
|
+
}
|
|
147
|
+
else if (contentType?.startsWith('text/plain')) {
|
|
148
|
+
requestInit.body = input;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
requestInit.body = JSON.stringify(input);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (globalQueryParams) {
|
|
156
|
+
for (const queryParamName in globalQueryParams) {
|
|
157
|
+
if (queryParamArgMap != null &&
|
|
158
|
+
queryParamName in queryParamArgMap &&
|
|
159
|
+
queryParamArgMap[queryParamName] in args) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
const interpolatedQueryParam = string_interpolation_1.stringInterpolator.parse(globalQueryParams[queryParamName].toString(), interpolationData);
|
|
163
|
+
const queryParamsString = (0, qs_1.stringify)({
|
|
164
|
+
[queryParamName]: interpolatedQueryParam,
|
|
165
|
+
}, {
|
|
166
|
+
...globalQueryStringOptions,
|
|
167
|
+
...queryStringOptionsByParam?.[queryParamName],
|
|
168
|
+
});
|
|
169
|
+
fullPath += fullPath.includes('?') ? '&' : '?';
|
|
170
|
+
fullPath += queryParamsString;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (queryParamArgMap) {
|
|
174
|
+
for (const queryParamName in queryParamArgMap) {
|
|
175
|
+
const argName = queryParamArgMap[queryParamName];
|
|
176
|
+
let argValue = args[argName];
|
|
177
|
+
if (argValue != null) {
|
|
178
|
+
// Somehow it doesn't serialize URLs so we need to do it manually.
|
|
179
|
+
if (argValue instanceof URL) {
|
|
180
|
+
argValue = argValue.toString();
|
|
181
|
+
}
|
|
182
|
+
const opts = {
|
|
183
|
+
...globalQueryStringOptions,
|
|
184
|
+
...queryStringOptionsByParam?.[queryParamName],
|
|
185
|
+
};
|
|
186
|
+
let queryParamObj = argValue;
|
|
187
|
+
if (Array.isArray(argValue) || !(typeof argValue === 'object' && opts.destructObject)) {
|
|
188
|
+
queryParamObj = {
|
|
189
|
+
[queryParamName]: argValue,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
queryParamObj = (0, resolveDataByUnionInputType_js_1.resolveDataByUnionInputType)(queryParamObj, field.args?.find(arg => arg.name === argName)?.type, schema);
|
|
194
|
+
}
|
|
195
|
+
const queryParamsString = (0, qs_1.stringify)(queryParamObj, opts);
|
|
196
|
+
fullPath += fullPath.includes('?') ? '&' : '?';
|
|
197
|
+
fullPath += queryParamsString;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (jsonApiFields) {
|
|
202
|
+
fullPath += fullPath.includes('?') ? '&' : '?';
|
|
203
|
+
fullPath += `fields=${(0, jsonApiFields_js_1.getJsonApiFieldsQuery)(info)}`;
|
|
204
|
+
}
|
|
205
|
+
operationLogger.debug(`=> Fetching `, fullPath, `=>`, requestInit);
|
|
206
|
+
const fetch = context?.fetch || globalFetch;
|
|
207
|
+
if (!fetch) {
|
|
208
|
+
return (0, utils_2.createGraphQLError)(`You should have fetch defined in either the config or the context!`, {
|
|
209
|
+
extensions: {
|
|
210
|
+
request: {
|
|
211
|
+
url: fullPath,
|
|
212
|
+
method: httpMethod,
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
// Trick to pass `sourceName` to the `fetch` function for tracing
|
|
218
|
+
const response = await fetch(fullPath, requestInit, context, {
|
|
219
|
+
...info,
|
|
220
|
+
sourceName,
|
|
221
|
+
});
|
|
222
|
+
// If return type is a file
|
|
223
|
+
if (returnNamedGraphQLType.name === 'File') {
|
|
224
|
+
return response.blob();
|
|
225
|
+
}
|
|
226
|
+
const responseText = await response.text();
|
|
227
|
+
operationLogger.debug(`=> Received`, {
|
|
228
|
+
headers: response.headers,
|
|
229
|
+
text: responseText,
|
|
230
|
+
});
|
|
231
|
+
let responseJson;
|
|
232
|
+
try {
|
|
233
|
+
responseJson = JSON.parse(responseText);
|
|
234
|
+
}
|
|
235
|
+
catch (error) {
|
|
236
|
+
// The result might be defined as scalar
|
|
237
|
+
if ((0, graphql_1.isScalarType)(returnNamedGraphQLType)) {
|
|
238
|
+
operationLogger.debug(` => Return type is not a JSON so returning ${responseText}`);
|
|
239
|
+
return responseText;
|
|
240
|
+
}
|
|
241
|
+
else if (response.status === 204 || (response.status === 200 && responseText === '')) {
|
|
242
|
+
responseJson = {};
|
|
243
|
+
}
|
|
244
|
+
else if (response.status.toString().startsWith('2')) {
|
|
245
|
+
logger.debug(`Unexpected response in ${field.name};\n\t${responseText}`);
|
|
246
|
+
return (0, utils_2.createGraphQLError)(`Unexpected response in ${field.name}`, {
|
|
247
|
+
extensions: {
|
|
248
|
+
http: {
|
|
249
|
+
status: response.status,
|
|
250
|
+
statusText: response.statusText,
|
|
251
|
+
headers: (0, utils_1.getHeadersObj)(response.headers),
|
|
252
|
+
},
|
|
253
|
+
request: {
|
|
254
|
+
url: fullPath,
|
|
255
|
+
method: httpMethod,
|
|
256
|
+
},
|
|
257
|
+
responseText,
|
|
258
|
+
originalError: {
|
|
259
|
+
message: error.message,
|
|
260
|
+
stack: error.stack,
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
return (0, utils_2.createGraphQLError)(`HTTP Error: ${response.status}, Could not invoke operation ${httpMethod} ${path}`, {
|
|
267
|
+
extensions: {
|
|
268
|
+
request: {
|
|
269
|
+
url: fullPath,
|
|
270
|
+
method: httpMethod,
|
|
271
|
+
},
|
|
272
|
+
responseText,
|
|
273
|
+
responseStatus: response.status,
|
|
274
|
+
responseStatusText: response.statusText,
|
|
275
|
+
responseHeaders: (0, utils_1.getHeadersObj)(response.headers),
|
|
276
|
+
},
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (!response.status.toString().startsWith('2')) {
|
|
281
|
+
if (!(0, graphql_1.isUnionType)(returnNamedGraphQLType)) {
|
|
282
|
+
return (0, utils_2.createGraphQLError)(`HTTP Error: ${response.status}, Could not invoke operation ${httpMethod} ${path}`, {
|
|
283
|
+
extensions: {
|
|
284
|
+
http: {
|
|
285
|
+
status: response.status,
|
|
286
|
+
statusText: response.statusText,
|
|
287
|
+
headers: (0, utils_1.getHeadersObj)(response.headers),
|
|
288
|
+
},
|
|
289
|
+
request: {
|
|
290
|
+
url: fullPath,
|
|
291
|
+
method: httpMethod,
|
|
292
|
+
},
|
|
293
|
+
responseJson,
|
|
294
|
+
},
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
operationLogger.debug(`Returning `, responseJson);
|
|
299
|
+
// Sometimes API returns an array but the return type is not an array
|
|
300
|
+
const isListReturnType = isListTypeOrNonNullListType(field.type);
|
|
301
|
+
const isArrayResponse = Array.isArray(responseJson);
|
|
302
|
+
if (isListReturnType && !isArrayResponse) {
|
|
303
|
+
operationLogger.debug(`Response is not array but return type is list. Normalizing the response`);
|
|
304
|
+
responseJson = [responseJson];
|
|
305
|
+
}
|
|
306
|
+
if (!isListReturnType && isArrayResponse) {
|
|
307
|
+
operationLogger.debug(`Response is array but return type is not list. Normalizing the response`);
|
|
308
|
+
responseJson = responseJson[0];
|
|
309
|
+
}
|
|
310
|
+
const addResponseMetadata = (obj) => {
|
|
311
|
+
if (typeof obj !== 'object') {
|
|
312
|
+
return obj;
|
|
313
|
+
}
|
|
314
|
+
Object.defineProperties(obj, {
|
|
315
|
+
$field: {
|
|
316
|
+
get() {
|
|
317
|
+
return field.name;
|
|
318
|
+
},
|
|
319
|
+
},
|
|
320
|
+
$url: {
|
|
321
|
+
get() {
|
|
322
|
+
return fullPath.split('?')[0];
|
|
323
|
+
},
|
|
324
|
+
},
|
|
325
|
+
$method: {
|
|
326
|
+
get() {
|
|
327
|
+
return httpMethod;
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
$statusCode: {
|
|
331
|
+
get() {
|
|
332
|
+
return response.status;
|
|
333
|
+
},
|
|
334
|
+
},
|
|
335
|
+
$statusText: {
|
|
336
|
+
get() {
|
|
337
|
+
return response.statusText;
|
|
338
|
+
},
|
|
339
|
+
},
|
|
340
|
+
$headers: {
|
|
341
|
+
get() {
|
|
342
|
+
return requestInit.headers;
|
|
343
|
+
},
|
|
344
|
+
},
|
|
345
|
+
$request: {
|
|
346
|
+
get() {
|
|
347
|
+
return new Proxy({}, {
|
|
348
|
+
get(_, requestProp) {
|
|
349
|
+
switch (requestProp) {
|
|
350
|
+
case 'query':
|
|
351
|
+
return (0, qs_1.parse)(fullPath.split('?')[1]);
|
|
352
|
+
case 'path':
|
|
353
|
+
return new Proxy(args, {
|
|
354
|
+
get(_, prop) {
|
|
355
|
+
return args[prop] || args.input?.[prop] || obj?.[prop];
|
|
356
|
+
},
|
|
357
|
+
has(_, prop) {
|
|
358
|
+
return prop in args || (args.input && prop in args.input) || obj?.[prop];
|
|
359
|
+
},
|
|
360
|
+
});
|
|
361
|
+
case 'header':
|
|
362
|
+
return requestInit.headers;
|
|
363
|
+
case 'body':
|
|
364
|
+
return requestInit.body;
|
|
365
|
+
}
|
|
366
|
+
},
|
|
367
|
+
});
|
|
368
|
+
},
|
|
369
|
+
},
|
|
370
|
+
$response: {
|
|
371
|
+
get() {
|
|
372
|
+
return new Proxy({}, {
|
|
373
|
+
get(_, responseProp) {
|
|
374
|
+
switch (responseProp) {
|
|
375
|
+
case 'header':
|
|
376
|
+
return (0, utils_1.getHeadersObj)(response.headers);
|
|
377
|
+
case 'body':
|
|
378
|
+
return obj;
|
|
379
|
+
case 'query':
|
|
380
|
+
return (0, qs_1.parse)(fullPath.split('?')[1]);
|
|
381
|
+
case 'path':
|
|
382
|
+
return new Proxy(args, {
|
|
383
|
+
get(_, prop) {
|
|
384
|
+
return args[prop] || args.input?.[prop] || obj?.[prop];
|
|
385
|
+
},
|
|
386
|
+
has(_, prop) {
|
|
387
|
+
return prop in args || (args.input && prop in args.input) || obj?.[prop];
|
|
388
|
+
},
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
},
|
|
392
|
+
});
|
|
393
|
+
},
|
|
394
|
+
},
|
|
395
|
+
});
|
|
396
|
+
return obj;
|
|
397
|
+
};
|
|
398
|
+
operationLogger.debug(`Adding response metadata to the response object`);
|
|
399
|
+
return Array.isArray(responseJson)
|
|
400
|
+
? responseJson.map(obj => addResponseMetadata(obj))
|
|
401
|
+
: addResponseMetadata(responseJson);
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
exports.addHTTPRootFieldResolver = addHTTPRootFieldResolver;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getJsonApiFieldsQuery = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const graphql_fields_1 = tslib_1.__importDefault(require("graphql-fields"));
|
|
6
|
+
function handleFields(fields) {
|
|
7
|
+
const fieldsStrsArray = [];
|
|
8
|
+
for (const fieldName in fields) {
|
|
9
|
+
const subFields = fields[fieldName];
|
|
10
|
+
const subFieldsStr = handleFields(subFields);
|
|
11
|
+
let fieldStr = fieldName;
|
|
12
|
+
if (subFieldsStr) {
|
|
13
|
+
fieldStr += `(${subFieldsStr})`;
|
|
14
|
+
}
|
|
15
|
+
fieldsStrsArray.push(fieldStr);
|
|
16
|
+
}
|
|
17
|
+
return fieldsStrsArray.join(',');
|
|
18
|
+
}
|
|
19
|
+
function getJsonApiFieldsQuery(info) {
|
|
20
|
+
const fields = (0, graphql_fields_1.default)(info);
|
|
21
|
+
return handleFields(fields);
|
|
22
|
+
}
|
|
23
|
+
exports.getJsonApiFieldsQuery = getJsonApiFieldsQuery;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processLengthAnnotations = void 0;
|
|
4
|
+
function processLengthAnnotations(scalar, { min: minLength, max: maxLength, }) {
|
|
5
|
+
function coerceString(value) {
|
|
6
|
+
if (value != null) {
|
|
7
|
+
const vStr = value.toString();
|
|
8
|
+
if (typeof minLength !== 'undefined' && vStr.length < minLength) {
|
|
9
|
+
throw new Error(`${scalar.name} cannot be less than ${minLength} but given ${vStr}`);
|
|
10
|
+
}
|
|
11
|
+
if (typeof maxLength !== 'undefined' && vStr.length > maxLength) {
|
|
12
|
+
throw new Error(`${scalar.name} cannot be more than ${maxLength} but given ${vStr}`);
|
|
13
|
+
}
|
|
14
|
+
return vStr;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
scalar.serialize = coerceString;
|
|
18
|
+
scalar.parseValue = coerceString;
|
|
19
|
+
scalar.parseLiteral = ast => {
|
|
20
|
+
if ('value' in ast) {
|
|
21
|
+
return coerceString(ast.value);
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
exports.processLengthAnnotations = processLengthAnnotations;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processLinkFieldAnnotations = void 0;
|
|
4
|
+
const dset_1 = require("dset");
|
|
5
|
+
const string_interpolation_1 = require("@graphql-mesh/string-interpolation");
|
|
6
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
7
|
+
function processLinkFieldAnnotations(field, defaultRootTypeName, defaultFieldName) {
|
|
8
|
+
field.resolve = function linkDirectiveHandler(root, args, context, info) {
|
|
9
|
+
const linkResolverMap = findLinkResolverMap({
|
|
10
|
+
schema: info.schema,
|
|
11
|
+
defaultRootTypeName,
|
|
12
|
+
defaultFieldName,
|
|
13
|
+
parentFieldName: root.$field,
|
|
14
|
+
operationType: info.operation.operation,
|
|
15
|
+
});
|
|
16
|
+
const linkResolverOpts = linkResolverMap[field.name];
|
|
17
|
+
return linkResolver(linkResolverOpts, { root, args, context, info, env: process.env });
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
exports.processLinkFieldAnnotations = processLinkFieldAnnotations;
|
|
21
|
+
function findLinkResolverMap({ schema, operationType, defaultRootTypeName, defaultFieldName, }) {
|
|
22
|
+
const parentType = schema.getRootType(operationType);
|
|
23
|
+
const parentField = parentType.getFields()[operationType];
|
|
24
|
+
if (parentField) {
|
|
25
|
+
const linkResolverMap = getLinkResolverMap(schema, parentField);
|
|
26
|
+
if (linkResolverMap) {
|
|
27
|
+
return linkResolverMap;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const defaultRootType = schema.getType(defaultRootTypeName);
|
|
31
|
+
if (defaultRootType) {
|
|
32
|
+
const defaultField = defaultRootType.getFields()[defaultFieldName];
|
|
33
|
+
if (defaultField) {
|
|
34
|
+
const linkResolverMap = getLinkResolverMap(schema, defaultField);
|
|
35
|
+
if (linkResolverMap) {
|
|
36
|
+
return linkResolverMap;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function getLinkResolverMap(schema, field) {
|
|
42
|
+
const parentFieldLinkResolverDirectives = (0, utils_1.getDirective)(schema, field, 'linkResolver');
|
|
43
|
+
if (parentFieldLinkResolverDirectives?.length) {
|
|
44
|
+
const linkResolverMap = parentFieldLinkResolverDirectives[0].linkResolverMap;
|
|
45
|
+
if (linkResolverMap) {
|
|
46
|
+
return linkResolverMap;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function linkResolver({ linkObjArgs, targetTypeName, targetFieldName }, { root, args, context, info, env }) {
|
|
51
|
+
for (const argKey in linkObjArgs) {
|
|
52
|
+
const argInterpolation = linkObjArgs[argKey];
|
|
53
|
+
const actualValue = typeof argInterpolation === 'string'
|
|
54
|
+
? string_interpolation_1.stringInterpolator.parse(argInterpolation, {
|
|
55
|
+
root,
|
|
56
|
+
args,
|
|
57
|
+
context,
|
|
58
|
+
info,
|
|
59
|
+
env,
|
|
60
|
+
})
|
|
61
|
+
: argInterpolation;
|
|
62
|
+
(0, dset_1.dset)(args, argKey, actualValue);
|
|
63
|
+
}
|
|
64
|
+
const type = info.schema.getType(targetTypeName);
|
|
65
|
+
const field = type.getFields()[targetFieldName];
|
|
66
|
+
return field.resolve(root, args, context, info);
|
|
67
|
+
}
|