Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useSuspenseQuery() is missing #454

Open
thedevdavid opened this issue May 29, 2024 · 2 comments
Open

useSuspenseQuery() is missing #454

thedevdavid opened this issue May 29, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@thedevdavid
Copy link

I don't see the implementation of Tanstack router's useSuspenseQuery(). Is this intentional?

@thedevdavid thedevdavid added the enhancement New feature or request label May 29, 2024
@psteinroe
Copy link
Owner

didn't do it yet. happy to support if you want to attempt it. should be pretty straightforward based off useQuery 🙏🏼

@alexgorbatchev
Copy link

alexgorbatchev commented Oct 21, 2024

it's relatively easy to get it working for useQuery, here's my version:

import { type UseQuerySingleReturn, useQuery } from '@supabase-cache-helpers/postgrest-react-query';
import type { PostgrestError, PostgrestSingleResponse } from '@supabase/supabase-js';
import type { UseBaseQueryOptions, UseQueryOptions } from '@tanstack/react-query';

export function useSuspenseQuery<Result>(
  query: PromiseLike<PostgrestSingleResponse<Result>>,
  config?: Omit<UseQueryOptions<PostgrestSingleResponse<Result>, PostgrestError>, 'queryKey' | 'queryFn'>,
): UseQuerySingleReturn<Result> {
  //
  // HACK
  //
  // original useSuspenseQuery uses useBaseQuery
  // https://github.com/TanStack/query/blob/main/packages/react-query/src/useSuspenseQuery.ts
  //
  // aaand, original useQuery also uses useBaseQuery
  // https://github.com/TanStack/query/blob/main/packages/react-query/src/useQuery.ts
  //
  // Therefore, we can just pass the same config that useSuspenseQuery passes to
  // useBaseQuery to supabase-cache-helpers' useQuery.
  //
  const suspense: Omit<UseBaseQueryOptions<PostgrestSingleResponse<Result>, PostgrestError>, 'queryKey' | 'queryFn'> = {
    suspense: true,
    placeholderData: undefined,
    throwOnError(_error, query) {
      return query.state.data === undefined;
    },
  };

  return useQuery(query, { ...config, ...suspense } as any);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants