Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure Explorer always has a value #775

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
[#767](https://github.com/aws/graph-explorer/pull/767),
[#768](https://github.com/aws/graph-explorer/pull/768),
[#769](https://github.com/aws/graph-explorer/pull/769),
[#770](https://github.com/aws/graph-explorer/pull/770))
[#770](https://github.com/aws/graph-explorer/pull/770),
[#775](https://github.com/aws/graph-explorer/pull/775),
- **Updated** dependencies
([#764](https://github.com/aws/graph-explorer/pull/764))

Expand Down
57 changes: 57 additions & 0 deletions packages/graph-explorer/src/connector/emptyExplorer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-disable @typescript-eslint/require-await */
import { Explorer } from "./useGEFetchTypes";

/**
* Empty explorer for when there is no connection.
*/
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