UNPKG

@graphql-mesh/utils

Version:
43 lines (42 loc) 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.printWithCache = void 0; exports.parseWithCache = parseWithCache; exports.gql = gql; const graphql_1 = require("graphql"); const utils_1 = require("@graphql-tools/utils"); const global_lru_cache_js_1 = require("./global-lru-cache.js"); const parseCache = (0, global_lru_cache_js_1.createLruCache)(1000, 3600); const printCache = (0, global_lru_cache_js_1.createLruCache)(1000, 3600); function parseWithCache(sdl) { const trimmedSdl = sdl.trim(); let document = parseCache.get(trimmedSdl); if (!document) { document = (0, graphql_1.parse)(trimmedSdl, { noLocation: true }); parseCache.set(trimmedSdl, document); printCache.set(JSON.stringify(document), trimmedSdl); } return document; } exports.printWithCache = (0, utils_1.memoize1)(function printWithCache(document) { const stringifedDocumentJson = JSON.stringify(document); let sdl = printCache.get(stringifedDocumentJson); if (!sdl) { sdl = (0, graphql_1.print)(document).trim(); printCache.set(stringifedDocumentJson, sdl); parseCache.set(sdl, document); } return sdl; }); function gql([sdl], ...args) { let result = sdl; for (const arg of args || []) { if (typeof arg === 'string') { result += arg; } else { result += (0, exports.printWithCache)(arg); } } return parseWithCache(result); }