-
Notifications
You must be signed in to change notification settings - Fork 149
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 #5723 from mjfwebb/cypher-filtering-1-to-1-relatio…
…nships Cypher filtering 1 to 1 relationships
- Loading branch information
Showing
17 changed files
with
3,743 additions
and
77 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 1 to 1 relationship 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
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
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
111 changes: 111 additions & 0 deletions
111
packages/graphql/src/translate/queryAST/ast/filters/CypherOneToOneRelationshipFilter.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,111 @@ | ||
/* | ||
* 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 type { RelationshipWhereOperator } from "../../../where/types"; | ||
import type { QueryASTContext } from "../QueryASTContext"; | ||
import type { QueryASTNode } from "../QueryASTNode"; | ||
import type { CustomCypherSelection } from "../selection/CustomCypherSelection"; | ||
import { Filter } from "./Filter"; | ||
|
||
export class CypherOneToOneRelationshipFilter extends Filter { | ||
private returnVariable: Cypher.Node; | ||
private attribute: AttributeAdapter; | ||
private selection: CustomCypherSelection; | ||
private operator: RelationshipWhereOperator; | ||
private targetNodeFilters: Filter[] = []; | ||
private isNot: boolean; | ||
private isNull: boolean; | ||
|
||
constructor({ | ||
selection, | ||
attribute, | ||
operator, | ||
isNot, | ||
isNull, | ||
returnVariable, | ||
}: { | ||
selection: CustomCypherSelection; | ||
attribute: AttributeAdapter; | ||
operator: RelationshipWhereOperator; | ||
isNot: boolean; | ||
isNull: boolean; | ||
returnVariable: Cypher.Node; | ||
}) { | ||
super(); | ||
this.selection = selection; | ||
this.attribute = attribute; | ||
this.isNot = isNot; | ||
this.isNull = isNull; | ||
this.operator = operator; | ||
this.returnVariable = returnVariable; | ||
} | ||
|
||
public getChildren(): QueryASTNode[] { | ||
return [...this.targetNodeFilters, this.selection]; | ||
} | ||
|
||
public addTargetNodeFilter(...filter: Filter[]): void { | ||
this.targetNodeFilters.push(...filter); | ||
} | ||
|
||
public print(): string { | ||
return `${super.print()} [${this.attribute.name}] <${this.isNot ? "NOT " : ""}${this.operator}>`; | ||
} | ||
|
||
public getSubqueries(context: QueryASTContext): Cypher.Clause[] { | ||
const { selection, nestedContext } = this.selection.apply(context); | ||
|
||
const cypherSubquery = selection.return([ | ||
Cypher.head(Cypher.collect(nestedContext.returnVariable)), | ||
this.returnVariable, | ||
]); | ||
|
||
return [cypherSubquery]; | ||
} | ||
|
||
public getPredicate(queryASTContext: QueryASTContext): Cypher.Predicate | undefined { | ||
const context = queryASTContext.setTarget(this.returnVariable); | ||
|
||
const predicate = this.createRelationshipOperation(context); | ||
if (predicate) { | ||
return this.wrapInNotIfNeeded(predicate); | ||
} | ||
} | ||
|
||
private createRelationshipOperation(queryASTContext: QueryASTContext): Cypher.Predicate | undefined { | ||
const targetNodePredicates = this.targetNodeFilters.map((c) => c.getPredicate(queryASTContext)); | ||
const innerPredicate = Cypher.and(...targetNodePredicates); | ||
|
||
if (this.isNull) { | ||
return Cypher.and(innerPredicate, Cypher.isNull(this.returnVariable)); | ||
} | ||
|
||
return innerPredicate; | ||
} | ||
|
||
private wrapInNotIfNeeded(predicate: Cypher.Predicate): Cypher.Predicate { | ||
if (this.isNot) { | ||
return Cypher.not(predicate); | ||
} | ||
|
||
return predicate; | ||
} | ||
} |
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
Oops, something went wrong.