1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | const utils = require('@graphql-tools/utils');
|
6 | const path = require('path');
|
7 | const fsExtra = require('fs-extra');
|
8 | const process = require('process');
|
9 | const _import = require('@graphql-tools/import');
|
10 |
|
11 | const FILE_EXTENSIONS = ['.gql', '.gqls', '.graphql', '.graphqls'];
|
12 | function isGraphQLImportFile(rawSDL) {
|
13 | const trimmedRawSDL = rawSDL.trim();
|
14 | return trimmedRawSDL.startsWith('# import') || trimmedRawSDL.startsWith('#import');
|
15 | }
|
16 | class 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 |
|
60 | exports.GraphQLFileLoader = GraphQLFileLoader;
|
61 |
|