{-# LANGUAGE ApplicativeDo #-}

-- | This module defines the schema aspect of the default implementation of
-- aggregation predicates.
module Hasura.GraphQL.Schema.BoolExp.AggregationPredicates
  ( defaultAggregationPredicatesParser,

    -- * Data types describing aggregation functions supported by a backend
    FunctionSignature (..),
    ArgumentsSignature (..),
    ArgumentSignature (..),
  )
where

import Data.Functor.Compose
import Data.Has (getter)
import Data.List.NonEmpty qualified as NE
import Data.Text.Casing qualified as C
import Hasura.GraphQL.Parser qualified as P
import Hasura.GraphQL.Schema.Backend
import Hasura.GraphQL.Schema.BoolExp
import Hasura.GraphQL.Schema.Common
import Hasura.GraphQL.Schema.Parser
  ( InputFieldsParser,
    Kind (..),
    Parser,
  )
import Hasura.GraphQL.Schema.Table
import Hasura.Name qualified as Name
import Hasura.Prelude
import Hasura.RQL.IR qualified as IR
import Hasura.RQL.IR.BoolExp.AggregationPredicates
import Hasura.RQL.IR.Value
import Hasura.RQL.Types.Backend qualified as B
import Hasura.RQL.Types.BackendType (BackendType)
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.Common (relNameToTxt)
import Hasura.RQL.Types.NamingCase
import Hasura.RQL.Types.Relationships.Local
import Hasura.RQL.Types.Schema.Options (IncludeAggregationPredicates (..))
import Hasura.RQL.Types.Schema.Options qualified as Options
import Hasura.RQL.Types.SchemaCache hiding (askTableInfo)
import Hasura.RQL.Types.Source (SourceInfo (..))
import Hasura.RQL.Types.SourceCustomization
import Hasura.Table.Cache
import Language.GraphQL.Draft.Syntax qualified as G

-- | This function is meant to serve as the default schema for Aggregation
-- Predicates represented in the IR by the type
-- 'Hasura.RQL.IR.BoolExp.AggregationPredicates.AggregationPredicates'.
defaultAggregationPredicatesParser ::
  forall b r m n.
  ( MonadBuildSchema b r m n,
    AggregationPredicatesSchema b
  ) =>
  [FunctionSignature b] ->
  TableInfo b ->
  SchemaT r m (Maybe (InputFieldsParser n [AggregationPredicatesImplementation b (UnpreparedValue b)]))
defaultAggregationPredicatesParser :: forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(MonadBuildSchema b r m n, AggregationPredicatesSchema b) =>
[FunctionSignature b]
-> TableInfo b
-> SchemaT
     r
     m
     (Maybe
        (InputFieldsParser
           n [AggregationPredicatesImplementation b (UnpreparedValue b)]))
defaultAggregationPredicatesParser [FunctionSignature b]
aggFns TableInfo b
ti = MaybeT
  (SchemaT r m)
  (InputFieldsParser
     n [AggregationPredicatesImplementation b (UnpreparedValue b)])
-> SchemaT
     r
     m
     (Maybe
        (InputFieldsParser
           n [AggregationPredicatesImplementation b (UnpreparedValue b)]))
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT do
  SourceInfo b
sourceInfo :: SourceInfo b <- (r -> SourceInfo b) -> MaybeT (SchemaT r m) (SourceInfo b)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks r -> SourceInfo b
forall a t. Has a t => t -> a
getter
  let customization :: ResolvedSourceCustomization
customization = SourceInfo b -> ResolvedSourceCustomization
forall (b :: BackendType).
SourceInfo b -> ResolvedSourceCustomization
_siCustomization SourceInfo b
sourceInfo
      tCase :: NamingCase
tCase = ResolvedSourceCustomization -> NamingCase
_rscNamingConvention ResolvedSourceCustomization
customization
  -- Check in schema options whether we should include aggregation predicates
  IncludeAggregationPredicates
include <- (SchemaOptions -> IncludeAggregationPredicates)
-> MaybeT (SchemaT r m) IncludeAggregationPredicates
forall r (m :: * -> *) a b.
(MonadReader r m, Has a r) =>
(a -> b) -> m b
retrieve SchemaOptions -> IncludeAggregationPredicates
Options.soIncludeAggregationPredicates
  case IncludeAggregationPredicates
include of
    IncludeAggregationPredicates
IncludeAggregationPredicates -> () -> MaybeT (SchemaT r m) ()
forall a. a -> MaybeT (SchemaT r m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    IncludeAggregationPredicates
Don'tIncludeAggregationPredicates -> SchemaT r m (Maybe ()) -> MaybeT (SchemaT r m) ()
forall (f :: * -> *) a. f (Maybe a) -> MaybeT f a
fails (SchemaT r m (Maybe ()) -> MaybeT (SchemaT r m) ())
-> SchemaT r m (Maybe ()) -> MaybeT (SchemaT r m) ()
forall a b. (a -> b) -> a -> b
$ Maybe () -> SchemaT r m (Maybe ())
forall a. a -> SchemaT r m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ()
forall a. Maybe a
Nothing

  NonEmpty (RelInfo b)
arrayRelationships <- SchemaT r m (Maybe (NonEmpty (RelInfo b)))
-> MaybeT (SchemaT r m) (NonEmpty (RelInfo b))
forall (f :: * -> *) a. f (Maybe a) -> MaybeT f a
fails (SchemaT r m (Maybe (NonEmpty (RelInfo b)))
 -> MaybeT (SchemaT r m) (NonEmpty (RelInfo b)))
-> SchemaT r m (Maybe (NonEmpty (RelInfo b)))
-> MaybeT (SchemaT r m) (NonEmpty (RelInfo b))
forall a b. (a -> b) -> a -> b
$ Maybe (NonEmpty (RelInfo b))
-> SchemaT r m (Maybe (NonEmpty (RelInfo b)))
forall a. a -> SchemaT r m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (NonEmpty (RelInfo b))
 -> SchemaT r m (Maybe (NonEmpty (RelInfo b))))
-> Maybe (NonEmpty (RelInfo b))
-> SchemaT r m (Maybe (NonEmpty (RelInfo b)))
forall a b. (a -> b) -> a -> b
$ [RelInfo b] -> Maybe (NonEmpty (RelInfo b))
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty ([RelInfo b] -> Maybe (NonEmpty (RelInfo b)))
-> [RelInfo b] -> Maybe (NonEmpty (RelInfo b))
forall a b. (a -> b) -> a -> b
$ TableInfo b -> [RelInfo b]
forall (b :: BackendType). TableInfo b -> [RelInfo b]
tableArrayRelationships TableInfo b
ti
  NonEmpty (FunctionSignature b)
aggregationFunctions <- SchemaT r m (Maybe (NonEmpty (FunctionSignature b)))
-> MaybeT (SchemaT r m) (NonEmpty (FunctionSignature b))
forall (f :: * -> *) a. f (Maybe a) -> MaybeT f a
fails (SchemaT r m (Maybe (NonEmpty (FunctionSignature b)))
 -> MaybeT (SchemaT r m) (NonEmpty (FunctionSignature b)))
-> SchemaT r m (Maybe (NonEmpty (FunctionSignature b)))
-> MaybeT (SchemaT r m) (NonEmpty (FunctionSignature b))
forall a b. (a -> b) -> a -> b
$ Maybe (NonEmpty (FunctionSignature b))
-> SchemaT r m (Maybe (NonEmpty (FunctionSignature b)))
forall a. a -> SchemaT r m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (NonEmpty (FunctionSignature b))
 -> SchemaT r m (Maybe (NonEmpty (FunctionSignature b))))
-> Maybe (NonEmpty (FunctionSignature b))
-> SchemaT r m (Maybe (NonEmpty (FunctionSignature b)))
forall a b. (a -> b) -> a -> b
$ [FunctionSignature b] -> Maybe (NonEmpty (FunctionSignature b))
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty [FunctionSignature b]
aggFns
  RoleName
roleName <- (SchemaContext -> RoleName) -> MaybeT (SchemaT r m) RoleName
forall r (m :: * -> *) a b.
(MonadReader r m, Has a r) =>
(a -> b) -> m b
retrieve SchemaContext -> RoleName
scRole

  MaybeT
  (SchemaT r m)
  (NonEmpty
     (InputFieldsParser
        n
        (Maybe
           (AggregationPredicatesImplementation b (UnpreparedValue b)))))
-> MaybeT
     (SchemaT r m)
     (InputFieldsParser
        n [AggregationPredicatesImplementation b (UnpreparedValue b)])
forall (f :: * -> *) a.
Functor f =>
MaybeT f (NonEmpty (InputFieldsParser n (Maybe a)))
-> MaybeT f (InputFieldsParser n [a])
collectOptionalFieldsNE
    (MaybeT
   (SchemaT r m)
   (NonEmpty
      (InputFieldsParser
         n
         (Maybe
            (AggregationPredicatesImplementation b (UnpreparedValue b)))))
 -> MaybeT
      (SchemaT r m)
      (InputFieldsParser
         n [AggregationPredicatesImplementation b (UnpreparedValue b)]))
-> (NonEmpty
      (MaybeT
         (SchemaT r m)
         (InputFieldsParser
            n
            (Maybe
               (AggregationPredicatesImplementation b (UnpreparedValue b)))))
    -> MaybeT
         (SchemaT r m)
         (NonEmpty
            (InputFieldsParser
               n
               (Maybe
                  (AggregationPredicatesImplementation b (UnpreparedValue b))))))
-> NonEmpty
     (MaybeT
        (SchemaT r m)
        (InputFieldsParser
           n
           (Maybe
              (AggregationPredicatesImplementation b (UnpreparedValue b)))))
-> MaybeT
     (SchemaT r m)
     (InputFieldsParser
        n [AggregationPredicatesImplementation b (UnpreparedValue b)])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty
  (MaybeT
     (SchemaT r m)
     (InputFieldsParser
        n
        (Maybe
           (AggregationPredicatesImplementation b (UnpreparedValue b)))))
-> MaybeT
     (SchemaT r m)
     (NonEmpty
        (InputFieldsParser
           n
           (Maybe
              (AggregationPredicatesImplementation b (UnpreparedValue b)))))
forall (f :: * -> *) a.
Applicative f =>
NonEmpty (MaybeT f a) -> MaybeT f (NonEmpty a)
succeedingBranchesNE
    (NonEmpty
   (MaybeT
      (SchemaT r m)
      (InputFieldsParser
         n
         (Maybe
            (AggregationPredicatesImplementation b (UnpreparedValue b)))))
 -> MaybeT
      (SchemaT r m)
      (InputFieldsParser
         n [AggregationPredicatesImplementation b (UnpreparedValue b)]))
-> NonEmpty
     (MaybeT
        (SchemaT r m)
        (InputFieldsParser
           n
           (Maybe
              (AggregationPredicatesImplementation b (UnpreparedValue b)))))
-> MaybeT
     (SchemaT r m)
     (InputFieldsParser
        n [AggregationPredicatesImplementation b (UnpreparedValue b)])
forall a b. (a -> b) -> a -> b
$ NonEmpty (RelInfo b)
arrayRelationships
    NonEmpty (RelInfo b)
-> (RelInfo b
    -> MaybeT
         (SchemaT r m)
         (InputFieldsParser
            n
            (Maybe
               (AggregationPredicatesImplementation b (UnpreparedValue b)))))
-> NonEmpty
     (MaybeT
        (SchemaT r m)
        (InputFieldsParser
           n
           (Maybe
              (AggregationPredicatesImplementation b (UnpreparedValue b)))))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \RelInfo b
rel -> do
      TableName b
relTableName <- case RelInfo b -> RelTarget b
forall (b :: BackendType). RelInfo b -> RelTarget b
riTarget RelInfo b
rel of
        RelTargetNativeQuery NativeQueryName
_ -> [Char] -> MaybeT (SchemaT r m) (TableName b)
forall a. HasCallStack => [Char] -> a
error [Char]
"defaultAggregationPredicatesParser RelTargetNativeQuery"
        RelTargetTable TableName b
tn -> TableName b -> MaybeT (SchemaT r m) (TableName b)
forall a. a -> MaybeT (SchemaT r m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TableName b
tn
      TableInfo b
relTable <- TableName b -> MaybeT (SchemaT r m) (TableInfo b)
forall (b :: BackendType) r (m :: * -> *).
(Backend b, MonadError QErr m, MonadReader r m,
 Has (SourceInfo b) r) =>
TableName b -> m (TableInfo b)
askTableInfo TableName b
relTableName
      SelPermInfo b
selectPermissions <- Maybe (SelPermInfo b) -> MaybeT (SchemaT r m) (SelPermInfo b)
forall (m :: * -> *) b. Applicative m => Maybe b -> MaybeT m b
hoistMaybe (Maybe (SelPermInfo b) -> MaybeT (SchemaT r m) (SelPermInfo b))
-> Maybe (SelPermInfo b) -> MaybeT (SchemaT r 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
relTable
      Bool -> MaybeT (SchemaT r m) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> MaybeT (SchemaT r m) ())
-> Bool -> MaybeT (SchemaT r m) ()
forall a b. (a -> b) -> a -> b
$ SelPermInfo b -> Bool
forall (b :: BackendType). SelPermInfo b -> Bool
spiAllowAgg SelPermInfo b
selectPermissions
      let rowPermissions :: GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
rowPermissions = (PartialSQLExp b -> UnpreparedValue b)
-> AnnBoolExpFld b (PartialSQLExp b)
-> AnnBoolExpFld b (UnpreparedValue b)
forall a b. (a -> b) -> AnnBoolExpFld b a -> AnnBoolExpFld b 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
selectPermissions
      Name
relGqlName <- Text -> MaybeT (SchemaT r m) Name
forall (m :: * -> *). MonadError QErr m => Text -> m Name
textToName (Text -> MaybeT (SchemaT r m) Name)
-> Text -> MaybeT (SchemaT r m) 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
rel
      GQLNameIdentifier
typeGqlName <- GQLNameIdentifier -> GQLNameIdentifier
mkTableAggregateBoolExpTypeName (GQLNameIdentifier -> GQLNameIdentifier)
-> MaybeT (SchemaT r m) GQLNameIdentifier
-> MaybeT (SchemaT r m) GQLNameIdentifier
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TableInfo b -> MaybeT (SchemaT r m) GQLNameIdentifier
forall (b :: BackendType) (m :: * -> *).
(Backend b, MonadError QErr m) =>
TableInfo b -> m GQLNameIdentifier
getTableIdentifierName TableInfo b
relTable

      -- We only make a field for aggregations over a relation if at least
      -- some aggregation predicates are callable.
      RelInfo b
-> GQLNameIdentifier
-> GQLNameIdentifier
-> NamingCase
-> GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
-> InputFieldsParser n [AggregationPredicate b (UnpreparedValue b)]
-> InputFieldsParser
     n
     (Maybe (AggregationPredicatesImplementation b (UnpreparedValue b)))
relAggregateField RelInfo b
rel (Name -> GQLNameIdentifier
C.fromCustomName Name
relGqlName) GQLNameIdentifier
typeGqlName NamingCase
tCase GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
rowPermissions
        -- We only return an InputFieldsParser for aggregation predicates,
        -- if we parse at least one aggregation predicate
        (InputFieldsParser n [AggregationPredicate b (UnpreparedValue b)]
 -> InputFieldsParser
      n
      (Maybe
         (AggregationPredicatesImplementation b (UnpreparedValue b))))
-> MaybeT
     (SchemaT r m)
     (InputFieldsParser n [AggregationPredicate b (UnpreparedValue b)])
-> MaybeT
     (SchemaT r m)
     (InputFieldsParser
        n
        (Maybe
           (AggregationPredicatesImplementation b (UnpreparedValue b))))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (MaybeT
  (SchemaT r m)
  (NonEmpty
     (InputFieldsParser
        n (Maybe (AggregationPredicate b (UnpreparedValue b)))))
-> MaybeT
     (SchemaT r m)
     (InputFieldsParser n [AggregationPredicate b (UnpreparedValue b)])
forall (f :: * -> *) a.
Functor f =>
MaybeT f (NonEmpty (InputFieldsParser n (Maybe a)))
-> MaybeT f (InputFieldsParser n [a])
collectOptionalFieldsNE (MaybeT
   (SchemaT r m)
   (NonEmpty
      (InputFieldsParser
         n (Maybe (AggregationPredicate b (UnpreparedValue b)))))
 -> MaybeT
      (SchemaT r m)
      (InputFieldsParser n [AggregationPredicate b (UnpreparedValue b)]))
-> (NonEmpty
      (MaybeT
         (SchemaT r m)
         (InputFieldsParser
            n (Maybe (AggregationPredicate b (UnpreparedValue b)))))
    -> MaybeT
         (SchemaT r m)
         (NonEmpty
            (InputFieldsParser
               n (Maybe (AggregationPredicate b (UnpreparedValue b))))))
-> NonEmpty
     (MaybeT
        (SchemaT r m)
        (InputFieldsParser
           n (Maybe (AggregationPredicate b (UnpreparedValue b)))))
-> MaybeT
     (SchemaT r m)
     (InputFieldsParser n [AggregationPredicate b (UnpreparedValue b)])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty
  (MaybeT
     (SchemaT r m)
     (InputFieldsParser
        n (Maybe (AggregationPredicate b (UnpreparedValue b)))))
-> MaybeT
     (SchemaT r m)
     (NonEmpty
        (InputFieldsParser
           n (Maybe (AggregationPredicate b (UnpreparedValue b)))))
forall (f :: * -> *) a.
Applicative f =>
NonEmpty (MaybeT f a) -> MaybeT f (NonEmpty a)
succeedingBranchesNE)
          ( NonEmpty (FunctionSignature b)
aggregationFunctions NonEmpty (FunctionSignature b)
-> (FunctionSignature b
    -> MaybeT
         (SchemaT r m)
         (InputFieldsParser
            n (Maybe (AggregationPredicate b (UnpreparedValue b)))))
-> NonEmpty
     (MaybeT
        (SchemaT r m)
        (InputFieldsParser
           n (Maybe (AggregationPredicate b (UnpreparedValue b)))))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \FunctionSignature {Text
Name
ScalarType b
ArgumentsSignature b
fnName :: Text
fnGQLName :: Name
fnArguments :: ArgumentsSignature b
fnReturnType :: ScalarType b
fnName :: forall (b :: BackendType). FunctionSignature b -> Text
fnGQLName :: forall (b :: BackendType). FunctionSignature b -> Name
fnArguments :: forall (b :: BackendType).
FunctionSignature b -> ArgumentsSignature b
fnReturnType :: forall (b :: BackendType). FunctionSignature b -> ScalarType b
..} -> do
              let relFunGqlName :: GQLNameIdentifier
relFunGqlName = GQLNameIdentifier -> Name -> GQLNameIdentifier
mkRelationFunctionIdentifier GQLNameIdentifier
typeGqlName Name
fnGQLName
              Name
-> GQLNameIdentifier
-> NamingCase
-> InputFieldsParser n (AggregationPredicate b (UnpreparedValue b))
-> InputFieldsParser
     n (Maybe (AggregationPredicate b (UnpreparedValue b)))
aggPredicateField Name
fnGQLName GQLNameIdentifier
typeGqlName NamingCase
tCase (InputFieldsParser n (AggregationPredicate b (UnpreparedValue b))
 -> InputFieldsParser
      n (Maybe (AggregationPredicate b (UnpreparedValue b))))
-> MaybeT
     (SchemaT r m)
     (InputFieldsParser n (AggregationPredicate b (UnpreparedValue b)))
-> MaybeT
     (SchemaT r m)
     (InputFieldsParser
        n (Maybe (AggregationPredicate b (UnpreparedValue b))))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Compose
  (MaybeT (SchemaT r m))
  (InputFieldsParser MetadataObjId n)
  (AggregationPredicate b (UnpreparedValue b))
-> MaybeT
     (SchemaT r m)
     (InputFieldsParser n (AggregationPredicate b (UnpreparedValue b)))
forall (f :: * -> *) a.
Compose (MaybeT f) (InputFieldsParser MetadataObjId n) a
-> MaybeT f (InputFieldsParser n a)
unfuse do
                AggregationPredicateArguments b
aggPredArguments <-
                  -- We only include an aggregation predicate if we are able to
                  -- access columns all its arguments. This might fail due to
                  -- permissions or due to no columns of suitable types
                  -- existing on the table.
                  case ArgumentsSignature b
fnArguments of
                    ArgumentsSignature b
ArgumentsStar ->
                      AggregationPredicateArguments b
-> (NonEmpty (Column b) -> AggregationPredicateArguments b)
-> Maybe (NonEmpty (Column b))
-> AggregationPredicateArguments b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe AggregationPredicateArguments b
forall (b :: BackendType). AggregationPredicateArguments b
AggregationPredicateArgumentsStar NonEmpty (Column b) -> AggregationPredicateArguments b
forall (b :: BackendType).
NonEmpty (Column b) -> AggregationPredicateArguments b
AggregationPredicateArguments
                        (Maybe (NonEmpty (Column b)) -> AggregationPredicateArguments b)
-> ([Column b] -> Maybe (NonEmpty (Column b)))
-> [Column b]
-> AggregationPredicateArguments b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Column b] -> Maybe (NonEmpty (Column b))
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty
                        -- TODO(redactionExp): Probably need to deal with the redaction expressions from tableSelectColumnsEnum here
                        ([Column b] -> AggregationPredicateArguments b)
-> Compose
     (MaybeT (SchemaT r m))
     (InputFieldsParser MetadataObjId n)
     [Column b]
-> Compose
     (MaybeT (SchemaT r m))
     (InputFieldsParser MetadataObjId n)
     (AggregationPredicateArguments b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MaybeT (SchemaT r m) (InputFieldsParser n [Column b])
-> Compose
     (MaybeT (SchemaT r m))
     (InputFieldsParser MetadataObjId n)
     [Column b]
forall (f :: * -> *) a.
MaybeT f (InputFieldsParser n a)
-> Compose (MaybeT f) (InputFieldsParser MetadataObjId n) a
fuse (Name
-> Maybe Description
-> [Column b]
-> Parser 'Both n [Column b]
-> InputFieldsParser n [Column b]
forall (k :: Kind) a.
('Input <: k) =>
Name
-> Maybe Description -> a -> Parser k n a -> InputFieldsParser n a
fieldOptionalDefault Name
Name._arguments Maybe Description
forall a. Maybe a
Nothing [] (Parser 'Both n [Column b] -> InputFieldsParser n [Column b])
-> (Parser
      MetadataObjId 'Both n (Column b, AnnRedactionExpUnpreparedValue b)
    -> Parser 'Both n [Column b])
-> Parser
     MetadataObjId 'Both n (Column b, AnnRedactionExpUnpreparedValue b)
-> InputFieldsParser n [Column b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser MetadataObjId 'Both n (Column b)
-> Parser 'Both n [Column b]
forall origin (k :: Kind) (m :: * -> *) a.
(MonadParse m, 'Input <: k) =>
Parser origin k m a -> Parser origin k m [a]
P.list (Parser MetadataObjId 'Both n (Column b)
 -> Parser 'Both n [Column b])
-> (Parser
      MetadataObjId 'Both n (Column b, AnnRedactionExpUnpreparedValue b)
    -> Parser MetadataObjId 'Both n (Column b))
-> Parser
     MetadataObjId 'Both n (Column b, AnnRedactionExpUnpreparedValue b)
-> Parser 'Both n [Column b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Column b, AnnRedactionExpUnpreparedValue b) -> Column b)
-> Parser
     MetadataObjId 'Both n (Column b, AnnRedactionExpUnpreparedValue b)
-> Parser MetadataObjId 'Both n (Column b)
forall a b.
(a -> b)
-> Parser MetadataObjId 'Both n a -> Parser MetadataObjId 'Both n b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Column b, AnnRedactionExpUnpreparedValue b) -> Column b
forall a b. (a, b) -> a
fst (Parser
   MetadataObjId 'Both n (Column b, AnnRedactionExpUnpreparedValue b)
 -> InputFieldsParser n [Column b])
-> MaybeT
     (SchemaT r m)
     (Parser
        MetadataObjId 'Both n (Column b, AnnRedactionExpUnpreparedValue b))
-> MaybeT (SchemaT r m) (InputFieldsParser n [Column b])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SchemaT
  r
  m
  (Maybe
     (Parser
        MetadataObjId
        'Both
        n
        (Column b, AnnRedactionExpUnpreparedValue b)))
-> MaybeT
     (SchemaT r m)
     (Parser
        MetadataObjId 'Both n (Column b, AnnRedactionExpUnpreparedValue b))
forall (f :: * -> *) a. f (Maybe a) -> MaybeT f a
fails (TableInfo b
-> SchemaT
     r
     m
     (Maybe
        (Parser
           MetadataObjId
           'Both
           n
           (Column b, AnnRedactionExpUnpreparedValue b)))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
TableInfo b
-> SchemaT
     r
     m
     (Maybe
        (Parser 'Both n (Column b, AnnRedactionExpUnpreparedValue b)))
tableSelectColumnsEnum TableInfo b
relTable))
                    SingleArgument ScalarType b
typ ->
                      NonEmpty (Column b) -> AggregationPredicateArguments b
forall (b :: BackendType).
NonEmpty (Column b) -> AggregationPredicateArguments b
AggregationPredicateArguments
                        (NonEmpty (Column b) -> AggregationPredicateArguments b)
-> (Column b -> NonEmpty (Column b))
-> Column b
-> AggregationPredicateArguments b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Column b -> [Column b] -> NonEmpty (Column b)
forall a. a -> [a] -> NonEmpty a
NE.:| [])
                        (Column b -> AggregationPredicateArguments b)
-> Compose
     (MaybeT (SchemaT r m))
     (InputFieldsParser MetadataObjId n)
     (Column b)
-> Compose
     (MaybeT (SchemaT r m))
     (InputFieldsParser MetadataObjId n)
     (AggregationPredicateArguments b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MaybeT (SchemaT r m) (InputFieldsParser n (Column b))
-> Compose
     (MaybeT (SchemaT r m))
     (InputFieldsParser MetadataObjId n)
     (Column b)
forall (f :: * -> *) a.
MaybeT f (InputFieldsParser n a)
-> Compose (MaybeT f) (InputFieldsParser MetadataObjId n) a
fuse
                          ( Name
-> Maybe Description
-> Parser MetadataObjId 'Both n (Column b)
-> InputFieldsParser n (Column b)
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m a
P.field Name
Name._arguments Maybe Description
forall a. Maybe a
Nothing
                              (Parser MetadataObjId 'Both n (Column b)
 -> InputFieldsParser n (Column b))
-> MaybeT (SchemaT r m) (Parser MetadataObjId 'Both n (Column b))
-> MaybeT (SchemaT r m) (InputFieldsParser n (Column b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SchemaT r m (Maybe (Parser MetadataObjId 'Both n (Column b)))
-> MaybeT (SchemaT r m) (Parser MetadataObjId 'Both n (Column b))
forall (f :: * -> *) a. f (Maybe a) -> MaybeT f a
fails ((ColumnType b -> Bool)
-> GQLNameIdentifier
-> TableInfo b
-> SchemaT r m (Maybe (Parser MetadataObjId 'Both n (Column b)))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
(ColumnType b -> Bool)
-> GQLNameIdentifier
-> TableInfo b
-> SchemaT r m (Maybe (Parser 'Both n (Column b)))
tableSelectColumnsPredEnum (ColumnType b -> ColumnType b -> Bool
forall a. Eq a => a -> a -> Bool
== (ScalarType b -> ColumnType b
forall (b :: BackendType). ScalarType b -> ColumnType b
ColumnScalar ScalarType b
typ)) GQLNameIdentifier
relFunGqlName TableInfo b
relTable)
                          )
                    Arguments NonEmpty (ArgumentSignature b)
args ->
                      NonEmpty (Column b) -> AggregationPredicateArguments b
forall (b :: BackendType).
NonEmpty (Column b) -> AggregationPredicateArguments b
AggregationPredicateArguments
                        (NonEmpty (Column b) -> AggregationPredicateArguments b)
-> Compose
     (MaybeT (SchemaT r m))
     (InputFieldsParser MetadataObjId n)
     (NonEmpty (Column b))
-> Compose
     (MaybeT (SchemaT r m))
     (InputFieldsParser MetadataObjId n)
     (AggregationPredicateArguments b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MaybeT (SchemaT r m) (InputFieldsParser n (NonEmpty (Column b)))
-> Compose
     (MaybeT (SchemaT r m))
     (InputFieldsParser MetadataObjId n)
     (NonEmpty (Column b))
forall (f :: * -> *) a.
MaybeT f (InputFieldsParser n a)
-> Compose (MaybeT f) (InputFieldsParser MetadataObjId n) a
fuse
                          ( Name
-> Maybe Description
-> Parser MetadataObjId 'Input n (NonEmpty (Column b))
-> InputFieldsParser n (NonEmpty (Column b))
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m a
P.field Name
Name._arguments Maybe Description
forall a. Maybe a
Nothing
                              (Parser MetadataObjId 'Input n (NonEmpty (Column b))
 -> InputFieldsParser n (NonEmpty (Column b)))
-> (InputFieldsParser n (NonEmpty (Column b))
    -> Parser MetadataObjId 'Input n (NonEmpty (Column b)))
-> InputFieldsParser n (NonEmpty (Column b))
-> InputFieldsParser n (NonEmpty (Column b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name
-> Maybe Description
-> InputFieldsParser n (NonEmpty (Column b))
-> Parser MetadataObjId 'Input n (NonEmpty (Column b))
forall (m :: * -> *) origin a.
MonadParse m =>
Name
-> Maybe Description
-> InputFieldsParser origin m a
-> Parser origin 'Input m a
P.object (NamingCase -> GQLNameIdentifier -> Name
applyFieldNameCaseIdentifier NamingCase
tCase (GQLNameIdentifier -> Name -> GQLNameIdentifier
mkRelationFunctionArgumentsFieldName GQLNameIdentifier
typeGqlName Name
fnGQLName)) Maybe Description
forall a. Maybe a
Nothing
                              (InputFieldsParser n (NonEmpty (Column b))
 -> InputFieldsParser n (NonEmpty (Column b)))
-> MaybeT (SchemaT r m) (InputFieldsParser n (NonEmpty (Column b)))
-> MaybeT (SchemaT r m) (InputFieldsParser n (NonEmpty (Column b)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MaybeT (SchemaT r m) (NonEmpty (InputFieldsParser n (Column b)))
-> MaybeT (SchemaT r m) (InputFieldsParser n (NonEmpty (Column b)))
forall (f :: * -> *) c.
Functor f =>
MaybeT f (NonEmpty (InputFieldsParser n c))
-> MaybeT f (InputFieldsParser n (NonEmpty c))
collectFieldsNE
                                ( NonEmpty (ArgumentSignature b)
args NonEmpty (ArgumentSignature b)
-> (ArgumentSignature b
    -> MaybeT (SchemaT r m) (InputFieldsParser n (Column b)))
-> MaybeT (SchemaT r m) (NonEmpty (InputFieldsParser n (Column b)))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
`for` \ArgumentSignature {Name
ScalarType b
argType :: ScalarType b
argName :: Name
argType :: forall (b :: BackendType). ArgumentSignature b -> ScalarType b
argName :: forall (b :: BackendType). ArgumentSignature b -> Name
..} ->
                                    Name
-> Maybe Description
-> Parser MetadataObjId 'Both n (Column b)
-> InputFieldsParser n (Column b)
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m a
P.field Name
argName Maybe Description
forall a. Maybe a
Nothing (Parser MetadataObjId 'Both n (Column b)
 -> InputFieldsParser n (Column b))
-> MaybeT (SchemaT r m) (Parser MetadataObjId 'Both n (Column b))
-> MaybeT (SchemaT r m) (InputFieldsParser n (Column b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SchemaT r m (Maybe (Parser MetadataObjId 'Both n (Column b)))
-> MaybeT (SchemaT r m) (Parser MetadataObjId 'Both n (Column b))
forall (f :: * -> *) a. f (Maybe a) -> MaybeT f a
fails ((ColumnType b -> Bool)
-> GQLNameIdentifier
-> TableInfo b
-> SchemaT r m (Maybe (Parser MetadataObjId 'Both n (Column b)))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
(ColumnType b -> Bool)
-> GQLNameIdentifier
-> TableInfo b
-> SchemaT r m (Maybe (Parser 'Both n (Column b)))
tableSelectColumnsPredEnum (ColumnType b -> ColumnType b -> Bool
forall a. Eq a => a -> a -> Bool
== (ScalarType b -> ColumnType b
forall (b :: BackendType). ScalarType b -> ColumnType b
ColumnScalar ScalarType b
argType)) GQLNameIdentifier
relFunGqlName TableInfo b
relTable)
                                )
                          )

                Bool
aggPredDistinct <- MaybeT (SchemaT r m) (InputFieldsParser n Bool)
-> Compose
     (MaybeT (SchemaT r m)) (InputFieldsParser MetadataObjId n) Bool
forall (f :: * -> *) a.
MaybeT f (InputFieldsParser n a)
-> Compose (MaybeT f) (InputFieldsParser MetadataObjId n) a
fuse (MaybeT (SchemaT r m) (InputFieldsParser n Bool)
 -> Compose
      (MaybeT (SchemaT r m)) (InputFieldsParser MetadataObjId n) Bool)
-> MaybeT (SchemaT r m) (InputFieldsParser n Bool)
-> Compose
     (MaybeT (SchemaT r m)) (InputFieldsParser MetadataObjId n) Bool
forall a b. (a -> b) -> a -> b
$ InputFieldsParser n Bool
-> MaybeT (SchemaT r m) (InputFieldsParser n Bool)
forall a. a -> MaybeT (SchemaT r m) a
forall (m :: * -> *) a. Monad m => a -> m a
return (InputFieldsParser n Bool
 -> MaybeT (SchemaT r m) (InputFieldsParser n Bool))
-> InputFieldsParser n Bool
-> MaybeT (SchemaT r m) (InputFieldsParser n Bool)
forall a b. (a -> b) -> a -> b
$ Name
-> Maybe Description
-> Bool
-> Parser 'Both n Bool
-> InputFieldsParser n Bool
forall (k :: Kind) a.
('Input <: k) =>
Name
-> Maybe Description -> a -> Parser k n a -> InputFieldsParser n a
fieldOptionalDefault Name
Name._distinct Maybe Description
forall a. Maybe a
Nothing Bool
False Parser 'Both n Bool
forall (m :: * -> *) origin.
MonadParse m =>
Parser origin 'Both m Bool
P.boolean
                let aggPredFunctionName :: Text
aggPredFunctionName = Text
fnName
                [ComparisonExp b]
aggPredPredicate <- MaybeT (SchemaT r m) (InputFieldsParser n [ComparisonExp b])
-> Compose
     (MaybeT (SchemaT r m))
     (InputFieldsParser MetadataObjId n)
     [ComparisonExp b]
forall (f :: * -> *) a.
MaybeT f (InputFieldsParser n a)
-> Compose (MaybeT f) (InputFieldsParser MetadataObjId n) a
fuse (MaybeT (SchemaT r m) (InputFieldsParser n [ComparisonExp b])
 -> Compose
      (MaybeT (SchemaT r m))
      (InputFieldsParser MetadataObjId n)
      [ComparisonExp b])
-> MaybeT (SchemaT r m) (InputFieldsParser n [ComparisonExp b])
-> Compose
     (MaybeT (SchemaT r m))
     (InputFieldsParser MetadataObjId n)
     [ComparisonExp b]
forall a b. (a -> b) -> a -> b
$ Name
-> Maybe Description
-> Parser MetadataObjId 'Input n [ComparisonExp b]
-> InputFieldsParser n [ComparisonExp b]
forall (m :: * -> *) (k :: Kind) origin a.
(MonadParse m, 'Input <: k) =>
Name
-> Maybe Description
-> Parser origin k m a
-> InputFieldsParser origin m a
P.field Name
Name._predicate Maybe Description
forall a. Maybe a
Nothing (Parser MetadataObjId 'Input n [ComparisonExp b]
 -> InputFieldsParser n [ComparisonExp b])
-> MaybeT
     (SchemaT r m) (Parser MetadataObjId 'Input n [ComparisonExp b])
-> MaybeT (SchemaT r m) (InputFieldsParser n [ComparisonExp b])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SchemaT r m (Parser MetadataObjId 'Input n [ComparisonExp b])
-> MaybeT
     (SchemaT r m) (Parser MetadataObjId 'Input n [ComparisonExp b])
forall (m :: * -> *) a. Monad m => m a -> MaybeT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(BackendSchema b, MonadBuildSchema b r m n) =>
ColumnType b -> SchemaT r m (Parser 'Input n [ComparisonExp b])
comparisonExps @b (ScalarType b -> ColumnType b
forall (b :: BackendType). ScalarType b -> ColumnType b
ColumnScalar ScalarType b
fnReturnType))
                Maybe (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b)))
aggPredFilter <- MaybeT
  (SchemaT r m)
  (InputFieldsParser
     n (Maybe (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b)))))
-> Compose
     (MaybeT (SchemaT r m))
     (InputFieldsParser MetadataObjId n)
     (Maybe (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))))
forall (f :: * -> *) a.
MaybeT f (InputFieldsParser n a)
-> Compose (MaybeT f) (InputFieldsParser MetadataObjId n) a
fuse (MaybeT
   (SchemaT r m)
   (InputFieldsParser
      n (Maybe (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b)))))
 -> Compose
      (MaybeT (SchemaT r m))
      (InputFieldsParser MetadataObjId n)
      (Maybe (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b)))))
-> MaybeT
     (SchemaT r m)
     (InputFieldsParser
        n (Maybe (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b)))))
-> Compose
     (MaybeT (SchemaT r m))
     (InputFieldsParser MetadataObjId n)
     (Maybe (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))))
forall a b. (a -> b) -> a -> b
$ Name
-> Maybe Description
-> Parser
     MetadataObjId
     'Input
     n
     (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b)))
-> InputFieldsParser
     n (Maybe (GBoolExp b (AnnBoolExpFld 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
Name._filter Maybe Description
forall a. Maybe a
Nothing (Parser
   MetadataObjId
   'Input
   n
   (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b)))
 -> InputFieldsParser
      n (Maybe (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b)))))
-> MaybeT
     (SchemaT r m)
     (Parser
        MetadataObjId
        'Input
        n
        (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))))
-> MaybeT
     (SchemaT r m)
     (InputFieldsParser
        n (Maybe (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b)))))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SchemaT
  r
  m
  (Parser
     MetadataObjId
     'Input
     n
     (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))))
-> MaybeT
     (SchemaT r m)
     (Parser
        MetadataObjId
        'Input
        n
        (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))))
forall (m :: * -> *) a. Monad m => m a -> MaybeT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TableInfo b
-> SchemaT
     r
     m
     (Parser
        MetadataObjId
        'Input
        n
        (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(MonadBuildSchema b r m n, AggregationPredicatesSchema b) =>
TableInfo b
-> SchemaT r m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
tableBoolExp TableInfo b
relTable)
                pure $ AggregationPredicate {Bool
[ComparisonExp b]
Maybe (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b)))
Text
AggregationPredicateArguments b
aggPredArguments :: AggregationPredicateArguments b
aggPredDistinct :: Bool
aggPredFunctionName :: Text
aggPredPredicate :: [ComparisonExp b]
aggPredFilter :: Maybe (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b)))
aggPredFunctionName :: Text
aggPredDistinct :: Bool
aggPredFilter :: Maybe (GBoolExp b (AnnBoolExpFld b (UnpreparedValue b)))
aggPredArguments :: AggregationPredicateArguments b
aggPredPredicate :: [ComparisonExp b]
..}
          )
  where
    -- Input field of the aggregation predicates for one array relation.
    relAggregateField ::
      RelInfo b ->
      C.GQLNameIdentifier ->
      C.GQLNameIdentifier ->
      NamingCase ->
      (IR.AnnBoolExp b (UnpreparedValue b)) ->
      (InputFieldsParser n [AggregationPredicate b (UnpreparedValue b)]) ->
      (InputFieldsParser n (Maybe (AggregationPredicatesImplementation b (UnpreparedValue b))))
    relAggregateField :: RelInfo b
-> GQLNameIdentifier
-> GQLNameIdentifier
-> NamingCase
-> GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
-> InputFieldsParser n [AggregationPredicate b (UnpreparedValue b)]
-> InputFieldsParser
     n
     (Maybe (AggregationPredicatesImplementation b (UnpreparedValue b)))
relAggregateField RelInfo b
rel GQLNameIdentifier
relGqlName GQLNameIdentifier
typeGqlName NamingCase
tCase GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
rowPermissions =
      Name
-> Maybe Description
-> Parser
     MetadataObjId
     'Input
     n
     (AggregationPredicatesImplementation b (UnpreparedValue b))
-> InputFieldsParser
     n
     (Maybe (AggregationPredicatesImplementation 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 (NamingCase -> GQLNameIdentifier -> Name
applyFieldNameCaseIdentifier NamingCase
tCase (GQLNameIdentifier -> GQLNameIdentifier
mkTableAggregateTypeName GQLNameIdentifier
relGqlName)) Maybe Description
forall a. Maybe a
Nothing
        (Parser
   MetadataObjId
   'Input
   n
   (AggregationPredicatesImplementation b (UnpreparedValue b))
 -> InputFieldsParser
      n
      (Maybe
         (AggregationPredicatesImplementation b (UnpreparedValue b))))
-> (InputFieldsParser
      n [AggregationPredicate b (UnpreparedValue b)]
    -> Parser
         MetadataObjId
         'Input
         n
         (AggregationPredicatesImplementation b (UnpreparedValue b)))
-> InputFieldsParser n [AggregationPredicate b (UnpreparedValue b)]
-> InputFieldsParser
     n
     (Maybe (AggregationPredicatesImplementation b (UnpreparedValue b)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name
-> Maybe Description
-> InputFieldsParser
     MetadataObjId
     n
     (AggregationPredicatesImplementation b (UnpreparedValue b))
-> Parser
     MetadataObjId
     'Input
     n
     (AggregationPredicatesImplementation b (UnpreparedValue b))
forall (m :: * -> *) origin a.
MonadParse m =>
Name
-> Maybe Description
-> InputFieldsParser origin m a
-> Parser origin 'Input m a
P.object (NamingCase -> GQLNameIdentifier -> Name
applyTypeNameCaseIdentifier NamingCase
tCase GQLNameIdentifier
typeGqlName) Maybe Description
forall a. Maybe a
Nothing
        (InputFieldsParser
   MetadataObjId
   n
   (AggregationPredicatesImplementation b (UnpreparedValue b))
 -> Parser
      MetadataObjId
      'Input
      n
      (AggregationPredicatesImplementation b (UnpreparedValue b)))
-> (InputFieldsParser
      n [AggregationPredicate b (UnpreparedValue b)]
    -> InputFieldsParser
         MetadataObjId
         n
         (AggregationPredicatesImplementation b (UnpreparedValue b)))
-> InputFieldsParser n [AggregationPredicate b (UnpreparedValue b)]
-> Parser
     MetadataObjId
     'Input
     n
     (AggregationPredicatesImplementation b (UnpreparedValue b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AggregationPredicate b (UnpreparedValue b)
 -> AggregationPredicatesImplementation b (UnpreparedValue b))
-> InputFieldsParser n (AggregationPredicate b (UnpreparedValue b))
-> InputFieldsParser
     MetadataObjId
     n
     (AggregationPredicatesImplementation b (UnpreparedValue b))
forall a b.
(a -> b)
-> InputFieldsParser MetadataObjId n a
-> InputFieldsParser MetadataObjId n b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (RelInfo b
-> GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
-> AggregationPredicate b (UnpreparedValue b)
-> AggregationPredicatesImplementation b (UnpreparedValue b)
forall (b :: BackendType) field.
RelInfo b
-> AnnBoolExp b field
-> AggregationPredicate b field
-> AggregationPredicatesImplementation b field
AggregationPredicatesImplementation RelInfo b
rel GBoolExp b (AnnBoolExpFld b (UnpreparedValue b))
rowPermissions)
        (InputFieldsParser n (AggregationPredicate b (UnpreparedValue b))
 -> InputFieldsParser
      MetadataObjId
      n
      (AggregationPredicatesImplementation b (UnpreparedValue b)))
-> (InputFieldsParser
      n [AggregationPredicate b (UnpreparedValue b)]
    -> InputFieldsParser
         n (AggregationPredicate b (UnpreparedValue b)))
-> InputFieldsParser n [AggregationPredicate b (UnpreparedValue b)]
-> InputFieldsParser
     MetadataObjId
     n
     (AggregationPredicatesImplementation b (UnpreparedValue b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ( InputFieldsParser n [AggregationPredicate b (UnpreparedValue b)]
-> ([AggregationPredicate b (UnpreparedValue b)]
    -> n (AggregationPredicate b (UnpreparedValue b)))
-> InputFieldsParser n (AggregationPredicate b (UnpreparedValue b))
forall (m :: * -> *) origin a b.
Monad m =>
InputFieldsParser origin m a
-> (a -> m b) -> InputFieldsParser origin m b
`P.bindFields`
              \case
                [AggregationPredicate b (UnpreparedValue b)
predicate] -> AggregationPredicate b (UnpreparedValue b)
-> n (AggregationPredicate b (UnpreparedValue b))
forall a. a -> n a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AggregationPredicate b (UnpreparedValue b)
predicate
                [AggregationPredicate b (UnpreparedValue b)]
_ -> ErrorMessage -> n (AggregationPredicate b (UnpreparedValue b))
forall (m :: * -> *) a. MonadParse m => ErrorMessage -> m a
P.parseError ErrorMessage
"exactly one predicate should be specified"
          )

    -- Input field for a single aggregation predicate.
    aggPredicateField ::
      G.Name ->
      C.GQLNameIdentifier ->
      NamingCase ->
      InputFieldsParser n (AggregationPredicate b (UnpreparedValue b)) ->
      InputFieldsParser n (Maybe (AggregationPredicate b (UnpreparedValue b)))
    aggPredicateField :: Name
-> GQLNameIdentifier
-> NamingCase
-> InputFieldsParser n (AggregationPredicate b (UnpreparedValue b))
-> InputFieldsParser
     n (Maybe (AggregationPredicate b (UnpreparedValue b)))
aggPredicateField Name
fnGQLName GQLNameIdentifier
typeGqlName NamingCase
tCase =
      Name
-> Maybe Description
-> Parser
     MetadataObjId 'Input n (AggregationPredicate b (UnpreparedValue b))
-> InputFieldsParser
     n (Maybe (AggregationPredicate 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
fnGQLName Maybe Description
forall a. Maybe a
Nothing (Parser
   MetadataObjId 'Input n (AggregationPredicate b (UnpreparedValue b))
 -> InputFieldsParser
      n (Maybe (AggregationPredicate b (UnpreparedValue b))))
-> (InputFieldsParser
      n (AggregationPredicate b (UnpreparedValue b))
    -> Parser
         MetadataObjId
         'Input
         n
         (AggregationPredicate b (UnpreparedValue b)))
-> InputFieldsParser n (AggregationPredicate b (UnpreparedValue b))
-> InputFieldsParser
     n (Maybe (AggregationPredicate b (UnpreparedValue b)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name
-> Maybe Description
-> InputFieldsParser n (AggregationPredicate b (UnpreparedValue b))
-> Parser
     MetadataObjId 'Input n (AggregationPredicate b (UnpreparedValue b))
forall (m :: * -> *) origin a.
MonadParse m =>
Name
-> Maybe Description
-> InputFieldsParser origin m a
-> Parser origin 'Input m a
P.object (NamingCase -> GQLNameIdentifier -> Name
applyFieldNameCaseIdentifier NamingCase
tCase (GQLNameIdentifier
typeGqlName GQLNameIdentifier -> GQLNameIdentifier -> GQLNameIdentifier
forall a. Semigroup a => a -> a -> a
<> Name -> GQLNameIdentifier
C.fromCustomName Name
fnGQLName)) Maybe Description
forall a. Maybe a
Nothing

    -- Collect all non-failing branches of optional field parsers.
    -- Fails only when all branches fail.
    -- buildAnyOptionalFields ::
    --   Applicative f =>
    --   NonEmpty (MaybeT f (InputFieldsParser n (Maybe c))) ->
    --   MaybeT f (InputFieldsParser n [c])
    -- buildAnyOptionalFields = fmap collectOptionalFields . succeedingBranchesNE
    --   where

    -- Collect all the non-failed branches, failing if all branches failed.
    succeedingBranchesNE :: forall f a. (Applicative f) => NonEmpty (MaybeT f a) -> MaybeT f (NonEmpty a)
    succeedingBranchesNE :: forall (f :: * -> *) a.
Applicative f =>
NonEmpty (MaybeT f a) -> MaybeT f (NonEmpty a)
succeedingBranchesNE NonEmpty (MaybeT f a)
xs = f (Maybe (NonEmpty a)) -> MaybeT f (NonEmpty a)
forall (f :: * -> *) a. f (Maybe a) -> MaybeT f a
MaybeT (f (Maybe (NonEmpty a)) -> MaybeT f (NonEmpty a))
-> f (Maybe (NonEmpty a)) -> MaybeT f (NonEmpty a)
forall a b. (a -> b) -> a -> b
$ [a] -> Maybe (NonEmpty a)
forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty ([a] -> Maybe (NonEmpty a))
-> (NonEmpty (Maybe a) -> [a])
-> NonEmpty (Maybe a)
-> Maybe (NonEmpty a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe a] -> [a]
forall a. [Maybe a] -> [a]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes ([Maybe a] -> [a])
-> (NonEmpty (Maybe a) -> [Maybe a]) -> NonEmpty (Maybe a) -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (Maybe a) -> [Maybe a]
forall a. NonEmpty a -> [a]
NE.toList (NonEmpty (Maybe a) -> Maybe (NonEmpty a))
-> f (NonEmpty (Maybe a)) -> f (Maybe (NonEmpty a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty (f (Maybe a)) -> f (NonEmpty (Maybe a))
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
NonEmpty (f a) -> f (NonEmpty a)
sequenceA (NonEmpty (MaybeT f a)
xs NonEmpty (MaybeT f a)
-> (MaybeT f a -> f (Maybe a)) -> NonEmpty (f (Maybe a))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> MaybeT f a -> f (Maybe a)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT)

    -- Collect a non-empty list of input field parsers into one input field
    -- parser parsing a non-empty list of the specified values.
    collectFieldsNE ::
      (Functor f) =>
      MaybeT f (NonEmpty (InputFieldsParser n c)) ->
      MaybeT f (InputFieldsParser n (NonEmpty c))
    collectFieldsNE :: forall (f :: * -> *) c.
Functor f =>
MaybeT f (NonEmpty (InputFieldsParser n c))
-> MaybeT f (InputFieldsParser n (NonEmpty c))
collectFieldsNE = (NonEmpty (InputFieldsParser n c)
 -> InputFieldsParser n (NonEmpty c))
-> MaybeT f (NonEmpty (InputFieldsParser n c))
-> MaybeT f (InputFieldsParser n (NonEmpty c))
forall a b. (a -> b) -> MaybeT f a -> MaybeT f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NonEmpty (InputFieldsParser n c)
-> InputFieldsParser n (NonEmpty c)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
NonEmpty (f a) -> f (NonEmpty a)
sequenceA

    -- Collect a non-empty list of optional input field parsers into one input field
    -- parser parsing a list of the specified values.
    collectOptionalFieldsNE ::
      (Functor f) =>
      MaybeT f (NonEmpty (InputFieldsParser n (Maybe a))) ->
      MaybeT f (InputFieldsParser n [a])
    collectOptionalFieldsNE :: forall (f :: * -> *) a.
Functor f =>
MaybeT f (NonEmpty (InputFieldsParser n (Maybe a)))
-> MaybeT f (InputFieldsParser n [a])
collectOptionalFieldsNE = (NonEmpty (InputFieldsParser n (Maybe a))
 -> InputFieldsParser n [a])
-> MaybeT f (NonEmpty (InputFieldsParser n (Maybe a)))
-> MaybeT f (InputFieldsParser n [a])
forall a b. (a -> b) -> MaybeT f a -> MaybeT f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((NonEmpty (InputFieldsParser n (Maybe a))
  -> InputFieldsParser n [a])
 -> MaybeT f (NonEmpty (InputFieldsParser n (Maybe a)))
 -> MaybeT f (InputFieldsParser n [a]))
-> (NonEmpty (InputFieldsParser n (Maybe a))
    -> InputFieldsParser n [a])
-> MaybeT f (NonEmpty (InputFieldsParser n (Maybe a)))
-> MaybeT f (InputFieldsParser n [a])
forall a b. (a -> b) -> a -> b
$ (NonEmpty (Maybe a) -> [a])
-> InputFieldsParser MetadataObjId n (NonEmpty (Maybe a))
-> InputFieldsParser n [a]
forall a b.
(a -> b)
-> InputFieldsParser MetadataObjId n a
-> InputFieldsParser MetadataObjId n b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([Maybe a] -> [a]
forall a. [Maybe a] -> [a]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes ([Maybe a] -> [a])
-> (NonEmpty (Maybe a) -> [Maybe a]) -> NonEmpty (Maybe a) -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (Maybe a) -> [Maybe a]
forall a. NonEmpty a -> [a]
NE.toList) (InputFieldsParser MetadataObjId n (NonEmpty (Maybe a))
 -> InputFieldsParser n [a])
-> (NonEmpty (InputFieldsParser n (Maybe a))
    -> InputFieldsParser MetadataObjId n (NonEmpty (Maybe a)))
-> NonEmpty (InputFieldsParser n (Maybe a))
-> InputFieldsParser n [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (InputFieldsParser n (Maybe a))
-> InputFieldsParser MetadataObjId n (NonEmpty (Maybe a))
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
NonEmpty (f a) -> f (NonEmpty a)
sequenceA

    -- Mark a computation as potentially failing.
    fails :: f (Maybe a) -> MaybeT f a
    fails :: forall (f :: * -> *) a. f (Maybe a) -> MaybeT f a
fails = f (Maybe a) -> MaybeT f a
forall (f :: * -> *) a. f (Maybe a) -> MaybeT f a
MaybeT

    -- Compose our monad with InputFieldsParser into one fused Applicative that
    -- acts on the parsed values directly.
    fuse :: MaybeT f (InputFieldsParser n a) -> Compose (MaybeT f) (InputFieldsParser n) a
    fuse :: forall (f :: * -> *) a.
MaybeT f (InputFieldsParser n a)
-> Compose (MaybeT f) (InputFieldsParser MetadataObjId n) a
fuse = MaybeT f (InputFieldsParser MetadataObjId n a)
-> Compose (MaybeT f) (InputFieldsParser MetadataObjId n) a
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose

    -- The inverse of 'fuse'.
    unfuse :: Compose (MaybeT f) (InputFieldsParser n) a -> MaybeT f (InputFieldsParser n a)
    unfuse :: forall (f :: * -> *) a.
Compose (MaybeT f) (InputFieldsParser MetadataObjId n) a
-> MaybeT f (InputFieldsParser n a)
unfuse = Compose (MaybeT f) (InputFieldsParser MetadataObjId n) a
-> MaybeT f (InputFieldsParser n a)
forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose

    -- Optional input field with a default value when the field is elided or null.
    fieldOptionalDefault ::
      forall k a. ('Input P.<: k) => G.Name -> Maybe G.Description -> a -> Parser k n a -> InputFieldsParser n a
    fieldOptionalDefault :: forall (k :: Kind) a.
('Input <: k) =>
Name
-> Maybe Description -> a -> Parser k n a -> InputFieldsParser n a
fieldOptionalDefault Name
n Maybe Description
d a
a Parser k n a
p = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
a (Maybe a -> a)
-> InputFieldsParser MetadataObjId n (Maybe a)
-> InputFieldsParser MetadataObjId n a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name
-> Maybe Description
-> Parser k n a
-> InputFieldsParser MetadataObjId n (Maybe a)
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
n Maybe Description
d Parser k n a
p

data FunctionSignature (b :: BackendType) = FunctionSignature
  { forall (b :: BackendType). FunctionSignature b -> Text
fnName :: Text,
    forall (b :: BackendType). FunctionSignature b -> Name
fnGQLName :: G.Name,
    forall (b :: BackendType).
FunctionSignature b -> ArgumentsSignature b
fnArguments :: ArgumentsSignature b,
    forall (b :: BackendType). FunctionSignature b -> ScalarType b
fnReturnType :: B.ScalarType b
  }

data ArgumentsSignature (b :: BackendType)
  = ArgumentsStar
  | SingleArgument (B.ScalarType b)
  | Arguments (NonEmpty (ArgumentSignature b))

data ArgumentSignature (b :: BackendType) = ArgumentSignature
  { forall (b :: BackendType). ArgumentSignature b -> ScalarType b
argType :: B.ScalarType b,
    forall (b :: BackendType). ArgumentSignature b -> Name
argName :: G.Name
  }