2023-05-03 12:59:21 +03:00
|
|
|
-- | Common codecs shared between similar logical model resolvers.
|
|
|
|
module Hasura.LogicalModelResolver.Codec
|
2023-05-16 18:07:41 +03:00
|
|
|
( nativeQueryRelationshipsCodec,
|
2023-05-03 12:59:21 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Autodocodec (HasCodec (), HasObjectCodec (..), bimapCodec)
|
|
|
|
import Autodocodec qualified as AC
|
|
|
|
import Data.Aeson (Value)
|
|
|
|
import Data.HashMap.Strict.InsOrd qualified as InsOrdHashMap
|
|
|
|
import Hasura.Prelude hiding (first)
|
|
|
|
import Hasura.RQL.Types.Backend (Backend (..))
|
|
|
|
import Hasura.RQL.Types.Common (RelName)
|
2023-05-16 18:07:41 +03:00
|
|
|
import Hasura.RQL.Types.Relationships.Local (RelDef, RelManualNativeQueryConfig)
|
2023-05-03 12:59:21 +03:00
|
|
|
|
2023-05-16 18:07:41 +03:00
|
|
|
-- | Codec for native-query-only relationships
|
|
|
|
nativeQueryRelationshipsCodec ::
|
2023-05-03 12:59:21 +03:00
|
|
|
forall b.
|
|
|
|
(Backend b) =>
|
|
|
|
AC.Codec
|
|
|
|
Value
|
2023-05-16 18:07:41 +03:00
|
|
|
(InsOrdHashMap.InsOrdHashMap RelName (RelDef (RelManualNativeQueryConfig b)))
|
|
|
|
(InsOrdHashMap.InsOrdHashMap RelName (RelDef (RelManualNativeQueryConfig b)))
|
|
|
|
nativeQueryRelationshipsCodec =
|
2023-05-03 12:59:21 +03:00
|
|
|
AC.dimapCodec
|
|
|
|
( InsOrdHashMap.fromList
|
|
|
|
. fmap
|
|
|
|
( \(MergedObject (NameField name) nst) ->
|
|
|
|
(name, nst)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
( fmap (\(fld, nst) -> MergedObject (NameField fld) nst) . InsOrdHashMap.toList
|
|
|
|
)
|
2023-05-24 16:51:56 +03:00
|
|
|
( AC.listCodec
|
|
|
|
$ AC.object "RelDefRelManualNativeQueryConfig"
|
|
|
|
$ AC.objectCodec @(MergedObject (NameField RelName) (RelDef (RelManualNativeQueryConfig b)))
|
2023-05-03 12:59:21 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
data MergedObject a b = MergedObject
|
|
|
|
{ moFst :: a,
|
|
|
|
moSnd :: b
|
|
|
|
}
|
|
|
|
|
|
|
|
instance (HasObjectCodec a, HasObjectCodec b) => HasObjectCodec (MergedObject a b) where
|
|
|
|
objectCodec = MergedObject <$> bimapCodec Right moFst objectCodec <*> bimapCodec Right moSnd objectCodec
|
|
|
|
|
|
|
|
newtype NameField a = NameField {nameField :: a}
|
|
|
|
|
|
|
|
instance (HasCodec a) => HasObjectCodec (NameField a) where
|
|
|
|
objectCodec = NameField <$> AC.requiredField "name" "name" AC..= nameField
|