-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into move-interface-projection-to-cypher-builder
- Loading branch information
Showing
7 changed files
with
193 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
packages/graphql/src/translate/cypher-builder/expressions/map/MapProjection.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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 { TestClause } from "../../utils/TestClause"; | ||
import * as CypherBuilder from "../../CypherBuilder"; | ||
|
||
describe("Map Projection", () => { | ||
test("Project empty map", () => { | ||
const mapProjection = new CypherBuilder.MapProjection(new CypherBuilder.Variable(), []); | ||
|
||
const queryResult = new TestClause(mapProjection).build(); | ||
|
||
expect(queryResult.cypher).toMatchInlineSnapshot(`"var0 { }"`); | ||
|
||
expect(queryResult.params).toMatchInlineSnapshot(`Object {}`); | ||
}); | ||
|
||
test("Project map with variables and nodes in projection", () => { | ||
const var1 = new CypherBuilder.Variable(); | ||
const var2 = new CypherBuilder.NamedVariable("NamedVar"); | ||
const node = new CypherBuilder.Node({}); | ||
|
||
const mapProjection = new CypherBuilder.MapProjection(new CypherBuilder.Variable(), [var1, var2, node]); | ||
|
||
const queryResult = new TestClause(mapProjection).build(); | ||
|
||
expect(queryResult.cypher).toMatchInlineSnapshot(`"var0 { .var1, .NamedVar, .this2 }"`); | ||
|
||
expect(queryResult.params).toMatchInlineSnapshot(`Object {}`); | ||
}); | ||
|
||
test("Project map with extra values only", () => { | ||
const var1 = new CypherBuilder.Variable(); | ||
const var2 = new CypherBuilder.NamedVariable("NamedVar"); | ||
|
||
const mapProjection = new CypherBuilder.MapProjection(new CypherBuilder.Variable(), [], { | ||
myValue: var1, | ||
namedValue: var2, | ||
}); | ||
|
||
const queryResult = new TestClause(mapProjection).build(); | ||
|
||
expect(queryResult.cypher).toMatchInlineSnapshot(`"var0 { myValue: var1, namedValue: NamedVar }"`); | ||
|
||
expect(queryResult.params).toMatchInlineSnapshot(`Object {}`); | ||
}); | ||
|
||
test("Project map with variables in projection and extra values", () => { | ||
const var1 = new CypherBuilder.Variable(); | ||
const var2 = new CypherBuilder.NamedVariable("NamedVar"); | ||
const node = new CypherBuilder.Node({}); | ||
|
||
const mapProjection = new CypherBuilder.MapProjection(new CypherBuilder.Variable(), [var1, var2], { | ||
namedValue: CypherBuilder.count(node), | ||
}); | ||
const queryResult = new TestClause(mapProjection).build(); | ||
|
||
expect(queryResult.cypher).toMatchInlineSnapshot(`"var0 { .var2, .NamedVar, namedValue: count(this1) }"`); | ||
|
||
expect(queryResult.params).toMatchInlineSnapshot(`Object {}`); | ||
}); | ||
|
||
test("Map Projection in return", () => { | ||
const mapVar = new CypherBuilder.Variable(); | ||
const var1 = new CypherBuilder.Variable(); | ||
const var2 = new CypherBuilder.NamedVariable("NamedVar"); | ||
const node = new CypherBuilder.Node({}); | ||
|
||
const mapProjection = new CypherBuilder.MapProjection(mapVar, [var1, var2], { | ||
namedValue: CypherBuilder.count(node), | ||
}); | ||
|
||
const queryResult = new CypherBuilder.Return([mapProjection, mapVar]).build(); | ||
|
||
expect(queryResult.cypher).toMatchInlineSnapshot( | ||
`"RETURN var0 { .var2, .NamedVar, namedValue: count(this1) } AS var0"` | ||
); | ||
|
||
expect(queryResult.params).toMatchInlineSnapshot(`Object {}`); | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
packages/graphql/src/translate/cypher-builder/expressions/map/MapProjection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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 { CypherEnvironment } from "../../Environment"; | ||
import type { CypherCompilable, Expr } from "../../types"; | ||
import { serializeMap } from "../../utils/serialize-map"; | ||
import { Variable } from "../../variables/Variable"; | ||
|
||
/** Represents a Map projection https://neo4j.com/docs/cypher-manual/current/syntax/maps/#cypher-map-projection */ | ||
export class MapProjection implements CypherCompilable { | ||
private extraValues: Record<string, Expr>; | ||
private variable: Variable; | ||
private projection: Array<Variable>; | ||
|
||
constructor(variable: Variable, projection: Array<Variable>, extraValues: Record<string, Expr> = {}) { | ||
this.variable = variable; | ||
this.projection = projection; | ||
this.extraValues = extraValues; | ||
} | ||
|
||
public set(values: Record<string, Expr> | Variable): void { | ||
if (values instanceof Variable) { | ||
this.projection.push(values); | ||
} else { | ||
this.extraValues = { ...this.extraValues, ...values }; | ||
} | ||
} | ||
|
||
public getCypher(env: CypherEnvironment): string { | ||
const variableStr = this.variable.getCypher(env); | ||
const extraValuesStr = serializeMap(env, this.extraValues, true); | ||
|
||
const projectionStr = this.projection.map((v) => `.${v.getCypher(env)}`).join(", "); | ||
|
||
const commaStr = extraValuesStr && projectionStr ? ", " : ""; | ||
|
||
return `${variableStr} { ${projectionStr}${commaStr}${extraValuesStr} }`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/graphql/src/translate/cypher-builder/utils/serialize-map.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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 { Expr } from "../CypherBuilder"; | ||
import type { CypherEnvironment } from "../Environment"; | ||
|
||
export function serializeMap(env: CypherEnvironment, obj: Record<string, Expr>, omitCurlyBraces = false): string { | ||
const valuesList = Object.entries(obj).map(([key, value]) => { | ||
return `${key}: ${value.getCypher(env)}`; | ||
}); | ||
|
||
const serializedContent = valuesList.join(", "); | ||
if (omitCurlyBraces) return serializedContent; | ||
|
||
return `{ ${serializedContent} }`; | ||
} |