cli icon indicating copy to clipboard operation
cli copied to clipboard

Return types for RPC cannot have nullable properties

Open dbuhon opened this issue 2 years ago • 2 comments

The supabase gen types appears to lack support for RPC functions returning objects with nullable properties.

Steps to reproduce:

Define an RPC function:

   CREATE TYPE status_type_enum AS ENUM ('rotten', 'ripe'); 
   
   CREATE FUNCTION get_bananas(
     _limit INT DEFAULT NULL,
     _offset INT DEFAULT 0
   )
   RETURNS TABLE (
     id UUID,
     text TEXT,
     current_status status_type_enum -- Is there a way to make this nullable in TS?
   )
   LANGUAGE plpgsql
   AS $$
   BEGIN
     RETURN QUERY
     SELECT
       b.id,
       b.text,
       s.status_type AS current_status
     FROM bananas b
     LEFT JOIN statuses s ON b.id = s.banana_id AND s.user_id = auth.uid()
     LIMIT _limit
     OFFSET _offset;
   END;
   $$;

Run the command:

npx supabase gen types

Examine the generated types: It doesn't seem possible to make current_status nullable out of the box, even though it can be missing.

   get_bananas: {
     Args: {
       _limit?: number;
       _offset?: number;
     };
     Returns: {
       id: string;
       text: string;
       current_status: Database['public']['Enums']['status_type_enum'];
       // current_status: null | Database['public']['Enums']['status_type_enum'];
     }[];
   };

Workaround

Extend the generated types from another file to overwrite properties.

dbuhon avatar Nov 09 '23 23:11 dbuhon

One thing that changing this will likely do is affect a lot of people's code.

(i.e. types that were previously non-null are now suddenly going to throw a lot of typescript compile errors)

So it may be wise to have this behind some sort of feature flag, i.e. --rpc-allow-null-results or something like that.

Marviel avatar Jan 04 '24 13:01 Marviel