Skip to content

Commit

Permalink
Merge pull request #515 from stfnsr/fix/retain-union-use-query
Browse files Browse the repository at this point in the history
  • Loading branch information
psteinroe authored Nov 3, 2024
2 parents 1a9c340 + 70854e1 commit 3f0a62e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-planes-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-react-query": minor
---

fix: retain union type in useQuery result
37 changes: 28 additions & 9 deletions packages/postgrest-react-query/src/query/use-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ import {

import { buildQueryOpts } from './build-query-opts';

/**
* Applies Omit over a union, while preserving its union-ness.
*/
type DistributiveOmit<T, K extends keyof any> = T extends any
? Omit<T, K>
: never;

/**
* Represents the return value of the `useQuery` hook when `query` is expected to return
* a single row.
*/
export type UseQuerySingleReturn<Result> = Omit<
export type UseQuerySingleReturn<Result> = DistributiveOmit<
UseReactQueryResult<PostgrestSingleResponse<Result>['data'], PostgrestError>,
'refetch'
> &
Expand All @@ -31,7 +38,7 @@ export type UseQuerySingleReturn<Result> = 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<Result> = Omit<
export type UseQueryMaybeSingleReturn<Result> = DistributiveOmit<
UseReactQueryResult<
PostgrestMaybeSingleResponse<Result>['data'],
PostgrestError
Expand All @@ -48,7 +55,7 @@ export type UseQueryMaybeSingleReturn<Result> = Omit<
* Represents the return value of the `useQuery` hook when `query` is expected to return
* one or more rows.
*/
export type UseQueryReturn<Result> = Omit<
export type UseQueryReturn<Result> = DistributiveOmit<
UseReactQueryResult<PostgrestResponse<Result>['data'], PostgrestError>,
'refetch'
> &
Expand All @@ -62,7 +69,7 @@ export type UseQueryReturn<Result> = Omit<
* Represents the return value of the `useQuery` hook when the type of the query response
* is not known.
*/
export type UseQueryAnyReturn<Result> = Omit<
export type UseQueryAnyReturn<Result> = DistributiveOmit<
UseReactQueryResult<AnyPostgrestResponse<Result>['data'], PostgrestError>,
'refetch'
> &
Expand Down Expand Up @@ -131,12 +138,24 @@ function useQuery<Result>(
'queryKey' | 'queryFn'
>,
): UseQueryAnyReturn<Result> {
const { data, ...rest } = useReactQuery<
AnyPostgrestResponse<Result>,
PostgrestError
>(buildQueryOpts<Result>(query, config));
const result = useReactQuery<AnyPostgrestResponse<Result>, PostgrestError>(
buildQueryOpts<Result>(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 };

0 comments on commit 3f0a62e

Please sign in to comment.