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

feat: add cypherParams #254

Merged
merged 1 commit into from
Jun 15, 2021
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
30 changes: 30 additions & 0 deletions docs/asciidoc/type-definitions/cypher.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,36 @@ type Query {
}
----


=== `cypherParams`
Use to inject values into the cypher query from the GraphQL context function.

Inject into context:

[source, typescript]
----
const server = new ApolloServer({
typeDefs,
context: () => {
return {
cypherParams: { userId: "user-id-01" }
}
}
});
----

Use in cypher query:

[source, graphql]
----
type Query {
userPosts: [Post] @cypher(statement: """
MATCH (:User {id: $cypherParams.userId})-[:POSTED]->(p:Post)
RETURN p
""")
}
----

== Return values

The return value of the Cypher statement must be of the same type to which the directive is applied.
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/schema/resolvers/cypher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function cypherResolver({ field, statement }: { field: BaseField;
async function resolve(_root: any, args: any, _context: unknown) {
const context = _context as Context;
const cypherStrs: string[] = [];
let params = { ...args, auth: createAuthParam({ context }) };
let params = { ...args, auth: createAuthParam({ context }), cypherParams: context.cypherParams };

const preAuth = createAuthAndParams({ entity: field, context });
if (preAuth[0]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ function createProjectionAndParams({
}
}

const initApocParamsStrs = [
"auth: $auth",
...(context.cypherParams ? ["cypherParams: $cypherParams"] : []),
];
const apocParams = Object.entries(field.args).reduce(
(r: { strs: string[]; params: any }, entry) => {
const argName = `${param}_${entry[0]}`;
Expand All @@ -216,9 +220,9 @@ function createProjectionAndParams({
params: { ...r.params, [argName]: entry[1] },
};
},
{ strs: ["auth: $auth"], params: {} }
{ strs: initApocParamsStrs, params: {} }
) as { strs: string[]; params: any };
res.params = { ...res.params, ...apocParams.params };
res.params = { ...res.params, ...apocParams.params, cypherParams: context.cypherParams };

const expectMultipleValues = referenceNode && cypherField.typeMeta.array ? "true" : "false";
const apocWhere = `${
Expand Down
186 changes: 186 additions & 0 deletions packages/graphql/tests/integration/cypher-params.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* 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 { Driver } from "neo4j-driver";
import { graphql } from "graphql";
import { generate } from "randomstring";
import { Neo4jGraphQL } from "../../src/classes";
import neo4j from "./neo4j";

describe("cypherParams", () => {
let driver: Driver;

beforeAll(async () => {
driver = await neo4j();
});

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

test("should inject cypherParams on top-level cypher query", async () => {
const session = driver.session();

const typeDefs = `
type Movie {
id: ID
}

type Query {
id: String! @cypher(statement: "RETURN $cypherParams.id")
}
`;

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

const id = generate({
charset: "alphabetic",
});

const source = `
{
id
}
`;

try {
const gqlResult = await graphql({
schema: neoSchema.schema,
source,
contextValue: { driver, cypherParams: { id } },
});

expect(gqlResult.errors).toBeFalsy();

expect((gqlResult.data as any).id).toEqual(id);
} finally {
await session.close();
}
});

test("should inject cypherParams on field level nested query", async () => {
const session = driver.session();

const typeDefs = `
type CypherParams {
id: ID
}

type Movie {
id: ID
cypherParams: CypherParams @cypher(statement: "RETURN $cypherParams")
}
`;

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

const movieId = generate({
charset: "alphabetic",
});
const cypherParamsId = generate({
charset: "alphabetic",
});

const source = `
query($id: ID) {
movies(where: {id: $id}) {
id
cypherParams {
id
}
}
}
`;

try {
await session.run(
`
CREATE (:Movie {id: $movieId})
`,
{ movieId }
);

const gqlResult = await graphql({
schema: neoSchema.schema,
source,
variableValues: {
id: movieId,
},
contextValue: { driver, cypherParams: { id: cypherParamsId } },
});

expect(gqlResult.errors).toBeFalsy();

expect((gqlResult.data as any).movies[0]).toEqual({
id: movieId,
cypherParams: {
id: cypherParamsId,
},
});
} finally {
await session.close();
}
});

test("should inject cypherParams on top-level cypher mutation", async () => {
const session = driver.session();

const typeDefs = `
type Movie {
id: ID
}

type Mutation {
id: String! @cypher(statement: "RETURN $cypherParams.id")
}
`;

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

const id = generate({
charset: "alphabetic",
});

const source = `
mutation {
id
}
`;

try {
const gqlResult = await graphql({
schema: neoSchema.schema,
source,
contextValue: { driver, cypherParams: { id } },
});

expect(gqlResult.errors).toBeFalsy();

expect((gqlResult.data as any).id).toEqual(id);
} finally {
await session.close();
}
});
});