2021-12-22 03:10:28 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
|
2022-02-25 19:08:18 +03:00
|
|
|
module Hasura.Backends.DataWrapper.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-03-16 07:12:15 +03:00
|
|
|
import Hasura.Backends.DataWrapper.API qualified as API
|
2022-02-25 19:08:18 +03:00
|
|
|
import Hasura.Backends.DataWrapper.IR.Column qualified as Column (Name)
|
2021-12-22 03:10:28 +03:00
|
|
|
import Hasura.Incremental (Cacheable)
|
|
|
|
import Hasura.Prelude
|
2022-03-16 07:12:15 +03:00
|
|
|
import Witch
|
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
|
|
|
|
{ column :: Column.Name,
|
|
|
|
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-03-16 07:12:15 +03:00
|
|
|
instance From API.OrderBy OrderBy where
|
|
|
|
from API.OrderBy {column, ordering} =
|
|
|
|
OrderBy (from column) (from ordering)
|
|
|
|
|
2022-04-08 09:48:37 +03:00
|
|
|
instance From OrderBy API.OrderBy where
|
|
|
|
from OrderBy {column, ordering} =
|
|
|
|
API.OrderBy (from column) (from ordering)
|
|
|
|
|
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-03-16 07:12:15 +03:00
|
|
|
instance From API.OrderType OrderType where
|
|
|
|
from API.Ascending = Ascending
|
|
|
|
from API.Descending = Ascending
|
2022-04-08 09:48:37 +03:00
|
|
|
|
|
|
|
instance From OrderType API.OrderType where
|
|
|
|
from Ascending = API.Ascending
|
|
|
|
from Descending = API.Ascending
|