{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE TemplateHaskellQuotes #-}

module Hasura.GraphQL.Schema.OrderBy
  ( orderByExp,
  )
where

import Data.Has
import Data.Text.Casing qualified as C
import Data.Text.Extended
import Hasura.GraphQL.Parser.Class
import Hasura.GraphQL.Schema.Backend
import Hasura.GraphQL.Schema.Common
import Hasura.GraphQL.Schema.NamingCase
import Hasura.GraphQL.Schema.Parser
  ( InputFieldsParser,
    Kind (..),
    Parser,
  )
import Hasura.GraphQL.Schema.Parser qualified as P
import Hasura.GraphQL.Schema.Table
import Hasura.GraphQL.Schema.Typename
import Hasura.Name qualified as Name
import Hasura.Prelude
import Hasura.RQL.IR.OrderBy qualified as IR
import Hasura.RQL.IR.Select qualified as IR
import Hasura.RQL.IR.Value qualified as IR
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.ComputedField
import Hasura.RQL.Types.Function
import Hasura.RQL.Types.Relationships.Local
import Hasura.RQL.Types.SchemaCache hiding (askTableInfo)
import Hasura.RQL.Types.Source
import Hasura.RQL.Types.SourceCustomization
import Hasura.RQL.Types.Table
import Language.GraphQL.Draft.Syntax qualified as G

{-# INLINE orderByOperator #-}
orderByOperator ::
  forall b n.
  (BackendSchema b, MonadParse n) =>
  NamingCase ->
  SourceInfo b ->
  Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperator :: NamingCase
-> SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperator NamingCase
tCase SourceInfo b
sourceInfo = case NamingCase
tCase of
  NamingCase
HasuraCase -> SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
forall (b :: BackendType) (n :: * -> *).
(BackendSchema b, MonadParse n) =>
SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperatorsHasuraCase @b SourceInfo b
sourceInfo
  NamingCase
GraphqlCase -> SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
forall (b :: BackendType) (n :: * -> *).
(BackendSchema b, MonadParse n) =>
SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperatorsGraphqlCase @b SourceInfo b
sourceInfo

-- | Corresponds to an object type for an order by.
--
-- > input table_order_by {
-- >   col1: order_by
-- >   col2: order_by
-- >   .     .
-- >   .     .
-- >   coln: order_by
-- >   obj-rel: <remote-table>_order_by
-- > }
orderByExp ::
  forall b r m n.
  MonadBuildSchema b r m n =>
  SourceInfo b ->
  TableInfo b ->
  m (Parser 'Input n [IR.AnnotatedOrderByItemG b (IR.UnpreparedValue b)])
orderByExp :: SourceInfo b
-> TableInfo b
-> m (Parser
        'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
orderByExp SourceInfo b
sourceInfo TableInfo b
tableInfo = Name
-> (SourceName, TableName b)
-> m (Parser
        'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> m (Parser
        'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
forall (m :: * -> *) a (p :: (* -> *) -> * -> *) (n :: * -> *) b.
(MonadMemoize m, Ord a, Typeable a, Typeable p, MonadParse n,
 Typeable b) =>
Name -> a -> m (p n b) -> m (p n b)
P.memoizeOn 'orderByExp (SourceInfo b -> SourceName
forall (b :: BackendType). SourceInfo b -> SourceName
_siName SourceInfo b
sourceInfo, TableInfo b -> TableName b
forall (b :: BackendType). TableInfo b -> TableName b
tableInfoName TableInfo b
tableInfo) (m (Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
 -> m (Parser
         'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)]))
-> m (Parser
        'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> m (Parser
        'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
forall a b. (a -> b) -> a -> b
$ do
  NamingCase
tCase <- (r -> NamingCase) -> m NamingCase
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks r -> NamingCase
forall a t. Has a t => t -> a
getter
  GQLNameIdentifier
tableGQLName <- TableInfo b -> m GQLNameIdentifier
forall (b :: BackendType) (m :: * -> *).
(Backend b, MonadError QErr m) =>
TableInfo b -> m GQLNameIdentifier
getTableIdentifierName TableInfo b
tableInfo
  Name
name <- Name -> m Name
forall r (m :: * -> *).
(MonadReader r m, Has MkTypename r) =>
Name -> m Name
mkTypename (Name -> m Name) -> Name -> m Name
forall a b. (a -> b) -> a -> b
$ NamingCase -> GQLNameIdentifier -> Name
applyTypeNameCaseIdentifier NamingCase
tCase (GQLNameIdentifier -> Name) -> GQLNameIdentifier -> Name
forall a b. (a -> b) -> a -> b
$ GQLNameIdentifier -> GQLNameIdentifier
mkTableOrderByTypeName GQLNameIdentifier
tableGQLName
  let description :: Description
description =
        Text -> Description
G.Description (Text -> Description) -> Text -> Description
forall a b. (a -> b) -> a -> b
$
          Text
"Ordering options when selecting data from " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TableInfo b -> TableName b
forall (b :: BackendType). TableInfo b -> TableName b
tableInfoName TableInfo b
tableInfo TableName b -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
"."
  [FieldInfo b]
tableFields <- SourceInfo b -> TableInfo b -> m [FieldInfo b]
forall (b :: BackendType) r (m :: * -> *).
(Backend b, MonadError QErr m, MonadReader r m,
 Has SchemaContext r) =>
SourceInfo b -> TableInfo b -> m [FieldInfo b]
tableSelectFields SourceInfo b
sourceInfo TableInfo b
tableInfo
  InputFieldsParser
  MetadataObjId
  n
  [Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]]
fieldParsers <- [InputFieldsParser
   MetadataObjId
   n
   (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])]
-> InputFieldsParser
     MetadataObjId
     n
     [Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA ([InputFieldsParser
    MetadataObjId
    n
    (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])]
 -> InputFieldsParser
      MetadataObjId
      n
      [Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]])
-> ([Maybe
       (InputFieldsParser
          MetadataObjId
          n
          (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))]
    -> [InputFieldsParser
          MetadataObjId
          n
          (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])])
-> [Maybe
      (InputFieldsParser
         MetadataObjId
         n
         (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))]
-> InputFieldsParser
     MetadataObjId
     n
     [Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe
   (InputFieldsParser
      MetadataObjId
      n
      (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))]
-> [InputFieldsParser
      MetadataObjId
      n
      (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes ([Maybe
    (InputFieldsParser
       MetadataObjId
       n
       (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))]
 -> InputFieldsParser
      MetadataObjId
      n
      [Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]])
-> m [Maybe
        (InputFieldsParser
           MetadataObjId
           n
           (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))]
-> m (InputFieldsParser
        MetadataObjId
        n
        [Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (FieldInfo b
 -> m (Maybe
         (InputFieldsParser
            MetadataObjId
            n
            (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))))
-> [FieldInfo b]
-> m [Maybe
        (InputFieldsParser
           MetadataObjId
           n
           (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (NamingCase
-> FieldInfo b
-> m (Maybe
        (InputFieldsParser
           MetadataObjId
           n
           (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])))
mkField NamingCase
tCase) [FieldInfo b]
tableFields
  pure $ [[AnnotatedOrderByItemG b (UnpreparedValue b)]]
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[AnnotatedOrderByItemG b (UnpreparedValue b)]]
 -> [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> ([Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]]
    -> [[AnnotatedOrderByItemG b (UnpreparedValue b)]])
-> [Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]]
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]]
-> [[AnnotatedOrderByItemG b (UnpreparedValue b)]]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes ([Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]]
 -> [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> Parser
     MetadataObjId
     'Input
     n
     [Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]]
-> Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name
-> Maybe Description
-> InputFieldsParser
     MetadataObjId
     n
     [Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]]
-> Parser
     MetadataObjId
     'Input
     n
     [Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]]
forall (m :: * -> *) origin a.
MonadParse m =>
Name
-> Maybe Description
-> InputFieldsParser origin m a
-> Parser origin 'Input m a
P.object Name
name (Description -> Maybe Description
forall a. a -> Maybe a
Just Description
description) InputFieldsParser
  MetadataObjId
  n
  [Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]]
fieldParsers
  where
    mkField ::
      NamingCase ->
      FieldInfo b ->
      m (Maybe (InputFieldsParser n (Maybe [IR.AnnotatedOrderByItemG b (IR.UnpreparedValue b)])))
    mkField :: NamingCase
-> FieldInfo b
-> m (Maybe
        (InputFieldsParser
           MetadataObjId
           n
           (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])))
mkField NamingCase
tCase FieldInfo b
fieldInfo = MaybeT
  m
  (InputFieldsParser
     MetadataObjId
     n
     (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))
-> m (Maybe
        (InputFieldsParser
           MetadataObjId
           n
           (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])))
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT
   m
   (InputFieldsParser
      MetadataObjId
      n
      (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))
 -> m (Maybe
         (InputFieldsParser
            MetadataObjId
            n
            (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))))
-> MaybeT
     m
     (InputFieldsParser
        MetadataObjId
        n
        (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))
-> m (Maybe
        (InputFieldsParser
           MetadataObjId
           n
           (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])))
forall a b. (a -> b) -> a -> b
$ do
      RoleName
roleName <- (SchemaContext -> RoleName) -> MaybeT m RoleName
forall r (m :: * -> *) a b.
(MonadReader r m, Has a r) =>
(a -> b) -> m b
retrieve SchemaContext -> RoleName
scRole
      case FieldInfo b
fieldInfo of
        FIColumn ColumnInfo b
columnInfo -> do
          let fieldName :: Name
fieldName = ColumnInfo b -> Name
forall (b :: BackendType). ColumnInfo b -> Name
ciName ColumnInfo b
columnInfo
          InputFieldsParser
  MetadataObjId
  n
  (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> MaybeT
     m
     (InputFieldsParser
        MetadataObjId
        n
        (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputFieldsParser
   MetadataObjId
   n
   (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
 -> MaybeT
      m
      (InputFieldsParser
         MetadataObjId
         n
         (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> MaybeT
     m
     (InputFieldsParser
        MetadataObjId
        n
        (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))
forall a b. (a -> b) -> a -> b
$
            Name
-> Maybe Description
-> Parser
     MetadataObjId 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe (BasicOrderType b, NullsOrderType b)))
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional
              Name
fieldName
              Maybe Description
forall a. Maybe a
Nothing
              (NamingCase
-> SourceInfo b
-> Parser
     MetadataObjId 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
forall (b :: BackendType) (n :: * -> *).
(BackendSchema b, MonadParse n) =>
NamingCase
-> SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperator @b NamingCase
tCase SourceInfo b
sourceInfo)
              InputFieldsParser
  MetadataObjId
  n
  (Maybe (Maybe (BasicOrderType b, NullsOrderType b)))
-> (Maybe (Maybe (BasicOrderType b, NullsOrderType b))
    -> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> ((BasicOrderType b, NullsOrderType b)
 -> [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> Maybe (BasicOrderType b, NullsOrderType b)
-> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (AnnotatedOrderByItemG b (UnpreparedValue b)
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AnnotatedOrderByItemG b (UnpreparedValue b)
 -> [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> ((BasicOrderType b, NullsOrderType b)
    -> AnnotatedOrderByItemG b (UnpreparedValue b))
-> (BasicOrderType b, NullsOrderType b)
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnnotatedOrderByElement b (UnpreparedValue b)
-> (BasicOrderType b, NullsOrderType b)
-> AnnotatedOrderByItemG b (UnpreparedValue b)
forall (b :: BackendType) a.
a -> (BasicOrderType b, NullsOrderType b) -> OrderByItemG b a
mkOrderByItemG @b (ColumnInfo b -> AnnotatedOrderByElement b (UnpreparedValue b)
forall (b :: BackendType) v.
ColumnInfo b -> AnnotatedOrderByElement b v
IR.AOCColumn ColumnInfo b
columnInfo)) (Maybe (BasicOrderType b, NullsOrderType b)
 -> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> (Maybe (Maybe (BasicOrderType b, NullsOrderType b))
    -> Maybe (BasicOrderType b, NullsOrderType b))
-> Maybe (Maybe (BasicOrderType b, NullsOrderType b))
-> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Maybe (BasicOrderType b, NullsOrderType b))
-> Maybe (BasicOrderType b, NullsOrderType b)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join
        FIRelationship RelInfo b
relationshipInfo -> do
          TableInfo b
remoteTableInfo <- SourceInfo b -> TableName b -> MaybeT m (TableInfo b)
forall (b :: BackendType) (m :: * -> *).
(Backend b, MonadError QErr m) =>
SourceInfo b -> TableName b -> m (TableInfo b)
askTableInfo SourceInfo b
sourceInfo (TableName b -> MaybeT m (TableInfo b))
-> TableName b -> MaybeT m (TableInfo b)
forall a b. (a -> b) -> a -> b
$ RelInfo b -> TableName b
forall (b :: BackendType). RelInfo b -> TableName b
riRTable RelInfo b
relationshipInfo
          SelPermInfo b
perms <- Maybe (SelPermInfo b) -> MaybeT m (SelPermInfo b)
forall (m :: * -> *) b. Applicative m => Maybe b -> MaybeT m b
hoistMaybe (Maybe (SelPermInfo b) -> MaybeT m (SelPermInfo b))
-> Maybe (SelPermInfo b) -> MaybeT m (SelPermInfo b)
forall a b. (a -> b) -> a -> b
$ RoleName -> TableInfo b -> Maybe (SelPermInfo b)
forall (b :: BackendType).
RoleName -> TableInfo b -> Maybe (SelPermInfo b)
tableSelectPermissions RoleName
roleName TableInfo b
remoteTableInfo
          Name
fieldName <- Maybe Name -> MaybeT m Name
forall (m :: * -> *) b. Applicative m => Maybe b -> MaybeT m b
hoistMaybe (Maybe Name -> MaybeT m Name) -> Maybe Name -> MaybeT m Name
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Name
G.mkName (Text -> Maybe Name) -> Text -> Maybe Name
forall a b. (a -> b) -> a -> b
$ RelName -> Text
relNameToTxt (RelName -> Text) -> RelName -> Text
forall a b. (a -> b) -> a -> b
$ RelInfo b -> RelName
forall (b :: BackendType). RelInfo b -> RelName
riName RelInfo b
relationshipInfo
          let newPerms :: GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
newPerms = (PartialSQLExp b -> UnpreparedValue b)
-> AnnBoolExpFld b (PartialSQLExp b)
-> AnnBoolExpFld b (UnpreparedValue b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PartialSQLExp b -> UnpreparedValue b
forall (b :: BackendType). PartialSQLExp b -> UnpreparedValue b
partialSQLExpToUnpreparedValue (AnnBoolExpFld b (PartialSQLExp b)
 -> AnnBoolExpFld b (UnpreparedValue b))
-> GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
-> GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SelPermInfo b -> GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
forall (b :: BackendType). SelPermInfo b -> AnnBoolExpPartialSQL b
spiFilter SelPermInfo b
perms
          case RelInfo b -> RelType
forall (b :: BackendType). RelInfo b -> RelType
riType RelInfo b
relationshipInfo of
            RelType
ObjRel -> do
              Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)]
otherTableParser <- m (Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> MaybeT
     m (Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
 -> MaybeT
      m (Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)]))
-> m (Parser
        'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> MaybeT
     m (Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
forall a b. (a -> b) -> a -> b
$ SourceInfo b
-> TableInfo b
-> m (Parser
        'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
SourceInfo b
-> TableInfo b
-> m (Parser
        'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
orderByExp SourceInfo b
sourceInfo TableInfo b
remoteTableInfo
              pure $ do
                Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]
otherTableOrderBy <- Maybe (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
 -> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name
-> Maybe Description
-> Parser
     MetadataObjId
     'Input
     n
     (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional Name
fieldName Maybe Description
forall a. Maybe a
Nothing (Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)]
-> Parser
     MetadataObjId
     'Input
     n
     (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
forall origin (k :: Kind) (m :: * -> *) a.
(MonadParse m, 'Input <: k) =>
Parser origin k m a -> Parser origin k m (Maybe a)
P.nullable Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)]
otherTableParser)
                pure $ ([AnnotatedOrderByItemG b (UnpreparedValue b)]
 -> [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]
-> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((AnnotatedOrderByItemG b (UnpreparedValue b)
 -> AnnotatedOrderByItemG b (UnpreparedValue b))
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall a b. (a -> b) -> [a] -> [b]
map ((AnnotatedOrderByItemG b (UnpreparedValue b)
  -> AnnotatedOrderByItemG b (UnpreparedValue b))
 -> [AnnotatedOrderByItemG b (UnpreparedValue b)]
 -> [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> (AnnotatedOrderByItemG b (UnpreparedValue b)
    -> AnnotatedOrderByItemG b (UnpreparedValue b))
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall a b. (a -> b) -> a -> b
$ (AnnotatedOrderByElement b (UnpreparedValue b)
 -> AnnotatedOrderByElement b (UnpreparedValue b))
-> AnnotatedOrderByItemG b (UnpreparedValue b)
-> AnnotatedOrderByItemG b (UnpreparedValue b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((AnnotatedOrderByElement b (UnpreparedValue b)
  -> AnnotatedOrderByElement b (UnpreparedValue b))
 -> AnnotatedOrderByItemG b (UnpreparedValue b)
 -> AnnotatedOrderByItemG b (UnpreparedValue b))
-> (AnnotatedOrderByElement b (UnpreparedValue b)
    -> AnnotatedOrderByElement b (UnpreparedValue b))
-> AnnotatedOrderByItemG b (UnpreparedValue b)
-> AnnotatedOrderByItemG b (UnpreparedValue b)
forall a b. (a -> b) -> a -> b
$ RelInfo b
-> GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
-> AnnotatedOrderByElement b (UnpreparedValue b)
-> AnnotatedOrderByElement b (UnpreparedValue b)
forall (b :: BackendType) v.
RelInfo b
-> AnnBoolExp b v
-> AnnotatedOrderByElement b v
-> AnnotatedOrderByElement b v
IR.AOCObjectRelation RelInfo b
relationshipInfo GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
newPerms) Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]
otherTableOrderBy
            RelType
ArrRel -> do
              let aggregateFieldName :: Name
aggregateFieldName = NamingCase -> GQLNameIdentifier -> Name
applyFieldNameCaseIdentifier NamingCase
tCase (GQLNameIdentifier -> Name) -> GQLNameIdentifier -> Name
forall a b. (a -> b) -> a -> b
$ (Name, [NameSuffix]) -> GQLNameIdentifier
C.fromAutogeneratedTuple (Name
fieldName, [Name -> NameSuffix
G.convertNameToSuffix Name
Name._aggregate])
              Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
aggregationParser <- m (Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> MaybeT
     m (Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
 -> MaybeT
      m (Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)]))
-> m (Parser
        'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> MaybeT
     m (Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall a b. (a -> b) -> a -> b
$ SourceInfo b
-> TableInfo b
-> m (Parser
        'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
SourceInfo b
-> TableInfo b
-> m (Parser
        'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
orderByAggregation SourceInfo b
sourceInfo TableInfo b
remoteTableInfo
              pure $ do
                Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]
aggregationOrderBy <- Maybe (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
 -> Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name
-> Maybe Description
-> Parser
     MetadataObjId
     'Input
     n
     (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]))
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional Name
aggregateFieldName Maybe Description
forall a. Maybe a
Nothing (Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
-> Parser
     MetadataObjId
     'Input
     n
     (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall origin (k :: Kind) (m :: * -> *) a.
(MonadParse m, 'Input <: k) =>
Parser origin k m a -> Parser origin k m (Maybe a)
P.nullable Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
aggregationParser)
                pure $ ([OrderByItemG b (AnnotatedAggregateOrderBy b)]
 -> [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]
-> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((OrderByItemG b (AnnotatedAggregateOrderBy b)
 -> AnnotatedOrderByItemG b (UnpreparedValue b))
-> [OrderByItemG b (AnnotatedAggregateOrderBy b)]
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall a b. (a -> b) -> [a] -> [b]
map ((OrderByItemG b (AnnotatedAggregateOrderBy b)
  -> AnnotatedOrderByItemG b (UnpreparedValue b))
 -> [OrderByItemG b (AnnotatedAggregateOrderBy b)]
 -> [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> (OrderByItemG b (AnnotatedAggregateOrderBy b)
    -> AnnotatedOrderByItemG b (UnpreparedValue b))
-> [OrderByItemG b (AnnotatedAggregateOrderBy b)]
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall a b. (a -> b) -> a -> b
$ (AnnotatedAggregateOrderBy b
 -> AnnotatedOrderByElement b (UnpreparedValue b))
-> OrderByItemG b (AnnotatedAggregateOrderBy b)
-> AnnotatedOrderByItemG b (UnpreparedValue b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((AnnotatedAggregateOrderBy b
  -> AnnotatedOrderByElement b (UnpreparedValue b))
 -> OrderByItemG b (AnnotatedAggregateOrderBy b)
 -> AnnotatedOrderByItemG b (UnpreparedValue b))
-> (AnnotatedAggregateOrderBy b
    -> AnnotatedOrderByElement b (UnpreparedValue b))
-> OrderByItemG b (AnnotatedAggregateOrderBy b)
-> AnnotatedOrderByItemG b (UnpreparedValue b)
forall a b. (a -> b) -> a -> b
$ RelInfo b
-> GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
-> AnnotatedAggregateOrderBy b
-> AnnotatedOrderByElement b (UnpreparedValue b)
forall (b :: BackendType) v.
RelInfo b
-> AnnBoolExp b v
-> AnnotatedAggregateOrderBy b
-> AnnotatedOrderByElement b v
IR.AOCArrayAggregation RelInfo b
relationshipInfo GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
newPerms) Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]
aggregationOrderBy
        FIComputedField ComputedFieldInfo {Maybe Text
ComputedFieldReturn b
XComputedField b
ComputedFieldFunction b
ComputedFieldName
_cfiDescription :: forall (b :: BackendType). ComputedFieldInfo b -> Maybe Text
_cfiReturnType :: forall (b :: BackendType).
ComputedFieldInfo b -> ComputedFieldReturn b
_cfiFunction :: forall (b :: BackendType).
ComputedFieldInfo b -> ComputedFieldFunction b
_cfiName :: forall (b :: BackendType). ComputedFieldInfo b -> ComputedFieldName
_cfiXComputedFieldInfo :: forall (b :: BackendType). ComputedFieldInfo b -> XComputedField b
_cfiDescription :: Maybe Text
_cfiReturnType :: ComputedFieldReturn b
_cfiFunction :: ComputedFieldFunction b
_cfiName :: ComputedFieldName
_cfiXComputedFieldInfo :: XComputedField b
..} -> do
          let ComputedFieldFunction {Maybe PGDescription
Seq (FunctionArgument b)
FunctionName b
ComputedFieldImplicitArguments b
_cffDescription :: forall (b :: BackendType).
ComputedFieldFunction b -> Maybe PGDescription
_cffComputedFieldImplicitArgs :: forall (b :: BackendType).
ComputedFieldFunction b -> ComputedFieldImplicitArguments b
_cffInputArgs :: forall (b :: BackendType).
ComputedFieldFunction b -> Seq (FunctionArgument b)
_cffName :: forall (b :: BackendType).
ComputedFieldFunction b -> FunctionName b
_cffDescription :: Maybe PGDescription
_cffComputedFieldImplicitArgs :: ComputedFieldImplicitArguments b
_cffInputArgs :: Seq (FunctionArgument b)
_cffName :: FunctionName b
..} = ComputedFieldFunction b
_cfiFunction
              mkComputedFieldOrderBy :: ComputedFieldOrderByElement b (UnpreparedValue b)
-> ComputedFieldOrderBy b (UnpreparedValue b)
mkComputedFieldOrderBy =
                let functionArgs :: FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b))
functionArgs =
                      ([FunctionArgumentExp b (UnpreparedValue b)]
 -> HashMap Text (FunctionArgumentExp b (UnpreparedValue b))
 -> FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b)))
-> HashMap Text (FunctionArgumentExp b (UnpreparedValue b))
-> [FunctionArgumentExp b (UnpreparedValue b)]
-> FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b))
forall a b c. (a -> b -> c) -> b -> a -> c
flip [FunctionArgumentExp b (UnpreparedValue b)]
-> HashMap Text (FunctionArgumentExp b (UnpreparedValue b))
-> FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b))
forall a. [a] -> HashMap Text a -> FunctionArgsExpG a
FunctionArgsExp HashMap Text (FunctionArgumentExp b (UnpreparedValue b))
forall a. Monoid a => a
mempty ([FunctionArgumentExp b (UnpreparedValue b)]
 -> FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b)))
-> [FunctionArgumentExp b (UnpreparedValue b)]
-> FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b))
forall a b. (a -> b) -> a -> b
$
                        UnpreparedValue b
-> ComputedFieldImplicitArguments b
-> [FunctionArgumentExp b (UnpreparedValue b)]
forall (b :: BackendType) v.
Backend b =>
v -> ComputedFieldImplicitArguments b -> [FunctionArgumentExp b v]
fromComputedFieldImplicitArguments @b UnpreparedValue b
forall (b :: BackendType). UnpreparedValue b
IR.UVSession ComputedFieldImplicitArguments b
_cffComputedFieldImplicitArgs
                 in XComputedField b
-> ComputedFieldName
-> FunctionName b
-> FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b))
-> ComputedFieldOrderByElement b (UnpreparedValue b)
-> ComputedFieldOrderBy b (UnpreparedValue b)
forall (b :: BackendType) v.
XComputedField b
-> ComputedFieldName
-> FunctionName b
-> FunctionArgsExp b v
-> ComputedFieldOrderByElement b v
-> ComputedFieldOrderBy b v
IR.ComputedFieldOrderBy XComputedField b
_cfiXComputedFieldInfo ComputedFieldName
_cfiName FunctionName b
_cffName FunctionArgsExpG (FunctionArgumentExp b (UnpreparedValue b))
functionArgs
          Name
fieldName <- Maybe Name -> MaybeT m Name
forall (m :: * -> *) b. Applicative m => Maybe b -> MaybeT m b
hoistMaybe (Maybe Name -> MaybeT m Name) -> Maybe Name -> MaybeT m Name
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Name
G.mkName (Text -> Maybe Name) -> Text -> Maybe Name
forall a b. (a -> b) -> a -> b
$ ComputedFieldName -> Text
forall a. ToTxt a => a -> Text
toTxt ComputedFieldName
_cfiName
          Bool -> MaybeT m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> MaybeT m ()) -> Bool -> MaybeT m ()
forall a b. (a -> b) -> a -> b
$ Seq (FunctionArgument b)
_cffInputArgs Seq (FunctionArgument b) -> Seq (FunctionArgument b) -> Bool
forall a. Eq a => a -> a -> Bool
== Seq (FunctionArgument b)
forall a. Monoid a => a
mempty -- No input arguments other than table row and session argument
          case ComputedFieldReturn b -> ComputedFieldReturnType b
forall (b :: BackendType).
Backend b =>
ComputedFieldReturn b -> ComputedFieldReturnType b
computedFieldReturnType @b ComputedFieldReturn b
_cfiReturnType of
            ReturnsScalar ScalarType b
scalarType -> do
              let computedFieldOrderBy :: ComputedFieldOrderBy b (UnpreparedValue b)
computedFieldOrderBy = ComputedFieldOrderByElement b (UnpreparedValue b)
-> ComputedFieldOrderBy b (UnpreparedValue b)
mkComputedFieldOrderBy (ComputedFieldOrderByElement b (UnpreparedValue b)
 -> ComputedFieldOrderBy b (UnpreparedValue b))
-> ComputedFieldOrderByElement b (UnpreparedValue b)
-> ComputedFieldOrderBy b (UnpreparedValue b)
forall a b. (a -> b) -> a -> b
$ ScalarType b -> ComputedFieldOrderByElement b (UnpreparedValue b)
forall (b :: BackendType) v.
ScalarType b -> ComputedFieldOrderByElement b v
IR.CFOBEScalar ScalarType b
scalarType
              InputFieldsParser
  MetadataObjId
  n
  (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> MaybeT
     m
     (InputFieldsParser
        MetadataObjId
        n
        (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputFieldsParser
   MetadataObjId
   n
   (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
 -> MaybeT
      m
      (InputFieldsParser
         MetadataObjId
         n
         (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> MaybeT
     m
     (InputFieldsParser
        MetadataObjId
        n
        (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))
forall a b. (a -> b) -> a -> b
$
                Name
-> Maybe Description
-> Parser
     MetadataObjId 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe (BasicOrderType b, NullsOrderType b)))
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional
                  Name
fieldName
                  Maybe Description
forall a. Maybe a
Nothing
                  (NamingCase
-> SourceInfo b
-> Parser
     MetadataObjId 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
forall (b :: BackendType) (n :: * -> *).
(BackendSchema b, MonadParse n) =>
NamingCase
-> SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperator @b NamingCase
tCase SourceInfo b
sourceInfo)
                  InputFieldsParser
  MetadataObjId
  n
  (Maybe (Maybe (BasicOrderType b, NullsOrderType b)))
-> (Maybe (Maybe (BasicOrderType b, NullsOrderType b))
    -> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> ((BasicOrderType b, NullsOrderType b)
 -> [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> Maybe (BasicOrderType b, NullsOrderType b)
-> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (AnnotatedOrderByItemG b (UnpreparedValue b)
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AnnotatedOrderByItemG b (UnpreparedValue b)
 -> [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> ((BasicOrderType b, NullsOrderType b)
    -> AnnotatedOrderByItemG b (UnpreparedValue b))
-> (BasicOrderType b, NullsOrderType b)
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnnotatedOrderByElement b (UnpreparedValue b)
-> (BasicOrderType b, NullsOrderType b)
-> AnnotatedOrderByItemG b (UnpreparedValue b)
forall (b :: BackendType) a.
a -> (BasicOrderType b, NullsOrderType b) -> OrderByItemG b a
mkOrderByItemG @b (ComputedFieldOrderBy b (UnpreparedValue b)
-> AnnotatedOrderByElement b (UnpreparedValue b)
forall (b :: BackendType) v.
ComputedFieldOrderBy b v -> AnnotatedOrderByElement b v
IR.AOCComputedField ComputedFieldOrderBy b (UnpreparedValue b)
computedFieldOrderBy)) (Maybe (BasicOrderType b, NullsOrderType b)
 -> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> (Maybe (Maybe (BasicOrderType b, NullsOrderType b))
    -> Maybe (BasicOrderType b, NullsOrderType b))
-> Maybe (Maybe (BasicOrderType b, NullsOrderType b))
-> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Maybe (BasicOrderType b, NullsOrderType b))
-> Maybe (BasicOrderType b, NullsOrderType b)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join
            ReturnsTable TableName b
table -> do
              let aggregateFieldName :: Name
aggregateFieldName = NamingCase -> GQLNameIdentifier -> Name
applyFieldNameCaseIdentifier NamingCase
tCase (GQLNameIdentifier -> Name) -> GQLNameIdentifier -> Name
forall a b. (a -> b) -> a -> b
$ (Name, [NameSuffix]) -> GQLNameIdentifier
C.fromAutogeneratedTuple (Name
fieldName, [Name -> NameSuffix
G.convertNameToSuffix Name
Name._aggregate])
              TableInfo b
tableInfo' <- SourceInfo b -> TableName b -> MaybeT m (TableInfo b)
forall (b :: BackendType) (m :: * -> *).
(Backend b, MonadError QErr m) =>
SourceInfo b -> TableName b -> m (TableInfo b)
askTableInfo SourceInfo b
sourceInfo TableName b
table
              SelPermInfo b
perms <- Maybe (SelPermInfo b) -> MaybeT m (SelPermInfo b)
forall (m :: * -> *) b. Applicative m => Maybe b -> MaybeT m b
hoistMaybe (Maybe (SelPermInfo b) -> MaybeT m (SelPermInfo b))
-> Maybe (SelPermInfo b) -> MaybeT m (SelPermInfo b)
forall a b. (a -> b) -> a -> b
$ RoleName -> TableInfo b -> Maybe (SelPermInfo b)
forall (b :: BackendType).
RoleName -> TableInfo b -> Maybe (SelPermInfo b)
tableSelectPermissions RoleName
roleName TableInfo b
tableInfo'
              let newPerms :: GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
newPerms = (PartialSQLExp b -> UnpreparedValue b)
-> AnnBoolExpFld b (PartialSQLExp b)
-> AnnBoolExpFld b (UnpreparedValue b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PartialSQLExp b -> UnpreparedValue b
forall (b :: BackendType). PartialSQLExp b -> UnpreparedValue b
partialSQLExpToUnpreparedValue (AnnBoolExpFld b (PartialSQLExp b)
 -> AnnBoolExpFld b (UnpreparedValue b))
-> GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
-> GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SelPermInfo b -> GBoolExp b (AnnBoolExpFld b (PartialSQLExp b))
forall (b :: BackendType). SelPermInfo b -> AnnBoolExpPartialSQL b
spiFilter SelPermInfo b
perms
              Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
aggregationParser <- m (Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> MaybeT
     m (Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
 -> MaybeT
      m (Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)]))
-> m (Parser
        'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> MaybeT
     m (Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall a b. (a -> b) -> a -> b
$ SourceInfo b
-> TableInfo b
-> m (Parser
        'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
SourceInfo b
-> TableInfo b
-> m (Parser
        'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
orderByAggregation SourceInfo b
sourceInfo TableInfo b
tableInfo'
              pure $ do
                Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]
aggregationOrderBy <- Maybe (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
 -> Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name
-> Maybe Description
-> Parser
     MetadataObjId
     'Input
     n
     (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]))
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional Name
aggregateFieldName Maybe Description
forall a. Maybe a
Nothing (Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
-> Parser
     MetadataObjId
     'Input
     n
     (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall origin (k :: Kind) (m :: * -> *) a.
(MonadParse m, 'Input <: k) =>
Parser origin k m a -> Parser origin k m (Maybe a)
P.nullable Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
aggregationParser)
                pure $
                  ([OrderByItemG b (AnnotatedAggregateOrderBy b)]
 -> [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]
-> Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
                    ( (OrderByItemG b (AnnotatedAggregateOrderBy b)
 -> AnnotatedOrderByItemG b (UnpreparedValue b))
-> [OrderByItemG b (AnnotatedAggregateOrderBy b)]
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall a b. (a -> b) -> [a] -> [b]
map ((OrderByItemG b (AnnotatedAggregateOrderBy b)
  -> AnnotatedOrderByItemG b (UnpreparedValue b))
 -> [OrderByItemG b (AnnotatedAggregateOrderBy b)]
 -> [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> (OrderByItemG b (AnnotatedAggregateOrderBy b)
    -> AnnotatedOrderByItemG b (UnpreparedValue b))
-> [OrderByItemG b (AnnotatedAggregateOrderBy b)]
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall a b. (a -> b) -> a -> b
$
                        (AnnotatedAggregateOrderBy b
 -> AnnotatedOrderByElement b (UnpreparedValue b))
-> OrderByItemG b (AnnotatedAggregateOrderBy b)
-> AnnotatedOrderByItemG b (UnpreparedValue b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((AnnotatedAggregateOrderBy b
  -> AnnotatedOrderByElement b (UnpreparedValue b))
 -> OrderByItemG b (AnnotatedAggregateOrderBy b)
 -> AnnotatedOrderByItemG b (UnpreparedValue b))
-> (AnnotatedAggregateOrderBy b
    -> AnnotatedOrderByElement b (UnpreparedValue b))
-> OrderByItemG b (AnnotatedAggregateOrderBy b)
-> AnnotatedOrderByItemG b (UnpreparedValue b)
forall a b. (a -> b) -> a -> b
$
                          ComputedFieldOrderBy b (UnpreparedValue b)
-> AnnotatedOrderByElement b (UnpreparedValue b)
forall (b :: BackendType) v.
ComputedFieldOrderBy b v -> AnnotatedOrderByElement b v
IR.AOCComputedField
                            (ComputedFieldOrderBy b (UnpreparedValue b)
 -> AnnotatedOrderByElement b (UnpreparedValue b))
-> (AnnotatedAggregateOrderBy b
    -> ComputedFieldOrderBy b (UnpreparedValue b))
-> AnnotatedAggregateOrderBy b
-> AnnotatedOrderByElement b (UnpreparedValue b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ComputedFieldOrderByElement b (UnpreparedValue b)
-> ComputedFieldOrderBy b (UnpreparedValue b)
mkComputedFieldOrderBy
                            (ComputedFieldOrderByElement b (UnpreparedValue b)
 -> ComputedFieldOrderBy b (UnpreparedValue b))
-> (AnnotatedAggregateOrderBy b
    -> ComputedFieldOrderByElement b (UnpreparedValue b))
-> AnnotatedAggregateOrderBy b
-> ComputedFieldOrderBy b (UnpreparedValue b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableName b
-> GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
-> AnnotatedAggregateOrderBy b
-> ComputedFieldOrderByElement b (UnpreparedValue b)
forall (b :: BackendType) v.
TableName b
-> AnnBoolExp b v
-> AnnotatedAggregateOrderBy b
-> ComputedFieldOrderByElement b v
IR.CFOBETableAggregation TableName b
table GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
newPerms
                    )
                    Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]
aggregationOrderBy
            ComputedFieldReturnType b
ReturnsOthers -> MaybeT
  m
  (InputFieldsParser
     MetadataObjId
     n
     (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))
forall (f :: * -> *) a. Alternative f => f a
empty
        FIRemoteRelationship RemoteFieldInfo (DBJoinField b)
_ -> MaybeT
  m
  (InputFieldsParser
     MetadataObjId
     n
     (Maybe [AnnotatedOrderByItemG b (UnpreparedValue b)]))
forall (f :: * -> *) a. Alternative f => f a
empty

-- FIXME!
-- those parsers are directly using Postgres' SQL representation of
-- order, rather than using a general intermediary representation

orderByAggregation ::
  forall b r m n.
  MonadBuildSchema b r m n =>
  SourceInfo b ->
  TableInfo b ->
  m (Parser 'Input n [IR.OrderByItemG b (IR.AnnotatedAggregateOrderBy b)])
orderByAggregation :: SourceInfo b
-> TableInfo b
-> m (Parser
        'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
orderByAggregation SourceInfo b
sourceInfo TableInfo b
tableInfo = Name
-> (SourceName, TableName b)
-> m (Parser
        'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> m (Parser
        'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall (m :: * -> *) a (p :: (* -> *) -> * -> *) (n :: * -> *) b.
(MonadMemoize m, Ord a, Typeable a, Typeable p, MonadParse n,
 Typeable b) =>
Name -> a -> m (p n b) -> m (p n b)
P.memoizeOn 'orderByAggregation (SourceInfo b -> SourceName
forall (b :: BackendType). SourceInfo b -> SourceName
_siName SourceInfo b
sourceInfo, TableName b
tableName) do
  -- WIP NOTE
  -- there is heavy duplication between this and Select.tableAggregationFields
  -- it might be worth putting some of it in common, just to avoid issues when
  -- we change one but not the other?
  Name
tableGQLName <- TableInfo b -> m Name
forall (b :: BackendType) (m :: * -> *).
(Backend b, MonadError QErr m) =>
TableInfo b -> m Name
getTableGQLName @b TableInfo b
tableInfo
  NamingCase
tCase <- (r -> NamingCase) -> m NamingCase
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks r -> NamingCase
forall a t. Has a t => t -> a
getter
  GQLNameIdentifier
tableIdentifierName <- TableInfo b -> m GQLNameIdentifier
forall (b :: BackendType) (m :: * -> *).
(Backend b, MonadError QErr m) =>
TableInfo b -> m GQLNameIdentifier
getTableIdentifierName @b TableInfo b
tableInfo
  [ColumnInfo b]
allColumns <- SourceInfo b -> TableInfo b -> m [ColumnInfo b]
forall (b :: BackendType) r (m :: * -> *).
(Backend b, MonadError QErr m, MonadReader r m,
 Has SchemaContext r) =>
SourceInfo b -> TableInfo b -> m [ColumnInfo b]
tableSelectColumns SourceInfo b
sourceInfo TableInfo b
tableInfo
  MkTypename
makeTypename <- (r -> MkTypename) -> m MkTypename
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks r -> MkTypename
forall a t. Has a t => t -> a
getter
  let numColumns :: [ColumnInfo b]
numColumns = [ColumnInfo b] -> [ColumnInfo b]
forall (b :: BackendType).
Backend b =>
[ColumnInfo b] -> [ColumnInfo b]
onlyNumCols [ColumnInfo b]
allColumns
      compColumns :: [ColumnInfo b]
compColumns = [ColumnInfo b] -> [ColumnInfo b]
forall (b :: BackendType).
Backend b =>
[ColumnInfo b] -> [ColumnInfo b]
onlyComparableCols [ColumnInfo b]
allColumns
      numFields :: InputFieldsParser
  MetadataObjId
  n
  [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
numFields = [Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
-> [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes ([Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
 -> [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))])
-> InputFieldsParser
     MetadataObjId
     n
     [Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
-> InputFieldsParser
     MetadataObjId
     n
     [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ColumnInfo b
 -> InputFieldsParser
      MetadataObjId
      n
      (Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b))))
-> [ColumnInfo b]
-> InputFieldsParser
     MetadataObjId
     n
     [Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (NamingCase
-> ColumnInfo b
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b)))
mkField NamingCase
tCase) [ColumnInfo b]
numColumns
      compFields :: InputFieldsParser
  MetadataObjId
  n
  [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
compFields = [Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
-> [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes ([Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
 -> [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))])
-> InputFieldsParser
     MetadataObjId
     n
     [Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
-> InputFieldsParser
     MetadataObjId
     n
     [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ColumnInfo b
 -> InputFieldsParser
      MetadataObjId
      n
      (Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b))))
-> [ColumnInfo b]
-> InputFieldsParser
     MetadataObjId
     n
     [Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (NamingCase
-> ColumnInfo b
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b)))
mkField NamingCase
tCase) [ColumnInfo b]
compColumns
      aggFields :: InputFieldsParser
  MetadataObjId n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
aggFields =
        ([[Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
 -> [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> InputFieldsParser
     MetadataObjId
     n
     [[Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
-> InputFieldsParser
     MetadataObjId n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([[OrderByItemG b (AnnotatedAggregateOrderBy b)]]
-> [OrderByItemG b (AnnotatedAggregateOrderBy b)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[OrderByItemG b (AnnotatedAggregateOrderBy b)]]
 -> [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> ([[Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
    -> [[OrderByItemG b (AnnotatedAggregateOrderBy b)]])
-> [[Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
-> [OrderByItemG b (AnnotatedAggregateOrderBy b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
-> [[OrderByItemG b (AnnotatedAggregateOrderBy b)]]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes ([Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
 -> [[OrderByItemG b (AnnotatedAggregateOrderBy b)]])
-> ([[Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
    -> [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]])
-> [[Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
-> [[OrderByItemG b (AnnotatedAggregateOrderBy b)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
-> [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat) (InputFieldsParser
   MetadataObjId
   n
   [[Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
 -> InputFieldsParser
      MetadataObjId n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> InputFieldsParser
     MetadataObjId
     n
     [[Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
-> InputFieldsParser
     MetadataObjId n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
forall a b. (a -> b) -> a -> b
$
          [InputFieldsParser
   MetadataObjId
   n
   [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
-> InputFieldsParser
     MetadataObjId
     n
     [[Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA ([InputFieldsParser
    MetadataObjId
    n
    [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
 -> InputFieldsParser
      MetadataObjId
      n
      [[Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]])
-> [InputFieldsParser
      MetadataObjId
      n
      [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
-> InputFieldsParser
     MetadataObjId
     n
     [[Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
forall a b. (a -> b) -> a -> b
$
            [Maybe
   (InputFieldsParser
      MetadataObjId
      n
      [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]])]
-> [InputFieldsParser
      MetadataObjId
      n
      [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes
              [ -- count
                InputFieldsParser
  MetadataObjId
  n
  [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
-> Maybe
     (InputFieldsParser
        MetadataObjId
        n
        [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]])
forall a. a -> Maybe a
Just (InputFieldsParser
   MetadataObjId
   n
   [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
 -> Maybe
      (InputFieldsParser
         MetadataObjId
         n
         [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]))
-> InputFieldsParser
     MetadataObjId
     n
     [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
-> Maybe
     (InputFieldsParser
        MetadataObjId
        n
        [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]])
forall a b. (a -> b) -> a -> b
$
                  Name
-> Maybe Description
-> Parser
     MetadataObjId 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe (BasicOrderType b, NullsOrderType b)))
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional
                    Name
Name._count
                    Maybe Description
forall a. Maybe a
Nothing
                    (NamingCase
-> SourceInfo b
-> Parser
     MetadataObjId 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
forall (b :: BackendType) (n :: * -> *).
(BackendSchema b, MonadParse n) =>
NamingCase
-> SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperator @b NamingCase
tCase SourceInfo b
sourceInfo)
                    InputFieldsParser
  MetadataObjId
  n
  (Maybe (Maybe (BasicOrderType b, NullsOrderType b)))
-> (Maybe (Maybe (BasicOrderType b, NullsOrderType b))
    -> [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]])
-> InputFieldsParser
     MetadataObjId
     n
     [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]
-> [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]
 -> [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]])
-> (Maybe (Maybe (BasicOrderType b, NullsOrderType b))
    -> Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> Maybe (Maybe (BasicOrderType b, NullsOrderType b))
-> [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((BasicOrderType b, NullsOrderType b)
 -> [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> Maybe (BasicOrderType b, NullsOrderType b)
-> Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (OrderByItemG b (AnnotatedAggregateOrderBy b)
-> [OrderByItemG b (AnnotatedAggregateOrderBy b)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (OrderByItemG b (AnnotatedAggregateOrderBy b)
 -> [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> ((BasicOrderType b, NullsOrderType b)
    -> OrderByItemG b (AnnotatedAggregateOrderBy b))
-> (BasicOrderType b, NullsOrderType b)
-> [OrderByItemG b (AnnotatedAggregateOrderBy b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnnotatedAggregateOrderBy b
-> (BasicOrderType b, NullsOrderType b)
-> OrderByItemG b (AnnotatedAggregateOrderBy b)
forall (b :: BackendType) a.
a -> (BasicOrderType b, NullsOrderType b) -> OrderByItemG b a
mkOrderByItemG @b AnnotatedAggregateOrderBy b
forall (b :: BackendType). AnnotatedAggregateOrderBy b
IR.AAOCount) (Maybe (BasicOrderType b, NullsOrderType b)
 -> Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> (Maybe (Maybe (BasicOrderType b, NullsOrderType b))
    -> Maybe (BasicOrderType b, NullsOrderType b))
-> Maybe (Maybe (BasicOrderType b, NullsOrderType b))
-> Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Maybe (BasicOrderType b, NullsOrderType b))
-> Maybe (BasicOrderType b, NullsOrderType b)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join,
                -- operators on numeric columns
                if [ColumnInfo b] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ColumnInfo b]
numColumns
                  then Maybe
  (InputFieldsParser
     MetadataObjId
     n
     [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]])
forall a. Maybe a
Nothing
                  else InputFieldsParser
  MetadataObjId
  n
  [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
-> Maybe
     (InputFieldsParser
        MetadataObjId
        n
        [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]])
forall a. a -> Maybe a
Just (InputFieldsParser
   MetadataObjId
   n
   [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
 -> Maybe
      (InputFieldsParser
         MetadataObjId
         n
         [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]))
-> InputFieldsParser
     MetadataObjId
     n
     [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
-> Maybe
     (InputFieldsParser
        MetadataObjId
        n
        [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]])
forall a b. (a -> b) -> a -> b
$
                    [Name]
-> (Name
    -> InputFieldsParser
         MetadataObjId
         n
         (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]))
-> InputFieldsParser
     MetadataObjId
     n
     [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for [Name]
numericAggOperators \Name
operator ->
                      MkTypename
-> Name
-> Name
-> InputFieldsParser
     MetadataObjId
     n
     [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
parseOperator MkTypename
makeTypename Name
operator Name
tableGQLName InputFieldsParser
  MetadataObjId
  n
  [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
numFields,
                -- operators on comparable columns
                if [ColumnInfo b] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ColumnInfo b]
compColumns
                  then Maybe
  (InputFieldsParser
     MetadataObjId
     n
     [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]])
forall a. Maybe a
Nothing
                  else InputFieldsParser
  MetadataObjId
  n
  [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
-> Maybe
     (InputFieldsParser
        MetadataObjId
        n
        [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]])
forall a. a -> Maybe a
Just (InputFieldsParser
   MetadataObjId
   n
   [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
 -> Maybe
      (InputFieldsParser
         MetadataObjId
         n
         [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]))
-> InputFieldsParser
     MetadataObjId
     n
     [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
-> Maybe
     (InputFieldsParser
        MetadataObjId
        n
        [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]])
forall a b. (a -> b) -> a -> b
$
                    [Name]
-> (Name
    -> InputFieldsParser
         MetadataObjId
         n
         (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]))
-> InputFieldsParser
     MetadataObjId
     n
     [Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)]]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for [Name]
comparisonAggOperators \Name
operator ->
                      MkTypename
-> Name
-> Name
-> InputFieldsParser
     MetadataObjId
     n
     [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
parseOperator MkTypename
makeTypename Name
operator Name
tableGQLName InputFieldsParser
  MetadataObjId
  n
  [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
compFields
              ]
  Name
objectName <- Name -> m Name
forall r (m :: * -> *).
(MonadReader r m, Has MkTypename r) =>
Name -> m Name
mkTypename (Name -> m Name) -> Name -> m Name
forall a b. (a -> b) -> a -> b
$ NamingCase -> GQLNameIdentifier -> Name
applyTypeNameCaseIdentifier NamingCase
tCase (GQLNameIdentifier -> Name) -> GQLNameIdentifier -> Name
forall a b. (a -> b) -> a -> b
$ GQLNameIdentifier -> GQLNameIdentifier
mkTableAggregateOrderByTypeName GQLNameIdentifier
tableIdentifierName
  let description :: Description
description = Text -> Description
G.Description (Text -> Description) -> Text -> Description
forall a b. (a -> b) -> a -> b
$ Text
"order by aggregate values of table " Text -> TableName b -> Text
forall t. ToTxt t => Text -> t -> Text
<>> TableName b
tableName
  Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
-> m (Parser
        'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
 -> m (Parser
         'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)]))
-> Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
-> m (Parser
        'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall a b. (a -> b) -> a -> b
$ Name
-> Maybe Description
-> InputFieldsParser
     MetadataObjId n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
-> Parser 'Input n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
forall (m :: * -> *) origin a.
MonadParse m =>
Name
-> Maybe Description
-> InputFieldsParser origin m a
-> Parser origin 'Input m a
P.object Name
objectName (Description -> Maybe Description
forall a. a -> Maybe a
Just Description
description) InputFieldsParser
  MetadataObjId n [OrderByItemG b (AnnotatedAggregateOrderBy b)]
aggFields
  where
    tableName :: TableName b
tableName = TableInfo b -> TableName b
forall (b :: BackendType). TableInfo b -> TableName b
tableInfoName TableInfo b
tableInfo

    mkField :: NamingCase -> ColumnInfo b -> InputFieldsParser n (Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b)))
    mkField :: NamingCase
-> ColumnInfo b
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b)))
mkField NamingCase
tCase ColumnInfo b
columnInfo =
      Name
-> Maybe Description
-> Parser
     MetadataObjId 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe (BasicOrderType b, NullsOrderType b)))
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional
        (ColumnInfo b -> Name
forall (b :: BackendType). ColumnInfo b -> Name
ciName ColumnInfo b
columnInfo)
        (ColumnInfo b -> Maybe Description
forall (b :: BackendType). ColumnInfo b -> Maybe Description
ciDescription ColumnInfo b
columnInfo)
        (NamingCase
-> SourceInfo b
-> Parser
     MetadataObjId 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
forall (b :: BackendType) (n :: * -> *).
(BackendSchema b, MonadParse n) =>
NamingCase
-> SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperator @b NamingCase
tCase SourceInfo b
sourceInfo)
        InputFieldsParser
  MetadataObjId
  n
  (Maybe (Maybe (BasicOrderType b, NullsOrderType b)))
-> (Maybe (Maybe (BasicOrderType b, NullsOrderType b))
    -> Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b)))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b)))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> ((BasicOrderType b, NullsOrderType b)
 -> (ColumnInfo b, (BasicOrderType b, NullsOrderType b)))
-> Maybe (BasicOrderType b, NullsOrderType b)
-> Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ColumnInfo b
columnInfo,) (Maybe (BasicOrderType b, NullsOrderType b)
 -> Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b)))
-> (Maybe (Maybe (BasicOrderType b, NullsOrderType b))
    -> Maybe (BasicOrderType b, NullsOrderType b))
-> Maybe (Maybe (BasicOrderType b, NullsOrderType b))
-> Maybe (ColumnInfo b, (BasicOrderType b, NullsOrderType b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Maybe (BasicOrderType b, NullsOrderType b))
-> Maybe (BasicOrderType b, NullsOrderType b)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join

    parseOperator ::
      MkTypename ->
      G.Name ->
      G.Name ->
      InputFieldsParser n [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))] ->
      InputFieldsParser n (Maybe [IR.OrderByItemG b (IR.AnnotatedAggregateOrderBy b)])
    parseOperator :: MkTypename
-> Name
-> Name
-> InputFieldsParser
     MetadataObjId
     n
     [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
parseOperator MkTypename
makeTypename Name
operator Name
tableGQLName InputFieldsParser
  MetadataObjId
  n
  [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
columns =
      let opText :: Text
opText = Name -> Text
G.unName Name
operator
          objectName :: Name
objectName = MkTypename -> Name -> Name
runMkTypename MkTypename
makeTypename (Name -> Name) -> Name -> Name
forall a b. (a -> b) -> a -> b
$ Name
tableGQLName Name -> Name -> Name
forall a. Semigroup a => a -> a -> a
<> Name
Name.__ Name -> Name -> Name
forall a. Semigroup a => a -> a -> a
<> Name
operator Name -> Name -> Name
forall a. Semigroup a => a -> a -> a
<> Name
Name.__order_by
          objectDesc :: Maybe Description
objectDesc = Description -> Maybe Description
forall a. a -> Maybe a
Just (Description -> Maybe Description)
-> Description -> Maybe Description
forall a b. (a -> b) -> a -> b
$ Text -> Description
G.Description (Text -> Description) -> Text -> Description
forall a b. (a -> b) -> a -> b
$ Text
"order by " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
opText Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"() on columns of table " Text -> TableName b -> Text
forall t. ToTxt t => Text -> t -> Text
<>> TableName b
tableName
       in Name
-> Maybe Description
-> Parser
     MetadataObjId
     'Input
     n
     [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))])
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m (Maybe a)
P.fieldOptional Name
operator Maybe Description
forall a. Maybe a
Nothing (Name
-> Maybe Description
-> InputFieldsParser
     MetadataObjId
     n
     [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
-> Parser
     MetadataObjId
     'Input
     n
     [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
forall (m :: * -> *) origin a.
MonadParse m =>
Name
-> Maybe Description
-> InputFieldsParser origin m a
-> Parser origin 'Input m a
P.object Name
objectName Maybe Description
objectDesc InputFieldsParser
  MetadataObjId
  n
  [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
columns)
            InputFieldsParser
  MetadataObjId
  n
  (Maybe [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))])
-> ([(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
    -> [OrderByItemG b (AnnotatedAggregateOrderBy b)])
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe [OrderByItemG b (AnnotatedAggregateOrderBy b)])
forall (m :: * -> *) a b.
Functor m =>
InputFieldsParser m (Maybe a)
-> (a -> b) -> InputFieldsParser m (Maybe b)
`mapField` ((ColumnInfo b, (BasicOrderType b, NullsOrderType b))
 -> OrderByItemG b (AnnotatedAggregateOrderBy b))
-> [(ColumnInfo b, (BasicOrderType b, NullsOrderType b))]
-> [OrderByItemG b (AnnotatedAggregateOrderBy b)]
forall a b. (a -> b) -> [a] -> [b]
map (\(ColumnInfo b
col, (BasicOrderType b, NullsOrderType b)
info) -> AnnotatedAggregateOrderBy b
-> (BasicOrderType b, NullsOrderType b)
-> OrderByItemG b (AnnotatedAggregateOrderBy b)
forall (b :: BackendType) a.
a -> (BasicOrderType b, NullsOrderType b) -> OrderByItemG b a
mkOrderByItemG (Text -> ColumnInfo b -> AnnotatedAggregateOrderBy b
forall (b :: BackendType).
Text -> ColumnInfo b -> AnnotatedAggregateOrderBy b
IR.AAOOp Text
opText ColumnInfo b
col) (BasicOrderType b, NullsOrderType b)
info)

orderByOperatorsHasuraCase ::
  forall b n.
  (BackendSchema b, MonadParse n) =>
  SourceInfo b ->
  Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperatorsHasuraCase :: SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperatorsHasuraCase = NamingCase
-> SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
forall (b :: BackendType) (n :: * -> *).
(BackendSchema b, MonadParse n) =>
NamingCase
-> SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperator' @b NamingCase
HasuraCase

orderByOperatorsGraphqlCase ::
  forall b n.
  (BackendSchema b, MonadParse n) =>
  SourceInfo b ->
  Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperatorsGraphqlCase :: SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperatorsGraphqlCase = NamingCase
-> SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
forall (b :: BackendType) (n :: * -> *).
(BackendSchema b, MonadParse n) =>
NamingCase
-> SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperator' @b NamingCase
GraphqlCase

orderByOperator' ::
  forall b n.
  (BackendSchema b, MonadParse n) =>
  NamingCase ->
  SourceInfo b ->
  Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperator' :: NamingCase
-> SourceInfo b
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
orderByOperator' NamingCase
tCase SourceInfo b
sourceInfo =
  let (Name
sourcePrefix, NonEmpty
  (Definition MetadataObjId EnumValueInfo,
   (BasicOrderType b, NullsOrderType b))
orderOperators) = SourceInfo b
-> NamingCase
-> (Name,
    NonEmpty
      (Definition MetadataObjId EnumValueInfo,
       (BasicOrderType b, NullsOrderType b)))
forall (b :: BackendType).
BackendSchema b =>
SourceInfo b
-> NamingCase
-> (Name,
    NonEmpty
      (Definition MetadataObjId EnumValueInfo,
       (BasicOrderType b, NullsOrderType b)))
orderByOperators @b SourceInfo b
sourceInfo NamingCase
tCase
   in Parser MetadataObjId 'Both n (BasicOrderType b, NullsOrderType b)
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
forall origin (k :: Kind) (m :: * -> *) a.
(MonadParse m, 'Input <: k) =>
Parser origin k m a -> Parser origin k m (Maybe a)
P.nullable (Parser MetadataObjId 'Both n (BasicOrderType b, NullsOrderType b)
 -> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b)))
-> Parser
     MetadataObjId 'Both n (BasicOrderType b, NullsOrderType b)
-> Parser 'Both n (Maybe (BasicOrderType b, NullsOrderType b))
forall a b. (a -> b) -> a -> b
$ Name
-> Maybe Description
-> NonEmpty
     (Definition MetadataObjId EnumValueInfo,
      (BasicOrderType b, NullsOrderType b))
-> Parser
     MetadataObjId 'Both n (BasicOrderType b, NullsOrderType b)
forall (m :: * -> *) origin a.
MonadParse m =>
Name
-> Maybe Description
-> NonEmpty (Definition origin EnumValueInfo, a)
-> Parser origin 'Both m a
P.enum (NamingCase -> Name -> Name
applyTypeNameCaseCust NamingCase
tCase Name
sourcePrefix) (Description -> Maybe Description
forall a. a -> Maybe a
Just Description
"column ordering options") (NonEmpty
   (Definition MetadataObjId EnumValueInfo,
    (BasicOrderType b, NullsOrderType b))
 -> Parser
      MetadataObjId 'Both n (BasicOrderType b, NullsOrderType b))
-> NonEmpty
     (Definition MetadataObjId EnumValueInfo,
      (BasicOrderType b, NullsOrderType b))
-> Parser
     MetadataObjId 'Both n (BasicOrderType b, NullsOrderType b)
forall a b. (a -> b) -> a -> b
$ NonEmpty
  (Definition MetadataObjId EnumValueInfo,
   (BasicOrderType b, NullsOrderType b))
orderOperators

mkOrderByItemG :: forall b a. a -> (BasicOrderType b, NullsOrderType b) -> IR.OrderByItemG b a
mkOrderByItemG :: a -> (BasicOrderType b, NullsOrderType b) -> OrderByItemG b a
mkOrderByItemG a
column (BasicOrderType b
orderType, NullsOrderType b
nullsOrder) =
  OrderByItemG :: forall (b :: BackendType) a.
Maybe (BasicOrderType b)
-> a -> Maybe (NullsOrderType b) -> OrderByItemG b a
IR.OrderByItemG
    { obiType :: Maybe (BasicOrderType b)
obiType = BasicOrderType b -> Maybe (BasicOrderType b)
forall a. a -> Maybe a
Just BasicOrderType b
orderType,
      obiColumn :: a
obiColumn = a
column,
      obiNulls :: Maybe (NullsOrderType b)
obiNulls = NullsOrderType b -> Maybe (NullsOrderType b)
forall a. a -> Maybe a
Just NullsOrderType b
nullsOrder
    }