2021-12-22 03:10:28 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
|
2022-05-02 08:03:12 +03:00
|
|
|
module Hasura.Backends.DataConnector.IR.OrderBy
|
2021-12-22 03:10:28 +03:00
|
|
|
( OrderBy (..),
|
|
|
|
OrderType (..),
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-04-08 09:48:37 +03:00
|
|
|
import Data.Aeson (ToJSON)
|
|
|
|
import Data.Aeson qualified as J
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.API qualified as API
|
|
|
|
import Hasura.Backends.DataConnector.IR.Column qualified as IR.C
|
2021-12-22 03:10:28 +03:00
|
|
|
import Hasura.Incremental (Cacheable)
|
|
|
|
import Hasura.Prelude
|
2022-04-28 04:51:58 +03:00
|
|
|
import Witch qualified
|
2021-12-22 03:10:28 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Indicates a particular sort order that should be applied based on some
|
|
|
|
-- 'Column.Name' returned within a data source query.
|
|
|
|
--
|
|
|
|
-- TODO: We should use a sum type like @Query.Field@ here so that we can handle
|
|
|
|
-- @order by@ constraints on object/array relationships as well.
|
|
|
|
--
|
|
|
|
-- cf. https://www.postgresql.org/docs/13/queries-order.html
|
2022-04-08 09:48:37 +03:00
|
|
|
--
|
|
|
|
-- NOTE: The 'ToJSON' instance is only intended for logging purposes.
|
2021-12-22 03:10:28 +03:00
|
|
|
data OrderBy = OrderBy
|
2022-04-28 04:51:58 +03:00
|
|
|
{ column :: IR.C.Name,
|
2021-12-22 03:10:28 +03:00
|
|
|
ordering :: OrderType
|
|
|
|
}
|
|
|
|
deriving stock (Data, Eq, Generic, Ord, Show)
|
|
|
|
deriving anyclass (Cacheable, Hashable, NFData)
|
|
|
|
|
2022-04-08 09:48:37 +03:00
|
|
|
instance ToJSON OrderBy where
|
|
|
|
toJSON = J.genericToJSON J.defaultOptions
|
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
instance Witch.From API.OrderBy OrderBy where
|
2022-03-16 07:12:15 +03:00
|
|
|
from API.OrderBy {column, ordering} =
|
2022-04-28 04:51:58 +03:00
|
|
|
OrderBy (Witch.from column) (Witch.from ordering)
|
2022-03-16 07:12:15 +03:00
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
instance Witch.From OrderBy API.OrderBy where
|
2022-04-08 09:48:37 +03:00
|
|
|
from OrderBy {column, ordering} =
|
2022-04-28 04:51:58 +03:00
|
|
|
API.OrderBy (Witch.from column) (Witch.from ordering)
|
2022-04-08 09:48:37 +03:00
|
|
|
|
2021-12-22 03:10:28 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | 'Column.Name's may be sorted in either ascending or descending order.
|
|
|
|
--
|
|
|
|
-- cf. https://www.postgresql.org/docs/13/queries-order.html
|
2022-04-08 09:48:37 +03:00
|
|
|
--
|
|
|
|
-- NOTE: The 'ToJSON' instance is only intended for logging purposes.
|
2021-12-22 03:10:28 +03:00
|
|
|
data OrderType
|
|
|
|
= Ascending
|
|
|
|
| Descending
|
|
|
|
deriving stock (Data, Eq, Generic, Ord, Show)
|
|
|
|
deriving anyclass (Cacheable, Hashable, NFData)
|
2022-03-16 07:12:15 +03:00
|
|
|
|
2022-04-08 09:48:37 +03:00
|
|
|
instance ToJSON OrderType where
|
|
|
|
toJSON = J.genericToJSON J.defaultOptions
|
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
instance Witch.From API.OrderType OrderType where
|
2022-03-16 07:12:15 +03:00
|
|
|
from API.Ascending = Ascending
|
|
|
|
from API.Descending = Ascending
|
2022-04-08 09:48:37 +03:00
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
instance Witch.From OrderType API.OrderType where
|
2022-04-08 09:48:37 +03:00
|
|
|
from Ascending = API.Ascending
|
|
|
|
from Descending = API.Ascending
|