From 650ec6ca5502c92d917a5199123d133b4dffd98d Mon Sep 17 00:00:00 2001 From: Mgrdich Date: Sat, 13 Apr 2024 00:27:20 +0400 Subject: [PATCH] add invalidation + typescript typing to the custom query schemas hooks. --- frontend/src/lib/hooks/api/schemas.ts | 51 ++++++++++++++++++++++----- frontend/src/lib/queries.ts | 2 ++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/frontend/src/lib/hooks/api/schemas.ts b/frontend/src/lib/hooks/api/schemas.ts index c8af680e8..4b03bcc71 100644 --- a/frontend/src/lib/hooks/api/schemas.ts +++ b/frontend/src/lib/hooks/api/schemas.ts @@ -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({ queryKey: [ SCHEMA_QUERY_KEY, LATEST_SCHEMA_QUERY_KEY, @@ -35,7 +38,7 @@ export function useGetSchemas({ perPage, search, }: GetSchemasRequest) { - return useQuery({ + return useQuery({ queryKey: [SCHEMA_QUERY_KEY, clusterName, page, perPage, search], queryFn: () => schemasApiClient.getSchemas({ @@ -51,7 +54,7 @@ export function useGetSchemasVersions({ clusterName, subject, }: GetAllVersionsBySubjectRequest) { - return useQuery({ + return useQuery>({ queryKey: [SCHEMAS_VERSION_QUERY_KEY, clusterName, subject], queryFn: () => schemasApiClient.getAllVersionsBySubject({ @@ -61,6 +64,16 @@ export function useGetSchemasVersions({ }); } +export function useGetGlobalCompatibilityLayer(clusterName: ClusterName) { + return useQuery({ + queryKey: [GLOBAL_COMPATIBILITY_SCHEMAS_QUERY_KEY, clusterName], + queryFn: () => + schemasApiClient.getGlobalSchemaCompatibilityLevel({ + clusterName, + }), + }); +} + export function useCreateSchema(clusterName: ClusterName) { const queryClient = useQueryClient(); return useMutation({ @@ -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, + }); }, }); } @@ -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, + }); }, }); } @@ -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, + }), + ]); }, }); } @@ -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, + }); }, }); } diff --git a/frontend/src/lib/queries.ts b/frontend/src/lib/queries.ts index 57086965f..4bbf9e59a 100644 --- a/frontend/src/lib/queries.ts +++ b/frontend/src/lib/queries.ts @@ -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';