UNPKG

gql-dedup

Version:

A graphql-tag improvement library which de-duplicates fragments.

33 lines 1.5 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const graphql_tag_1 = __importDefault(require("graphql-tag")); const cache = new Map(); function gql(strings, ...docs) { var _a; const parts = [trim(strings[0])]; for (let i = 0; i < docs.length; i++) { const doc = docs[i]; let body = (_a = doc.loc) === null || _a === void 0 ? void 0 : _a.source.body; if (!body) { throw Error(`Document must have a source: ${JSON.stringify(doc)}`); } const cacheParts = cache.get(body); if (!cacheParts) { throw Error(`Are you sure you don't mix gql-dedup usage with the plain graphql-tag? (If you use tsc compiler, also make sure to check if the compiler output file is not stale; tsc has some bugs, especially during incremental builds.) We can't find the following document node in gql-dedup registry: [${body}]`); } parts.push(...cacheParts); parts.push(trim(strings[i + 1])); } const uniqueParts = [...new Set(parts).keys()]; const uniqueBody = uniqueParts.filter((part) => part !== "").join("\n"); cache.set(uniqueBody, uniqueParts); return (0, graphql_tag_1.default)(uniqueBody); } exports.default = gql; function trim(str) { return str.replace(/^([ \t\r]*\n)+/, "").trimRight(); } //# sourceMappingURL=index.js.map