Skip to content

Commit

Permalink
Combine vertex id and idType (#731)
Browse files Browse the repository at this point in the history
* Combine vertex id and idType

* Update neighbors count request

* Update neighbor request

* Update random vertex id type

* Rename to VertexRef

* Ensure query key remains stable

* Remove unused query
  • Loading branch information
kmcginnes authored Jan 7, 2025
1 parent e03a5dd commit 64e7760
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 132 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import globalMockFetch from "@/connector/testUtils/globalMockFetch";
import mockGremlinFetch from "@/connector/testUtils/mockGremlinFetch";
import fetchNeighbors from "./fetchNeighbors";
import { VertexId } from "@/@types/entities";

describe("Gremlin > fetchNeighbors", () => {
beforeEach(globalMockFetch);
Expand Down Expand Up @@ -89,8 +90,7 @@ describe("Gremlin > fetchNeighbors", () => {
];

const response = await fetchNeighbors(mockGremlinFetch(), {
vertexId: "2018",
idType: "string",
vertex: { id: "2018" as VertexId, idType: "string" },
vertexType: "airport",
});

Expand Down Expand Up @@ -224,8 +224,7 @@ describe("Gremlin > fetchNeighbors", () => {
];

const response = await fetchNeighbors(mockGremlinFetch(), {
vertexId: "2018",
idType: "string",
vertex: { id: "2018" as VertexId, idType: "string" },
vertexType: "airport",
filterByVertexTypes: ["airport"],
filterCriteria: [{ name: "code", value: "TF", operator: "LIKE" }],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import globalMockFetch from "@/connector/testUtils/globalMockFetch";
import mockGremlinFetch from "@/connector/testUtils/mockGremlinFetch";
import fetchNeighborsCount from "./fetchNeighborsCount";
import { VertexId } from "@/@types/entities";

describe("Gremlin > fetchNeighborsCount", () => {
beforeEach(globalMockFetch);

it("Should return neighbors counts for node 2018", async () => {
const response = await fetchNeighborsCount(mockGremlinFetch(), {
vertexId: "123",
idType: "string",
vertex: {
id: "123" as VertexId,
idType: "string",
},
});

expect(response).toMatchObject({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { VertexId } from "@/@types/entities";
import neighborsCountTemplate from "./neighborsCountTemplate";
import { normalizeWithNoSpace as normalize } from "@/utils/testing";

describe("Gremlin > neighborsCountTemplate", () => {
it("Should return a template for the given vertex id", () => {
const template = neighborsCountTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
});

expect(normalize(template)).toBe(
Expand All @@ -17,8 +17,7 @@ describe("Gremlin > neighborsCountTemplate", () => {

it("Should return a template for the given vertex id with number type", () => {
const template = neighborsCountTemplate({
vertexId: "12",
idType: "number",
vertex: { id: "12" as VertexId, idType: "number" },
});

expect(normalize(template)).toBe(
Expand All @@ -30,8 +29,7 @@ describe("Gremlin > neighborsCountTemplate", () => {

it("Should return a template for the given vertex id with defined limit", () => {
const template = neighborsCountTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
limit: 20,
});

Expand All @@ -44,8 +42,7 @@ describe("Gremlin > neighborsCountTemplate", () => {

it("Should return a template for the given vertex id with no limit", () => {
const template = neighborsCountTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
limit: 0,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ import type { NeighborsCountRequest } from "@/connector/useGEFetchTypes";
* .group().by(label).by(count())
*/
export default function neighborsCountTemplate({
vertexId,
vertex,
limit = 0,
idType,
}: NeighborsCountRequest) {
let template = "";
if (idType === "number") {
template = `g.V(${vertexId}L).both()`;
if (vertex.idType === "number") {
template = `g.V(${vertex.id}L).both()`;
} else {
template = `g.V("${vertexId}").both()`;
template = `g.V("${vertex.id}").both()`;
}

if (limit > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { normalizeWithNoSpace as normalize } from "@/utils/testing";
import oneHopTemplate from "./oneHopTemplate";
import { VertexId } from "@/@types/entities";

describe("Gremlin > oneHopTemplate", () => {
it("Should return a template for a simple vertex id", () => {
const template = oneHopTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
});

expect(normalize(template)).toBe(
Expand All @@ -25,8 +25,7 @@ describe("Gremlin > oneHopTemplate", () => {

it("Should return a template for a simple vertex id with number type", () => {
const template = oneHopTemplate({
vertexId: "12",
idType: "number",
vertex: { id: "12" as VertexId, idType: "number" },
});

expect(normalize(template)).toBe(
Expand All @@ -46,8 +45,7 @@ describe("Gremlin > oneHopTemplate", () => {

it("Should return a template with an offset and limit", () => {
const template = oneHopTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
offset: 5,
limit: 5,
});
Expand All @@ -69,8 +67,7 @@ describe("Gremlin > oneHopTemplate", () => {

it("Should return a template for specific vertex type", () => {
const template = oneHopTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
filterByVertexTypes: ["country"],
offset: 5,
limit: 10,
Expand All @@ -93,8 +90,7 @@ describe("Gremlin > oneHopTemplate", () => {

it("Should return a template for multiple vertex type", () => {
const template = oneHopTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
filterByVertexTypes: ["country", "airport", "continent"],
offset: 5,
limit: 10,
Expand All @@ -117,8 +113,7 @@ describe("Gremlin > oneHopTemplate", () => {

it("Should return a template with specific filter criteria", () => {
const template = oneHopTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
filterByVertexTypes: ["country"],
filterCriteria: [
{ name: "longest", value: 10000, operator: "gte", dataType: "Number" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ function criterionTemplate(criterion: Criterion): string {
* )
*/
export default function oneHopTemplate({
vertexId,
idType,
vertex,
filterByVertexTypes = [],
edgeTypes = [],
filterCriteria = [],
limit = 0,
offset = 0,
}: Omit<NeighborsRequest, "vertexType">): string {
const idTemplate = idType === "number" ? `${vertexId}L` : `"${vertexId}"`;
const idTemplate =
vertex.idType === "number" ? `${vertex.id}L` : `"${vertex.id}"`;
const range = limit > 0 ? `.range(${offset}, ${offset + limit})` : "";

const vertexTypes = filterByVertexTypes.flatMap(type => type.split("::"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { normalize } from "@/utils/testing";
import neighborsCountTemplate from "./neighborsCountTemplate";
import { VertexId } from "@/@types/entities";

describe("OpenCypher > neighborsCountTemplate", () => {
it("Should return a template for the given vertex id", () => {
const template = neighborsCountTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
});

expect(normalize(template)).toBe(
Expand All @@ -22,8 +22,7 @@ describe("OpenCypher > neighborsCountTemplate", () => {

it("Should return a template for the given vertex id with defined limit", () => {
const template = neighborsCountTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
limit: 20,
});

Expand All @@ -42,8 +41,7 @@ describe("OpenCypher > neighborsCountTemplate", () => {

it("Should return a template for the given vertex id with no limit", () => {
const template = neighborsCountTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
limit: 0,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import type { NeighborsCountRequest } from "@/connector/useGEFetchTypes";
*
*/
export default function neighborsCountTemplate({
vertexId,
vertex,
limit = 0,
}: NeighborsCountRequest) {
return query`
MATCH (v)-[]-(neighbor)
WHERE ID(v) = "${vertexId}"
WHERE ID(v) = "${vertex.id}"
WITH DISTINCT neighbor
${limit > 0 ? `LIMIT ${limit}` : ``}
RETURN labels(neighbor) AS vertexLabel, count(DISTINCT neighbor) AS count
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { normalize } from "@/utils/testing";
import oneHopTemplate from "./oneHopTemplate";
import { VertexId } from "@/@types/entities";

describe("OpenCypher > oneHopTemplate", () => {
it("Should return a template for a simple vertex id", () => {
const template = oneHopTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
});

expect(normalize(template)).toEqual(
Expand All @@ -25,8 +25,7 @@ describe("OpenCypher > oneHopTemplate", () => {

it("Should return a template with an offset and limit", () => {
const template = oneHopTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
offset: 5,
limit: 5,
});
Expand All @@ -50,8 +49,7 @@ describe("OpenCypher > oneHopTemplate", () => {

it("Should return a template for specific vertex type", () => {
const template = oneHopTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
filterByVertexTypes: ["country"],
offset: 5,
limit: 10,
Expand All @@ -76,8 +74,7 @@ describe("OpenCypher > oneHopTemplate", () => {

it("Should return a template for many vertex types", () => {
const template = oneHopTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
filterByVertexTypes: ["country", "continent", "airport", "person"],
});

Expand All @@ -98,8 +95,7 @@ describe("OpenCypher > oneHopTemplate", () => {

it("Should return a template for specific edge type", () => {
const template = oneHopTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
edgeTypes: ["locatedIn"],
offset: 5,
limit: 10,
Expand All @@ -124,8 +120,7 @@ describe("OpenCypher > oneHopTemplate", () => {

it("Should return a template with specific filter criteria", () => {
const template = oneHopTemplate({
vertexId: "12",
idType: "string",
vertex: { id: "12" as VertexId, idType: "string" },
filterByVertexTypes: ["country"],
filterCriteria: [
{ name: "longest", value: 10000, operator: "gte", dataType: "Number" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const criterionTemplate = (criterion: Criterion): string => {
* LIMIT 10
*/
const oneHopTemplate = ({
vertexId,
vertex,
filterByVertexTypes = [],
edgeTypes = [],
filterCriteria = [],
Expand All @@ -129,7 +129,7 @@ const oneHopTemplate = ({

// Combine all the WHERE conditions
const whereConditions = [
`ID(v) = "${vertexId}"`,
`ID(v) = "${vertex.id}"`,
formattedVertexTypes,
...(filterCriteria?.map(criterionTemplate) ?? []),
]
Expand Down
Loading

0 comments on commit 64e7760

Please sign in to comment.