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

De-duplicate relationship projection when not using connection API #5936

Merged
merged 8 commits into from
Jan 14, 2025
Merged
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
7 changes: 7 additions & 0 deletions .changeset/few-sloths-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@neo4j/graphql": major
---

Changes the result projection where there are multiple relationships between two nodes.

In the case of using the connection API then multiple relationships will still be represented, as there is the ability to select the relationship properties. In the non-connection API case, the duplicate results will only return distinct results.
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,13 @@ export class ReadOperation extends Operation {
return filterTruthy(this.authFilters.map((f) => f.getPredicate(context)));
}

protected getProjectionClause(
context: QueryASTContext,
returnVariable: Cypher.Variable,
isArray: boolean
): Cypher.Return {
protected getProjectionClause(context: QueryASTContext, returnVariable: Cypher.Variable): Cypher.Return {
if (!hasTarget(context)) {
throw new Error("No parent node found!");
}
const projection = this.getProjectionMap(context);

let aggregationExpr: Cypher.Expr = Cypher.collect(context.target);
if (!isArray) {
aggregationExpr = Cypher.head(aggregationExpr);
}
const aggregationExpr = Cypher.collect(context.target);

const withClause = new Cypher.With([projection, context.target]);
if (this.sortFields.length > 0 || this.pagination) {
Expand Down Expand Up @@ -148,7 +141,7 @@ export class ReadOperation extends Operation {

const authFiltersPredicate = this.getAuthFilterPredicate(nestedContext);
const ret: Cypher.Return = this.relationship
? this.getProjectionClause(nestedContext, context.returnVariable, this.relationship.isList)
? this.getProjectionClause(nestedContext, context.returnVariable)
: this.getReturnStatement(
isCreateSelection || isUpdateSelection ? context : nestedContext,
nestedContext.returnVariable
Expand Down Expand Up @@ -215,6 +208,11 @@ export class ReadOperation extends Operation {
}
matchBlock.push(...extraMatches, extraMatchesWith);

if (this.relationship) {
const distinctTargetWith = new Cypher.With(nestedContext.target).distinct();
matchBlock.push(distinctTargetWith);
}

clause = Cypher.utils.concat(
...matchBlock,
...authFilterSubqueries,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* 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 type { UniqueType } from "../../../../utils/graphql-types";
import { TestHelper } from "../../../../utils/tests-helper";

describe("Create: Multiple relationships results difference between Connection API and Simple API", () => {
const testHelper: TestHelper = new TestHelper();
const Movie: UniqueType = testHelper.createUniqueType("Movie");
const Actor: UniqueType = testHelper.createUniqueType("Actor");

beforeEach(async () => {
const typeDefs = /* GraphQL */ `
type ${Movie} @node {
title: String!
actors: [${Actor}!]! @relationship(type: "ACTED_IN", properties: "ActedIn", direction: IN)
}

type ${Actor} @node {
name: String!
movies: [${Movie}!]! @relationship(type: "ACTED_IN", properties: "ActedIn", direction: OUT)
}

type ActedIn @relationshipProperties {
role: String!
}
`;
await testHelper.initNeo4jGraphQL({ typeDefs });
});

afterEach(async () => {
await testHelper.close();
});

test("should return multiple relationship results for connection API", async () => {
const source = /* GraphQL */ `
mutation {
${Movie.operations.create}(
input: [
{
title: "Movie One"
actors: {
create: [{ edge: { role: "Role One" }, node: { name: "Actor One" } }]
connect: [{ edge: { role: "Role Two" } }]
}
}
]
) {
${Movie.plural} {
title
actorsConnection {
edges {
properties {
role
}
node {
name
}
}
}
}
}
}
`;

const gqlResult = await testHelper.executeGraphQL(source);

expect(gqlResult.errors).toBeFalsy();
expect(gqlResult.data).toEqual({
[Movie.operations.create]: {
[Movie.plural]: [
{
title: "Movie One",
actorsConnection: {
edges: expect.toIncludeSameMembers([
{
node: {
name: "Actor One",
},
properties: {
role: "Role One",
},
},
{
node: {
name: "Actor One",
},
properties: {
role: "Role Two",
},
},
]),
},
},
],
},
});
});

test("should only return a single relationship result for simple API", async () => {
const source = /* GraphQL */ `
mutation {
${Movie.operations.create}(
input: [
{
title: "Movie One"
actors: {
create: [{ edge: { role: "Role One" }, node: { name: "Actor One" } }]
connect: [{ edge: { role: "Role Two" } }]
}
}
]
) {
${Movie.plural} {
title
actors {
name
}
}
}
}
`;

const gqlResult = await testHelper.executeGraphQL(source);

expect(gqlResult.errors).toBeFalsy();
expect(gqlResult.data).toEqual({
[Movie.operations.create]: {
[Movie.plural]: [
{
title: "Movie One",
actors: [
{
name: "Actor One",
},
],
},
],
},
});
});
});
Loading
Loading