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

Supabase improperly types postgrest computed relationships #1364

Open
2 tasks done
gwax opened this issue Jan 28, 2025 · 1 comment
Open
2 tasks done

Supabase improperly types postgrest computed relationships #1364

gwax opened this issue Jan 28, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@gwax
Copy link

gwax commented Jan 28, 2025

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

Supabase supports making calls using postgrest computed relationships: https://postgrest.org/en/v12/references/api/resource_embedding.html#computed-relationships

However, these show up as typescript errors of the form SelectQueryError<"could not find the relation between ...">

The types for computed relationships (mostly) worked in 2.47.7 but broke starting in 2.47.8

To Reproduce

Minimal reproduction is available in the gwax/computed-relationships branch here: https://github.com/manapoolinc/supabase-playground/tree/gwax/computed-relationships

  1. Create a computed relationship: https://github.com/manapoolinc/supabase-playground/blob/gwax/computed-relationships/supabase/migrations/20250127194033_create_country_fns.sql#L1
  2. Use the computed relationship in a query: https://github.com/manapoolinc/supabase-playground/blob/gwax/computed-relationships/src/lib/db/countries.ts#L47
  3. See type error in IDE / typescript compiler: https://github.com/manapoolinc/supabase-playground/blob/gwax/computed-relationships/src/lib/db/countries.ts#L101

Expected behavior

No Type Errors in reproduction code

System information

  • Version of supabase: 2.6.8
  • Version of supabase-js: 2.48.1
@gwax gwax added the bug Something isn't working label Jan 28, 2025
@stefan-girlich
Copy link

stefan-girlich commented Feb 6, 2025

Thanks for documenting this issue! I've been using a custom utility type to manually override the faulty type, but it has become a burden for deeply embedded resources - see the code below.

I am exploring how to improve the utility, but I hope a native solution is possible.

Show code applying custom utility type
type RawCustomerContractLineItem = NonNullable<
  typeof rawData
>['customer_contract_line_items'][number]
type RawLineItemToPurchaseAssignment =
  RawCustomerContractLineItem['customer_contract_line_items__token_purchases'][number]
type RawTokenPurchase = RawLineItemToPurchaseAssignment['token_purchases']
type RawBatch = RawTokenPurchase['batches_flat']
type RawOutputFraction = RawBatch['output_fractions'][number]

type Result = FixSupabaseQueryDataSingle<
  typeof rawData,
  'customer_contract_line_items',
  {
    customer_contract_line_items: FixSupabaseQueryDataSingle<
      RawCustomerContractLineItem,
      'customer_contract_line_items__token_purchases',
      {
        customer_contract_line_items__token_purchases: FixSupabaseQueryDataSingle<
          RawLineItemToPurchaseAssignment,
          'token_purchases',
          FixSupabaseQueryDataSingle<
            RawTokenPurchase,
            'batches_flat',
            {
              batches_flat: FixSupabaseQueryDataSingle<
                RawBatch,
                'output_fractions',
                FixSupabaseQueryDataSingle<
                  RawOutputFraction,
                  'output_materials',
                  {
                    output_materials: {
                      id: string
                      name: string
                    }
                  }
                >
              >
            }
          >
        >[]
      }
    >[]
  }
>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants