From ca16dcf93922d63dd1783dc471b2ac0c61f8d11f Mon Sep 17 00:00:00 2001 From: "netop://ウィビ" Date: Sat, 11 Apr 2026 14:19:10 -0700 Subject: initial commit --- src/import.ts | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/import.ts (limited to 'src/import.ts') diff --git a/src/import.ts b/src/import.ts new file mode 100644 index 0000000..ffc2c68 --- /dev/null +++ b/src/import.ts @@ -0,0 +1,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 { + 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 = 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 -- cgit v1.2.3