1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | const utils = require('@graphql-tools/utils');
|
8 | const path = require('path');
|
9 | const fs = require('fs');
|
10 | const process = require('process');
|
11 | const _import = require('@graphql-tools/import');
|
12 | const globby = _interopDefault(require('globby'));
|
13 | const unixify = _interopDefault(require('unixify'));
|
14 |
|
15 | const { readFile, access } = fs.promises;
|
16 | const FILE_EXTENSIONS = ['.gql', '.gqls', '.graphql', '.graphqls'];
|
17 | function isGraphQLImportFile(rawSDL) {
|
18 | const trimmedRawSDL = rawSDL.trim();
|
19 | return trimmedRawSDL.startsWith('# import') || trimmedRawSDL.startsWith('#import');
|
20 | }
|
21 | function createGlobbyOptions(options) {
|
22 | return { absolute: true, ...options, ignore: [] };
|
23 | }
|
24 | const buildIgnoreGlob = (path) => `!${path}`;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | class GraphQLFileLoader {
|
49 | async canLoad(pointer, options) {
|
50 | if (utils.isValidPath(pointer)) {
|
51 | if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
|
52 | const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd || process.cwd(), pointer);
|
53 | try {
|
54 | await access(normalizedFilePath);
|
55 | return true;
|
56 | }
|
57 | catch (_a) {
|
58 | return false;
|
59 | }
|
60 | }
|
61 | }
|
62 | return false;
|
63 | }
|
64 | canLoadSync(pointer, options) {
|
65 | if (utils.isValidPath(pointer)) {
|
66 | if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
|
67 | const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd || process.cwd(), pointer);
|
68 | return fs.existsSync(normalizedFilePath);
|
69 | }
|
70 | }
|
71 | return false;
|
72 | }
|
73 | _buildGlobs(glob, options) {
|
74 | const ignores = utils.asArray(options.ignore || []);
|
75 | const globs = [unixify(glob), ...ignores.map(v => buildIgnoreGlob(unixify(v)))];
|
76 | return globs;
|
77 | }
|
78 | async resolveGlobs(glob, options) {
|
79 | const globs = this._buildGlobs(glob, options);
|
80 | const result = await globby(globs, createGlobbyOptions(options));
|
81 | return result;
|
82 | }
|
83 | resolveGlobsSync(glob, options) {
|
84 | const globs = this._buildGlobs(glob, options);
|
85 | const result = globby.sync(globs, createGlobbyOptions(options));
|
86 | return result;
|
87 | }
|
88 | async load(pointer, options) {
|
89 | const resolvedPaths = await this.resolveGlobs(pointer, options);
|
90 | const finalResult = [];
|
91 | await Promise.all(resolvedPaths.map(async (path$1) => {
|
92 | if (await this.canLoad(path$1, options)) {
|
93 | const normalizedFilePath = path.isAbsolute(path$1) ? path$1 : path.resolve(options.cwd || process.cwd(), path$1);
|
94 | const rawSDL = await readFile(normalizedFilePath, { encoding: 'utf8' });
|
95 | finalResult.push(this.handleFileContent(rawSDL, normalizedFilePath, options));
|
96 | }
|
97 | }));
|
98 | return finalResult;
|
99 | }
|
100 | loadSync(pointer, options) {
|
101 | const resolvedPaths = this.resolveGlobsSync(pointer, options);
|
102 | const finalResult = [];
|
103 | for (const path$1 of resolvedPaths) {
|
104 | if (this.canLoadSync(path$1, options)) {
|
105 | const normalizedFilePath = path.isAbsolute(path$1) ? path$1 : path.resolve(options.cwd || process.cwd(), path$1);
|
106 | const rawSDL = fs.readFileSync(normalizedFilePath, { encoding: 'utf8' });
|
107 | finalResult.push(this.handleFileContent(rawSDL, normalizedFilePath, options));
|
108 | }
|
109 | }
|
110 | return finalResult;
|
111 | }
|
112 | handleFileContent(rawSDL, pointer, options) {
|
113 | if (!options.skipGraphQLImport && isGraphQLImportFile(rawSDL)) {
|
114 | const document = _import.processImport(pointer, options.cwd);
|
115 | return {
|
116 | location: pointer,
|
117 | document,
|
118 | };
|
119 | }
|
120 | return utils.parseGraphQLSDL(pointer, rawSDL, options);
|
121 | }
|
122 | }
|
123 |
|
124 | exports.GraphQLFileLoader = GraphQLFileLoader;
|