0

I followed the guide in the docs to create a custom SQL function. However, I am having trouble generating types for it on my frontend. This is what my function looks like:

CREATE
OR REPLACE FUNCTION public.search_deals(
  search text,
  hasura_session json,
  order_by_full_name boolean DEFAULT false,
  order_by_email boolean DEFAULT false,
  order_by_deal_status boolean DEFAULT false,
  order_by_loan_status boolean DEFAULT false,
  order_by_created_at boolean DEFAULT false,
  limit_param integer DEFAULT 8,
  offset_param integer DEFAULT 0
) RETURNS SETOF sort_types.__deals LANGUAGE plpgsql AS $ function $ BEGIN RETURN QUERY
SELECT
  deal.id,
  deal."_createdAt",
  deal."dealStatusID",
  dp."partyID",
  party."partyRoleID",
  pp."personID",
  person."fullName",
  person."primaryEmail",
  loan.id AS "loanID",
  loan."loanStatusID"
FROM
  deal
  JOIN "DealParty" dp ON deal.id = dp."dealID"
  JOIN party ON dp."partyID" = party.id
  JOIN "PartyPerson" pp ON party.id = pp."partyID"
  JOIN loan ON deal.id = loan."dealID"
  JOIN person ON person.id = pp."personID"
WHERE
  (
    party."partyRoleID" = 1
    AND person."primaryEmail" ILIKE '%' || search || '%'
  )
  OR (
    party."partyRoleID" = 1
    AND person."fullName" ILIKE '%' || search || '%'
  )
  AND EXISTS (
    SELECT
      1
    FROM
      "DealParty" dp2
      JOIN party party2 ON dp2."partyID" = party2.id
      JOIN "PartyPerson" pp2 ON party2.id = pp2."partyID"
      JOIN person person2 ON pp2."personID" = person2.id
    WHERE
      dp2."dealID" = deal.id
      AND party2."partyRoleID" = 2
      AND person2."primaryEmail" = hasura_session ->> 'x-hasura-user-id'
  )
ORDER BY
  CASE
    WHEN order_by_full_name THEN person."fullName" :: text
    WHEN order_by_email THEN person."primaryEmail" :: text
    WHEN order_by_deal_status THEN deal."dealStatusID" :: text
    WHEN order_by_loan_status THEN loan."loanStatusID" :: text
    WHEN order_by_created_at THEN deal."_createdAt" :: text
    ELSE deal."_lastUpdatedAt" :: text
  END DESC
LIMIT
  limit_param OFFSET offset_param;
END;
$ function $

Now, I am using React with TypeScript and @graphql-codegen for generating types. This is my codegen config:

const config: CodegenConfig = {
  schema: {
    [process.env.NEXT_PUBLIC_HASURA_HOST!]: {
      headers: {
        'x-hasura-admin-secret': process.env.HASURA_ADMIN_TOKEN!,
      },
    },
  },
  documents: [
    './pages/**/*.{ts,tsx}',
    './components/**/*.{ts,tsx}',
    './hooks/**/*.{ts,tsx}',
  ],
  ignoreNoDocuments: true,
  generates: {
    './gql/': {
      preset: 'client',
      plugins: [],
      config: {
        strictScalars: true,
        scalars: {
          date: 'string',
          timestamp: 'string',
          timestamptz: 'string',
          bigint: 'number',
        },
      },
    },
  },
};

export default config;

When I run graphql-codegen, I get the following error:

GraphQL Document Validation failed with 1 errors;
Error 0: Cannot query field "search_deals" on type "query_root"

How can I generate types for this function?

chomprrr
  • 406
  • 4
  • 15
  • I needed to use the `pg_track_function` method via the API to track this function as a query since tracking it manually via the console would add it as a mutation. This is the body of the request that I used to do this: ` { "type": "pg_track_function", "args": { "source": "default", "function": { "schema": "public", "name": "search_deals" }, "configuration": { "session_argument": "hasura_session", "exposed_as": "query" }, } } ` – chomprrr May 08 '23 at 13:24

0 Answers0