Skip to content

Commit

Permalink
Move node count by type query (#441)
Browse files Browse the repository at this point in the history
* Ensure vertexType is defined

* Move query to queries file
  • Loading branch information
kmcginnes committed Jun 18, 2024
1 parent 6feb2f5 commit ae6e7d9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
21 changes: 21 additions & 0 deletions packages/graph-explorer/src/connector/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { queryOptions } from "@tanstack/react-query";
import {
CountsByTypeResponse,
Explorer,
NeighborsRequest,
NeighborsResponse,
Expand Down Expand Up @@ -62,3 +63,23 @@ export const neighborsCountQuery = (id: VertexId, explorer: Explorer | null) =>
};
},
});

/**
* Retrieves the count of nodes for a specific node type.
* @param nodeType A node label or class.
* @param explorer The service client to use for fetching.
* @returns An object with the total nodes for the given node type.
*/
export const nodeCountByNodeTypeQuery = (
nodeType: string,
explorer: Explorer | null
) =>
queryOptions({
queryKey: ["node-count-by-node-type", nodeType, explorer],
enabled: Boolean(explorer),
queryFn: () =>
explorer?.fetchVertexCountsByType({
label: nodeType,
}) ?? nodeCountByNodeTypeEmptyResponse,
});
const nodeCountByNodeTypeEmptyResponse: CountsByTypeResponse = { total: 0 };
21 changes: 4 additions & 17 deletions packages/graph-explorer/src/hooks/useUpdateVertexTypeCounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,14 @@ import { useConfiguration } from "../core";
import { explorerSelector } from "../core/connector";
import useUpdateSchema from "./useUpdateSchema";
import { useRecoilValue } from "recoil";
import { nodeCountByNodeTypeQuery } from "../connector/queries";

const useUpdateVertexTypeCounts = (vertexType?: string) => {
export default function useUpdateVertexTypeCounts(vertexType: string) {
const config = useConfiguration();
const configId = config?.id;
const explorer = useRecoilValue(explorerSelector);

const query = useQuery({
queryKey: ["fetchCountsByType", vertexType, explorer],
queryFn: () => {
if (!vertexType) {
return { total: 0 };
}

return explorer?.fetchVertexCountsByType({
label: vertexType,
});
},
enabled: Boolean(vertexType),
});
const query = useQuery(nodeCountByNodeTypeQuery(vertexType, explorer));

// Sync the result over to the schema in Recoil state
const updateSchemaState = useUpdateSchema();
Expand Down Expand Up @@ -51,6 +40,4 @@ const useUpdateVertexTypeCounts = (vertexType?: string) => {
};
});
}, [query.data, configId, updateSchemaState, vertexType]);
};

export default useUpdateVertexTypeCounts;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,32 @@ import TopBarWithLogo from "../common/TopBarWithLogo";
import defaultStyles from "./DataExplorer.styles";

export type ConnectionsProps = {
vertexType: string;
classNamePrefix?: string;
};

const DEFAULT_COLUMN = {
width: 150,
};

const DataExplorer = ({ classNamePrefix = "ft" }: ConnectionsProps) => {
export default function DataExplorer() {
const { vertexType } = useParams();

if (!vertexType) {
// React Router will redirect if vertexType is not defined before reaching here.
return <>No vertex type was defined</>;
}

return <DataExplorerContent vertexType={vertexType} />;
}

function DataExplorerContent({
vertexType,
classNamePrefix = "ft",
}: ConnectionsProps) {
const styleWithTheme = useWithTheme();
const pfx = withClassNamePrefix(classNamePrefix);
const navigate = useNavigate();
const { vertexType } = useParams<{ vertexType: string }>();
const [searchParams, setSearchParams] = useSearchParams();

const config = useConfiguration();
Expand All @@ -74,10 +88,6 @@ const DataExplorer = ({ classNamePrefix = "ft" }: ConnectionsProps) => {
useUpdateVertexTypeCounts(vertexType);

const vertexConfig = useMemo(() => {
if (!vertexType) {
return;
}

return config?.getVertexTypeConfig(vertexType);
}, [config, vertexType]);

Expand Down Expand Up @@ -198,7 +208,7 @@ const DataExplorer = ({ classNamePrefix = "ft" }: ConnectionsProps) => {
const { data, isFetching } = useQuery({
queryKey: ["keywordSearch", vertexType, pageIndex, pageSize, explorer],
queryFn: () => {
if (!vertexType || !explorer) {
if (!explorer) {
return { vertices: [] } as KeywordSearchResponse;
}

Expand All @@ -209,7 +219,7 @@ const DataExplorer = ({ classNamePrefix = "ft" }: ConnectionsProps) => {
});
},
placeholderData: keepPreviousData,
enabled: Boolean(vertexType) && Boolean(explorer),
enabled: Boolean(explorer),
});

useEffect(() => {
Expand All @@ -223,10 +233,6 @@ const DataExplorer = ({ classNamePrefix = "ft" }: ConnectionsProps) => {
const setUserStyling = useSetRecoilState(userStylingAtom);
const onDisplayNameChange = useCallback(
(field: "name" | "longName") => (value: string | string[]) => {
if (!vertexType) {
return;
}

setUserStyling(prevStyling => {
const vtItem =
clone(prevStyling.vertices?.find(v => v.type === vertexType)) ||
Expand Down Expand Up @@ -355,6 +361,4 @@ const DataExplorer = ({ classNamePrefix = "ft" }: ConnectionsProps) => {
</Workspace.Content>
</Workspace>
);
};

export default DataExplorer;
}

0 comments on commit ae6e7d9

Please sign in to comment.