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
56 lines (51 loc) • 1.54 kB
Flow
// @flow
import type { Plugin } from "graphile-build";
export default (function PgConnectionArgOrderByDefaultValue(builder) {
builder.hook(
"GraphQLObjectType:fields:field:args",
(args, build, context) => {
const {
extend,
getTypeByName,
pgGetGqlTypeByTypeIdAndModifier,
inflection,
} = build;
const {
scope: { fieldName, isPgFieldConnection, pgFieldIntrospection: table },
Self,
} = context;
if (
!isPgFieldConnection ||
!table ||
table.kind !== "class" ||
!table.namespace ||
!table.isSelectable ||
!args.orderBy
) {
return args;
}
const TableType = pgGetGqlTypeByTypeIdAndModifier(table.type.id, null);
const tableTypeName = TableType.name;
const TableOrderByType = getTypeByName(
inflection.orderByType(tableTypeName)
);
if (!TableOrderByType) {
return args;
}
const primaryKeyAsc = inflection.builtin("PRIMARY_KEY_ASC");
const defaultValueEnum =
TableOrderByType.getValues().find(v => v.name === primaryKeyAsc) ||
TableOrderByType.getValues()[0];
return extend(args, {
orderBy: extend(
args.orderBy,
{
defaultValue: defaultValueEnum && [defaultValueEnum.value],
},
`Adding defaultValue to orderBy for field '${fieldName}' of '${Self.name}'`
),
});
},
["PgConnectionArgOrderByDefaultValue"]
);
}: Plugin);