2022-03-16 07:12:15 +03:00
{- # LANGUAGE DeriveAnyClass # -}
2022-03-31 07:45:03 +03:00
{- # LANGUAGE OverloadedLists # -}
2022-03-16 07:12:15 +03:00
2022-05-02 08:03:12 +03:00
module Hasura.Backends.DataConnector.API.V0.OrderBy
2022-03-16 07:12:15 +03:00
( OrderBy ( .. ) ,
2022-08-19 10:00:46 +03:00
OrderByRelation ( .. ) ,
OrderByElement ( .. ) ,
OrderByTarget ( .. ) ,
OrderDirection ( .. ) ,
2022-03-16 07:12:15 +03:00
)
where
2022-07-27 08:27:34 +03:00
import Autodocodec
2022-03-31 07:45:03 +03:00
import Autodocodec.OpenAPI ( )
2022-04-01 04:20:23 +03:00
import Control.DeepSeq ( NFData )
2022-03-16 07:12:15 +03:00
import Data.Aeson ( FromJSON , ToJSON )
2022-04-01 04:20:23 +03:00
import Data.Data ( Data )
2022-08-19 10:00:46 +03:00
import Data.HashMap.Strict ( HashMap )
import Data.HashMap.Strict qualified as HashMap
2022-04-01 04:20:23 +03:00
import Data.Hashable ( Hashable )
2022-08-19 10:00:46 +03:00
import Data.List.NonEmpty ( NonEmpty )
2022-03-31 07:45:03 +03:00
import Data.OpenApi ( ToSchema )
2022-04-01 04:20:23 +03:00
import GHC.Generics ( Generic )
2022-08-19 10:00:46 +03:00
import Hasura.Backends.DataConnector.API.V0.Aggregate qualified as API . V0
2022-05-02 08:03:12 +03:00
import Hasura.Backends.DataConnector.API.V0.Column qualified as API . V0
2022-08-19 10:00:46 +03:00
import Hasura.Backends.DataConnector.API.V0.Expression qualified as API . V0
import Hasura.Backends.DataConnector.API.V0.Relationships qualified as API . V0
2022-04-01 04:20:23 +03:00
import Prelude
2022-03-16 07:12:15 +03:00
--------------------------------------------------------------------------------
data OrderBy = OrderBy
2022-08-19 10:00:46 +03:00
{ _obRelations :: HashMap API . V0 . RelationshipName OrderByRelation ,
_obElements :: NonEmpty OrderByElement
2022-03-16 07:12:15 +03:00
}
deriving stock ( Data , Eq , Generic , Ord , Show )
2022-03-31 07:45:03 +03:00
deriving ( FromJSON , ToJSON , ToSchema ) via Autodocodec OrderBy
2022-03-16 07:12:15 +03:00
2022-03-31 07:45:03 +03:00
instance HasCodec OrderBy where
codec =
object " OrderBy " $
OrderBy
2022-08-19 10:00:46 +03:00
<$> requiredField " relations " " A map of relationships from the current query table to target tables. The key of the map is the relationship name. The relationships are used within the order by elements. " .= _obRelations
<*> requiredField " elements " " The elements to order by, in priority order " .= _obElements
2022-03-16 07:12:15 +03:00
2022-08-19 10:00:46 +03:00
data OrderByRelation = OrderByRelation
{ _obrWhere :: Maybe API . V0 . Expression ,
_obrSubrelations :: HashMap API . V0 . RelationshipName OrderByRelation
}
deriving stock ( Data , Eq , Generic , Ord , Show )
deriving anyclass ( Hashable , NFData )
deriving ( FromJSON , ToJSON , ToSchema ) via Autodocodec OrderByRelation
2022-03-16 07:12:15 +03:00
2022-08-19 10:00:46 +03:00
instance HasCodec OrderByRelation where
codec =
named " OrderByRelation " $
object " OrderByRelation " $
OrderByRelation
<$> optionalFieldOrNull " where " " An expression to apply to the relationship's target table to filter it " .= _obrWhere
<*> requiredField " subrelations " " Further relationships to follow from the relationship's target table. The key of the map is the relationship name. " .= _obrSubrelations
data OrderByElement = OrderByElement
{ _obeTargetPath :: [ API . V0 . RelationshipName ] ,
_obeTarget :: OrderByTarget ,
_obeOrderDirection :: OrderDirection
}
deriving stock ( Data , Eq , Generic , Ord , Show )
deriving ( FromJSON , ToJSON , ToSchema ) via Autodocodec OrderByElement
instance HasCodec OrderByElement where
codec =
object " OrderByElement " $
OrderByElement
<$> requiredField " target_path " " The relationship path from the current query table to the table that contains the target to order by. This is always non-empty for aggregate order by targets " .= _obeTargetPath
<*> requiredField " target " " The target column or aggregate to order by " .= _obeTarget
<*> requiredField " order_direction " " The direction of ordering to apply " .= _obeOrderDirection
data OrderByTarget
= OrderByColumn API . V0 . ColumnName
| OrderByStarCountAggregate
| OrderBySingleColumnAggregate API . V0 . SingleColumnAggregate
deriving stock ( Data , Eq , Generic , Ord , Show )
deriving ( FromJSON , ToJSON , ToSchema ) via Autodocodec OrderByTarget
instance HasCodec OrderByTarget where
codec =
object " OrderByTarget " $
discriminatedUnionCodec " type " enc dec
where
columnCodec = requiredField' " column "
starAggregateCodec = pureCodec ()
singleColumnAggregateCodec = API . V0 . singleColumnAggregateObjectCodec
enc = \ case
OrderByColumn c -> ( " column " , mapToEncoder c columnCodec )
OrderByStarCountAggregate -> ( " star_count_aggregate " , mapToEncoder () starAggregateCodec )
OrderBySingleColumnAggregate agg -> ( " single_column_aggregate " , mapToEncoder agg singleColumnAggregateCodec )
dec =
HashMap . fromList
[ ( " column " , ( " OrderByColumn " , mapToDecoder OrderByColumn columnCodec ) ) ,
( " star_count_aggregate " , ( " OrderByStarCountAggregate " , mapToDecoder ( const OrderByStarCountAggregate ) starAggregateCodec ) ) ,
( " single_column_aggregate " , ( " OrderBySingleColumnAggregate " , mapToDecoder OrderBySingleColumnAggregate singleColumnAggregateCodec ) )
]
data OrderDirection
2022-03-16 07:12:15 +03:00
= Ascending
| Descending
2022-03-31 07:45:03 +03:00
deriving stock ( Data , Eq , Generic , Ord , Show , Enum , Bounded )
2022-08-19 10:00:46 +03:00
deriving ( FromJSON , ToJSON , ToSchema ) via Autodocodec OrderDirection
2022-03-16 07:12:15 +03:00
2022-08-19 10:00:46 +03:00
instance HasCodec OrderDirection where
2022-03-31 07:45:03 +03:00
codec =
2022-08-19 10:00:46 +03:00
named " OrderDirection " $
2022-07-27 08:27:34 +03:00
stringConstCodec [ ( Ascending , " asc " ) , ( Descending , " desc " ) ]