@neo4j/graphql
Version:
A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations
104 lines • 4.89 kB
JavaScript
"use strict";
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AggregationAttributeField = void 0;
const cypher_builder_1 = __importDefault(require("@neo4j/cypher-builder"));
const utils_1 = require("../../../../../utils/utils");
const AggregationField_1 = require("./AggregationField");
class AggregationAttributeField extends AggregationField_1.AggregationField {
constructor({ alias, attribute, aggregationProjection, }) {
super(alias);
this.attribute = attribute;
this.aggregationProjection = aggregationProjection;
}
getChildren() {
return [];
}
getProjectionField(variable) {
return { [this.alias]: variable };
}
getAggregationExpr(target) {
const variable = target.property(this.attribute.databaseName);
return this.createAggregationExpr(variable);
}
getAggregationProjection(target, returnVar) {
if (this.attribute.typeHelper.isString()) {
const aggrProp = target.property(this.attribute.databaseName);
const listVar = new cypher_builder_1.default.NamedVariable("list");
const projection = new cypher_builder_1.default.Return([this.createAggregationExpr(listVar), returnVar]);
return new cypher_builder_1.default.With(target)
.distinct()
.orderBy([cypher_builder_1.default.size(aggrProp), "DESC"])
.with([cypher_builder_1.default.collect(aggrProp), listVar])
.return(projection);
}
return new cypher_builder_1.default.With(target).distinct().return([this.getAggregationExpr(target), returnVar]);
}
createAggregationExpr(target) {
if (this.attribute.typeHelper.isString()) {
const listVar = new cypher_builder_1.default.NamedVariable("list");
return new cypher_builder_1.default.Map(this.filterProjection({
longest: cypher_builder_1.default.head(listVar),
shortest: cypher_builder_1.default.last(listVar),
}));
}
// NOTE: These are types that are treated as numeric by aggregation
if (this.attribute.typeHelper.isNumeric()) {
return new cypher_builder_1.default.Map(this.filterProjection({
min: cypher_builder_1.default.min(target),
max: cypher_builder_1.default.max(target),
average: cypher_builder_1.default.avg(target),
sum: cypher_builder_1.default.sum(target),
}));
}
if (this.attribute.typeHelper.isDateTime()) {
return new cypher_builder_1.default.Map(this.filterProjection({
min: this.createDatetimeProjection(cypher_builder_1.default.min(target)),
max: this.createDatetimeProjection(cypher_builder_1.default.max(target)),
}));
}
if (this.attribute.typeHelper.isTemporal()) {
return new cypher_builder_1.default.Map(this.filterProjection({
min: cypher_builder_1.default.min(target),
max: cypher_builder_1.default.max(target),
}));
}
if (this.attribute.typeHelper.isID()) {
return new cypher_builder_1.default.Map(this.filterProjection({
shortest: cypher_builder_1.default.min(target),
longest: cypher_builder_1.default.max(target),
}));
}
throw new Error(`Invalid aggregation type ${this.attribute.type.name}`);
}
// Filters and apply aliases in the projection
filterProjection(projectionFields) {
const filteredFields = (0, utils_1.filterFields)(projectionFields, Object.keys(this.aggregationProjection));
return (0, utils_1.renameFields)(filteredFields, this.aggregationProjection);
}
createDatetimeProjection(expr) {
return cypher_builder_1.default.apoc.date.convertFormat(expr, "iso_zoned_date_time", "iso_offset_date_time");
}
}
exports.AggregationAttributeField = AggregationAttributeField;
//# sourceMappingURL=AggregationAttributeField.js.map