-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5553 from mjfwebb/feat/cypher-field-filtering
Custom cypher scalar field filtering
- Loading branch information
Showing
22 changed files
with
3,175 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@neo4j/graphql": minor | ||
--- | ||
|
||
Add filtering on scalar custom cypher fields |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
packages/graphql/src/translate/queryAST/ast/filters/property-filters/CypherFilter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* 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 Cypher from "@neo4j/cypher-builder"; | ||
import type { AttributeAdapter } from "../../../../../schema-model/attribute/model-adapters/AttributeAdapter"; | ||
import { createComparisonOperation } from "../../../utils/create-comparison-operator"; | ||
import type { QueryASTContext } from "../../QueryASTContext"; | ||
import type { QueryASTNode } from "../../QueryASTNode"; | ||
import type { CustomCypherSelection } from "../../selection/CustomCypherSelection"; | ||
import type { FilterOperator } from "../Filter"; | ||
import { Filter } from "../Filter"; | ||
import { coalesceValueIfNeeded } from "../utils/coalesce-if-needed"; | ||
import { createDurationOperation } from "../utils/create-duration-operation"; | ||
import { createPointOperation } from "../utils/create-point-operation"; | ||
|
||
/** A property which comparison has already been parsed into a Param */ | ||
export class CypherFilter extends Filter { | ||
private returnVariable: Cypher.Variable = new Cypher.Variable(); | ||
private attribute: AttributeAdapter; | ||
private selection: CustomCypherSelection; | ||
private operator: FilterOperator; | ||
protected comparisonValue: unknown; | ||
|
||
constructor({ | ||
selection, | ||
attribute, | ||
operator, | ||
comparisonValue, | ||
}: { | ||
selection: CustomCypherSelection; | ||
attribute: AttributeAdapter; | ||
operator: FilterOperator; | ||
comparisonValue: unknown; | ||
}) { | ||
super(); | ||
this.selection = selection; | ||
this.attribute = attribute; | ||
this.operator = operator; | ||
this.comparisonValue = comparisonValue; | ||
} | ||
|
||
public getChildren(): QueryASTNode[] { | ||
return [this.selection]; | ||
} | ||
|
||
public print(): string { | ||
return `${super.print()} [${this.attribute.name}] <${this.operator}>`; | ||
} | ||
|
||
public getSubqueries(context: QueryASTContext): Cypher.Clause[] { | ||
const { selection: cypherSubquery, nestedContext } = this.selection.apply(context); | ||
|
||
const clause = Cypher.concat( | ||
cypherSubquery, | ||
new Cypher.Return([nestedContext.returnVariable, this.returnVariable]) | ||
); | ||
|
||
return [clause]; | ||
} | ||
|
||
public getPredicate(_queryASTContext: QueryASTContext): Cypher.Predicate { | ||
const operation = this.createBaseOperation({ | ||
operator: this.operator, | ||
property: this.returnVariable, | ||
param: new Cypher.Param(this.comparisonValue), | ||
}); | ||
|
||
return operation; | ||
} | ||
|
||
/** Returns the default operation for a given filter */ | ||
private createBaseOperation({ | ||
operator, | ||
property, | ||
param, | ||
}: { | ||
operator: FilterOperator; | ||
property: Cypher.Expr; | ||
param: Cypher.Expr; | ||
}): Cypher.ComparisonOp { | ||
const coalesceProperty = coalesceValueIfNeeded(this.attribute, property); | ||
|
||
// This could be solved with specific a specific CypherDurationFilter but | ||
// we need to use the return variable for the cypher subquery. | ||
// To allow us to extend the DurationFilter class with a CypherDurationFilter class | ||
// we would need to have a way to provide the return variable | ||
// to the PropertyFilter's getPropertyRef method. | ||
if (this.attribute.typeHelper.isDuration()) { | ||
return createDurationOperation({ | ||
operator, | ||
property: coalesceProperty, | ||
param: new Cypher.Param(this.comparisonValue), | ||
}); | ||
} | ||
|
||
if (this.attribute.typeHelper.isSpatial()) { | ||
return createPointOperation({ | ||
operator, | ||
property: coalesceProperty, | ||
param: new Cypher.Param(this.comparisonValue), | ||
attribute: this.attribute, | ||
}); | ||
} | ||
|
||
return createComparisonOperation({ operator, property: coalesceProperty, param }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/graphql/src/translate/queryAST/ast/filters/utils/coalesce-if-needed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Cypher from "@neo4j/cypher-builder"; | ||
import type { AttributeAdapter } from "../../../../../schema-model/attribute/model-adapters/AttributeAdapter"; | ||
|
||
export function coalesceValueIfNeeded(attribute: AttributeAdapter, expr: Cypher.Expr): Cypher.Expr { | ||
if (attribute.annotations.coalesce) { | ||
const value = attribute.annotations.coalesce.value; | ||
const literal = new Cypher.Literal(value); | ||
return Cypher.coalesce(expr, literal); | ||
} | ||
return expr; | ||
} |
Oops, something went wrong.