blob: ffc2c68c95b4f32b5b80472a4b94da82362bbd81 (
plain) (
blame)
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
|
/// import
import { dirname, join } from "https://deno.land/std/path/mod.ts";
import { gql } from "./gql.ts";
import type { DocumentNode } from "npm:graphql@16.6.0";
/// util
const { cwd, readFileSync } = Deno;
const importRegex = /^#\s(import)\s.*(.graphql")/gm;
const fileRegex = /\w*(.graphql)/g;
/// export
export function importQL(path: string): DocumentNode | Record<string, unknown> {
try {
const decoder = new TextDecoder("utf-8");
const file = readFileSync(join(cwd(), String(path)));
const imports = decoder.decode(file).match(importRegex) || [];
let parsedFile = decoder.decode(file);
/// `import` statements in the supplied schema file
/// are parsed to dynamically bring in linked files
imports.map(imp => {
const matchedFilename: null | Array<string> = imp.match(fileRegex);
if (!matchedFilename || !matchedFilename.length || matchedFilename.length < 1)
return;
const filename = matchedFilename[0];
const importedFileDecoder = new TextDecoder("utf-8");
const importedFile = readFileSync(join(cwd(), dirname(String(path)), filename));
const decodedFile = importedFileDecoder.decode(importedFile);
parsedFile = parsedFile.replace(imp, decodedFile);
});
return gql`
${parsedFile}
`;
} catch(parseError) {
console.error(new Error(`error parsing file [${String(path)}]`), parseError);
return {};
}
}
/// fork of https://github.com/crewdevio/importql
|