Skip to content

Commit

Permalink
Serialize id type in to id string
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcginnes committed Jan 24, 2025
1 parent b676ac1 commit 45b821b
Show file tree
Hide file tree
Showing 44 changed files with 342 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function edgeDetails(
request: EdgeDetailsRequest
): Promise<EdgeDetailsResponse> {
const template = query`
g.E(${idParam(request.edge)})
g.E(${idParam(request.edge.id)})
`;

// Fetch the vertex details
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import globalMockFetch from "@/connector/testUtils/globalMockFetch";
import mockGremlinFetch from "@/connector/testUtils/mockGremlinFetch";
import fetchNeighbors from ".";
import { VertexId } from "@/core";
import { createEdgeId, createVertexId } from "@/core";

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

it("Should return all neighbors from node 2018", async () => {
const expectedVertices = [
{
id: "486",
id: createVertexId("486"),
type: "airport",
types: ["airport"],
attributes: {
Expand All @@ -28,7 +28,7 @@ describe("Gremlin > fetchNeighbors", () => {
},
},
{
id: "228",
id: createVertexId("228"),
type: "airport",
types: ["airport"],
attributes: {
Expand All @@ -47,7 +47,7 @@ describe("Gremlin > fetchNeighbors", () => {
},
},
{
id: "124",
id: createVertexId("124"),
type: "airport",
types: ["airport"],
attributes: {
Expand All @@ -66,96 +66,96 @@ describe("Gremlin > fetchNeighbors", () => {
},
},
{
id: "3741",
id: createVertexId("3741"),
type: "continent",
types: ["continent"],
attributes: { code: "EU", type: "continent", desc: "Europe" },
},
{
id: "3701",
id: createVertexId("3701"),
type: "country",
types: ["country"],
attributes: { code: "ES", type: "country", desc: "Spain" },
},
];

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

expect(response).toMatchObject({
vertices: expectedVertices,
edges: [
{
id: "49540",
id: createEdgeId("49540"),
type: "route",
source: "2018",
source: createVertexId("2018"),
sourceType: "airport",
target: "486",
target: createVertexId("486"),
targetType: "airport",
attributes: { dist: 82 },
},
{
id: "33133",
id: createEdgeId("33133"),
type: "route",
source: "486",
source: createVertexId("486"),
sourceType: "airport",
target: "2018",
target: createVertexId("2018"),
targetType: "airport",
attributes: { dist: 82 },
},
{
id: "49539",
id: createEdgeId("49539"),
type: "route",
source: "2018",
source: createVertexId("2018"),
sourceType: "airport",
target: "228",
target: createVertexId("228"),
targetType: "airport",
attributes: { dist: 153 },
},
{
id: "24860",
id: createEdgeId("24860"),
type: "route",
source: "228",
source: createVertexId("228"),
sourceType: "airport",
target: "2018",
target: createVertexId("2018"),
targetType: "airport",
attributes: { dist: 153 },
},
{
id: "49538",
id: createEdgeId("49538"),
type: "route",
source: "2018",
source: createVertexId("2018"),
sourceType: "airport",
target: "124",
target: createVertexId("124"),
targetType: "airport",
attributes: { dist: 105 },
},
{
id: "18665",
id: createEdgeId("18665"),
type: "route",
source: "124",
source: createVertexId("124"),
sourceType: "airport",
target: "2018",
target: createVertexId("2018"),
targetType: "airport",
attributes: { dist: 105 },
},
{
id: "59800",
id: createEdgeId("59800"),
type: "contains",
source: "3741",
source: createVertexId("3741"),
sourceType: "continent",
target: "2018",
target: createVertexId("2018"),
targetType: "airport",
attributes: {},
},
{
id: "56297",
id: createEdgeId("56297"),
type: "contains",
source: "3701",
source: createVertexId("3701"),
sourceType: "country",
target: "2018",
target: createVertexId("2018"),
targetType: "airport",
attributes: {},
},
Expand All @@ -166,7 +166,7 @@ describe("Gremlin > fetchNeighbors", () => {
it("Should return filtered neighbors from node 2018", async () => {
const expectedVertices = [
{
id: "486",
id: createVertexId("486"),
type: "airport",
types: ["airport"],
attributes: {
Expand All @@ -185,7 +185,7 @@ describe("Gremlin > fetchNeighbors", () => {
},
},
{
id: "124",
id: createVertexId("124"),
type: "airport",
types: ["airport"],
attributes: {
Expand All @@ -206,7 +206,7 @@ describe("Gremlin > fetchNeighbors", () => {
];

const response = await fetchNeighbors(mockGremlinFetch(), {
vertex: { id: "2018" as VertexId, idType: "string" },
vertex: { id: createVertexId("2018"), idType: "string" },
vertexType: "airport",
filterByVertexTypes: ["airport"],
filterCriteria: [{ name: "code", value: "TF", operator: "LIKE" }],
Expand All @@ -216,38 +216,38 @@ describe("Gremlin > fetchNeighbors", () => {
vertices: expectedVertices,
edges: [
{
id: "49540",
id: createEdgeId("49540"),
type: "route",
source: "2018",
source: createVertexId("2018"),
sourceType: "airport",
target: "486",
target: createVertexId("486"),
targetType: "airport",
attributes: { dist: 82 },
},
{
id: "33133",
id: createEdgeId("33133"),
type: "route",
source: "486",
source: createVertexId("486"),
sourceType: "airport",
target: "2018",
target: createVertexId("2018"),
targetType: "airport",
attributes: { dist: 82 },
},
{
id: "49538",
id: createEdgeId("49538"),
type: "route",
source: "2018",
source: createVertexId("2018"),
sourceType: "airport",
target: "124",
target: createVertexId("124"),
targetType: "airport",
attributes: { dist: 105 },
},
{
id: "18665",
id: createEdgeId("18665"),
type: "route",
source: "124",
source: createVertexId("124"),
sourceType: "airport",
target: "2018",
target: createVertexId("2018"),
targetType: "airport",
attributes: { dist: 105 },
},
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 "@/core";
import { createVertexId } from "@/core";

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

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

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

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

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

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

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

it("Should return a template with specific filter criteria", () => {
const template = oneHopTemplate({
vertex: { id: "12" as VertexId, idType: "string" },
vertex: { id: createVertexId("12"), 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
@@ -1,5 +1,6 @@
import { query } from "@/utils";
import type { Criterion, NeighborsRequest } from "@/connector/useGEFetchTypes";
import { idParam } from "../idParam";

function criterionNumberTemplate({
name,
Expand Down Expand Up @@ -134,8 +135,7 @@ export default function oneHopTemplate({
limit = 0,
offset = 0,
}: Omit<NeighborsRequest, "vertexType">): string {
const idTemplate =
vertex.idType === "number" ? `${vertex.id}L` : `"${vertex.id}"`;
const idTemplate = idParam(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 { VertexId } from "@/core";
import { createVertexId } from "@/core";
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({
vertex: { id: "12" as VertexId, idType: "string" },
vertex: { id: createVertexId("12"), idType: "string" },
});

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

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

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { NeighborsCountRequest } from "@/connector/useGEFetchTypes";
import { idParam } from "../idParam";

/**
* Given a single node ids, it returns a Gremlin template with
Expand All @@ -15,12 +16,7 @@ export default function neighborsCountTemplate({
vertex,
limit = 0,
}: NeighborsCountRequest) {
let template = "";
if (vertex.idType === "number") {
template = `g.V(${vertex.id}L).both()`;
} else {
template = `g.V("${vertex.id}").both()`;
}
let template = `g.V(${idParam(vertex.id)}).both()`;

if (limit > 0) {
template += `.limit(${limit})`;
Expand Down
7 changes: 4 additions & 3 deletions packages/graph-explorer/src/connector/gremlin/idParam.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EdgeRef, VertexRef } from "../useGEFetchTypes";
import { EdgeId, getRawId, VertexId } from "@/core";

/** Formats the ID parameter for a gremlin query based on the ID type. */
export function idParam(entity: VertexRef | EdgeRef) {
return entity.idType === "number" ? `${entity.id}L` : `"${entity.id}"`;
export function idParam(entityId: VertexId | EdgeId) {
const rawId = getRawId(entityId);
return typeof rawId === "number" ? `${rawId}L` : `"${rawId}"`;
}
Loading

0 comments on commit 45b821b

Please sign in to comment.