graphile-utils
Version:
Utilities to help with building graphile-build plugins
76 lines • 3.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.orderByAscDesc = void 0;
function makeAddPgTableOrderByPlugin(schemaName, tableName, ordersGenerator, hint = `Adding orders with makeAddPgTableOrderByPlugin to "${schemaName}"."${tableName}"`) {
const displayName = `makeAddPgTableOrderByPlugin_${schemaName}_${tableName}`;
const plugin = builder => {
builder.hook("GraphQLEnumType:values", (values, build, context) => {
const { extend } = build;
const { scope: { isPgRowSortEnum, pgIntrospection: table }, } = context;
if (!isPgRowSortEnum ||
!table ||
table.kind !== "class" ||
table.namespaceName !== schemaName ||
table.name !== tableName) {
return values;
}
const newValues = ordersGenerator(build);
return extend(values, newValues, hint);
});
};
plugin.displayName = displayName;
return plugin;
}
exports.default = makeAddPgTableOrderByPlugin;
function orderByAscDesc(baseName, columnOrSqlFragment, uniqueOrOptions = false) {
const options = typeof uniqueOrOptions === "boolean"
? { unique: uniqueOrOptions }
: uniqueOrOptions !== null && uniqueOrOptions !== void 0 ? uniqueOrOptions : {};
const { unique = false, nulls } = options;
if (typeof unique !== "boolean") {
throw new Error(`Invalid value for "unique" passed to orderByAscDesc for ${baseName}. Unique must be a boolean.`);
}
const isValidNullsOption = [
"first",
"last",
"first-iff-ascending",
"last-iff-ascending",
undefined,
].includes(nulls);
if (!isValidNullsOption) {
throw new Error(`Invalid value for "nulls" passed to orderByAscDesc for ${baseName}. Nulls must be sorted by one of: undefined | "first" | "last" | "first-iff-ascending" | "last-iff-ascending".`);
}
const defaultAscendingSpec = [columnOrSqlFragment, true];
const defaultDescendingSpec = [columnOrSqlFragment, false];
const ascendingSpec = typeof nulls === "undefined"
? defaultAscendingSpec
: [
...defaultAscendingSpec,
["first", "first-iff-ascending"].includes(nulls),
];
const descendingSpec = typeof nulls === "undefined"
? defaultDescendingSpec
: [
...defaultDescendingSpec,
["first", "last-iff-ascending"].includes(nulls),
];
const orders = {
[`${baseName}_ASC`]: {
value: {
alias: `${baseName}_ASC`,
specs: [ascendingSpec],
unique,
},
},
[`${baseName}_DESC`]: {
value: {
alias: `${baseName}_DESC`,
specs: [descendingSpec],
unique,
},
},
};
return orders;
}
exports.orderByAscDesc = orderByAscDesc;
//# sourceMappingURL=makeAddPgTableOrderByPlugin.js.map