Tanu
Tanu

Reputation: 1634

GraphQL : OverLoaded function error when adding a computed field

I am trying to add a computed field to my graphql table "user" in schema "abc" using the hasura API but receiving the following error :

**Saving computed field failed**
in table "abc.user": in computed field "allaccounts": function "abc.get_accounts" 
is overloaded. Overloaded functions are not supported

The function is added correctly :

  CREATE OR REPLACE FUNCTION abc.get_accounts(id bigint)
    RETURNS VARCHAR immutable AS $$
DECLARE
    value VARCHAR;
BEGIN
    SELECT array_to_string(ARRAY_AGG( name ORDER BY name ASC )::varchar[], ',', '')
           into value
    FROM abc.account
             INNER JOIN abc.user_account ON (account.id=user_account.account_id)
    where user_account.user_id = id group by user_id;
    return value;
END;
$$ LANGUAGE plpgsql;

id field is present in user table.

I am able to select the function "get_accounts" from the dropdown but getting an error on adding computed field. Any guidance is appreciated. Thank you.

Upvotes: 1

Views: 1255

Answers (2)

Tanu
Tanu

Reputation: 1634

Adding STABLE worked for me :

CREATE OR REPLACE FUNCTION abc.all_accounts(user_row abc.user)
  RETURNS VARCHAR AS $$
DECLARE
  value VARCHAR;
BEGIN
  SELECT array_to_string(ARRAY_AGG( name ORDER BY name ASC )::varchar[], ',', '')
      into value
               FROM abc.account
                      INNER JOIN abc.user_account ON (account.id=user_account.account_id)
                   where abc.user_account.user_id = user_row.id
               group by user_id;
  return value;
END;
$$ LANGUAGE plpgsql STABLE;

Upvotes: 1

GMB
GMB

Reputation: 222572

Your function is overloaded. That is, you have (at least) another function in the database that uses the same name and another list of arguments.

You typically need to identify the conflicting function, and drop it (unless you have a good reason not to). You can exhibit the "homonyms" with the following query, that generates drop function statements which you can directly use:

select format('drop %s %s;', case when proisagg then 'aggregate' else 'function' end, oid::regprocedure) as stmt
from pg_catalog.pg_proc
where proname = 'get_accounts'

Credits go to Erwin Brandstetter in this SO answer, that goes into more details on how to solve the problem.

Upvotes: 1

Related Questions