From 3c40e6f8f66212449ae0ddb6075daaea9e6e5d74 Mon Sep 17 00:00:00 2001 From: Stefan Osorio Date: Sun, 3 Nov 2024 12:47:32 +0100 Subject: [PATCH 1/3] fix: allow useQuery (react-query version) to return a union --- .../src/query/use-query.ts | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/packages/postgrest-react-query/src/query/use-query.ts b/packages/postgrest-react-query/src/query/use-query.ts index e83aa7a5c..914e742ec 100644 --- a/packages/postgrest-react-query/src/query/use-query.ts +++ b/packages/postgrest-react-query/src/query/use-query.ts @@ -13,11 +13,20 @@ import { import { buildQueryOpts } from './build-query-opts'; + +/** + * Applies Omit over a union, while preserving its union-ness. + */ +type DistributiveOmit = T extends any + ? Omit + : never; + + /** * Represents the return value of the `useQuery` hook when `query` is expected to return * a single row. */ -export type UseQuerySingleReturn = Omit< +export type UseQuerySingleReturn = DistributiveOmit< UseReactQueryResult['data'], PostgrestError>, 'refetch' > & @@ -31,7 +40,7 @@ export type UseQuerySingleReturn = Omit< * Represents the return value of the `useQuery` hook when `query` is expected to return * either a single row or an empty response. */ -export type UseQueryMaybeSingleReturn = Omit< +export type UseQueryMaybeSingleReturn = DistributiveOmit< UseReactQueryResult< PostgrestMaybeSingleResponse['data'], PostgrestError @@ -48,7 +57,7 @@ export type UseQueryMaybeSingleReturn = Omit< * Represents the return value of the `useQuery` hook when `query` is expected to return * one or more rows. */ -export type UseQueryReturn = Omit< +export type UseQueryReturn = DistributiveOmit< UseReactQueryResult['data'], PostgrestError>, 'refetch' > & @@ -62,7 +71,7 @@ export type UseQueryReturn = Omit< * Represents the return value of the `useQuery` hook when the type of the query response * is not known. */ -export type UseQueryAnyReturn = Omit< +export type UseQueryAnyReturn = DistributiveOmit< UseReactQueryResult['data'], PostgrestError>, 'refetch' > & @@ -131,12 +140,24 @@ function useQuery( 'queryKey' | 'queryFn' >, ): UseQueryAnyReturn { - const { data, ...rest } = useReactQuery< - AnyPostgrestResponse, - PostgrestError - >(buildQueryOpts(query, config)); + const result = useReactQuery, PostgrestError>( + buildQueryOpts(query, config) + ); + + // isPending and isLoadingError are the only cases in which no data is present + if (result.isPending || result.isLoadingError) { + return { + ...result, + data: undefined, + count: null, + }; + } - return { data: data?.data, count: data?.count ?? null, ...rest }; + return { + ...result, + data: result.data?.data, + count: result.data?.count, + }; } export { useQuery }; From 74571ce7dc4f941531183cd83342c7f93b404ec9 Mon Sep 17 00:00:00 2001 From: Stefan Osorio Date: Sun, 3 Nov 2024 13:57:36 +0100 Subject: [PATCH 2/3] add changeset --- .changeset/tame-planes-report.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tame-planes-report.md diff --git a/.changeset/tame-planes-report.md b/.changeset/tame-planes-report.md new file mode 100644 index 000000000..5035fa138 --- /dev/null +++ b/.changeset/tame-planes-report.md @@ -0,0 +1,5 @@ +--- +"@supabase-cache-helpers/postgrest-react-query": minor +--- + +fix: retain union type in useQuery result From 70854e154d1040fcb1c9ef2c4324a31356814235 Mon Sep 17 00:00:00 2001 From: Stefan Osorio Date: Sun, 3 Nov 2024 14:42:12 +0100 Subject: [PATCH 3/3] formatting --- packages/postgrest-react-query/src/query/use-query.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/postgrest-react-query/src/query/use-query.ts b/packages/postgrest-react-query/src/query/use-query.ts index 914e742ec..914d45967 100644 --- a/packages/postgrest-react-query/src/query/use-query.ts +++ b/packages/postgrest-react-query/src/query/use-query.ts @@ -13,7 +13,6 @@ import { import { buildQueryOpts } from './build-query-opts'; - /** * Applies Omit over a union, while preserving its union-ness. */ @@ -21,7 +20,6 @@ type DistributiveOmit = T extends any ? Omit : never; - /** * Represents the return value of the `useQuery` hook when `query` is expected to return * a single row. @@ -141,7 +139,7 @@ function useQuery( >, ): UseQueryAnyReturn { const result = useReactQuery, PostgrestError>( - buildQueryOpts(query, config) + buildQueryOpts(query, config), ); // isPending and isLoadingError are the only cases in which no data is present