psteinroe / supabase-cache-helpers

A collection of framework specific Cache utilities for working with Supabase.

Home Page:https://supabase-cache-helpers.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Supabase v2 Types

drewbietron opened this issue · comments

Describe the bug
I see this package uses the v2 Supabase JS client, but the types are not showing for me in my queries. useQuery is returning

const category = useQuery(
  client.from("categories").select("id, name").eq("id", props.route.params.category.id),
  "single"
);

category -> SWRResponse<unknown, PostgrestError>

I can confirm that the types from the client are in fact working. That same query outside of useQuery returns

const categoryQuery: PromiseLike<PostgrestSingleResponse<{
    id: string;
} & {
    name: string;
}>>

Expected behavior
Types from the query are available with the SWRResponse.

Appreciate you making this library! I have been trying to React Query to play nicely with the new v2 types and its been a challenge. Nice to see you tackle this already using SWR!

Hey @drewbietron, thanks for the kind words!

It seems like there was an issue with the types generated by the Supabase cli: If a schema doesn't contain e.g. a View, the property was omitted from the types. Since the client expects a GenericSchema with Views, Tables, and Functions,

Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
    ? Database[SchemaName]
    : any

resolved to any. I regenerated the types for the tests in this repo (#54) and the types are now correctly resolved. Could you also try that on your side?

@psteinroe - Thanks for the response. You mean regenerate the Supabase types and try using it again? If it helps, I am using Supabase CLI v1.11.2 and generating them using

supabase gen types typescript

Yes. If it is still returning unknown type, could you please share a small reproduction?

@drewbietron what supabase-js version are you using?

@drewbietron what supabase-js version are you using?

2.0.4

Just tried updating the types once more and the issues still exists.

The problem is this this pr (supabase/postgrest-js@4081771) which was releases in postgrest-js v1.1.0 which in turn was added to supabase-js with 2.0.4. I am currently working on adding that upgrade to cache helpers, but it might take a few days since it is a larger breaking change. Until then, I recommend downgrading supabase-js again. The types should work then.

Hence, this is indeed a bug and thanks for reporting!

Appreciate you looking into it!

Curious on your approach to how you created the cache keys? React Query out of the box will infer the type of the Supabase client query, but the cache keys are still totally up in the air. I was hoping there would be a way to infer the cache keys from the query itself, but it's complex. The URL params that are sent to the rest client would be awesome, but not sure how to get them within the request, or make some sort of generic component that could do it all. I asked about doing this in a round about way here.

I just published a new release that uses postgrest-js v1.1.0! Please upgrade to @supabase-cache-helpers/postgrest-swr@0.3.1.

Appreciate you looking into it!

Curious on your approach to how you created the cache keys? React Query out of the box will infer the type of the Supabase client query, but the cache keys are still totally up in the air. I was hoping there would be a way to infer the cache keys from the query itself, but it's complex. The URL params that are sent to the rest client would be awesome, but not sure how to get them within the request, or make some sort of generic component that could do it all. I asked about doing this in a round about way here.

lets move this to #57