UNPKG

2.47 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5const utils = require('@graphql-tools/utils');
6const path = require('path');
7const fsExtra = require('fs-extra');
8const process = require('process');
9const _import = require('@graphql-tools/import');
10
11const FILE_EXTENSIONS = ['.gql', '.gqls', '.graphql', '.graphqls'];
12function isGraphQLImportFile(rawSDL) {
13 const trimmedRawSDL = rawSDL.trim();
14 return trimmedRawSDL.startsWith('# import') || trimmedRawSDL.startsWith('#import');
15}
16class GraphQLFileLoader {
17 loaderId() {
18 return 'graphql-file';
19 }
20 async canLoad(pointer, options) {
21 if (utils.isValidPath(pointer)) {
22 if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
23 const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd, pointer);
24 return fsExtra.pathExists(normalizedFilePath);
25 }
26 }
27 return false;
28 }
29 canLoadSync(pointer, options) {
30 if (utils.isValidPath(pointer)) {
31 if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
32 const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd, pointer);
33 return fsExtra.pathExistsSync(normalizedFilePath);
34 }
35 }
36 return false;
37 }
38 async load(pointer, options) {
39 const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd, pointer);
40 const rawSDL = await fsExtra.readFile(normalizedFilePath, { encoding: 'utf8' });
41 return this.handleFileContent(rawSDL, pointer, options);
42 }
43 loadSync(pointer, options) {
44 const cwd = options.cwd || process.cwd();
45 const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(cwd, pointer);
46 const rawSDL = fsExtra.readFileSync(normalizedFilePath, { encoding: 'utf8' });
47 return this.handleFileContent(rawSDL, pointer, options);
48 }
49 handleFileContent(rawSDL, pointer, options) {
50 if (!options.skipGraphQLImport && isGraphQLImportFile(rawSDL)) {
51 return {
52 location: pointer,
53 document: _import.processImport(pointer, options.cwd),
54 };
55 }
56 return utils.parseGraphQLSDL(pointer, rawSDL.trim(), options);
57 }
58}
59
60exports.GraphQLFileLoader = GraphQLFileLoader;
61//# sourceMappingURL=index.cjs.js.map