{-# LANGUAGE UndecidableInstances #-}

-- | The representation of native queries as derived from the schema cache.
module Hasura.NativeQuery.Cache
  ( NativeQueryInfo (..),
    NativeQueryCache,
  )
where

import Data.Aeson (ToJSON (toJSON), genericToJSON)
import Hasura.LogicalModel.Cache (LogicalModelInfo)
import Hasura.NativeQuery.Metadata (ArgumentName, InterpolatedQuery, NativeQueryName)
import Hasura.NativeQuery.Types (NullableScalarType)
import Hasura.Prelude
import Hasura.RQL.Types.Backend (Backend)
import Hasura.RQL.Types.BackendType (BackendType)
import Hasura.RQL.Types.Common (RelName)
import Hasura.RQL.Types.Relationships.Local (RelInfo)
import Hasura.Table.Cache (RolePermInfoMap)

type NativeQueryCache b = HashMap NativeQueryName (NativeQueryInfo b)

-- | The type into which 'NativeQueryMetadata' is resolved in
-- 'Hasura/RQL/DDL/Schema/Cache.buildSchemaCacheRule'.
data NativeQueryInfo (b :: BackendType) = NativeQueryInfo
  { forall (b :: BackendType). NativeQueryInfo b -> NativeQueryName
_nqiRootFieldName :: NativeQueryName,
    forall (b :: BackendType).
NativeQueryInfo b -> InterpolatedQuery ArgumentName
_nqiCode :: InterpolatedQuery ArgumentName,
    forall (b :: BackendType). NativeQueryInfo b -> LogicalModelInfo b
_nqiReturns :: LogicalModelInfo b,
    forall (b :: BackendType).
NativeQueryInfo b -> HashMap ArgumentName (NullableScalarType b)
_nqiArguments :: HashMap ArgumentName (NullableScalarType b),
    forall (b :: BackendType).
NativeQueryInfo b -> InsOrdHashMap RelName (RelInfo b)
_nqiRelationships :: InsOrdHashMap RelName (RelInfo b),
    forall (b :: BackendType). NativeQueryInfo b -> Maybe Text
_nqiDescription :: Maybe Text
  }
  deriving stock ((forall x. NativeQueryInfo b -> Rep (NativeQueryInfo b) x)
-> (forall x. Rep (NativeQueryInfo b) x -> NativeQueryInfo b)
-> Generic (NativeQueryInfo b)
forall x. Rep (NativeQueryInfo b) x -> NativeQueryInfo b
forall x. NativeQueryInfo b -> Rep (NativeQueryInfo b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) x.
Rep (NativeQueryInfo b) x -> NativeQueryInfo b
forall (b :: BackendType) x.
NativeQueryInfo b -> Rep (NativeQueryInfo b) x
$cfrom :: forall (b :: BackendType) x.
NativeQueryInfo b -> Rep (NativeQueryInfo b) x
from :: forall x. NativeQueryInfo b -> Rep (NativeQueryInfo b) x
$cto :: forall (b :: BackendType) x.
Rep (NativeQueryInfo b) x -> NativeQueryInfo b
to :: forall x. Rep (NativeQueryInfo b) x -> NativeQueryInfo b
Generic)

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