UNPKG

4.62 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7const utils = require('@graphql-tools/utils');
8const path = require('path');
9const fs = require('fs');
10const process = require('process');
11const _import = require('@graphql-tools/import');
12const globby = _interopDefault(require('globby'));
13const unixify = _interopDefault(require('unixify'));
14
15const { readFile, access } = fs.promises;
16const FILE_EXTENSIONS = ['.gql', '.gqls', '.graphql', '.graphqls'];
17function isGraphQLImportFile(rawSDL) {
18 const trimmedRawSDL = rawSDL.trim();
19 return trimmedRawSDL.startsWith('# import') || trimmedRawSDL.startsWith('#import');
20}
21function createGlobbyOptions(options) {
22 return { absolute: true, ...options, ignore: [] };
23}
24const buildIgnoreGlob = (path) => `!${path}`;
25/**
26 * This loader loads documents and type definitions from `.graphql` files.
27 *
28 * You can load a single source:
29 *
30 * ```js
31 * const schema = await loadSchema('schema.graphql', {
32 * loaders: [
33 * new GraphQLFileLoader()
34 * ]
35 * });
36 * ```
37 *
38 * Or provide a glob pattern to load multiple sources:
39 *
40 * ```js
41 * const schema = await loadSchema('graphql/*.graphql', {
42 * loaders: [
43 * new GraphQLFileLoader()
44 * ]
45 * });
46 * ```
47 */
48class 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
124exports.GraphQLFileLoader = GraphQLFileLoader;