postgraphile-plugin-connection-filter
Version:
Filtering on PostGraphile connections
69 lines • 2.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isComputedScalarAttributeResource = isComputedScalarAttributeResource;
exports.getComputedAttributeResources = getComputedAttributeResources;
exports.makeAssertAllowed = makeAssertAllowed;
exports.isEmpty = isEmpty;
function isComputedScalarAttributeResource(s) {
if (!s.parameters || s.parameters.length < 1) {
return false;
}
if (s.codec.attributes) {
return false;
}
if (!s.isUnique) {
return false;
}
const firstParameter = s.parameters[0];
if (!firstParameter?.codec.attributes) {
return false;
}
return true;
}
function getComputedAttributeResources(build, source) {
const computedAttributeSources = Object.values(build.input.pgRegistry.pgResources).filter((s) => isComputedScalarAttributeResource(s) &&
s.parameters[0].codec === source.codec);
return computedAttributeSources;
}
// TODO: rename. (Checks that the arguments aren't null/empty.)
function makeAssertAllowed(build) {
const { options, EXPORTABLE } = build;
const { connectionFilterAllowNullInput, connectionFilterAllowEmptyObjectInput, } = options;
const assertAllowed = EXPORTABLE((connectionFilterAllowEmptyObjectInput, connectionFilterAllowNullInput, isEmpty) => function (value, mode) {
if (mode === "object" &&
!connectionFilterAllowEmptyObjectInput &&
isEmpty(value)) {
throw Object.assign(new Error("Empty objects are forbidden in filter argument input."), {
//TODO: mark this error as safe
});
}
if (mode === "list" && !connectionFilterAllowEmptyObjectInput) {
const arr = value;
if (arr) {
const l = arr.length;
for (let i = 0; i < l; i++) {
if (isEmpty(arr[i])) {
throw Object.assign(new Error("Empty objects are forbidden in filter argument input."), {
//TODO: mark this error as safe
});
}
}
}
}
// For all modes, check null
if (!connectionFilterAllowNullInput && value === null) {
throw Object.assign(new Error("Null literals are forbidden in filter argument input."), {
//TODO: mark this error as safe
});
}
}, [
connectionFilterAllowEmptyObjectInput,
connectionFilterAllowNullInput,
isEmpty,
]);
return assertAllowed;
}
function isEmpty(o) {
return typeof o === "object" && o !== null && Object.keys(o).length === 0;
}
//# sourceMappingURL=utils.js.map