Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregation filters #5879

Merged
merged 6 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/silly-toys-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@neo4j/graphql": patch
---

Add generic filters for aggregations:

```graphql
{
posts(where: { likesAggregate: { node: { rating: { average: { eq: 3.2 } } } } }) {
title
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.
*/

import { GraphQLInputObjectType } from "graphql";
import { BigIntScalarFilters } from "../generic-operators/BigIntScalarFilters";

export const BigIntScalarAggregationFilters = new GraphQLInputObjectType({
name: "BigIntScalarAggregationFilters",
description: "Filters for an aggregation of an BigInt field",
fields: {
average: { type: BigIntScalarFilters },
max: { type: BigIntScalarFilters },
min: { type: BigIntScalarFilters },
sum: { type: BigIntScalarFilters },
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/

import { GraphQLInputObjectType } from "graphql";
import { DateTimeScalarFilters } from "../generic-operators/DateTimeScalarFilters";

export const DateTimeScalarAggregationFilters = new GraphQLInputObjectType({
name: "DateTimeScalarAggregationFilters",
description: "Filters for an aggregation of an DateTime input field",
fields: {
max: { type: DateTimeScalarFilters },
min: { type: DateTimeScalarFilters },
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.
*/

import { GraphQLInputObjectType } from "graphql";
import { DurationScalarFilters } from "../generic-operators/DurationScalarFilters";

export const DurationScalarAggregationFilters = new GraphQLInputObjectType({
name: "DurationScalarAggregationFilters",
description: "Filters for an aggregation of a Dutation input field",
fields: {
max: { type: DurationScalarFilters },
min: { type: DurationScalarFilters },
average: { type: DurationScalarFilters },
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.
*/

import { GraphQLInputObjectType } from "graphql";
import { FloatScalarFilters } from "../generic-operators/FloatScalarFilters";

export const FloatScalarAggregationFilters = new GraphQLInputObjectType({
name: "FloatScalarAggregationFilters",
description: "Filters for an aggregation of a float field",
fields: {
average: { type: FloatScalarFilters },
max: { type: FloatScalarFilters },
min: { type: FloatScalarFilters },
sum: { type: FloatScalarFilters },
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/

import { GraphQLInputObjectType } from "graphql";
import { IDScalarFilters } from "../generic-operators/IDScalarFilters";

export const IDScalarAggregationFilters = new GraphQLInputObjectType({
name: "IDScalarAggregationFilters",
description: "Filters for an aggregation of an ID input field",
fields: {
max: { type: IDScalarFilters },
min: { type: IDScalarFilters },
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/

import { GraphQLInputObjectType } from "graphql";
import { FloatScalarFilters } from "../generic-operators/FloatScalarFilters";
import { IntScalarFilters } from "../generic-operators/IntScalarFilters";

export const IntScalarAggregationFilters = new GraphQLInputObjectType({
name: "IntScalarAggregationFilters",
description: "Filters for an aggregation of an int field",
fields: {
average: { type: FloatScalarFilters },
max: { type: IntScalarFilters },
min: { type: IntScalarFilters },
sum: { type: IntScalarFilters },
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/

import { GraphQLInputObjectType } from "graphql";
import { LocalDateTimeScalarFilters } from "../generic-operators/LocalDateTimeScalarFilters";

export const LocalDateTimeScalarAggregationFilters = new GraphQLInputObjectType({
name: "LocalDateTimeScalarAggregationFilters",
description: "Filters for an aggregation of an LocalDateTime input field",
fields: {
max: { type: LocalDateTimeScalarFilters },
min: { type: LocalDateTimeScalarFilters },
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/

import { GraphQLInputObjectType } from "graphql";
import { LocalTimeScalarFilters } from "../generic-operators/LocalTimeScalarFilters";

export const LocalTimeScalarAggregationFilters = new GraphQLInputObjectType({
name: "LocalTimeScalarAggregationFilters",
description: "Filters for an aggregation of an LocalTime input field",
fields: {
max: { type: LocalTimeScalarFilters },
min: { type: LocalTimeScalarFilters },
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.
*/

import { GraphQLInputObjectType } from "graphql";
import { FloatScalarFilters } from "../generic-operators/FloatScalarFilters";
import { IntScalarFilters } from "../generic-operators/IntScalarFilters";

export const StringScalarAggregationFilters = new GraphQLInputObjectType({
name: "StringScalarAggregationFilters",
description: "Filters for an aggregation of a string field",
fields: {
averageLength: { type: FloatScalarFilters },
shortestLength: { type: IntScalarFilters },
longestLength: { type: IntScalarFilters },
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/

import { GraphQLInputObjectType } from "graphql";
import { TimeScalarFilters } from "../generic-operators/TimeScalarFilters";

export const TimeScalarAggregationFilters = new GraphQLInputObjectType({
name: "TimeScalarAggregationFilters",
description: "Filters for an aggregation of an Time input field",
fields: {
max: { type: TimeScalarFilters },
min: { type: TimeScalarFilters },
},
});
8 changes: 8 additions & 0 deletions packages/graphql/src/schema/generation/aggregate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
SchemaComposer,
} from "graphql-compose";
import { AGGREGATION_COMPARISON_OPERATORS, DEPRECATED } from "../../constants";
import { IntScalarFilters } from "../../graphql/input-objects/generic-operators/IntScalarFilters";
import type { AttributeAdapter } from "../../schema-model/attribute/model-adapters/AttributeAdapter";
import { ConcreteEntityAdapter } from "../../schema-model/entity/model-adapters/ConcreteEntityAdapter";
import { InterfaceEntityAdapter } from "../../schema-model/entity/model-adapters/InterfaceEntityAdapter";
Expand All @@ -35,6 +36,7 @@ import type { Neo4jFeaturesSettings } from "../../types";
import type { AggregationTypesMapper } from "../aggregations/aggregation-types-mapper";
import { numericalResolver } from "../resolvers/field/numerical";
import { graphqlDirectivesToCompose } from "../to-compose";
import { getAggregationFilterFromAttributeType } from "./get-aggregation-filter-from-attribute-type";

export function withAggregateSelectionType({
entityAdapter,
Expand Down Expand Up @@ -106,6 +108,7 @@ export function withAggregateInputType({
count_LTE: GraphQLInt,
count_GT: GraphQLInt,
count_GTE: GraphQLInt,
count: IntScalarFilters,
},
});

Expand Down Expand Up @@ -190,6 +193,9 @@ function makeAggregationFields(
const fields: InputTypeComposerFieldConfigMapDefinition = {};
for (const attribute of attributes) {
addAggregationFieldsByType(attribute, userDefinedDirectivesOnTargetFields?.get(attribute.name), fields);
if (attribute.isAggregationWhereField()) {
fields[attribute.name] = getAggregationFilterFromAttributeType(attribute);
}
}
return fields;
}
Expand Down Expand Up @@ -218,6 +224,7 @@ function addAggregationFieldsByType(
directives: deprecatedDirectives,
};
}

return fields;
}
if (attribute.typeHelper.isNumeric() || attribute.typeHelper.isDuration()) {
Expand Down Expand Up @@ -247,6 +254,7 @@ function addAggregationFieldsByType(
: GraphQLFloat;
fields[`${attribute.name}_AVERAGE_${operator}`] = { type: averageType, directives: deprecatedDirectives };
}

return fields;
}
for (const operator of AGGREGATION_COMPARISON_OPERATORS) {
Expand Down
Loading