graphql-engine/server/src-lib/Hasura/RQL/IR/OrderBy.hs
Antoine Leblanc a978b6d683 Remove several outdated backend constraints
## Description

This PR removes as many constraints as possible from Backend without refactoring the code. Thanks to #1844, a few ToJSON functions can be removed from the IR, and several constraints were simply redundant.

This PR borrows from similar work done as part of #1666.

## Note

To remove constraints more aggressively, I have explored the possibility of _removing Representable altogether_, in a [separate commit](https://github.com/hasura/graphql-engine-mono/compare/nicuveo/remove-extension-constraints..nicuveo/tentative-remove-representable). I am not convinced it's a good idea in terms of readability of the code, but it's a possibility.

Further work includes deciding what we want to do with `Show` and `ToTxt`; see #1747.

https://github.com/hasura/graphql-engine-mono/pull/1843

GitOrigin-RevId: 337324ad90cb8f86f06e1c5a36aa44bb414e195a
2021-07-27 13:52:20 +00:00

66 lines
1.6 KiB
Haskell

module Hasura.RQL.IR.OrderBy
( OrderByCol(..)
, OrderByItemG(..)
, OrderByItem
-- used by RQL.DML.Types
, orderByColFromTxt
) where
import Hasura.Prelude
import qualified Data.Text as T
import Data.Aeson
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.Common
import Hasura.SQL.Backend
-- order by col
data OrderByCol
= OCPG !FieldName
| OCRel !FieldName !OrderByCol
deriving (Show, Eq)
instance FromJSON OrderByCol where
parseJSON = \case
(String t) -> orderByColFromToks $ T.split (=='.') t
v -> parseJSON v >>= orderByColFromToks
orderByColFromToks
:: (MonadFail m)
=> [Text] -> m OrderByCol
orderByColFromToks toks = do
when (any T.null toks) $ fail "col/rel cannot be empty"
case toks of
[] -> fail "failed to parse an OrderByCol: found empty cols"
x:xs -> return $ go (FieldName x) xs
where
go fld = \case
[] -> OCPG fld
x:xs -> OCRel fld $ go (FieldName x) xs
orderByColFromTxt
:: (MonadFail m)
=> Text -> m OrderByCol
orderByColFromTxt =
orderByColFromToks . T.split (=='.')
-- order by item
data OrderByItemG (b :: BackendType) a
= OrderByItemG
{ obiType :: !(Maybe (BasicOrderType b))
, obiColumn :: !a
, obiNulls :: !(Maybe (NullsOrderType b))
} deriving (Functor, Foldable, Traversable, Generic)
deriving instance (Backend b, Show a) => Show (OrderByItemG b a)
deriving instance (Backend b, Eq a) => Eq (OrderByItemG b a)
instance (Backend b, Hashable a) => Hashable (OrderByItemG b a)
type OrderByItem b = OrderByItemG b OrderByCol