Skip to content

Commit

Permalink
Merge pull request #5969 from neo4j/cypher-version-lts
Browse files Browse the repository at this point in the history
Add addVersionPrefix to cypherQueryOptions in LTS
  • Loading branch information
angrykoala authored Feb 11, 2025
2 parents b0d161d + c911466 commit 5a69d4b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
22 changes: 22 additions & 0 deletions .changeset/soft-socks-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"@neo4j/graphql": patch
---

Add `addVersionPrefix` to `cypherQueryOptions` in context to add a Cypher version with `CYPHER` before each query:

```js
{
cypherQueryOptions: {
addVersionPrefix: true,
},
}
```

This prepends all Cypher queries with a `CYPHER [version]` statement:

```cypher
CYPHER 5
MATCH (this:Movie)
WHERE this.title = $param0
RETURN this { .title } AS this
```
37 changes: 29 additions & 8 deletions packages/graphql/src/classes/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
import { debugCypherAndParams } from "../debug/debug-cypher-and-params";
import environment from "../environment";
import type { CypherQueryOptions } from "../types";
import { isInArray } from "../utils/is-in-array";
import {
Neo4jGraphQLAuthenticationError,
Neo4jGraphQLConstraintValidationError,
Expand All @@ -49,6 +50,8 @@ import {

const debug = Debug(DEBUG_EXECUTE);

const SUPPORTED_CYPHER_VERSION = "5";

interface DriverLike {
session(config);
}
Expand Down Expand Up @@ -179,16 +182,34 @@ export class Executor {
return error;
}

private generateQuery(query: string): string {
if (this.cypherQueryOptions && Object.keys(this.cypherQueryOptions).length) {
const cypherQueryOptions = `CYPHER ${Object.entries(this.cypherQueryOptions)
.map(([key, value]) => `${key}=${value}`)
.join(" ")}`;
private addCypherOptionsToQuery(query: string): string {
const cypherVersion = this.getCypherVersionStatement();

const cypherQueryOptions = this.getCypherQueryOptionsStatement();

return `${cypherVersion}${cypherQueryOptions}${query}`;
}

return `${cypherQueryOptions}\n${query}`;
private getCypherVersionStatement(): string {
if (this.cypherQueryOptions?.addVersionPrefix) {
return `CYPHER ${SUPPORTED_CYPHER_VERSION}\n`;
}
return "";
}

return query;
private getCypherQueryOptionsStatement(): string {
const ignoredCypherQueryOptions: Array<keyof CypherQueryOptions> = ["addVersionPrefix"];
const cypherQueryOptions = Object.entries(this.cypherQueryOptions ?? []).filter(([key, _value]) => {
return !isInArray(ignoredCypherQueryOptions, key);
});
if (cypherQueryOptions.length) {
return `CYPHER ${cypherQueryOptions
.map(([key, value]) => {
return `${key}=${value}`;
})
.join(" ")}\n`;
}
return "";
}

private getTransactionConfig(info?: GraphQLResolveInfo): TransactionConfig {
Expand Down Expand Up @@ -290,7 +311,7 @@ export class Executor {
parameters: Record<string, any>,
transaction: Transaction | ManagedTransaction
): Result {
const queryToRun = this.generateQuery(query);
const queryToRun = this.addCypherOptionsToQuery(query);

debugCypherAndParams(debug, queryToRun, parameters);

Expand Down
6 changes: 3 additions & 3 deletions packages/graphql/src/translate/translate-resolve-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
*/

import type Cypher from "@neo4j/cypher-builder";
import type { Neo4jGraphQLTranslationContext } from "../types/neo4j-graphql-translation-context";
import Debug from "debug";
import { QueryASTFactory } from "./queryAST/factory/QueryASTFactory";
import type { EntityAdapter } from "../schema-model/entity/EntityAdapter";
import { DEBUG_TRANSLATE } from "../constants";
import type { EntityAdapter } from "../schema-model/entity/EntityAdapter";
import type { Neo4jGraphQLTranslationContext } from "../types/neo4j-graphql-translation-context";
import { QueryASTFactory } from "./queryAST/factory/QueryASTFactory";

const debug = Debug(DEBUG_TRANSLATE);

Expand Down
1 change: 1 addition & 0 deletions packages/graphql/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export interface CypherQueryOptions {
operatorEngine?: "default" | "interpreted" | "compiled";
interpretedPipesFallback?: "default" | "disabled" | "whitelisted_plans_only" | "all";
replan?: "default" | "force" | "skip";
addVersionPrefix?: boolean;
}

/** Input field for graphql-compose */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ describe("query options", () => {

expect(result?.data?.[Movie.plural]).toEqual([{ id }, { id }, { id }]);
});

});

0 comments on commit 5a69d4b

Please sign in to comment.