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