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

Fix for connection with multiple aliases #393

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

💡 Welcome to the Monorepo for [Neo4j](https://neo4j.com/) + [GraphQL](https://graphql.org/).

> We are working on Neo4j GraphQL 2.0.0, you can download the release candidate via `npm install @neo4j/graphql@next` and checkout the docs [here](https://neo4j.com/docs/graphql-manual/2.0/).

![Neo4j + GraphQL](./docs/images/banner.png)

<p align="center">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function createConnectionAndParams({
context,
nodeVariable: relatedNodeVariable,
parameterPrefix: `${parameterPrefix ? `${parameterPrefix}.` : `${nodeVariable}_`}${
resolveTree.name
resolveTree.alias
}.edges.node`,
});
nestedSubqueries.push(nestedConnection[0]);
Expand All @@ -141,7 +141,7 @@ function createConnectionAndParams({
...globalParams,
...Object.entries(nestedConnection[1]).reduce<Record<string, unknown>>(
(res, [k, v]) => {
if (k !== `${relatedNodeVariable}_${connectionResolveTree.name}`) {
if (k !== `${relatedNodeVariable}_${connectionResolveTree.alias}`) {
res[k] = v;
}
return res;
Expand All @@ -150,13 +150,15 @@ function createConnectionAndParams({
),
};

if (nestedConnection[1][`${relatedNodeVariable}_${connectionResolveTree.name}`]) {
if (nestedConnection[1][`${relatedNodeVariable}_${connectionResolveTree.alias}`]) {
if (!nestedConnectionFieldParams) nestedConnectionFieldParams = {};
nestedConnectionFieldParams = {
...nestedConnectionFieldParams,
...{
[connectionResolveTree.name]:
nestedConnection[1][`${relatedNodeVariable}_${connectionResolveTree.name}`],
[connectionResolveTree.alias]:
nestedConnection[1][
`${relatedNodeVariable}_${connectionResolveTree.alias}`
],
},
};
}
Expand Down Expand Up @@ -197,7 +199,7 @@ function createConnectionAndParams({
relationshipVariable,
context,
parameterPrefix: `${parameterPrefix ? `${parameterPrefix}.` : `${nodeVariable}_`}${
resolveTree.name
resolveTree.alias
}.args.where.${n.name}`,
});
const [whereClause] = where;
Expand Down Expand Up @@ -268,7 +270,7 @@ function createConnectionAndParams({
relationshipVariable,
context,
parameterPrefix: `${parameterPrefix ? `${parameterPrefix}.` : `${nodeVariable}_`}${
resolveTree.name
resolveTree.alias
}.args.where`,
});
const [whereClause] = where;
Expand Down Expand Up @@ -350,28 +352,28 @@ function createConnectionAndParams({
context,
nodeVariable: relatedNodeVariable,
parameterPrefix: `${parameterPrefix ? `${parameterPrefix}.` : `${nodeVariable}_`}${
resolveTree.name
resolveTree.alias
}.edges.node`,
});
nestedSubqueries.push(nestedConnection[0]);

globalParams = {
...globalParams,
...Object.entries(nestedConnection[1]).reduce<Record<string, unknown>>((res, [k, v]) => {
if (k !== `${relatedNodeVariable}_${connectionResolveTree.name}`) {
if (k !== `${relatedNodeVariable}_${connectionResolveTree.alias}`) {
res[k] = v;
}
return res;
}, {}),
};

if (nestedConnection[1][`${relatedNodeVariable}_${connectionResolveTree.name}`]) {
if (nestedConnection[1][`${relatedNodeVariable}_${connectionResolveTree.alias}`]) {
if (!nestedConnectionFieldParams) nestedConnectionFieldParams = {};
nestedConnectionFieldParams = {
...nestedConnectionFieldParams,
...{
[connectionResolveTree.name]:
nestedConnection[1][`${relatedNodeVariable}_${connectionResolveTree.name}`],
[connectionResolveTree.alias]:
nestedConnection[1][`${parameterPrefix}_${connectionResolveTree.alias}`],
},
};
}
Expand Down Expand Up @@ -403,7 +405,7 @@ function createConnectionAndParams({
const params = {
...globalParams,
...((whereInput || nestedConnectionFieldParams) && {
[`${nodeVariable}_${resolveTree.name}`]: {
[`${nodeVariable}_${resolveTree.alias}`]: {
...(whereInput && { args: { where: whereInput } }),
...(nestedConnectionFieldParams && { edges: { node: { ...nestedConnectionFieldParams } } }),
},
Expand Down
72 changes: 72 additions & 0 deletions packages/graphql/tests/integration/connections/alias.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { Driver } from "neo4j-driver";
import faker from "faker";
import { graphql } from "graphql";
import { generate } from "randomstring";
import { gql } from "apollo-server";
Expand Down Expand Up @@ -858,4 +859,75 @@ describe("Connections Alias", () => {
await session.close();
}
});

test("should allow multiple aliases on the same connection", async () => {
const session = driver.session();

const typeDefs = gql`
type Post {
title: String!
comments: [Comment!]! @relationship(type: "HAS_COMMENT", direction: OUT)
}

type Comment {
flag: Boolean!
post: Post! @relationship(type: "HAS_COMMENT", direction: IN)
}
`;

const { schema } = new Neo4jGraphQL({ typeDefs });

const flags = new Array(faker.random.number({ min: 2, max: 5 })).map(() => faker.random.boolean());

const flaggedCount = flags.filter((flag) => flag).length;
const unflaggedCount = flags.filter((flag) => !flag).length;

const query = `
{
posts {
flagged: commentsConnection(where: { node: { flag: true } }) {
edges {
node {
flag
}
}
}
unflagged: commentsConnection(where: { node: { flag: false } }) {
edges {
node {
flag
}
}
}
}
}
`;

try {
await session.run(
`
CREATE (post:Post {title: "title"})
FOREACH(flag in $flags |
CREATE (:Comment {flag: flag})<-[:HAS_COMMENT]-(post)
)
`,
{
flags,
}
);

const result = await graphql({
schema,
source: query,
contextValue: { driver, driverConfig: { bookmarks: [session.lastBookmark()] } },
});

expect(result.errors).toBeUndefined();

expect((result.data as any).posts[0].flagged.edges).toHaveLength(flaggedCount);
expect((result.data as any).posts[0].unflagged.edges).toHaveLength(unflaggedCount);
} finally {
await session.close();
}
});
});