in packages/graph-explorer/src/connector/gremlin/fetchNeighbors/oneHopTemplate.ts [135:193]
export default function oneHopTemplate({
vertexId,
excludedVertices = new Set(),
filterByVertexTypes = [],
edgeTypes = [],
filterCriteria = [],
limit = 0,
offset = 0,
}: Omit<NeighborsRequest, "vertexTypes">): string {
const idTemplate = idParam(vertexId);
const range = limit > 0 ? `.range(${offset}, ${offset + limit})` : "";
const vertexTypes = filterByVertexTypes.flatMap(type => type.split("::"));
const vertexTypesTemplate =
vertexTypes.length > 0
? `hasLabel(${vertexTypes.map(type => `"${type}"`).join(", ")})`
: ``;
const filterCriteriaTemplate =
filterCriteria.length > 0
? `and(${filterCriteria.map(criterionTemplate).join(", ")})`
: ``;
const nodeFilters = [vertexTypesTemplate, filterCriteriaTemplate].filter(
Boolean
);
const nodeFiltersTemplate =
nodeFilters.length > 0 ? `.${nodeFilters.join(".")}` : ``;
const edgeTypesTemplate = edgeTypes.map(type => `"${type}"`).join(",");
const excludedList = excludedVertices
.values()
.map(id => idParam(id))
.toArray()
.join(",");
const excludedTemplate = excludedList
? `.filter(__.not(__.hasId(${excludedList})))`
: ``;
return query`
g.V(${idTemplate})
.both()
${nodeFiltersTemplate}
${excludedTemplate}
.dedup()
.order().by(id())
${range}
.as("v")
.project("vertex", "edges")
.by()
.by(
__.select("v").bothE(${edgeTypesTemplate})
.where(otherV().id().is(${idTemplate}))
.dedup().fold()
)
`;
}