Skip to content

Commit c49e8b0

Browse files
authored
Merge pull request #7027 from neo4j/7026-cypher-error-var-not-defined-when-using-totalcount-with-aggregations
fix #7026
2 parents cdf3ff5 + eba4216 commit c49e8b0

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

.changeset/short-paths-bet.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
"@neo4j/graphql": patch
3+
---
4+
5+
Fix failing query when aggregation and totalCount is queried in a connection, but not edges. For example:
6+
7+
```graphql
8+
type Actor @node {
9+
name: String!
10+
}
11+
```
12+
13+
```graphql
14+
query {
15+
actorsConnection {
16+
totalCount
17+
aggregate {
18+
node {
19+
name {
20+
shortest
21+
}
22+
}
23+
count {
24+
nodes
25+
}
26+
}
27+
}
28+
}
29+
```

packages/graphql/src/translate/queryAST/ast/operations/ConnectionReadOperation.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ export class ConnectionReadOperation extends Operation {
248248
);
249249

250250
if (aggregationSubqueries.length > 0) {
251-
const returnClause = new Cypher.Return(edgesProjectionVar);
251+
const returnClause = new Cypher.Return();
252+
if (hasProjectionFields) {
253+
returnClause.addColumns(edgesProjectionVar);
254+
}
252255
if (this.hasTotalCount) {
253256
returnClause.addColumns(totalCount);
254257
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import type { UniqueType } from "../../utils/graphql-types";
21+
import { TestHelper } from "../../utils/tests-helper";
22+
23+
describe("https://github.com/neo4j/graphql/issues/6917", () => {
24+
let Actor: UniqueType;
25+
26+
const testHelper = new TestHelper();
27+
28+
beforeAll(async () => {
29+
Actor = testHelper.createUniqueType("Actor");
30+
31+
const typeDefs = /* GraphQL */ `
32+
type ${Actor} @node {
33+
name: String!
34+
}
35+
`;
36+
37+
await testHelper.initNeo4jGraphQL({
38+
typeDefs,
39+
});
40+
41+
await testHelper.executeCypher(`
42+
CREATE(:${Actor} {name: "Keanu"})
43+
CREATE(:${Actor} {name: "Pepe"})
44+
`);
45+
});
46+
47+
afterAll(async () => {
48+
await testHelper.close();
49+
});
50+
51+
test("should return totalCount and aggregate, without edges", async () => {
52+
const query = /* GraphQL */ `
53+
query {
54+
${Actor.operations.connection} {
55+
totalCount
56+
aggregate {
57+
node {
58+
name {
59+
shortest
60+
}
61+
}
62+
count {
63+
nodes
64+
}
65+
}
66+
}
67+
}
68+
`;
69+
70+
const result = await testHelper.executeGraphQL(query);
71+
expect(result.errors).toBeUndefined();
72+
expect(result.data as any).toEqual({
73+
[Actor.operations.connection]: {
74+
totalCount: 2,
75+
aggregate: {
76+
node: {
77+
name: {
78+
shortest: "Pepe",
79+
},
80+
},
81+
count: {
82+
nodes: 2,
83+
},
84+
},
85+
},
86+
});
87+
});
88+
});

0 commit comments

Comments
 (0)