Skip to content

Commit

Permalink
Make sure Explorer always has a value, added emptyExplorer
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcginnes committed Feb 2, 2025
1 parent 1e5972d commit 6794076
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 40 deletions.
54 changes: 54 additions & 0 deletions packages/graph-explorer/src/connector/emptyExplorer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable @typescript-eslint/require-await */
import { Explorer } from "./useGEFetchTypes";

export const emptyExplorer: Explorer = {
connection: {
url: "",
queryEngine: "gremlin",
proxyConnection: false,
awsAuthEnabled: false,
},
fetchSchema: async () => {
return {
totalVertices: 0,
vertices: [],
totalEdges: 0,
edges: [],
};
},
fetchVertexCountsByType: async () => {
return {
total: 0,
};
},
fetchNeighbors: async () => {
return {
vertices: [],
edges: [],
scalars: [],
};
},
fetchNeighborsCount: async () => {
return {
totalCount: 0,
counts: {},
};
},
keywordSearch: async () => {
return {
vertices: [],
edges: [],
scalars: [],
};
},
vertexDetails: async () => {
return {
vertex: null,
};
},
edgeDetails: async () => {
return {
edge: null,
};
},
};
46 changes: 13 additions & 33 deletions packages/graph-explorer/src/connector/queries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { QueryClient, queryOptions } from "@tanstack/react-query";
import {
CountsByTypeResponse,
EdgeDetailsRequest,
EdgeDetailsResponse,
Explorer,
Expand All @@ -20,14 +19,13 @@ import { Edge, Vertex, VertexId } from "@/core";
*/
export function searchQuery(
request: KeywordSearchRequest,
explorer: Explorer | null,
explorer: Explorer,
queryClient: QueryClient
) {
return queryOptions({
queryKey: ["keyword-search", request, explorer, queryClient],
enabled: Boolean(explorer),
queryFn: async ({ signal }): Promise<KeywordSearchResponse | null> => {
if (!explorer || !request) {
queryFn: async ({ signal }): Promise<KeywordSearchResponse> => {
if (!request) {
return { vertices: [], edges: [], scalars: [] };
}
const results = await explorer.keywordSearch(request, { signal });
Expand Down Expand Up @@ -58,19 +56,11 @@ export type NeighborCountsQueryResponse = {
*/
export function neighborsCountQuery(
request: NeighborCountsQueryRequest,
explorer: Explorer | null
explorer: Explorer
) {
return queryOptions({
queryKey: ["neighborsCount", request, explorer],
queryFn: async (): Promise<NeighborCountsQueryResponse> => {
if (!explorer) {
return {
nodeId: request.vertexId,
totalCount: 0,
counts: {},
};
}

const limit = explorer.connection.nodeExpansionLimit;

const result = await explorer.fetchNeighborsCount({
Expand All @@ -95,45 +85,35 @@ export function neighborsCountQuery(
*/
export const nodeCountByNodeTypeQuery = (
nodeType: string,
explorer: Explorer | null
explorer: Explorer
) =>
queryOptions({
queryKey: ["node-count-by-node-type", nodeType, explorer],
enabled: Boolean(explorer),
queryFn: () =>
explorer?.fetchVertexCountsByType({
explorer.fetchVertexCountsByType({
label: nodeType,
}) ?? nodeCountByNodeTypeEmptyResponse,
}),
});
const nodeCountByNodeTypeEmptyResponse: CountsByTypeResponse = { total: 0 };

export function vertexDetailsQuery(
request: VertexDetailsRequest,
explorer: Explorer | null
explorer: Explorer
) {
return queryOptions({
queryKey: ["db", "vertex", "details", request, explorer],
queryFn: async ({ signal }): Promise<VertexDetailsResponse> => {
if (!explorer) {
return { vertex: null };
}
return await explorer.vertexDetails(request, { signal });
},
queryFn: ({ signal }): Promise<VertexDetailsResponse> =>
explorer.vertexDetails(request, { signal }),
});
}

export function edgeDetailsQuery(
request: EdgeDetailsRequest,
explorer: Explorer | null
explorer: Explorer
) {
return queryOptions({
queryKey: ["db", "edge", "details", request, explorer],
queryFn: async ({ signal }): Promise<EdgeDetailsResponse> => {
if (!explorer) {
return { edge: null };
}
return await explorer.edgeDetails(request, { signal });
},
queryFn: ({ signal }): Promise<EdgeDetailsResponse> =>
explorer.edgeDetails(request, { signal }),
});
}

Expand Down
6 changes: 2 additions & 4 deletions packages/graph-explorer/src/core/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ConnectionConfig } from "@shared/types";
import { logger } from "@/utils";
import { featureFlagsSelector } from "./featureFlags";
import { Explorer } from "@/connector/useGEFetchTypes";
import { emptyExplorer } from "@/connector/emptyExplorer";

/**
* Active connection where the value will only change when one of the
Expand Down Expand Up @@ -62,7 +63,7 @@ export const explorerSelector = selector({
const featureFlags = get(featureFlagsSelector);

if (!connection) {
return null;
return emptyExplorer;
}
switch (connection.queryEngine) {
case "openCypher":
Expand All @@ -77,9 +78,6 @@ export const explorerSelector = selector({

export function useExplorer() {
const explorer = useRecoilValue(explorerSelector);
if (!explorer) {
throw new Error("No explorer found");
}
return explorer;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/graph-explorer/src/hooks/useExpandNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ export default function useExpandNode() {
limit,
};

if (!explorer || !request) {
if (!request) {
return null;
}

return await explorer.fetchNeighbors(request);
},
onSuccess: data => {
if (!data || !explorer) {
if (!data) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/graph-explorer/src/hooks/useSchemaSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const useSchemaSync = (onSyncChange?: (isSyncing: boolean) => void) => {

const { replaceSchema, setSyncFailure } = useUpdateSchema();
return useCallback(async () => {
if (!config || !explorer) {
if (!config) {
return;
}

Expand Down

0 comments on commit 6794076

Please sign in to comment.