@graphql-tools/load 7.6.0 → 7.7.0-alpha-b76ec274.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/documents.js +39 -0
- package/cjs/filter-document-kind.js +35 -0
- package/cjs/index.js +7 -0
- package/cjs/load-typedefs/collect-sources.js +195 -0
- package/cjs/load-typedefs/load-file.js +86 -0
- package/cjs/load-typedefs/options.js +10 -0
- package/cjs/load-typedefs/parse.js +62 -0
- package/cjs/load-typedefs.js +84 -0
- package/cjs/package.json +1 -0
- package/cjs/schema.js +86 -0
- package/cjs/utils/custom-loader.js +50 -0
- package/cjs/utils/helpers.js +42 -0
- package/cjs/utils/pointers.js +31 -0
- package/cjs/utils/queue.js +32 -0
- package/esm/documents.js +34 -0
- package/esm/filter-document-kind.js +31 -0
- package/esm/index.js +4 -0
- package/esm/load-typedefs/collect-sources.js +167 -0
- package/esm/load-typedefs/load-file.js +81 -0
- package/esm/load-typedefs/options.js +6 -0
- package/esm/load-typedefs/parse.js +58 -0
- package/esm/load-typedefs.js +79 -0
- package/esm/schema.js +81 -0
- package/esm/utils/custom-loader.js +44 -0
- package/esm/utils/helpers.js +35 -0
- package/esm/utils/pointers.js +27 -0
- package/esm/utils/queue.js +26 -0
- package/package.json +33 -12
- package/{documents.d.ts → typings/documents.d.ts} +1 -1
- package/{filter-document-kind.d.ts → typings/filter-document-kind.d.ts} +0 -0
- package/typings/index.d.ts +4 -0
- package/{load-typedefs → typings/load-typedefs}/collect-sources.d.ts +1 -1
- package/{load-typedefs → typings/load-typedefs}/load-file.d.ts +1 -1
- package/{load-typedefs → typings/load-typedefs}/options.d.ts +1 -1
- package/{load-typedefs → typings/load-typedefs}/parse.d.ts +0 -0
- package/{load-typedefs.d.ts → typings/load-typedefs.d.ts} +0 -0
- package/{schema.d.ts → typings/schema.d.ts} +1 -1
- package/{utils → typings/utils}/custom-loader.d.ts +0 -0
- package/{utils → typings/utils}/helpers.d.ts +0 -0
- package/{utils → typings/utils}/pointers.d.ts +1 -1
- package/{utils → typings/utils}/queue.d.ts +0 -0
- package/README.md +0 -8
- package/index.d.ts +0 -4
- package/index.js +0 -691
- package/index.mjs +0 -658
package/cjs/documents.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadDocumentsSync = exports.loadDocuments = exports.NON_OPERATION_KINDS = exports.OPERATION_KINDS = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const load_typedefs_js_1 = require("./load-typedefs.js");
|
|
6
|
+
/**
|
|
7
|
+
* Kinds of AST nodes that are included in executable documents
|
|
8
|
+
*/
|
|
9
|
+
exports.OPERATION_KINDS = [graphql_1.Kind.OPERATION_DEFINITION, graphql_1.Kind.FRAGMENT_DEFINITION];
|
|
10
|
+
/**
|
|
11
|
+
* Kinds of AST nodes that are included in type system definition documents
|
|
12
|
+
*/
|
|
13
|
+
exports.NON_OPERATION_KINDS = Object.keys(graphql_1.Kind)
|
|
14
|
+
.reduce((prev, v) => [...prev, graphql_1.Kind[v]], [])
|
|
15
|
+
.filter(v => !exports.OPERATION_KINDS.includes(v));
|
|
16
|
+
/**
|
|
17
|
+
* Asynchronously loads executable documents (i.e. operations and fragments) from
|
|
18
|
+
* the provided pointers. The pointers may be individual files or a glob pattern.
|
|
19
|
+
* The files themselves may be `.graphql` files or `.js` and `.ts` (in which
|
|
20
|
+
* case they will be parsed using graphql-tag-pluck).
|
|
21
|
+
* @param pointerOrPointers Pointers to the files to load the documents from
|
|
22
|
+
* @param options Additional options
|
|
23
|
+
*/
|
|
24
|
+
function loadDocuments(pointerOrPointers, options) {
|
|
25
|
+
return (0, load_typedefs_js_1.loadTypedefs)(pointerOrPointers, { noRequire: true, filterKinds: exports.NON_OPERATION_KINDS, ...options });
|
|
26
|
+
}
|
|
27
|
+
exports.loadDocuments = loadDocuments;
|
|
28
|
+
/**
|
|
29
|
+
* Synchronously loads executable documents (i.e. operations and fragments) from
|
|
30
|
+
* the provided pointers. The pointers may be individual files or a glob pattern.
|
|
31
|
+
* The files themselves may be `.graphql` files or `.js` and `.ts` (in which
|
|
32
|
+
* case they will be parsed using graphql-tag-pluck).
|
|
33
|
+
* @param pointerOrPointers Pointers to the files to load the documents from
|
|
34
|
+
* @param options Additional options
|
|
35
|
+
*/
|
|
36
|
+
function loadDocumentsSync(pointerOrPointers, options) {
|
|
37
|
+
return (0, load_typedefs_js_1.loadTypedefsSync)(pointerOrPointers, { noRequire: true, filterKinds: exports.NON_OPERATION_KINDS, ...options });
|
|
38
|
+
}
|
|
39
|
+
exports.loadDocumentsSync = loadDocumentsSync;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.filterKind = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const process_1 = require("process");
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
const filterKind = (content, filterKinds) => {
|
|
10
|
+
if (content && content.definitions && content.definitions.length && filterKinds && filterKinds.length > 0) {
|
|
11
|
+
const invalidDefinitions = [];
|
|
12
|
+
const validDefinitions = [];
|
|
13
|
+
for (const definitionNode of content.definitions) {
|
|
14
|
+
if (filterKinds.includes(definitionNode.kind)) {
|
|
15
|
+
invalidDefinitions.push(definitionNode);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
validDefinitions.push(definitionNode);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (invalidDefinitions.length > 0) {
|
|
22
|
+
if (process_1.env['DEBUG']) {
|
|
23
|
+
for (const d of invalidDefinitions) {
|
|
24
|
+
console.log(`Filtered document of kind ${d.kind} due to filter policy (${filterKinds.join(', ')})`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
kind: graphql_1.Kind.DOCUMENT,
|
|
30
|
+
definitions: validDefinitions,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
return content;
|
|
34
|
+
};
|
|
35
|
+
exports.filterKind = filterKind;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./load-typedefs.js"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./schema.js"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./documents.js"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./filter-document-kind.js"), exports);
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.collectSourcesSync = exports.collectSources = void 0;
|
|
27
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
28
|
+
const graphql_1 = require("graphql");
|
|
29
|
+
const load_file_js_1 = require("./load-file.js");
|
|
30
|
+
const helpers_js_1 = require("../utils/helpers.js");
|
|
31
|
+
const custom_loader_js_1 = require("../utils/custom-loader.js");
|
|
32
|
+
const queue_js_1 = require("../utils/queue.js");
|
|
33
|
+
const module_1 = require("module");
|
|
34
|
+
const process_1 = require("process");
|
|
35
|
+
const CONCURRENCY_LIMIT = 50;
|
|
36
|
+
async function collectSources({ pointerOptionMap, options, }) {
|
|
37
|
+
const sources = [];
|
|
38
|
+
const queue = (0, queue_js_1.useQueue)({ concurrency: CONCURRENCY_LIMIT });
|
|
39
|
+
const { addSource, collect } = createHelpers({
|
|
40
|
+
sources,
|
|
41
|
+
stack: [collectDocumentString, collectCustomLoader, collectFallback],
|
|
42
|
+
});
|
|
43
|
+
for (const pointer in pointerOptionMap) {
|
|
44
|
+
const pointerOptions = pointerOptionMap[pointer];
|
|
45
|
+
collect({
|
|
46
|
+
pointer,
|
|
47
|
+
pointerOptions,
|
|
48
|
+
pointerOptionMap,
|
|
49
|
+
options,
|
|
50
|
+
addSource,
|
|
51
|
+
queue: queue.add,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
await queue.runAll();
|
|
55
|
+
return sources;
|
|
56
|
+
}
|
|
57
|
+
exports.collectSources = collectSources;
|
|
58
|
+
function collectSourcesSync({ pointerOptionMap, options, }) {
|
|
59
|
+
const sources = [];
|
|
60
|
+
const queue = (0, queue_js_1.useSyncQueue)();
|
|
61
|
+
const { addSource, collect } = createHelpers({
|
|
62
|
+
sources,
|
|
63
|
+
stack: [collectDocumentString, collectCustomLoaderSync, collectFallbackSync],
|
|
64
|
+
});
|
|
65
|
+
for (const pointer in pointerOptionMap) {
|
|
66
|
+
const pointerOptions = pointerOptionMap[pointer];
|
|
67
|
+
collect({
|
|
68
|
+
pointer,
|
|
69
|
+
pointerOptions,
|
|
70
|
+
pointerOptionMap,
|
|
71
|
+
options,
|
|
72
|
+
addSource,
|
|
73
|
+
queue: queue.add,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
queue.runAll();
|
|
77
|
+
return sources;
|
|
78
|
+
}
|
|
79
|
+
exports.collectSourcesSync = collectSourcesSync;
|
|
80
|
+
function createHelpers({ sources, stack }) {
|
|
81
|
+
const addSource = ({ source }) => {
|
|
82
|
+
sources.push(source);
|
|
83
|
+
};
|
|
84
|
+
const collect = (0, helpers_js_1.useStack)(...stack);
|
|
85
|
+
return {
|
|
86
|
+
addSource,
|
|
87
|
+
collect,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function addResultOfCustomLoader({ pointer, result, addSource, }) {
|
|
91
|
+
if ((0, graphql_1.isSchema)(result)) {
|
|
92
|
+
addSource({
|
|
93
|
+
source: {
|
|
94
|
+
location: pointer,
|
|
95
|
+
schema: result,
|
|
96
|
+
document: (0, utils_1.getDocumentNodeFromSchema)(result),
|
|
97
|
+
},
|
|
98
|
+
pointer,
|
|
99
|
+
noCache: true,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
else if (result.kind && result.kind === graphql_1.Kind.DOCUMENT) {
|
|
103
|
+
addSource({
|
|
104
|
+
source: {
|
|
105
|
+
document: result,
|
|
106
|
+
location: pointer,
|
|
107
|
+
},
|
|
108
|
+
pointer,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
else if (result.document) {
|
|
112
|
+
addSource({
|
|
113
|
+
source: {
|
|
114
|
+
location: pointer,
|
|
115
|
+
...result,
|
|
116
|
+
},
|
|
117
|
+
pointer,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function collectDocumentString({ pointer, pointerOptions, options, addSource, queue }, next) {
|
|
122
|
+
if ((0, utils_1.isDocumentString)(pointer)) {
|
|
123
|
+
return queue(() => {
|
|
124
|
+
const source = (0, utils_1.parseGraphQLSDL)(`${(0, helpers_js_1.stringToHash)(pointer)}.graphql`, pointer, {
|
|
125
|
+
...options,
|
|
126
|
+
...pointerOptions,
|
|
127
|
+
});
|
|
128
|
+
addSource({
|
|
129
|
+
source,
|
|
130
|
+
pointer,
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
next();
|
|
135
|
+
}
|
|
136
|
+
function collectCustomLoader({ pointer, pointerOptions, queue, addSource, options, pointerOptionMap }, next) {
|
|
137
|
+
if (pointerOptions.loader) {
|
|
138
|
+
return queue(async () => {
|
|
139
|
+
await Promise.all((0, utils_1.asArray)(pointerOptions.require).map(m => Promise.resolve().then(() => __importStar(require(m)))));
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
141
|
+
// @ts-ignore TODO options.cwd is possibly undefined, but it seems like no test covers this path
|
|
142
|
+
const loader = await (0, custom_loader_js_1.useCustomLoader)(pointerOptions.loader, options.cwd);
|
|
143
|
+
const result = await loader(pointer, { ...options, ...pointerOptions }, pointerOptionMap);
|
|
144
|
+
if (!result) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
addResultOfCustomLoader({ pointer, result, addSource });
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
next();
|
|
151
|
+
}
|
|
152
|
+
function collectCustomLoaderSync({ pointer, pointerOptions, queue, addSource, options, pointerOptionMap }, next) {
|
|
153
|
+
if (pointerOptions.loader) {
|
|
154
|
+
return queue(() => {
|
|
155
|
+
const cwdRequire = (0, module_1.createRequire)(options.cwd || (0, process_1.cwd)());
|
|
156
|
+
for (const m of (0, utils_1.asArray)(pointerOptions.require)) {
|
|
157
|
+
cwdRequire(m);
|
|
158
|
+
}
|
|
159
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
160
|
+
// @ts-ignore TODO options.cwd is possibly undefined, but it seems like no test covers this path
|
|
161
|
+
const loader = (0, custom_loader_js_1.useCustomLoaderSync)(pointerOptions.loader, options.cwd);
|
|
162
|
+
const result = loader(pointer, { ...options, ...pointerOptions }, pointerOptionMap);
|
|
163
|
+
if (result) {
|
|
164
|
+
addResultOfCustomLoader({ pointer, result, addSource });
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
next();
|
|
169
|
+
}
|
|
170
|
+
function collectFallback({ queue, pointer, options, pointerOptions, addSource }) {
|
|
171
|
+
return queue(async () => {
|
|
172
|
+
const sources = await (0, load_file_js_1.loadFile)(pointer, {
|
|
173
|
+
...options,
|
|
174
|
+
...pointerOptions,
|
|
175
|
+
});
|
|
176
|
+
if (sources) {
|
|
177
|
+
for (const source of sources) {
|
|
178
|
+
addSource({ source, pointer });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
function collectFallbackSync({ queue, pointer, options, pointerOptions, addSource }) {
|
|
184
|
+
return queue(() => {
|
|
185
|
+
const sources = (0, load_file_js_1.loadFileSync)(pointer, {
|
|
186
|
+
...options,
|
|
187
|
+
...pointerOptions,
|
|
188
|
+
});
|
|
189
|
+
if (sources) {
|
|
190
|
+
for (const source of sources) {
|
|
191
|
+
addSource({ source, pointer });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadFileSync = exports.loadFile = void 0;
|
|
4
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
5
|
+
const process_1 = require("process");
|
|
6
|
+
async function loadFile(pointer, options) {
|
|
7
|
+
var _a;
|
|
8
|
+
let results = (_a = options.cache) === null || _a === void 0 ? void 0 : _a[pointer];
|
|
9
|
+
if (!results) {
|
|
10
|
+
results = [];
|
|
11
|
+
const errors = [];
|
|
12
|
+
await Promise.all(options.loaders.map(async (loader) => {
|
|
13
|
+
try {
|
|
14
|
+
const loaderResults = await loader.load(pointer, options);
|
|
15
|
+
loaderResults === null || loaderResults === void 0 ? void 0 : loaderResults.forEach(result => results.push(result));
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
if (process_1.env['DEBUG']) {
|
|
19
|
+
console.error(error);
|
|
20
|
+
}
|
|
21
|
+
if (error instanceof utils_1.AggregateError) {
|
|
22
|
+
for (const errorElement of error.errors) {
|
|
23
|
+
errors.push(errorElement);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
errors.push(error);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}));
|
|
31
|
+
if (results.length === 0 && errors.length > 0) {
|
|
32
|
+
if (errors.length === 1) {
|
|
33
|
+
throw errors[0];
|
|
34
|
+
}
|
|
35
|
+
throw new utils_1.AggregateError(errors, `Failed to find any GraphQL type definitions in: ${pointer};\n - ${errors
|
|
36
|
+
.map(error => error.message)
|
|
37
|
+
.join('\n - ')}`);
|
|
38
|
+
}
|
|
39
|
+
if (options.cache) {
|
|
40
|
+
options.cache[pointer] = results;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return results;
|
|
44
|
+
}
|
|
45
|
+
exports.loadFile = loadFile;
|
|
46
|
+
function loadFileSync(pointer, options) {
|
|
47
|
+
var _a;
|
|
48
|
+
let results = (_a = options.cache) === null || _a === void 0 ? void 0 : _a[pointer];
|
|
49
|
+
if (!results) {
|
|
50
|
+
results = [];
|
|
51
|
+
const errors = [];
|
|
52
|
+
for (const loader of options.loaders) {
|
|
53
|
+
try {
|
|
54
|
+
// We check for the existence so it is okay to force non null
|
|
55
|
+
const loaderResults = loader.loadSync(pointer, options);
|
|
56
|
+
loaderResults === null || loaderResults === void 0 ? void 0 : loaderResults.forEach(result => results.push(result));
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (process_1.env['DEBUG']) {
|
|
60
|
+
console.error(error);
|
|
61
|
+
}
|
|
62
|
+
if (error instanceof utils_1.AggregateError) {
|
|
63
|
+
for (const errorElement of error.errors) {
|
|
64
|
+
errors.push(errorElement);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
errors.push(error);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (results.length === 0 && errors.length > 0) {
|
|
73
|
+
if (errors.length === 1) {
|
|
74
|
+
throw errors[0];
|
|
75
|
+
}
|
|
76
|
+
throw new utils_1.AggregateError(errors, `Failed to find any GraphQL type definitions in: ${pointer};\n - ${errors
|
|
77
|
+
.map(error => error.message)
|
|
78
|
+
.join('\n - ')}`);
|
|
79
|
+
}
|
|
80
|
+
if (options.cache) {
|
|
81
|
+
options.cache[pointer] = results;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return results;
|
|
85
|
+
}
|
|
86
|
+
exports.loadFileSync = loadFileSync;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.applyDefaultOptions = void 0;
|
|
4
|
+
const process_1 = require("process");
|
|
5
|
+
function applyDefaultOptions(options) {
|
|
6
|
+
options.cache = options.cache || {};
|
|
7
|
+
options.cwd = options.cwd || (0, process_1.cwd)();
|
|
8
|
+
options.sort = 'sort' in options ? options.sort : true;
|
|
9
|
+
}
|
|
10
|
+
exports.applyDefaultOptions = applyDefaultOptions;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseSource = void 0;
|
|
4
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
5
|
+
const filter_document_kind_js_1 = require("../filter-document-kind.js");
|
|
6
|
+
function parseSource({ partialSource, options, pointerOptionMap, addValidSource }) {
|
|
7
|
+
if (partialSource) {
|
|
8
|
+
const input = prepareInput({
|
|
9
|
+
source: partialSource,
|
|
10
|
+
options,
|
|
11
|
+
pointerOptionMap,
|
|
12
|
+
});
|
|
13
|
+
parseSchema(input);
|
|
14
|
+
parseRawSDL(input);
|
|
15
|
+
if (input.source.document) {
|
|
16
|
+
useKindsFilter(input);
|
|
17
|
+
useComments(input);
|
|
18
|
+
collectValidSources(input, addValidSource);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.parseSource = parseSource;
|
|
23
|
+
//
|
|
24
|
+
function prepareInput({ source, options, pointerOptionMap, }) {
|
|
25
|
+
let specificOptions = {
|
|
26
|
+
...options,
|
|
27
|
+
};
|
|
28
|
+
if (source.location) {
|
|
29
|
+
specificOptions = {
|
|
30
|
+
...specificOptions,
|
|
31
|
+
...pointerOptionMap[source.location],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return { source: { ...source }, options: specificOptions };
|
|
35
|
+
}
|
|
36
|
+
function parseSchema(input) {
|
|
37
|
+
if (input.source.schema) {
|
|
38
|
+
input.source.rawSDL = (0, utils_1.printSchemaWithDirectives)(input.source.schema, input.options);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function parseRawSDL(input) {
|
|
42
|
+
if (input.source.rawSDL) {
|
|
43
|
+
input.source.document = (0, utils_1.parseGraphQLSDL)(input.source.location, input.source.rawSDL, input.options).document;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function useKindsFilter(input) {
|
|
47
|
+
if (input.options.filterKinds) {
|
|
48
|
+
input.source.document = (0, filter_document_kind_js_1.filterKind)(input.source.document, input.options.filterKinds);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function useComments(input) {
|
|
52
|
+
if (!input.source.rawSDL && input.source.document) {
|
|
53
|
+
input.source.rawSDL = (0, utils_1.printWithComments)(input.source.document);
|
|
54
|
+
(0, utils_1.resetComments)();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function collectValidSources(input, addValidSource) {
|
|
58
|
+
var _a;
|
|
59
|
+
if (((_a = input.source.document) === null || _a === void 0 ? void 0 : _a.definitions) && input.source.document.definitions.length > 0) {
|
|
60
|
+
addValidSource(input.source);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadTypedefsSync = exports.loadTypedefs = void 0;
|
|
4
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
5
|
+
const pointers_js_1 = require("./utils/pointers.js");
|
|
6
|
+
const options_js_1 = require("./load-typedefs/options.js");
|
|
7
|
+
const collect_sources_js_1 = require("./load-typedefs/collect-sources.js");
|
|
8
|
+
const parse_js_1 = require("./load-typedefs/parse.js");
|
|
9
|
+
const helpers_js_1 = require("./utils/helpers.js");
|
|
10
|
+
const CONCURRENCY_LIMIT = 100;
|
|
11
|
+
/**
|
|
12
|
+
* Asynchronously loads any GraphQL documents (i.e. executable documents like
|
|
13
|
+
* operations and fragments as well as type system definitions) from the
|
|
14
|
+
* provided pointers.
|
|
15
|
+
* loadTypedefs does not merge the typeDefs when `#import` is used ( https://github.com/ardatan/graphql-tools/issues/2980#issuecomment-1003692728 )
|
|
16
|
+
* @param pointerOrPointers Pointers to the sources to load the documents from
|
|
17
|
+
* @param options Additional options
|
|
18
|
+
*/
|
|
19
|
+
async function loadTypedefs(pointerOrPointers, options) {
|
|
20
|
+
const { ignore, pointerOptionMap } = (0, pointers_js_1.normalizePointers)(pointerOrPointers);
|
|
21
|
+
options.ignore = (0, utils_1.asArray)(options.ignore || []);
|
|
22
|
+
options.ignore.push(...ignore);
|
|
23
|
+
(0, options_js_1.applyDefaultOptions)(options);
|
|
24
|
+
const sources = await (0, collect_sources_js_1.collectSources)({
|
|
25
|
+
pointerOptionMap,
|
|
26
|
+
options,
|
|
27
|
+
});
|
|
28
|
+
const validSources = [];
|
|
29
|
+
// If we have few k of files it may be an issue
|
|
30
|
+
const limit = (0, helpers_js_1.useLimit)(CONCURRENCY_LIMIT);
|
|
31
|
+
await Promise.all(sources.map(partialSource => limit(() => (0, parse_js_1.parseSource)({
|
|
32
|
+
partialSource,
|
|
33
|
+
options,
|
|
34
|
+
pointerOptionMap,
|
|
35
|
+
addValidSource(source) {
|
|
36
|
+
validSources.push(source);
|
|
37
|
+
},
|
|
38
|
+
}))));
|
|
39
|
+
return prepareResult({ options, pointerOptionMap, validSources });
|
|
40
|
+
}
|
|
41
|
+
exports.loadTypedefs = loadTypedefs;
|
|
42
|
+
/**
|
|
43
|
+
* Synchronously loads any GraphQL documents (i.e. executable documents like
|
|
44
|
+
* operations and fragments as well as type system definitions) from the
|
|
45
|
+
* provided pointers.
|
|
46
|
+
* @param pointerOrPointers Pointers to the sources to load the documents from
|
|
47
|
+
* @param options Additional options
|
|
48
|
+
*/
|
|
49
|
+
function loadTypedefsSync(pointerOrPointers, options) {
|
|
50
|
+
const { ignore, pointerOptionMap } = (0, pointers_js_1.normalizePointers)(pointerOrPointers);
|
|
51
|
+
options.ignore = (0, utils_1.asArray)(options.ignore || []).concat(ignore);
|
|
52
|
+
(0, options_js_1.applyDefaultOptions)(options);
|
|
53
|
+
const sources = (0, collect_sources_js_1.collectSourcesSync)({
|
|
54
|
+
pointerOptionMap,
|
|
55
|
+
options,
|
|
56
|
+
});
|
|
57
|
+
const validSources = [];
|
|
58
|
+
for (const partialSource of sources) {
|
|
59
|
+
(0, parse_js_1.parseSource)({
|
|
60
|
+
partialSource,
|
|
61
|
+
options,
|
|
62
|
+
pointerOptionMap,
|
|
63
|
+
addValidSource(source) {
|
|
64
|
+
validSources.push(source);
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return prepareResult({ options, pointerOptionMap, validSources });
|
|
69
|
+
}
|
|
70
|
+
exports.loadTypedefsSync = loadTypedefsSync;
|
|
71
|
+
//
|
|
72
|
+
function prepareResult({ options, pointerOptionMap, validSources, }) {
|
|
73
|
+
const pointerList = Object.keys(pointerOptionMap);
|
|
74
|
+
if (pointerList.length > 0 && validSources.length === 0) {
|
|
75
|
+
throw new Error(`
|
|
76
|
+
Unable to find any GraphQL type definitions for the following pointers:
|
|
77
|
+
${pointerList.map(p => `
|
|
78
|
+
- ${p}
|
|
79
|
+
`)}`);
|
|
80
|
+
}
|
|
81
|
+
return options.sort
|
|
82
|
+
? validSources.sort((left, right) => (0, utils_1.compareStrings)(left.location, right.location))
|
|
83
|
+
: validSources;
|
|
84
|
+
}
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/cjs/schema.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadSchemaSync = exports.loadSchema = void 0;
|
|
4
|
+
const load_typedefs_js_1 = require("./load-typedefs.js");
|
|
5
|
+
const graphql_1 = require("graphql");
|
|
6
|
+
const documents_js_1 = require("./documents.js");
|
|
7
|
+
const schema_1 = require("@graphql-tools/schema");
|
|
8
|
+
/**
|
|
9
|
+
* Asynchronously loads a schema from the provided pointers.
|
|
10
|
+
* @param schemaPointers Pointers to the sources to load the schema from
|
|
11
|
+
* @param options Additional options
|
|
12
|
+
*/
|
|
13
|
+
async function loadSchema(schemaPointers, options) {
|
|
14
|
+
var _a, _b;
|
|
15
|
+
const sources = await (0, load_typedefs_js_1.loadTypedefs)(schemaPointers, {
|
|
16
|
+
...options,
|
|
17
|
+
filterKinds: documents_js_1.OPERATION_KINDS,
|
|
18
|
+
});
|
|
19
|
+
const { schemas, typeDefs } = collectSchemasAndTypeDefs(sources);
|
|
20
|
+
schemas.push(...((_a = options.schemas) !== null && _a !== void 0 ? _a : []));
|
|
21
|
+
const mergeSchemasOptions = {
|
|
22
|
+
...options,
|
|
23
|
+
schemas: schemas.concat((_b = options.schemas) !== null && _b !== void 0 ? _b : []),
|
|
24
|
+
typeDefs,
|
|
25
|
+
};
|
|
26
|
+
const schema = (typeDefs === null || typeDefs === void 0 ? void 0 : typeDefs.length) === 0 && (schemas === null || schemas === void 0 ? void 0 : schemas.length) === 1 ? schemas[0] : (0, schema_1.mergeSchemas)(mergeSchemasOptions);
|
|
27
|
+
if (options === null || options === void 0 ? void 0 : options.includeSources) {
|
|
28
|
+
includeSources(schema, sources);
|
|
29
|
+
}
|
|
30
|
+
return options.sort ? (0, graphql_1.lexicographicSortSchema)(schema) : schema;
|
|
31
|
+
}
|
|
32
|
+
exports.loadSchema = loadSchema;
|
|
33
|
+
/**
|
|
34
|
+
* Synchronously loads a schema from the provided pointers.
|
|
35
|
+
* @param schemaPointers Pointers to the sources to load the schema from
|
|
36
|
+
* @param options Additional options
|
|
37
|
+
*/
|
|
38
|
+
function loadSchemaSync(schemaPointers, options) {
|
|
39
|
+
const sources = (0, load_typedefs_js_1.loadTypedefsSync)(schemaPointers, {
|
|
40
|
+
filterKinds: documents_js_1.OPERATION_KINDS,
|
|
41
|
+
...options,
|
|
42
|
+
});
|
|
43
|
+
const { schemas, typeDefs } = collectSchemasAndTypeDefs(sources);
|
|
44
|
+
const schema = (0, schema_1.mergeSchemas)({
|
|
45
|
+
schemas,
|
|
46
|
+
typeDefs,
|
|
47
|
+
...options,
|
|
48
|
+
});
|
|
49
|
+
if (options === null || options === void 0 ? void 0 : options.includeSources) {
|
|
50
|
+
includeSources(schema, sources);
|
|
51
|
+
}
|
|
52
|
+
return options.sort ? (0, graphql_1.lexicographicSortSchema)(schema) : schema;
|
|
53
|
+
}
|
|
54
|
+
exports.loadSchemaSync = loadSchemaSync;
|
|
55
|
+
function includeSources(schema, sources) {
|
|
56
|
+
const finalSources = [];
|
|
57
|
+
for (const source of sources) {
|
|
58
|
+
if (source.rawSDL) {
|
|
59
|
+
finalSources.push(new graphql_1.Source(source.rawSDL, source.location));
|
|
60
|
+
}
|
|
61
|
+
else if (source.document) {
|
|
62
|
+
finalSources.push(new graphql_1.Source((0, graphql_1.print)(source.document), source.location));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
schema.extensions = {
|
|
66
|
+
...schema.extensions,
|
|
67
|
+
sources: finalSources,
|
|
68
|
+
extendedSources: sources,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function collectSchemasAndTypeDefs(sources) {
|
|
72
|
+
const schemas = [];
|
|
73
|
+
const typeDefs = [];
|
|
74
|
+
for (const source of sources) {
|
|
75
|
+
if (source.schema) {
|
|
76
|
+
schemas.push(source.schema);
|
|
77
|
+
}
|
|
78
|
+
else if (source.document) {
|
|
79
|
+
typeDefs.push(source.document);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
schemas,
|
|
84
|
+
typeDefs,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useCustomLoaderSync = exports.useCustomLoader = exports.getCustomLoaderByPath = void 0;
|
|
4
|
+
const module_1 = require("module");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
function getCustomLoaderByPath(path, cwd) {
|
|
7
|
+
try {
|
|
8
|
+
const requireFn = (0, module_1.createRequire)((0, path_1.join)(cwd, 'noop.js'));
|
|
9
|
+
const requiredModule = requireFn(path);
|
|
10
|
+
if (requiredModule) {
|
|
11
|
+
if (requiredModule.default && typeof requiredModule.default === 'function') {
|
|
12
|
+
return requiredModule.default;
|
|
13
|
+
}
|
|
14
|
+
if (typeof requiredModule === 'function') {
|
|
15
|
+
return requiredModule;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
catch (e) { }
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
exports.getCustomLoaderByPath = getCustomLoaderByPath;
|
|
23
|
+
async function useCustomLoader(loaderPointer, cwd) {
|
|
24
|
+
let loader;
|
|
25
|
+
if (typeof loaderPointer === 'string') {
|
|
26
|
+
loader = await getCustomLoaderByPath(loaderPointer, cwd);
|
|
27
|
+
}
|
|
28
|
+
else if (typeof loaderPointer === 'function') {
|
|
29
|
+
loader = loaderPointer;
|
|
30
|
+
}
|
|
31
|
+
if (typeof loader !== 'function') {
|
|
32
|
+
throw new Error(`Failed to load custom loader: ${loaderPointer}`);
|
|
33
|
+
}
|
|
34
|
+
return loader;
|
|
35
|
+
}
|
|
36
|
+
exports.useCustomLoader = useCustomLoader;
|
|
37
|
+
function useCustomLoaderSync(loaderPointer, cwd) {
|
|
38
|
+
let loader;
|
|
39
|
+
if (typeof loaderPointer === 'string') {
|
|
40
|
+
loader = getCustomLoaderByPath(loaderPointer, cwd);
|
|
41
|
+
}
|
|
42
|
+
else if (typeof loaderPointer === 'function') {
|
|
43
|
+
loader = loaderPointer;
|
|
44
|
+
}
|
|
45
|
+
if (typeof loader !== 'function') {
|
|
46
|
+
throw new Error(`Failed to load custom loader: ${loaderPointer}`);
|
|
47
|
+
}
|
|
48
|
+
return loader;
|
|
49
|
+
}
|
|
50
|
+
exports.useCustomLoaderSync = useCustomLoaderSync;
|