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 #5467 #5470

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
5 changes: 5 additions & 0 deletions .changeset/tough-parents-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@neo4j/graphql": patch
---

Fix problem with parameters colliding in the cypher directive
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,19 @@ export function replaceArgumentsInStatement({
return statement;
}
const reg = new RegExp(`\\$(${argNames.join("|")})\\b`, "g");

const paramsRecord: Record<string, string> = {};
const paramsRecord = new Map<unknown, string>();

return statement.replaceAll(reg, (_match, arg): string => {
const value = rawArguments[arg];
if (value === undefined || value === null) {
return "NULL";
} else {
const storedParamName = paramsRecord[value];
const storedParamName = paramsRecord.get(value);
if (storedParamName) {
return storedParamName;
}
const paramName = new Cypher.Param(value).getCypher(env);
paramsRecord[value] = paramName;
paramsRecord.set(value, paramName);
return paramName;
}
});
Expand Down
82 changes: 82 additions & 0 deletions packages/graphql/tests/integration/issues/5467.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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("https://github.com/neo4j/graphql/issues/5467", () => {
const testHelper = new TestHelper();

let Test: UniqueType;

beforeAll(async () => {
Test = testHelper.createUniqueType("Test");

const typeDefs = /* GraphQL */ `
type ${Test} {
name: String!
groups: [String!]
}

type Mutation {
mergeTest(name: String!, groups: [String!]): ${Test}
@cypher(
statement: """
MERGE (t:Test {name: $name}) SET t.groups = $groups
return t
"""
columnName: "t"
)
}
`;
await testHelper.initNeo4jGraphQL({
typeDefs,
});
});

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

test("custom Cypher should correctly interpret array parameters with a single item", async () => {
const query = /* GraphQL */ `
mutation ($name: String!, $groups: [String!]) {
mergeTest(name: $name, groups: $groups) {
name
groups
}
}
`;

const response = await testHelper.executeGraphQL(query, {
variableValues: {
name: "test",
groups: ["test"],
},
});

expect(response.errors).toBeFalsy();
expect(response.data).toEqual({
mergeTest: {
name: "test",
groups: ["test"],
},
});
});
});
87 changes: 87 additions & 0 deletions packages/graphql/tests/tck/issues/5467.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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 { Neo4jGraphQL } from "../../../src";
import { formatCypher, formatParams, translateQuery } from "../utils/tck-test-utils";

describe("https://github.com/neo4j/graphql/issues/5467", () => {
let typeDefs: string;
let neoSchema: Neo4jGraphQL;

beforeAll(() => {
typeDefs = /* GraphQL */ `
type Test {
name: String!
groups: [String!]
}

type Mutation {
mergeTest(name: String!, groups: [String!]): Test
@cypher(
statement: """
MERGE (t:Test {name: $name}) SET t.groups = $groups
return t
"""
columnName: "t"
)
}
`;

neoSchema = new Neo4jGraphQL({
typeDefs,
});
});

test("custom Cypher should correctly interpret array parameters with a single item", async () => {
const query = /* GraphQL */ `
mutation ($name: String!, $groups: [String!]) {
mergeTest(name: $name, groups: $groups) {
name
groups
}
}
`;

const result = await translateQuery(neoSchema, query, {
variableValues: {
name: "test",
groups: ["test"],
},
});

expect(formatCypher(result.cypher)).toMatchInlineSnapshot(`
"CALL {
MERGE (t:Test {name: $param0}) SET t.groups = $param1
return t
}
WITH t AS this0
WITH this0 { .name, .groups } AS this0
RETURN this0 AS this"
`);

expect(formatParams(result.params)).toMatchInlineSnapshot(`
"{
\\"param0\\": \\"test\\",
\\"param1\\": [
\\"test\\"
]
}"
`);
});
});
Loading