graphile-build
Version:
Build a GraphQL schema from plugins
55 lines • 2.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.$$arrayHints = void 0;
exports.default = append;
const tslib_1 = require("tslib");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const grafast_1 = require("grafast");
const extend_ts_1 = require("./extend.js");
exports.$$arrayHints = Symbol("hints");
/**
* Appends the entries from `extra` into `base` tracking the `hint` as to when
* they were added. If a conflict is found (where `base` already has an entry
* with the same `identity(item)` as a new item) an error will be thrown
* describing what happened.
*/
function append(base, extra, getIdentity, hint) {
if (grafast_1.isDev && !(Array.isArray(base) && Array.isArray(extra))) {
throw new Error(`Both arguments must be arrays`);
}
const identify = typeof getIdentity === "function"
? getIdentity
: (entry) => entry[getIdentity];
base[exports.$$arrayHints] ??= [];
const hints = base[exports.$$arrayHints];
const existingIdentities = base.map(identify);
const extraHints = extra[exports.$$arrayHints];
for (let extraIndex = 0, extraL = extra.length; extraIndex < extraL; extraIndex++) {
const hintB = extraHints?.[extraIndex] || hint;
const entry = extra[extraIndex];
const identity = identify(entry);
const existingIndex = existingIdentities.indexOf(identity);
if (existingIndex >= 0) {
if (base[existingIndex] === entry) {
// Ignore duplicates
}
else {
// Throw error
const hintA = hints[existingIndex];
const firstEntityDetails = !hintA
? "We don't have any information about the first entity."
: `The first entity was:\n\n${(0, extend_ts_1.indent)(chalk_1.default.magenta(hintA))}`;
const secondEntityDetails = !hintB
? "We don't have any information about the second entity."
: `The second entity was:\n\n${(0, extend_ts_1.indent)(chalk_1.default.yellow(hintB))}`;
throw new Error(`A list conflict has occurred - two different entities with the same identifier '${chalk_1.default.bold(identity)}' were added.\n\n${(0, extend_ts_1.indent)(firstEntityDetails)}\n\n${(0, extend_ts_1.indent)(secondEntityDetails)}.\n Details: ${chalk_1.default.blue.bold.underline `https://err.red/pnc`}`);
}
}
else {
const idx = base.push(entry) - 1;
hints[idx] = hintB;
}
}
return base;
}
//# sourceMappingURL=append.js.map