{-# LANGUAGE UndecidableInstances #-}

-- | The representation of stored procedures as derived from the schema cache.
module Hasura.StoredProcedure.Cache
  ( StoredProcedureInfo (..),
    StoredProcedureCache,
  )
where

import Data.Aeson (ToJSON (toJSON), genericToJSON)
import Hasura.LogicalModel.Cache (LogicalModelInfo)
import Hasura.Prelude
import Hasura.RQL.Types.Backend (Backend, FunctionName)
import Hasura.RQL.Types.BackendType (BackendType)
import Hasura.StoredProcedure.Metadata (ArgumentName)
import Hasura.StoredProcedure.Types (NullableScalarType, StoredProcedureConfig)
import Hasura.Table.Cache (RolePermInfoMap)
import Language.GraphQL.Draft.Syntax qualified as G

type StoredProcedureCache b = HashMap (FunctionName b) (StoredProcedureInfo b)

-- | The type into which 'StoredProcedureMetadata' is resolved in
-- 'Hasura/RQL/DDL/Schema/Cache.buildSchemaCacheRule'.
data StoredProcedureInfo (b :: BackendType) = StoredProcedureInfo
  { forall (b :: BackendType). StoredProcedureInfo b -> FunctionName b
_spiStoredProcedure :: FunctionName b,
    -- | The GraphQL name of the stored procedure.
    forall (b :: BackendType). StoredProcedureInfo b -> Name
_spiGraphqlName :: G.Name,
    forall (b :: BackendType).
StoredProcedureInfo b -> StoredProcedureConfig
_spiConfig :: StoredProcedureConfig,
    forall (b :: BackendType).
StoredProcedureInfo b -> LogicalModelInfo b
_spiReturns :: LogicalModelInfo b,
    forall (b :: BackendType).
StoredProcedureInfo b
-> HashMap ArgumentName (NullableScalarType b)
_spiArguments :: HashMap ArgumentName (NullableScalarType b),
    forall (b :: BackendType). StoredProcedureInfo b -> Maybe Text
_spiDescription :: Maybe Text
  }
  deriving stock ((forall x. StoredProcedureInfo b -> Rep (StoredProcedureInfo b) x)
-> (forall x.
    Rep (StoredProcedureInfo b) x -> StoredProcedureInfo b)
-> Generic (StoredProcedureInfo b)
forall x. Rep (StoredProcedureInfo b) x -> StoredProcedureInfo b
forall x. StoredProcedureInfo b -> Rep (StoredProcedureInfo b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) x.
Rep (StoredProcedureInfo b) x -> StoredProcedureInfo b
forall (b :: BackendType) x.
StoredProcedureInfo b -> Rep (StoredProcedureInfo b) x
$cfrom :: forall (b :: BackendType) x.
StoredProcedureInfo b -> Rep (StoredProcedureInfo b) x
from :: forall x. StoredProcedureInfo b -> Rep (StoredProcedureInfo b) x
$cto :: forall (b :: BackendType) x.
Rep (StoredProcedureInfo b) x -> StoredProcedureInfo b
to :: forall x. Rep (StoredProcedureInfo b) x -> StoredProcedureInfo b
Generic)

instance
  (Backend b, ToJSON (RolePermInfoMap b)) =>
  ToJSON (StoredProcedureInfo b)
  where
  toJSON :: StoredProcedureInfo b -> Value
toJSON = Options -> StoredProcedureInfo b -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
hasuraJSON