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