Skip to content

Commit

Permalink
add invalidation + typescript typing to the custom query schemas hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mgrdich committed Apr 12, 2024
1 parent c4b2bd7 commit 650ec6c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
51 changes: 43 additions & 8 deletions frontend/src/lib/hooks/api/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import {
GLOBAL_COMPATIBILITY_SCHEMAS_QUERY_KEY,
LATEST_SCHEMA_QUERY_KEY,
SCHEMA_QUERY_KEY,
SCHEMAS_VERSION_QUERY_KEY,
} from 'lib/queries';
import {
CompatibilityLevel,
DeleteSchemaRequest,
GetAllVersionsBySubjectRequest,
GetLatestSchemaRequest,
GetSchemasRequest,
NewSchemaSubject,
SchemaSubject,
SchemaSubjectsResponse,
UpdateGlobalSchemaCompatibilityLevelRequest,
UpdateSchemaCompatibilityLevelRequest,
} from 'generated-sources';
import { schemasApiClient } from 'lib/api';
import { ClusterName } from 'redux/interfaces';

export function useGetLatestSchemas(param: GetLatestSchemaRequest) {
return useQuery({
export function useGetLatestSchema(param: GetLatestSchemaRequest) {
return useQuery<SchemaSubject>({
queryKey: [
SCHEMA_QUERY_KEY,
LATEST_SCHEMA_QUERY_KEY,
Expand All @@ -35,7 +38,7 @@ export function useGetSchemas({
perPage,
search,
}: GetSchemasRequest) {
return useQuery({
return useQuery<SchemaSubjectsResponse>({
queryKey: [SCHEMA_QUERY_KEY, clusterName, page, perPage, search],
queryFn: () =>
schemasApiClient.getSchemas({
Expand All @@ -51,7 +54,7 @@ export function useGetSchemasVersions({
clusterName,
subject,
}: GetAllVersionsBySubjectRequest) {
return useQuery({
return useQuery<Array<SchemaSubject>>({
queryKey: [SCHEMAS_VERSION_QUERY_KEY, clusterName, subject],
queryFn: () =>
schemasApiClient.getAllVersionsBySubject({
Expand All @@ -61,6 +64,16 @@ export function useGetSchemasVersions({
});
}

export function useGetGlobalCompatibilityLayer(clusterName: ClusterName) {
return useQuery<CompatibilityLevel>({
queryKey: [GLOBAL_COMPATIBILITY_SCHEMAS_QUERY_KEY, clusterName],
queryFn: () =>
schemasApiClient.getGlobalSchemaCompatibilityLevel({
clusterName,
}),
});
}

export function useCreateSchema(clusterName: ClusterName) {
const queryClient = useQueryClient();
return useMutation<SchemaSubject, void, NewSchemaSubject>({
Expand All @@ -70,7 +83,11 @@ export function useCreateSchema(clusterName: ClusterName) {
newSchemaSubject: { subject, schema, schemaType },
}),
onSuccess: () => {
return queryClient.invalidateQueries();
return queryClient.invalidateQueries({
predicate: (query) =>
query.queryKey[0] === SCHEMA_QUERY_KEY &&
query.queryKey[1] === clusterName,
});
},
});
}
Expand All @@ -89,7 +106,11 @@ export function useUpdateSchemaCompatibilityLayer(clusterName: ClusterName) {
compatibilityLevel,
}),
onSuccess: () => {
return queryClient.invalidateQueries();
return queryClient.invalidateQueries({
predicate: (query) =>
query.queryKey[0] === SCHEMA_QUERY_KEY &&
query.queryKey[1] === clusterName,
});
},
});
}
Expand All @@ -110,7 +131,17 @@ export function useUpdateGlobalSchemaCompatibilityLevel(
compatibilityLevel,
}),
onSuccess: () => {
return queryClient.invalidateQueries();
return Promise.all([
queryClient.invalidateQueries([
GLOBAL_COMPATIBILITY_SCHEMAS_QUERY_KEY,
clusterName,
]),
queryClient.invalidateQueries({
predicate: (query) =>
query.queryKey[0] === SCHEMA_QUERY_KEY &&
query.queryKey[1] === clusterName,
}),
]);
},
});
}
Expand All @@ -125,7 +156,11 @@ export function useDeleteSchema(clusterName: ClusterName) {
subject,
}),
onSuccess: () => {
return queryClient.invalidateQueries();
return queryClient.invalidateQueries({
predicate: (query) =>
query.queryKey[0] === SCHEMA_QUERY_KEY &&
query.queryKey[1] === clusterName,
});
},
});
}
2 changes: 2 additions & 0 deletions frontend/src/lib/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
export const SCHEMA_QUERY_KEY = 'schemas';
export const LATEST_SCHEMA_QUERY_KEY = 'latest_schemas';
export const SCHEMAS_VERSION_QUERY_KEY = 'schemas_version';
export const GLOBAL_COMPATIBILITY_SCHEMAS_QUERY_KEY =
'global_compatibility_schemas';

0 comments on commit 650ec6c

Please sign in to comment.