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

Feature/optimize interfaces #4574

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion packages/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@apollo/gateway": "2.7.0",
"@apollo/server": "4.10.0",
"@faker-js/faker": "8.3.1",
"@neo4j/cypher-builder": "../../../cypher-builder",
"@types/deep-equal": "1.0.4",
"@types/is-uuid": "1.0.2",
"@types/jest": "29.5.11",
Expand Down Expand Up @@ -91,7 +92,6 @@
"@graphql-tools/resolvers-composition": "^7.0.0",
"@graphql-tools/schema": "10.0.2",
"@graphql-tools/utils": "^10.0.0",
"@neo4j/cypher-builder": "^1.7.1",
"camelcase": "^6.3.0",
"debug": "^4.3.4",
"deep-equal": "^2.0.5",
Expand All @@ -105,6 +105,7 @@
"typescript-memoize": "^1.1.1"
},
"peerDependencies": {
"@neo4j/cypher-builder": "^1.7.0",
"graphql": "^16.0.0",
"neo4j-driver": "^5.8.0"
}
Expand Down
129 changes: 81 additions & 48 deletions packages/graphql/src/translate/create-projection-and-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import { createAuthorizationBeforePredicateField } from "./authorization/create-
import { checkAuthentication } from "./authorization/check-authentication";
import { compileCypher } from "../utils/compile-cypher";
import type { Neo4jGraphQLTranslationContext } from "../types/neo4j-graphql-translation-context";
import { uniqSubQueries } from "./queryAST/ast/operations/composite/optimization";
import { UNION_UNIFICATION_ENABLED } from "./queryAST/ast/operations/optimizationSettings";

interface Res {
projection: Cypher.Expr[];
Expand Down Expand Up @@ -141,7 +143,8 @@ export default function createProjectionAndParams({

const subqueryReturnAlias = new Cypher.Variable();
if (relationField.interface || relationField.union) {
let referenceNodes;
let isSelectingAllChildren = false;
let referenceNodes: Node[];
if (relationField.interface) {
const interfaceImplementations = context.nodes.filter((x) =>
relationField.interface?.implementations?.includes(x.name)
Expand Down Expand Up @@ -172,6 +175,9 @@ export default function createProjectionAndParams({
// where exists and has a filter on this implementation
Object.prototype.hasOwnProperty.call(field.args.where, x.name)
);

isSelectingAllChildren =
relationField.interface && interfaceImplementations.length === referenceNodes.length;
} else {
referenceNodes = context.nodes.filter(
(x) =>
Expand All @@ -182,54 +188,81 @@ export default function createProjectionAndParams({

const parentNode = varName;

const unionSubqueries: Cypher.Clause[] = [];
for (const refNode of referenceNodes) {
const targetNode = new Cypher.Node({ labels: refNode.getLabels(context) });
const recurse = createProjectionAndParams({
resolveTree: field,
node: refNode,
context,
varName: targetNode,
cypherFieldAliasMap,
});
res.params = { ...res.params, ...recurse.params };

const direction = getCypherRelationshipDirection(relationField, field.args);

const nestedProjection = new Cypher.Raw((env) => {
// The nested projection will be surrounded by brackets, so we want to remove
// any linebreaks, and then the first opening and the last closing bracket of the line,
// as well as any surrounding whitespace.
const nestedProj = compileCypher(recurse.projection, env).replaceAll(
/(^\s*{\s*)|(\s*}\s*$)/g,
""
);
const matchByInterfaceOrUnion =
UNION_UNIFICATION_ENABLED && isSelectingAllChildren
? relationField.interface?.typeMeta.name
: undefined;

return `{ __resolveType: "${refNode.name}", __id: id(${compileCypher(varName, env)})${
nestedProj && `, ${nestedProj}`
} }`;
});

const subquery = createProjectionSubquery({
parentNode,
whereInput: field.args.where ? field.args.where[refNode.name] : {},
node: refNode,
context,
subqueryReturnAlias,
nestedProjection,
nestedSubqueries: [...recurse.subqueriesBeforeSort, ...recurse.subqueries],
targetNode,
relationField,
relationshipDirection: direction,
optionsInput,
addSkipAndLimit: false,
collect: false,
nestedPredicates: recurse.predicates,
});

const unionWith = new Cypher.With("*");
unionSubqueries.push(Cypher.concat(unionWith, subquery));
}
const unionSubqueries = uniqSubQueries(
context,
matchByInterfaceOrUnion,
referenceNodes,
(node) => node,
(subs) => {
const unionSubqueries: Cypher.Clause[][] = [];
for (const { child: refNode, unifyViaDataModelType, exclusionPredicates } of subs) {
const labels =
unifyViaDataModelType && context.labelManager
? context.labelManager.getLabelSelectorExpressionObject(unifyViaDataModelType)
: refNode.getLabels(context);

const targetNode = new Cypher.Node({ labels });
const recurse = createProjectionAndParams({
resolveTree: field,
node: refNode,
context,
varName: targetNode,
cypherFieldAliasMap,
});

res.params = { ...res.params, ...recurse.params };

const combinedPredicates = [
...(recurse.predicates ?? []),
...(exclusionPredicates?.(targetNode) ?? []),
];

const direction = getCypherRelationshipDirection(relationField, field.args);

const nestedProjection = new Cypher.Raw((env) => {
// The nested projection will be surrounded by brackets, so we want to remove
// any linebreaks, and then the first opening and the last closing bracket of the line,
// as well as any surrounding whitespace.
const nestedProj = compileCypher(recurse.projection, env).replaceAll(
/(^\s*{\s*)|(\s*}\s*$)/g,
""
);

return `{ __resolveType: ${
UNION_UNIFICATION_ENABLED && context.labelManager?.hasMainType(refNode.name)
? new Cypher.Property(targetNode, "mainType").getCypher(env)
: `"${refNode.name}"`
}, __id: id(${compileCypher(varName, env)})${nestedProj && `, ${nestedProj}`} }`;
});

const subquery = createProjectionSubquery({
parentNode,
whereInput: field.args.where ? field.args.where[refNode.name] : {},
node: refNode,
context,
subqueryReturnAlias,
nestedProjection,
nestedSubqueries: [...recurse.subqueriesBeforeSort, ...recurse.subqueries],
targetNode,
relationField,
relationshipDirection: direction,
optionsInput,
addSkipAndLimit: false,
collect: false,
nestedPredicates: combinedPredicates,
});

const unionWith = new Cypher.With("*");
unionSubqueries.push([Cypher.concat(unionWith, subquery)]);
}
return unionSubqueries;
}
).flat();

const unionClause = new Cypher.Union(...unionSubqueries);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ export class ConnectionFilter extends Filter {
protected getLabelPredicate(context: QueryASTContext): Cypher.Predicate | undefined {
if (!hasTarget(context)) throw new Error("No parent node found!");
if (isConcreteEntity(this.target)) return undefined;
if (context.neo4jGraphQLContext.labelManager) {
const filterExpr = context.neo4jGraphQLContext.labelManager.getLabelSelectorExpression(this.target.name);
return new Cypher.Raw((env) => `${context.target.getCypher(env)}:${filterExpr}`);
}
const labelPredicate = this.target.concreteEntities.map((e) => {
return context.target.hasLabels(...e.labels);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { CypherPropertySort } from "../sort/CypherPropertySort";
import type { Sort } from "../sort/Sort";
import type { OperationTranspileResult } from "./operations";
import { Operation } from "./operations";
import { READ_LOWER_TARGET_INTERFACE_ENABLED } from "./optimizationSettings";

export class ReadOperation extends Operation {
public readonly target: ConcreteEntityAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type { Sort, SortField } from "../../sort/Sort";
import type { OperationTranspileResult } from "../operations";
import { Operation } from "../operations";
import type { CompositeReadPartial } from "./CompositeReadPartial";
import { uniqSubQueries } from "./optimization";

export class CompositeReadOperation extends Operation {
private children: CompositeReadPartial[];
Expand Down Expand Up @@ -57,15 +58,25 @@ export class CompositeReadOperation extends Operation {

public transpile(context: QueryASTContext): OperationTranspileResult {
const parentNode = context.target;
const nestedSubqueries = this.children.flatMap((c) => {
const result = c.transpile(context);

let clauses = result.clauses;
if (parentNode) {
clauses = clauses.map((sq) => Cypher.concat(new Cypher.With("*"), sq));
}
return clauses;
});
const isSelectingAllChildren = this.entity.concreteEntities.length === this.children.length;
const matchByInterfaceOrUnion = isSelectingAllChildren ? this.entity.name : undefined;
const nestedSubqueries = uniqSubQueries(
context.neo4jGraphQLContext,
matchByInterfaceOrUnion,
this.children,
(child) => child.target,
(subs) =>
subs.map(({ child, unifyViaDataModelType, exclusionPredicates }) => {
const result = child.transpile(context, unifyViaDataModelType, exclusionPredicates);

let clauses = result.clauses;
if (parentNode) {
clauses = clauses.map((sq) => Cypher.concat(new Cypher.With("*"), sq));
}
return clauses;
})
).flat();

let aggrExpr: Cypher.Expr = Cypher.collect(context.returnVariable);
if (!this.relationship) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,30 @@ import type { QueryASTContext } from "../../QueryASTContext";
import type { SelectionClause } from "../../selection/EntitySelection";
import { ReadOperation } from "../ReadOperation";
import type { OperationTranspileResult } from "../operations";
import { UNION_UNIFICATION_ENABLED } from "../optimizationSettings";

export class CompositeReadPartial extends ReadOperation {
public transpile(context: QueryASTContext) {
public transpile(
context: QueryASTContext,
matchByInterfaceOrUnion?: string,
exclusionPredicates?: (matchNode: Cypher.Node) => Cypher.Predicate[]
) {
if (this.relationship) {
return this.transpileNestedCompositeRelationship(this.relationship, context);
return this.transpileNestedCompositeRelationship(context, matchByInterfaceOrUnion, exclusionPredicates);
} else {
return this.transpileTopLevelCompositeEntity(context);
}
}

private transpileNestedCompositeRelationship(
entity: RelationshipAdapter,
context: QueryASTContext
context: QueryASTContext,
matchByInterfaceOrUnion?: string,
exclusionPredicates?: (matchNode: Cypher.Node) => Cypher.Predicate[]
): OperationTranspileResult {
if (!hasTarget(context)) throw new Error("No parent node found!");

// eslint-disable-next-line prefer-const
let { selection: matchClause, nestedContext } = this.selection.apply(context);
let { selection: matchClause, nestedContext } = this.selection.apply(context, matchByInterfaceOrUnion);

let extraMatches: SelectionClause[] = this.getChildren().flatMap((f) => {
return f.getSelection(nestedContext);
Expand All @@ -56,7 +62,11 @@ export class CompositeReadPartial extends ReadOperation {
const authFilterSubqueries = this.getAuthFilterSubqueries(nestedContext);
const authFiltersPredicate = this.getAuthFilterPredicate(nestedContext);

const wherePredicate = Cypher.and(filterPredicates, ...authFiltersPredicate);
const wherePredicate = Cypher.and(
filterPredicates,
...authFiltersPredicate,
...(exclusionPredicates?.(nestedContext.target) ?? [])
);
if (wherePredicate) {
// NOTE: This is slightly different to ReadOperation for cypher compatibility, this could use `WITH *`
matchClause.where(wherePredicate);
Expand Down Expand Up @@ -124,7 +134,10 @@ export class CompositeReadPartial extends ReadOperation {

const targetNodeName = this.target.name;
projection.set({
__resolveType: new Cypher.Literal(targetNodeName),
__resolveType:
UNION_UNIFICATION_ENABLED && context.neo4jGraphQLContext.labelManager?.hasMainType(targetNodeName)
? new Cypher.Property(context.target, "mainType")
: new Cypher.Literal(targetNodeName),
__id: Cypher.id(context.target),
});

Expand Down
Loading
Loading