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
59 lines (58 loc) • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var PgConnectionTotalCount = function PgConnectionTotalCount(builder) {
builder.hook("GraphQLObjectType:fields", (fields, build, context) => {
const {
extend,
inflection,
graphql: {
GraphQLInt,
GraphQLNonNull
},
pgSql: sql
} = build;
const {
scope: {
isPgRowConnectionType,
pgIntrospection: table,
nodeType
},
fieldWithHooks,
Self
} = context;
if (!isPgRowConnectionType) {
return fields;
}
const nodeTypeName = nodeType && nodeType.name ? nodeType.name : table && table.kind === "class" ? inflection.tableType(table) : null;
if (!nodeTypeName) {
return fields;
}
return extend(fields, {
totalCount: fieldWithHooks("totalCount", ({
addDataGenerator
}) => {
addDataGenerator(() => {
return {
pgAggregateQuery: aggregateQueryBuilder => {
aggregateQueryBuilder.select(sql.fragment`count(1)`, "totalCount");
}
};
});
return {
description: build.wrapDescription(`The count of *all* \`${nodeTypeName}\` you could get from the connection.`, "field"),
type: new GraphQLNonNull(GraphQLInt),
resolve(parent) {
return parent.aggregates && parent.aggregates.totalCount || 0;
}
};
}, {
isPgConnectionTotalCountField: true
})
}, `Adding totalCount to connection '${Self.name}'`);
}, ["PgConnectionTotalCount"]);
};
exports.default = PgConnectionTotalCount;
//# sourceMappingURL=PgConnectionTotalCount.js.map