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: cypherParams are not passed on to nested query #1882

Merged
merged 5 commits into from
Aug 11, 2022
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/shy-penguins-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@neo4j/graphql": patch
---

fix: Pass the cypherParams from the top-level context to the translate functions.
2 changes: 1 addition & 1 deletion packages/graphql/src/translate/translate-aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { translateTopLevelMatch } from "./translate-top-level-match";
function translateAggregate({ node, context }: { node: Node; context: Context }): [string, any] {
const { fieldsByTypeName } = context.resolveTree;
const varName = "this";
let cypherParams: { [k: string]: any } = {};
let cypherParams: { [k: string]: any } = context.cypherParams ? { cypherParams: context.cypherParams } : {};
const cypherStrs: string[] = [];

const topLevelMatch = translateTopLevelMatch({ node, context, varName, operation: "READ" });
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/translate/translate-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function translateDelete({ context, node }: { context: Context; node: Nod
let matchAndWhereStr = "";
let allowStr = "";
let deleteStr = "";
let cypherParams: { [k: string]: any } = {};
let cypherParams: { [k: string]: any } = context.cypherParams ? { cypherParams: context.cypherParams } : {};

const withVars = [varName];

Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/translate/translate-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function translateRead({
let authStr = "";
let projAuth = "";

let cypherParams: { [k: string]: any } = {};
let cypherParams: { [k: string]: any } = context.cypherParams ? { cypherParams: context.cypherParams } : {};
const connectionStrs: string[] = [];
const interfaceStrs: string[] = [];

Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/src/translate/translate-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default async function translateUpdate({
let deleteStr = "";
let projAuth = "";
let projStr = "";
let cypherParams: { [k: string]: any } = {};
let cypherParams: { [k: string]: any } = context.cypherParams ? { cypherParams: context.cypherParams } : {};
const assumeReconnecting = Boolean(connectInput) && Boolean(disconnectInput);

const topLevelMatch = translateTopLevelMatch({ node, context, varName, operation: "UPDATE" });
Expand Down Expand Up @@ -363,7 +363,7 @@ export default async function translateUpdate({
refNode,
context,
withVars,
callbackBucket
callbackBucket,
});
connectStrs.push(cypher);
cypherParams = { ...cypherParams, ...params };
Expand Down
105 changes: 105 additions & 0 deletions packages/graphql/tests/integration/issues/1249.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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 { GraphQLSchema } from "graphql";
import { graphql } from "graphql";
import type { Driver } from "neo4j-driver";
import Neo4j from "../neo4j";
import { Neo4jGraphQL } from "../../../src";

describe("https://github.com/neo4j/graphql/issues/1249", () => {
let schema: GraphQLSchema;
let driver: Driver;
let neo4j: Neo4j;

const typeDefs = `
type Bulk
@exclude(operations: [CREATE, DELETE, UPDATE])
@node(additionalLabels: ["$context.cypherParams.tenant"]) {
id: ID!
supplierMaterialNumber: String!
material: Material! @relationship(type: "MATERIAL_BULK", direction: OUT)
}

type Material @exclude(operations: [CREATE, DELETE, UPDATE]) {
id: ID!
itemNumber: String!

suppliers: [Supplier!]!
@relationship(type: "MATERIAL_SUPPLIER", properties: "RelationMaterialSupplier", direction: OUT)
}

type Supplier @exclude(operations: [CREATE, DELETE, UPDATE]) {
id: ID!
name: String
supplierId: String!
}

interface RelationMaterialSupplier @relationshipProperties {
supplierMaterialNumber: String!
}
`;

beforeAll(async () => {
neo4j = new Neo4j();
driver = await neo4j.getDriver();
});

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

test("should pass the cypherParams from the context correctly at the top level translate", async () => {
const neoGraphql = new Neo4jGraphQL({
typeDefs,
driver,
});
schema = await neoGraphql.getSchema();

const query = `
query {
bulks {
supplierMaterialNumber
material {
id
suppliersConnection {
edges {
supplierMaterialNumber
node {
supplierId
}
}
}
}
}
}
`;

const res = await graphql({
schema,
source: query,
contextValue: neo4j.getContextValues({ cypherParams: { tenant: "BULK" } }),
});

expect(res.errors).toBeUndefined();
expect(res.data).toEqual({
bulks: [],
});
});
});
109 changes: 109 additions & 0 deletions packages/graphql/tests/tck/tck-test-files/issues/1249.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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 { gql } from "apollo-server";
import type { DocumentNode } from "graphql";
import { Neo4jGraphQL } from "../../../../src";
import { formatCypher, formatParams, translateQuery } from "../../utils/tck-test-utils";

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

beforeAll(() => {
typeDefs = gql`
type Bulk
@exclude(operations: [CREATE, DELETE, UPDATE])
@node(additionalLabels: ["$context.cypherParams.tenant"]) {
id: ID!
supplierMaterialNumber: String!
material: Material! @relationship(type: "MATERIAL_BULK", direction: OUT)
}

type Material @exclude(operations: [CREATE, DELETE, UPDATE]) {
id: ID!
itemNumber: String!

suppliers: [Supplier!]!
@relationship(type: "MATERIAL_SUPPLIER", properties: "RelationMaterialSupplier", direction: OUT)
}

type Supplier @exclude(operations: [CREATE, DELETE, UPDATE]) {
id: ID!
name: String
supplierId: String!
}

interface RelationMaterialSupplier @relationshipProperties {
supplierMaterialNumber: String!
}
`;

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

test("should contain the cypherParams that are passed via the context", async () => {
const query = gql`
query {
bulks {
supplierMaterialNumber
material {
id
suppliersConnection {
edges {
supplierMaterialNumber
node {
supplierId
}
}
}
}
}
}
`;

const result = await translateQuery(neoSchema, query, { contextValues: { cypherParams: { tenant: "BULK" } } });

expect(formatCypher(result.cypher)).toMatchInlineSnapshot(`
"MATCH (this:\`Bulk\`:\`BULK\`)
RETURN this { .supplierMaterialNumber, material: head([ (this)-[:MATERIAL_BULK]->(this_material:Material) | this_material { .id, suppliersConnection: apoc.cypher.runFirstColumnSingle(\\"CALL {
WITH this_material
MATCH (this_material)-[this_material_material_supplier_relationship:MATERIAL_SUPPLIER]->(this_material_supplier:Supplier)
WITH collect({ supplierMaterialNumber: this_material_material_supplier_relationship.supplierMaterialNumber, node: { supplierId: this_material_supplier.supplierId } }) AS edges
UNWIND edges as edge
WITH collect(edge) AS edges, size(collect(edge)) AS totalCount
RETURN { edges: edges, totalCount: totalCount } AS suppliersConnection
} RETURN suppliersConnection\\", { this_material: this_material, auth: $auth, cypherParams: $cypherParams }) } ]) } as this"
`);

expect(formatParams(result.params)).toMatchInlineSnapshot(`
"{
\\"cypherParams\\": {
\\"tenant\\": \\"BULK\\"
},
\\"auth\\": {
\\"isAuthenticated\\": false,
\\"roles\\": []
}
}"
`);
});
});
7 changes: 6 additions & 1 deletion packages/graphql/tests/tck/utils/tck-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,20 @@ export async function translateQuery(
options?: {
req?: IncomingMessage;
variableValues?: Record<string, any>;
contextValues?: Record<string, any>;
}
): Promise<{ cypher: string; params: Record<string, any> }> {
const driverBuilder = new DriverBuilder();

const contextValue: Record<string, any> = { driver: driverBuilder.instance() };
let contextValue: Record<string, any> = { driver: driverBuilder.instance() };
if (options?.req) {
contextValue.req = options.req;
}

if (options?.contextValues) {
contextValue = { ...contextValue, ...options.contextValues };
}

const graphqlArgs: GraphQLArgs = {
schema: await neoSchema.getSchema(),
source: getQuerySource(query),
Expand Down