Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

1.4 KB
Raw Blame
import { useQuery } from '@tanstack/react-query'

import { contentApiKeys } from './keys'
import { executeGraphQL } from '@/data/graphql/execute'
import { graphql } from '@/data/graphql/gql'
import { Service } from '@/data/graphql/graphql'
import { UseCustomQueryOptions } from '@/types'

const ErrorCodeQuery = graphql(`
  query ErrorCodeQuery($code: String!, $service: Service) {
    errors(code: $code, service: $service) {
      nodes {
        code
        service
        message
      }
    }
  }
`)

interface Variables {
  code: string
  service?: Service
}

async function getErrorCodeDescriptions({ code, service }: Variables, signal?: AbortSignal) {
  return await executeGraphQL(ErrorCodeQuery, { variables: { code, service }, signal })
}

type ErrorCodeDescriptionsData = Awaited<ReturnType<typeof getErrorCodeDescriptions>>
type ErrorCodeDescriptionsError = unknown

export const useErrorCodesQuery = <TData = ErrorCodeDescriptionsData>(
  variables: Variables,
  {
    enabled = true,
    ...options
  }: UseCustomQueryOptions<ErrorCodeDescriptionsData, ErrorCodeDescriptionsError, TData> = {}
) => {
  return useQuery<ErrorCodeDescriptionsData, ErrorCodeDescriptionsError, TData>({
    queryKey: contentApiKeys.errorCodes(variables),
    queryFn: ({ signal }) => getErrorCodeDescriptions(variables, signal),
    enabled,
    retry: false,
    staleTime: Infinity,
    ...options,
  })
}