1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | const common = require('@graphql-toolkit/common');
|
6 |
|
7 | const FILE_EXTENSIONS = ['.gql', '.gqls', '.graphql', '.graphqls'];
|
8 | class GraphQLFileLoader {
|
9 | loaderId() {
|
10 | return 'graphql-file';
|
11 | }
|
12 | async canLoad(pointer, options) {
|
13 | return this.canLoadSync(pointer, options);
|
14 | }
|
15 | canLoadSync(pointer, options) {
|
16 | if (common.isValidPath(pointer) && options.path && options.fs) {
|
17 | const { resolve, isAbsolute } = options.path;
|
18 | if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
|
19 | const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || process.cwd(), pointer);
|
20 | const { existsSync } = options.fs;
|
21 | if (existsSync(normalizedFilePath)) {
|
22 | return true;
|
23 | }
|
24 | }
|
25 | }
|
26 | return false;
|
27 | }
|
28 | async load(pointer, options) {
|
29 | return this.loadSync(pointer, options);
|
30 | }
|
31 | loadSync(pointer, options) {
|
32 | const { resolve, isAbsolute } = options.path;
|
33 | const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || process.cwd(), pointer);
|
34 | const { readFileSync } = options.fs;
|
35 | const rawSDL = readFileSync(normalizedFilePath, 'utf-8').trim();
|
36 | return common.parseGraphQLSDL(pointer, rawSDL, options);
|
37 | }
|
38 | }
|
39 |
|
40 | exports.GraphQLFileLoader = GraphQLFileLoader;
|