graphile-build-pg
Version:
Build a GraphQL schema by reflection over a PostgreSQL schema. Easy to customize since it's built with plugins on graphile-build
60 lines (59 loc) • 2.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var PgOrderAllColumnsPlugin = function PgOrderAllColumnsPlugin(builder) {
builder.hook("GraphQLEnumType:values", (values, build, context) => {
const {
extend,
pgColumnFilter,
inflection,
pgOmit: omit,
describePgEntity,
sqlCommentByAddingTags
} = build;
const {
scope: {
isPgRowSortEnum,
pgIntrospection: table
}
} = context;
if (!isPgRowSortEnum || !table || table.kind !== "class") {
return values;
}
return extend(values, table.attributes.reduce((memo, attr) => {
// PERFORMANCE: These used to be .filter(...) calls
if (!pgColumnFilter(attr, build, context)) return memo;
if (omit(attr, "order")) return memo;
const unique = attr.isUnique;
const ascFieldName = inflection.orderByColumnEnum(attr, true);
const descFieldName = inflection.orderByColumnEnum(attr, false);
memo = extend(memo, {
[ascFieldName]: {
value: {
alias: ascFieldName.toLowerCase(),
specs: [[attr.name, true]],
unique
}
}
}, `Adding ascending orderBy enum value for ${describePgEntity(attr)}. You can rename this field with a 'Smart Comment':\n\n ${sqlCommentByAddingTags(attr, {
name: "newNameHere"
})}`);
memo = extend(memo, {
[descFieldName]: {
value: {
alias: descFieldName.toLowerCase(),
specs: [[attr.name, false]],
unique
}
}
}, `Adding descending orderBy enum value for ${describePgEntity(attr)}. You can rename this field with a 'Smart Comment':\n\n ${sqlCommentByAddingTags(attr, {
name: "newNameHere"
})}`);
return memo;
}, {}), `Adding order values from table '${table.name}'`);
}, ["PgOrderAllColumns"]);
};
exports.default = PgOrderAllColumnsPlugin;
//# sourceMappingURL=PgOrderAllColumnsPlugin.js.map