UNPKG

1.18 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5const common = require('@graphql-toolkit/common');
6const path = require('path');
7const fs = require('fs');
8
9const GQL_EXTENSIONS = ['.gql', '.graphql', '.graphqls'];
10class 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
33exports.GraphQLFileLoader = GraphQLFileLoader;