@graphql-mesh/utils 1.0.0-alpha-3fc47d119.0 → 1.0.0-alpha-20230420181317-a95037648
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/apply-transforms.js +35 -0
- package/cjs/defaultImportFn.js +57 -0
- package/cjs/extract-resolvers.js +23 -0
- package/cjs/fileURLToPath.js +50 -0
- package/cjs/fs-operations.js +65 -0
- package/cjs/getHeadersObj.js +61 -0
- package/cjs/global-lru-cache.js +9 -0
- package/cjs/group-transforms.js +17 -0
- package/cjs/index.js +20 -0
- package/cjs/load-from-module-export-expression.js +29 -0
- package/cjs/logger.js +127 -0
- package/cjs/package.json +1 -0
- package/cjs/parseAndPrintWithCache.js +42 -0
- package/cjs/pubsub.js +57 -0
- package/cjs/read-file-or-url.js +132 -0
- package/cjs/resolve-additional-resolvers.js +350 -0
- package/cjs/sanitize-name-for-graphql.js +96 -0
- package/cjs/with-cancel.js +34 -0
- package/cjs/with-cookies.js +18 -0
- package/cjs/with-filter.js +53 -0
- package/esm/apply-transforms.js +29 -0
- package/esm/defaultImportFn.js +32 -0
- package/esm/extract-resolvers.js +19 -0
- package/esm/fileURLToPath.js +46 -0
- package/esm/fs-operations.js +57 -0
- package/esm/getHeadersObj.js +57 -0
- package/esm/global-lru-cache.js +4 -0
- package/esm/group-transforms.js +13 -0
- package/esm/index.js +17 -0
- package/esm/load-from-module-export-expression.js +25 -0
- package/esm/logger.js +118 -0
- package/esm/parseAndPrintWithCache.js +37 -0
- package/esm/pubsub.js +53 -0
- package/esm/read-file-or-url.js +124 -0
- package/esm/resolve-additional-resolvers.js +344 -0
- package/esm/sanitize-name-for-graphql.js +91 -0
- package/esm/with-cancel.js +30 -0
- package/esm/with-cookies.js +14 -0
- package/esm/with-filter.js +49 -0
- package/package.json +28 -23
- package/typings/apply-transforms.d.cts +6 -0
- package/{apply-transforms.d.ts → typings/apply-transforms.d.ts} +3 -3
- package/typings/defaultImportFn.d.cts +2 -0
- package/typings/defaultImportFn.d.ts +2 -0
- package/typings/extract-resolvers.d.ts +2 -0
- package/typings/fileURLToPath.d.ts +1 -0
- package/typings/fs-operations.d.ts +7 -0
- package/typings/getHeadersObj.d.cts +1 -0
- package/typings/getHeadersObj.d.ts +1 -0
- package/typings/global-lru-cache.d.cts +3 -0
- package/{global-lru-cache.d.ts → typings/global-lru-cache.d.ts} +2 -2
- package/typings/group-transforms.d.ts +5 -0
- package/typings/index.d.cts +17 -0
- package/typings/index.d.ts +17 -0
- package/typings/load-from-module-export-expression.d.cts +8 -0
- package/{load-from-module-export-expression.d.ts → typings/load-from-module-export-expression.d.ts} +1 -1
- package/typings/logger.d.cts +22 -0
- package/{logger.d.ts → typings/logger.d.ts} +1 -1
- package/typings/parseAndPrintWithCache.d.ts +4 -0
- package/typings/pubsub.d.cts +13 -0
- package/{pubsub.d.ts → typings/pubsub.d.ts} +3 -2
- package/typings/read-file-or-url.d.cts +14 -0
- package/{read-file-or-url.d.ts → typings/read-file-or-url.d.ts} +3 -3
- package/typings/resolve-additional-resolvers.d.cts +4 -0
- package/{resolve-additional-resolvers.d.ts → typings/resolve-additional-resolvers.d.ts} +1 -1
- package/typings/sanitize-name-for-graphql.d.cts +2 -0
- package/{sanitize-name-for-graphql.d.ts → typings/sanitize-name-for-graphql.d.ts} +1 -0
- package/typings/with-cancel.d.cts +1 -0
- package/typings/with-cancel.d.ts +1 -0
- package/typings/with-cookies.d.cts +3 -0
- package/typings/with-cookies.d.ts +3 -0
- package/typings/with-filter.d.cts +4 -0
- package/typings/with-filter.d.ts +4 -0
- package/defaultImportFn.d.ts +0 -1
- package/getHeadersObj.d.ts +0 -1
- package/index.d.ts +0 -16
- package/index.js +0 -1112
- package/index.mjs +0 -1054
- package/with-cancel.d.ts +0 -1
- package/with-filter.d.ts +0 -4
- /package/{extract-resolvers.d.ts → typings/extract-resolvers.d.cts} +0 -0
- /package/{fileURLToPath.d.ts → typings/fileURLToPath.d.cts} +0 -0
- /package/{fs-operations.d.ts → typings/fs-operations.d.cts} +0 -0
- /package/{group-transforms.d.ts → typings/group-transforms.d.cts} +0 -0
- /package/{parseAndPrintWithCache.d.ts → typings/parseAndPrintWithCache.d.cts} +0 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readUrl = exports.readFile = exports.loadYaml = exports.readFileOrUrl = exports.isUrl = void 0;
|
|
4
|
+
const js_yaml_1 = require("js-yaml");
|
|
5
|
+
const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
|
|
6
|
+
const load_from_module_export_expression_js_1 = require("./load-from-module-export-expression.js");
|
|
7
|
+
function isUrl(str) {
|
|
8
|
+
return /^https?:\/\//.test(str);
|
|
9
|
+
}
|
|
10
|
+
exports.isUrl = isUrl;
|
|
11
|
+
async function readFileOrUrl(filePathOrUrl, config) {
|
|
12
|
+
if (isUrl(filePathOrUrl)) {
|
|
13
|
+
config.logger.debug(`Fetching ${filePathOrUrl} via HTTP`);
|
|
14
|
+
return readUrl(filePathOrUrl, config);
|
|
15
|
+
}
|
|
16
|
+
else if (filePathOrUrl.startsWith('{') || filePathOrUrl.startsWith('[')) {
|
|
17
|
+
return JSON.parse(filePathOrUrl);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
config.logger.debug(`Reading ${filePathOrUrl} from the file system`);
|
|
21
|
+
return readFile(filePathOrUrl, config);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.readFileOrUrl = readFileOrUrl;
|
|
25
|
+
function getSchema(filepath, logger) {
|
|
26
|
+
return js_yaml_1.DEFAULT_SCHEMA.extend([
|
|
27
|
+
new js_yaml_1.Type('!include', {
|
|
28
|
+
kind: 'scalar',
|
|
29
|
+
resolve(path) {
|
|
30
|
+
return typeof path === 'string';
|
|
31
|
+
},
|
|
32
|
+
construct(path) {
|
|
33
|
+
const newCwd = cross_helpers_1.path.dirname(filepath);
|
|
34
|
+
const absoluteFilePath = cross_helpers_1.path.isAbsolute(path)
|
|
35
|
+
? path
|
|
36
|
+
: cross_helpers_1.path.resolve(newCwd, path);
|
|
37
|
+
const content = cross_helpers_1.fs.readFileSync(absoluteFilePath, 'utf8');
|
|
38
|
+
return loadYaml(absoluteFilePath, content, logger);
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
new js_yaml_1.Type('!includes', {
|
|
42
|
+
kind: 'scalar',
|
|
43
|
+
resolve(path) {
|
|
44
|
+
return typeof path === 'string';
|
|
45
|
+
},
|
|
46
|
+
construct(path) {
|
|
47
|
+
const newCwd = cross_helpers_1.path.dirname(filepath);
|
|
48
|
+
const absoluteDirPath = cross_helpers_1.path.isAbsolute(path)
|
|
49
|
+
? path
|
|
50
|
+
: cross_helpers_1.path.resolve(newCwd, path);
|
|
51
|
+
const files = cross_helpers_1.fs.readdirSync(absoluteDirPath);
|
|
52
|
+
return files.map(filePath => {
|
|
53
|
+
const absoluteFilePath = cross_helpers_1.path.resolve(absoluteDirPath, filePath);
|
|
54
|
+
const fileContent = cross_helpers_1.fs.readFileSync(absoluteFilePath, 'utf8');
|
|
55
|
+
return loadYaml(absoluteFilePath, fileContent, logger);
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
}),
|
|
59
|
+
]);
|
|
60
|
+
}
|
|
61
|
+
function loadYaml(filepath, content, logger) {
|
|
62
|
+
return (0, js_yaml_1.load)(content, {
|
|
63
|
+
filename: filepath,
|
|
64
|
+
schema: getSchema(filepath, logger),
|
|
65
|
+
onWarning(warning) {
|
|
66
|
+
logger.warn(`${filepath}: ${warning.message}\n${warning.stack}`);
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
exports.loadYaml = loadYaml;
|
|
71
|
+
async function readFile(fileExpression, { allowUnknownExtensions, cwd, fallbackFormat, importFn, logger }) {
|
|
72
|
+
const [filePath] = fileExpression.split('#');
|
|
73
|
+
if (/js$/.test(filePath) || /ts$/.test(filePath)) {
|
|
74
|
+
return (0, load_from_module_export_expression_js_1.loadFromModuleExportExpression)(fileExpression, {
|
|
75
|
+
cwd,
|
|
76
|
+
importFn,
|
|
77
|
+
defaultExportName: 'default',
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
const actualPath = cross_helpers_1.path.isAbsolute(filePath) ? filePath : cross_helpers_1.path.join(cwd, filePath);
|
|
81
|
+
const rawResult = await cross_helpers_1.fs.promises.readFile(actualPath, 'utf-8');
|
|
82
|
+
if (/json$/.test(actualPath)) {
|
|
83
|
+
return JSON.parse(rawResult);
|
|
84
|
+
}
|
|
85
|
+
if (/yaml$/.test(actualPath) || /yml$/.test(actualPath)) {
|
|
86
|
+
return loadYaml(actualPath, rawResult, logger);
|
|
87
|
+
}
|
|
88
|
+
else if (fallbackFormat) {
|
|
89
|
+
switch (fallbackFormat) {
|
|
90
|
+
case 'json':
|
|
91
|
+
return JSON.parse(rawResult);
|
|
92
|
+
case 'yaml':
|
|
93
|
+
return loadYaml(actualPath, rawResult, logger);
|
|
94
|
+
case 'ts':
|
|
95
|
+
case 'js':
|
|
96
|
+
return importFn(actualPath);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else if (!allowUnknownExtensions) {
|
|
100
|
+
throw new Error(`Failed to parse JSON/YAML. Ensure file '${filePath}' has ` +
|
|
101
|
+
`the correct extension (i.e. '.json', '.yaml', or '.yml).`);
|
|
102
|
+
}
|
|
103
|
+
return rawResult;
|
|
104
|
+
}
|
|
105
|
+
exports.readFile = readFile;
|
|
106
|
+
async function readUrl(path, config) {
|
|
107
|
+
var _a, _b;
|
|
108
|
+
const { allowUnknownExtensions, fallbackFormat } = config || {};
|
|
109
|
+
config.headers = config.headers || {};
|
|
110
|
+
const response = await config.fetch(path, config);
|
|
111
|
+
const contentType = ((_a = response.headers) === null || _a === void 0 ? void 0 : _a.get('content-type')) || '';
|
|
112
|
+
const responseText = await response.text();
|
|
113
|
+
(_b = config === null || config === void 0 ? void 0 : config.logger) === null || _b === void 0 ? void 0 : _b.debug(`${path} returned `, responseText);
|
|
114
|
+
if (/json$/.test(path) ||
|
|
115
|
+
contentType.startsWith('application/json') ||
|
|
116
|
+
fallbackFormat === 'json') {
|
|
117
|
+
return JSON.parse(responseText);
|
|
118
|
+
}
|
|
119
|
+
else if (/yaml$/.test(path) ||
|
|
120
|
+
/yml$/.test(path) ||
|
|
121
|
+
contentType.includes('yaml') ||
|
|
122
|
+
contentType.includes('yml') ||
|
|
123
|
+
fallbackFormat === 'yaml') {
|
|
124
|
+
return loadYaml(path, responseText, config === null || config === void 0 ? void 0 : config.logger);
|
|
125
|
+
}
|
|
126
|
+
else if (!allowUnknownExtensions) {
|
|
127
|
+
throw new Error(`Failed to parse JSON/YAML. Ensure URL '${path}' has ` +
|
|
128
|
+
`the correct extension (i.e. '.json', '.yaml', or '.yml) or mime type in the response headers.`);
|
|
129
|
+
}
|
|
130
|
+
return responseText;
|
|
131
|
+
}
|
|
132
|
+
exports.readUrl = readUrl;
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveAdditionalResolvers = exports.resolveAdditionalResolversWithoutImport = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const dset_1 = require("dset");
|
|
6
|
+
const graphql_1 = require("graphql");
|
|
7
|
+
const lodash_get_1 = tslib_1.__importDefault(require("lodash.get"));
|
|
8
|
+
const lodash_topath_1 = tslib_1.__importDefault(require("lodash.topath"));
|
|
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-tools/utils");
|
|
12
|
+
const load_from_module_export_expression_js_1 = require("./load-from-module-export-expression.js");
|
|
13
|
+
const with_filter_js_1 = require("./with-filter.js");
|
|
14
|
+
function getTypeByPath(type, path) {
|
|
15
|
+
if ('ofType' in type) {
|
|
16
|
+
return getTypeByPath((0, graphql_1.getNamedType)(type), path);
|
|
17
|
+
}
|
|
18
|
+
if (path.length === 0) {
|
|
19
|
+
return (0, graphql_1.getNamedType)(type);
|
|
20
|
+
}
|
|
21
|
+
if (!('getFields' in type)) {
|
|
22
|
+
throw new Error(`${type} cannot have a path ${path.join('.')}`);
|
|
23
|
+
}
|
|
24
|
+
const fieldMap = type.getFields();
|
|
25
|
+
const currentFieldName = path[0];
|
|
26
|
+
// Might be an index of an array
|
|
27
|
+
if (!Number.isNaN(parseInt(currentFieldName))) {
|
|
28
|
+
return getTypeByPath(type, path.slice(1));
|
|
29
|
+
}
|
|
30
|
+
const field = fieldMap[currentFieldName];
|
|
31
|
+
if (!(field === null || field === void 0 ? void 0 : field.type)) {
|
|
32
|
+
throw new Error(`${type}.${currentFieldName} is not a valid field.`);
|
|
33
|
+
}
|
|
34
|
+
return getTypeByPath(field.type, path.slice(1));
|
|
35
|
+
}
|
|
36
|
+
function generateSelectionSetFactory(schema, additionalResolver) {
|
|
37
|
+
if (additionalResolver.sourceSelectionSet) {
|
|
38
|
+
return () => (0, utils_1.parseSelectionSet)(additionalResolver.sourceSelectionSet);
|
|
39
|
+
// If result path provided without a selectionSet
|
|
40
|
+
}
|
|
41
|
+
else if (additionalResolver.result) {
|
|
42
|
+
const resultPath = (0, lodash_topath_1.default)(additionalResolver.result);
|
|
43
|
+
let abstractResultTypeName;
|
|
44
|
+
const sourceType = schema.getType(additionalResolver.sourceTypeName);
|
|
45
|
+
const sourceTypeFields = sourceType.getFields();
|
|
46
|
+
const sourceField = sourceTypeFields[additionalResolver.sourceFieldName];
|
|
47
|
+
const resultFieldType = getTypeByPath(sourceField.type, resultPath);
|
|
48
|
+
if ((0, graphql_1.isAbstractType)(resultFieldType)) {
|
|
49
|
+
if (additionalResolver.resultType) {
|
|
50
|
+
abstractResultTypeName = additionalResolver.resultType;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const targetType = schema.getType(additionalResolver.targetTypeName);
|
|
54
|
+
const targetTypeFields = targetType.getFields();
|
|
55
|
+
const targetField = targetTypeFields[additionalResolver.targetFieldName];
|
|
56
|
+
const targetFieldType = (0, graphql_1.getNamedType)(targetField.type);
|
|
57
|
+
abstractResultTypeName = targetFieldType === null || targetFieldType === void 0 ? void 0 : targetFieldType.name;
|
|
58
|
+
}
|
|
59
|
+
if (abstractResultTypeName !== resultFieldType.name) {
|
|
60
|
+
const abstractResultType = schema.getType(abstractResultTypeName);
|
|
61
|
+
if (((0, graphql_1.isInterfaceType)(abstractResultType) || (0, graphql_1.isObjectType)(abstractResultType)) &&
|
|
62
|
+
!schema.isSubType(resultFieldType, abstractResultType)) {
|
|
63
|
+
throw new Error(`${additionalResolver.sourceTypeName}.${additionalResolver.sourceFieldName}.${resultPath.join('.')} doesn't implement ${abstractResultTypeName}.}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return (subtree) => {
|
|
68
|
+
let finalSelectionSet = subtree;
|
|
69
|
+
let isLastResult = true;
|
|
70
|
+
const resultPathReversed = [...resultPath].reverse();
|
|
71
|
+
for (const pathElem of resultPathReversed) {
|
|
72
|
+
// Ensure the path elem is not array index
|
|
73
|
+
if (Number.isNaN(parseInt(pathElem))) {
|
|
74
|
+
if (isLastResult &&
|
|
75
|
+
abstractResultTypeName &&
|
|
76
|
+
abstractResultTypeName !== resultFieldType.name) {
|
|
77
|
+
finalSelectionSet = {
|
|
78
|
+
kind: graphql_1.Kind.SELECTION_SET,
|
|
79
|
+
selections: [
|
|
80
|
+
{
|
|
81
|
+
kind: graphql_1.Kind.INLINE_FRAGMENT,
|
|
82
|
+
typeCondition: {
|
|
83
|
+
kind: graphql_1.Kind.NAMED_TYPE,
|
|
84
|
+
name: {
|
|
85
|
+
kind: graphql_1.Kind.NAME,
|
|
86
|
+
value: abstractResultTypeName,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
selectionSet: finalSelectionSet,
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
finalSelectionSet = {
|
|
95
|
+
kind: graphql_1.Kind.SELECTION_SET,
|
|
96
|
+
selections: [
|
|
97
|
+
{
|
|
98
|
+
// we create a wrapping AST Field
|
|
99
|
+
kind: graphql_1.Kind.FIELD,
|
|
100
|
+
name: {
|
|
101
|
+
kind: graphql_1.Kind.NAME,
|
|
102
|
+
value: pathElem,
|
|
103
|
+
},
|
|
104
|
+
// Inside the field selection
|
|
105
|
+
selectionSet: finalSelectionSet,
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
};
|
|
109
|
+
isLastResult = false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return finalSelectionSet;
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
function generateValuesFromResults(resultExpression) {
|
|
118
|
+
return function valuesFromResults(result) {
|
|
119
|
+
if (Array.isArray(result)) {
|
|
120
|
+
return result.map(valuesFromResults);
|
|
121
|
+
}
|
|
122
|
+
return (0, lodash_get_1.default)(result, resultExpression);
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function resolveAdditionalResolversWithoutImport(additionalResolver, pubsub) {
|
|
126
|
+
const baseOptions = {};
|
|
127
|
+
if (additionalResolver.result) {
|
|
128
|
+
baseOptions.valuesFromResults = generateValuesFromResults(additionalResolver.result);
|
|
129
|
+
}
|
|
130
|
+
if ('pubsubTopic' in additionalResolver) {
|
|
131
|
+
return {
|
|
132
|
+
[additionalResolver.targetTypeName]: {
|
|
133
|
+
[additionalResolver.targetFieldName]: {
|
|
134
|
+
subscribe: (0, with_filter_js_1.withFilter)((root, args, context, info) => {
|
|
135
|
+
const resolverData = { root, args, context, info, env: cross_helpers_1.process.env };
|
|
136
|
+
const topic = string_interpolation_1.stringInterpolator.parse(additionalResolver.pubsubTopic, resolverData);
|
|
137
|
+
return pubsub.asyncIterator(topic);
|
|
138
|
+
}, (root, args, context, info) => {
|
|
139
|
+
return additionalResolver.filterBy
|
|
140
|
+
? // eslint-disable-next-line no-new-func
|
|
141
|
+
new Function(`return ${additionalResolver.filterBy}`)()
|
|
142
|
+
: true;
|
|
143
|
+
}),
|
|
144
|
+
resolve: (payload) => {
|
|
145
|
+
if (baseOptions.valuesFromResults) {
|
|
146
|
+
return baseOptions.valuesFromResults(payload);
|
|
147
|
+
}
|
|
148
|
+
return payload;
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
else if ('keysArg' in additionalResolver) {
|
|
155
|
+
return {
|
|
156
|
+
[additionalResolver.targetTypeName]: {
|
|
157
|
+
[additionalResolver.targetFieldName]: {
|
|
158
|
+
selectionSet: additionalResolver.requiredSelectionSet || `{ ${additionalResolver.keyField} }`,
|
|
159
|
+
resolve: async (root, args, context, info) => {
|
|
160
|
+
if (!baseOptions.selectionSet) {
|
|
161
|
+
baseOptions.selectionSet = generateSelectionSetFactory(info.schema, additionalResolver);
|
|
162
|
+
}
|
|
163
|
+
const resolverData = { root, args, context, info, env: cross_helpers_1.process.env };
|
|
164
|
+
const targetArgs = {};
|
|
165
|
+
for (const argPath in additionalResolver.additionalArgs || {}) {
|
|
166
|
+
(0, dset_1.dset)(targetArgs, argPath, string_interpolation_1.stringInterpolator.parse(additionalResolver.additionalArgs[argPath], resolverData));
|
|
167
|
+
}
|
|
168
|
+
const options = {
|
|
169
|
+
...baseOptions,
|
|
170
|
+
root,
|
|
171
|
+
context,
|
|
172
|
+
info,
|
|
173
|
+
argsFromKeys: (keys) => {
|
|
174
|
+
const args = {};
|
|
175
|
+
(0, dset_1.dset)(args, additionalResolver.keysArg, keys);
|
|
176
|
+
Object.assign(args, targetArgs);
|
|
177
|
+
return args;
|
|
178
|
+
},
|
|
179
|
+
key: (0, lodash_get_1.default)(root, additionalResolver.keyField),
|
|
180
|
+
};
|
|
181
|
+
return context[additionalResolver.sourceName][additionalResolver.sourceTypeName][additionalResolver.sourceFieldName](options);
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
else if ('targetTypeName' in additionalResolver) {
|
|
188
|
+
return {
|
|
189
|
+
[additionalResolver.targetTypeName]: {
|
|
190
|
+
[additionalResolver.targetFieldName]: {
|
|
191
|
+
selectionSet: additionalResolver.requiredSelectionSet,
|
|
192
|
+
resolve: (root, args, context, info) => {
|
|
193
|
+
// Assert source exists
|
|
194
|
+
if (!context[additionalResolver.sourceName]) {
|
|
195
|
+
throw new Error(`No source found named "${additionalResolver.sourceName}"`);
|
|
196
|
+
}
|
|
197
|
+
if (!context[additionalResolver.sourceName][additionalResolver.sourceTypeName]) {
|
|
198
|
+
throw new Error(`No root type found named "${additionalResolver.sourceTypeName}" exists in the source ${additionalResolver.sourceName}\n` +
|
|
199
|
+
`It should be one of the following; ${Object.keys(context[additionalResolver.sourceName]).join(',')})}}`);
|
|
200
|
+
}
|
|
201
|
+
if (!context[additionalResolver.sourceName][additionalResolver.sourceTypeName][additionalResolver.sourceFieldName]) {
|
|
202
|
+
throw new Error(`No field named "${additionalResolver.sourceFieldName}" exists in the type ${additionalResolver.sourceTypeName} from the source ${additionalResolver.sourceName}`);
|
|
203
|
+
}
|
|
204
|
+
if (!baseOptions.selectionSet) {
|
|
205
|
+
baseOptions.selectionSet = generateSelectionSetFactory(info.schema, additionalResolver);
|
|
206
|
+
}
|
|
207
|
+
const resolverData = { root, args, context, info, env: cross_helpers_1.process.env };
|
|
208
|
+
const targetArgs = {};
|
|
209
|
+
for (const argPath in additionalResolver.sourceArgs) {
|
|
210
|
+
(0, dset_1.dset)(targetArgs, argPath, string_interpolation_1.stringInterpolator.parse(additionalResolver.sourceArgs[argPath].toString(), resolverData));
|
|
211
|
+
}
|
|
212
|
+
const options = {
|
|
213
|
+
...baseOptions,
|
|
214
|
+
root,
|
|
215
|
+
args: targetArgs,
|
|
216
|
+
context,
|
|
217
|
+
info,
|
|
218
|
+
};
|
|
219
|
+
return context[additionalResolver.sourceName][additionalResolver.sourceTypeName][additionalResolver.sourceFieldName](options);
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
return additionalResolver;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
exports.resolveAdditionalResolversWithoutImport = resolveAdditionalResolversWithoutImport;
|
|
230
|
+
function resolveAdditionalResolvers(baseDir, additionalResolvers, importFn, pubsub) {
|
|
231
|
+
return Promise.all((additionalResolvers || []).map(async (additionalResolver) => {
|
|
232
|
+
if (typeof additionalResolver === 'string') {
|
|
233
|
+
const resolvers = await (0, load_from_module_export_expression_js_1.loadFromModuleExportExpression)(additionalResolver, {
|
|
234
|
+
cwd: baseDir,
|
|
235
|
+
defaultExportName: 'resolvers',
|
|
236
|
+
importFn,
|
|
237
|
+
});
|
|
238
|
+
if (!resolvers) {
|
|
239
|
+
console.warn(`Unable to load resolvers from file: ${additionalResolver}`);
|
|
240
|
+
return {};
|
|
241
|
+
}
|
|
242
|
+
return resolvers;
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
const baseOptions = {};
|
|
246
|
+
if (additionalResolver.result) {
|
|
247
|
+
baseOptions.valuesFromResults = generateValuesFromResults(additionalResolver.result);
|
|
248
|
+
}
|
|
249
|
+
if ('pubsubTopic' in additionalResolver) {
|
|
250
|
+
return {
|
|
251
|
+
[additionalResolver.targetTypeName]: {
|
|
252
|
+
[additionalResolver.targetFieldName]: {
|
|
253
|
+
subscribe: (0, with_filter_js_1.withFilter)((root, args, context, info) => {
|
|
254
|
+
const resolverData = { root, args, context, info, env: cross_helpers_1.process.env };
|
|
255
|
+
const topic = string_interpolation_1.stringInterpolator.parse(additionalResolver.pubsubTopic, resolverData);
|
|
256
|
+
return pubsub.asyncIterator(topic);
|
|
257
|
+
}, (root, args, context, info) => {
|
|
258
|
+
return additionalResolver.filterBy
|
|
259
|
+
? // eslint-disable-next-line no-new-func
|
|
260
|
+
new Function(`return ${additionalResolver.filterBy}`)()
|
|
261
|
+
: true;
|
|
262
|
+
}),
|
|
263
|
+
resolve: (payload) => {
|
|
264
|
+
if (baseOptions.valuesFromResults) {
|
|
265
|
+
return baseOptions.valuesFromResults(payload);
|
|
266
|
+
}
|
|
267
|
+
return payload;
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
else if ('keysArg' in additionalResolver) {
|
|
274
|
+
return {
|
|
275
|
+
[additionalResolver.targetTypeName]: {
|
|
276
|
+
[additionalResolver.targetFieldName]: {
|
|
277
|
+
selectionSet: additionalResolver.requiredSelectionSet || `{ ${additionalResolver.keyField} }`,
|
|
278
|
+
resolve: async (root, args, context, info) => {
|
|
279
|
+
if (!baseOptions.selectionSet) {
|
|
280
|
+
baseOptions.selectionSet = generateSelectionSetFactory(info.schema, additionalResolver);
|
|
281
|
+
}
|
|
282
|
+
const resolverData = { root, args, context, info, env: cross_helpers_1.process.env };
|
|
283
|
+
const targetArgs = {};
|
|
284
|
+
for (const argPath in additionalResolver.additionalArgs || {}) {
|
|
285
|
+
(0, dset_1.dset)(targetArgs, argPath, string_interpolation_1.stringInterpolator.parse(additionalResolver.additionalArgs[argPath], resolverData));
|
|
286
|
+
}
|
|
287
|
+
const options = {
|
|
288
|
+
...baseOptions,
|
|
289
|
+
root,
|
|
290
|
+
context,
|
|
291
|
+
info,
|
|
292
|
+
argsFromKeys: (keys) => {
|
|
293
|
+
const args = {};
|
|
294
|
+
(0, dset_1.dset)(args, additionalResolver.keysArg, keys);
|
|
295
|
+
Object.assign(args, targetArgs);
|
|
296
|
+
return args;
|
|
297
|
+
},
|
|
298
|
+
key: (0, lodash_get_1.default)(root, additionalResolver.keyField),
|
|
299
|
+
};
|
|
300
|
+
return context[additionalResolver.sourceName][additionalResolver.sourceTypeName][additionalResolver.sourceFieldName](options);
|
|
301
|
+
},
|
|
302
|
+
},
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
else if ('targetTypeName' in additionalResolver) {
|
|
307
|
+
return {
|
|
308
|
+
[additionalResolver.targetTypeName]: {
|
|
309
|
+
[additionalResolver.targetFieldName]: {
|
|
310
|
+
selectionSet: additionalResolver.requiredSelectionSet,
|
|
311
|
+
resolve: (root, args, context, info) => {
|
|
312
|
+
// Assert source exists
|
|
313
|
+
if (!context[additionalResolver.sourceName]) {
|
|
314
|
+
throw new Error(`No source found named "${additionalResolver.sourceName}"`);
|
|
315
|
+
}
|
|
316
|
+
if (!context[additionalResolver.sourceName][additionalResolver.sourceTypeName]) {
|
|
317
|
+
throw new Error(`No root type found named "${additionalResolver.sourceTypeName}" exists in the source ${additionalResolver.sourceName}\n` +
|
|
318
|
+
`It should be one of the following; ${Object.keys(context[additionalResolver.sourceName]).join(',')})}}`);
|
|
319
|
+
}
|
|
320
|
+
if (!context[additionalResolver.sourceName][additionalResolver.sourceTypeName][additionalResolver.sourceFieldName]) {
|
|
321
|
+
throw new Error(`No field named "${additionalResolver.sourceFieldName}" exists in the type ${additionalResolver.sourceTypeName} from the source ${additionalResolver.sourceName}`);
|
|
322
|
+
}
|
|
323
|
+
if (!baseOptions.selectionSet) {
|
|
324
|
+
baseOptions.selectionSet = generateSelectionSetFactory(info.schema, additionalResolver);
|
|
325
|
+
}
|
|
326
|
+
const resolverData = { root, args, context, info, env: cross_helpers_1.process.env };
|
|
327
|
+
const targetArgs = {};
|
|
328
|
+
for (const argPath in additionalResolver.sourceArgs) {
|
|
329
|
+
(0, dset_1.dset)(targetArgs, argPath, string_interpolation_1.stringInterpolator.parse(additionalResolver.sourceArgs[argPath].toString(), resolverData));
|
|
330
|
+
}
|
|
331
|
+
const options = {
|
|
332
|
+
...baseOptions,
|
|
333
|
+
root,
|
|
334
|
+
args: targetArgs,
|
|
335
|
+
context,
|
|
336
|
+
info,
|
|
337
|
+
};
|
|
338
|
+
return context[additionalResolver.sourceName][additionalResolver.sourceTypeName][additionalResolver.sourceFieldName](options);
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
return additionalResolver;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}));
|
|
349
|
+
}
|
|
350
|
+
exports.resolveAdditionalResolvers = resolveAdditionalResolvers;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sanitizeNameForGraphQL = exports.removeClosedBrackets = void 0;
|
|
4
|
+
const reservedNames = ['Query', 'Mutation', 'Subscription', 'File'];
|
|
5
|
+
const KNOWN_CHARACTERS = {
|
|
6
|
+
'+': 'PLUS',
|
|
7
|
+
'-': 'MINUS',
|
|
8
|
+
'>': 'GREATER_THAN',
|
|
9
|
+
'<': 'LESS_THAN',
|
|
10
|
+
'=': 'EQUALS',
|
|
11
|
+
'&': 'AMPERSAND',
|
|
12
|
+
'|': 'PIPE',
|
|
13
|
+
'@': 'AT',
|
|
14
|
+
'*': 'STAR',
|
|
15
|
+
':': 'COLON',
|
|
16
|
+
'{': 'LEFT_CURLY_BRACE',
|
|
17
|
+
'}': 'RIGHT_CURLY_BRACE',
|
|
18
|
+
'[': 'LEFT_SQUARE_BRACE',
|
|
19
|
+
']': 'RIGHT_SQUARE_BRACE',
|
|
20
|
+
',': 'COMMA',
|
|
21
|
+
'%': 'PERCENT',
|
|
22
|
+
$: 'DOLLAR',
|
|
23
|
+
'#': 'POUND',
|
|
24
|
+
'^': 'CARET',
|
|
25
|
+
'~': 'TILDE',
|
|
26
|
+
'?': 'QUESTION_MARK',
|
|
27
|
+
'!': 'EXCLAMATION_MARK',
|
|
28
|
+
'"': 'QUOTATION_MARK',
|
|
29
|
+
"'": 'SINGLE_QUOTE',
|
|
30
|
+
'\\': 'BACKSLASH',
|
|
31
|
+
'/': 'SLASH',
|
|
32
|
+
'.': 'DOT',
|
|
33
|
+
'`': 'BACKTICK',
|
|
34
|
+
';': 'SEMICOLON',
|
|
35
|
+
'(': 'LEFT_PARENTHESIS',
|
|
36
|
+
')': 'RIGHT_PARENTHESIS',
|
|
37
|
+
};
|
|
38
|
+
function getKnownCharacterOrCharCode(ch) {
|
|
39
|
+
return KNOWN_CHARACTERS[ch] || ch.charCodeAt(0).toString();
|
|
40
|
+
}
|
|
41
|
+
function removeClosedBrackets(val) {
|
|
42
|
+
let out = val;
|
|
43
|
+
for (;;) {
|
|
44
|
+
// finds the first and shortest closed bracket match, starting from the left
|
|
45
|
+
const match = out.match(/\(.+?\)/);
|
|
46
|
+
const yesbrack = match === null || match === void 0 ? void 0 : match[0];
|
|
47
|
+
if (!yesbrack) {
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
// remove the brackets
|
|
51
|
+
const nobrack = yesbrack.substring(1, yesbrack.length - 1);
|
|
52
|
+
// replace the match removing bracks, just once starting from the left
|
|
53
|
+
out = out.replace(yesbrack, nobrack);
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
exports.removeClosedBrackets = removeClosedBrackets;
|
|
58
|
+
function sanitizeNameForGraphQL(unsafeName) {
|
|
59
|
+
let sanitizedName = unsafeName.trim();
|
|
60
|
+
sanitizedName = removeClosedBrackets(sanitizedName);
|
|
61
|
+
if (!isNaN(parseInt(sanitizedName))) {
|
|
62
|
+
if (sanitizedName.startsWith('-')) {
|
|
63
|
+
sanitizedName = sanitizedName.replace('-', 'NEGATIVE_');
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
sanitizedName = '_' + sanitizedName;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (!/^[_a-zA-Z0-9]*$/.test(sanitizedName)) {
|
|
70
|
+
const unsanitizedName = sanitizedName;
|
|
71
|
+
sanitizedName = '';
|
|
72
|
+
for (const ch of unsanitizedName) {
|
|
73
|
+
if (/^[_a-zA-Z0-9]$/.test(ch)) {
|
|
74
|
+
sanitizedName += ch;
|
|
75
|
+
}
|
|
76
|
+
else if (ch === ' ' || ch === '-' || ch === '.' || ch === '/' || ch === ':') {
|
|
77
|
+
sanitizedName += '_';
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
sanitizedName += `_${getKnownCharacterOrCharCode(ch)}_`;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// Names cannot start with __
|
|
85
|
+
if (sanitizedName.startsWith('__')) {
|
|
86
|
+
sanitizedName = sanitizedName.replace('__', '_0');
|
|
87
|
+
}
|
|
88
|
+
if (reservedNames.includes(sanitizedName)) {
|
|
89
|
+
sanitizedName += '_';
|
|
90
|
+
}
|
|
91
|
+
if (sanitizedName.length === 0) {
|
|
92
|
+
return '_';
|
|
93
|
+
}
|
|
94
|
+
return sanitizedName;
|
|
95
|
+
}
|
|
96
|
+
exports.sanitizeNameForGraphQL = sanitizeNameForGraphQL;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withCancel = void 0;
|
|
4
|
+
function withCancel(asyncIterable, onCancel) {
|
|
5
|
+
return new Proxy(asyncIterable, {
|
|
6
|
+
get(asyncIterable, prop) {
|
|
7
|
+
if (prop === Symbol.asyncIterator) {
|
|
8
|
+
return function getIteratorWithCancel() {
|
|
9
|
+
const asyncIterator = asyncIterable[Symbol.asyncIterator]();
|
|
10
|
+
return {
|
|
11
|
+
next: asyncIterator.next ? (...args) => asyncIterator.next(...args) : undefined,
|
|
12
|
+
return: async (...args) => {
|
|
13
|
+
onCancel();
|
|
14
|
+
if (asyncIterator.return) {
|
|
15
|
+
return asyncIterator.return(...args);
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
value: undefined,
|
|
19
|
+
done: true,
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
throw: asyncIterator.throw ? (...args) => asyncIterator.throw(...args) : undefined,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const propVal = asyncIterable[prop];
|
|
27
|
+
if (typeof propVal === 'function') {
|
|
28
|
+
return propVal.bind(asyncIterable);
|
|
29
|
+
}
|
|
30
|
+
return propVal;
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
exports.withCancel = withCancel;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withCookies = void 0;
|
|
4
|
+
// withCookies - embeds cookies object into the request
|
|
5
|
+
const withCookies = (request) => {
|
|
6
|
+
request.cookies = {};
|
|
7
|
+
try {
|
|
8
|
+
request.cookies = (request.headers.get('Cookie') || '')
|
|
9
|
+
.split(/;\s*/)
|
|
10
|
+
.map(pair => pair.split(/=(.+)/))
|
|
11
|
+
.reduce((acc, [key, value]) => {
|
|
12
|
+
acc[key] = value;
|
|
13
|
+
return acc;
|
|
14
|
+
}, {});
|
|
15
|
+
}
|
|
16
|
+
catch (err) { }
|
|
17
|
+
};
|
|
18
|
+
exports.withCookies = withCookies;
|