/
index.ts
93 lines (82 loc) · 2.96 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Source, parseGraphQLJSON, Loader, isValidPath, BaseLoaderOptions } from '@graphql-tools/utils';
import { isAbsolute, resolve } from 'path';
import { readFileSync, promises as fsPromises, existsSync } from 'fs';
import { cwd } from 'process';
const { readFile, access } = fsPromises;
const FILE_EXTENSIONS = ['.json'];
/**
* Additional options for loading from a JSON file
*/
export interface JsonFileLoaderOptions extends BaseLoaderOptions {}
/**
* This loader loads documents and type definitions from JSON files.
*
* The JSON file can be the result of an introspection query made against a schema:
*
* ```js
* const schema = await loadSchema('schema-introspection.json', {
* loaders: [
* new JsonFileLoader()
* ]
* });
* ```
*
* Or it can be a `DocumentNode` object representing a GraphQL document or type definitions:
*
* ```js
* const documents = await loadDocuments('queries/*.json', {
* loaders: [
* new GraphQLFileLoader()
* ]
* });
* ```
*/
export class JsonFileLoader implements Loader {
async canLoad(pointer: string, options: JsonFileLoaderOptions): Promise<boolean> {
if (isValidPath(pointer)) {
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
try {
await access(normalizedFilePath);
return true;
} catch {
return false;
}
}
}
return false;
}
canLoadSync(pointer: string, options: JsonFileLoaderOptions): boolean {
if (isValidPath(pointer)) {
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
return existsSync(normalizedFilePath);
}
}
return false;
}
async load(pointer: string, options: JsonFileLoaderOptions): Promise<Source[]> {
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
if (!(await this.canLoad(normalizedFilePath, options))) {
return [];
}
try {
const jsonContent: string = await readFile(normalizedFilePath, { encoding: 'utf8' });
return [parseGraphQLJSON(pointer, jsonContent, options)];
} catch (e) {
throw new Error(`Unable to read JSON file: ${normalizedFilePath}: ${e.message || /* istanbul ignore next */ e}`);
}
}
loadSync(pointer: string, options: JsonFileLoaderOptions): Source[] {
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
if (!this.canLoadSync(normalizedFilePath, options)) {
return [];
}
try {
const jsonContent = readFileSync(normalizedFilePath, 'utf8');
return [parseGraphQLJSON(pointer, jsonContent, options)];
} catch (e) {
throw new Error(`Unable to read JSON file: ${normalizedFilePath}: ${e.message || /* istanbul ignore next */ e}`);
}
}
}