2022-08-22 18:57:46 +03:00
|
|
|
{-# 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.List.NonEmpty qualified as NE
|
|
|
|
import Hasura.GraphQL.Parser qualified as P
|
|
|
|
import Hasura.GraphQL.Schema.Backend
|
|
|
|
import Hasura.GraphQL.Schema.BoolExp
|
2022-09-06 19:48:04 +03:00
|
|
|
import Hasura.GraphQL.Schema.Common
|
2022-08-22 18:57:46 +03:00
|
|
|
import Hasura.GraphQL.Schema.Parser
|
|
|
|
( InputFieldsParser,
|
|
|
|
Kind (..),
|
|
|
|
Parser,
|
|
|
|
)
|
|
|
|
import Hasura.GraphQL.Schema.Table
|
|
|
|
import Hasura.Name qualified as Name
|
|
|
|
import Hasura.Prelude
|
2022-09-22 14:13:49 +03:00
|
|
|
import Hasura.RQL.IR qualified as IR
|
2022-08-22 18:57:46 +03:00
|
|
|
import Hasura.RQL.IR.BoolExp.AggregationPredicates
|
|
|
|
import Hasura.RQL.IR.Value
|
|
|
|
import Hasura.RQL.Types.Backend qualified as B
|
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.Common (relNameToTxt)
|
|
|
|
import Hasura.RQL.Types.Relationships.Local
|
|
|
|
import Hasura.RQL.Types.SchemaCache hiding (askTableInfo)
|
|
|
|
import Hasura.RQL.Types.Source
|
|
|
|
import Hasura.RQL.Types.Table
|
|
|
|
import Hasura.SQL.Backend (BackendType)
|
|
|
|
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.
|
2022-09-06 19:48:04 +03:00
|
|
|
( MonadBuildSchema b r m n,
|
2022-08-22 18:57:46 +03:00
|
|
|
AggregationPredicatesSchema b
|
|
|
|
) =>
|
|
|
|
[FunctionSignature b] ->
|
|
|
|
SourceInfo b ->
|
|
|
|
TableInfo b ->
|
2022-09-06 19:48:04 +03:00
|
|
|
SchemaT r m (Maybe (InputFieldsParser n [AggregationPredicatesImplementation b (UnpreparedValue b)]))
|
2022-08-22 18:57:46 +03:00
|
|
|
defaultAggregationPredicatesParser aggFns si ti = runMaybeT do
|
|
|
|
arrayRelationships <- fails $ return $ nonEmpty $ tableArrayRelationships ti
|
|
|
|
aggregationFunctions <- fails $ return $ nonEmpty aggFns
|
2022-09-22 14:13:49 +03:00
|
|
|
roleName <- retrieve scRole
|
2022-08-22 18:57:46 +03:00
|
|
|
|
2022-09-23 16:48:38 +03:00
|
|
|
collectOptionalFieldsNE . succeedingBranchesNE $
|
2022-08-22 18:57:46 +03:00
|
|
|
arrayRelationships <&> \rel -> do
|
|
|
|
relTable <- askTableInfo si (riRTable rel)
|
2022-09-22 14:13:49 +03:00
|
|
|
selectPermissions <- hoistMaybe $ tableSelectPermissions roleName relTable
|
2022-09-22 15:51:15 +03:00
|
|
|
guard $ spiAllowAgg selectPermissions
|
2022-09-22 14:13:49 +03:00
|
|
|
let rowPermissions = fmap partialSQLExpToUnpreparedValue <$> spiFilter selectPermissions
|
2022-08-22 18:57:46 +03:00
|
|
|
relGqlName <- textToName $ relNameToTxt $ riName rel
|
2022-09-07 15:09:41 +03:00
|
|
|
typeGqlName <- (<> Name.__ <> relGqlName <> Name.__ <> Name._aggregate) <$> getTableGQLName ti
|
2022-08-22 18:57:46 +03:00
|
|
|
|
|
|
|
-- We only make a field for aggregations over a relation if at least
|
|
|
|
-- some aggregation predicates are callable.
|
2022-09-22 14:13:49 +03:00
|
|
|
relAggregateField rel relGqlName typeGqlName rowPermissions
|
2022-08-22 18:57:46 +03:00
|
|
|
-- We only return an InputFieldsParser for aggregation predicates,
|
|
|
|
-- if we parse at least one aggregation predicate
|
2022-09-23 16:48:38 +03:00
|
|
|
<$> (collectOptionalFieldsNE . succeedingBranchesNE)
|
2022-08-22 18:57:46 +03:00
|
|
|
( aggregationFunctions <&> \FunctionSignature {..} -> do
|
2022-09-30 12:35:21 +03:00
|
|
|
let relFunGqlName = typeGqlName <> Name.__ <> fnGQLName <> Name.__ <> Name._arguments <> Name.__ <> Name._columns
|
2022-08-22 18:57:46 +03:00
|
|
|
aggPredicateField fnGQLName typeGqlName <$> unfuse do
|
|
|
|
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 fnArguments of
|
|
|
|
ArgumentsStar ->
|
|
|
|
maybe AggregationPredicateArgumentsStar AggregationPredicateArguments . nonEmpty
|
|
|
|
<$> fuse (fieldOptionalDefault Name._arguments Nothing [] . P.list <$> fails (tableSelectColumnsEnum si relTable))
|
2022-09-23 16:48:38 +03:00
|
|
|
SingleArgument typ ->
|
|
|
|
AggregationPredicateArguments . (NE.:| [])
|
|
|
|
<$> fuse
|
|
|
|
( P.field Name._arguments Nothing
|
2022-09-30 12:35:21 +03:00
|
|
|
<$> fails (tableSelectColumnsPredEnum (== (ColumnScalar typ)) relFunGqlName si relTable)
|
2022-09-23 16:48:38 +03:00
|
|
|
)
|
2022-08-22 18:57:46 +03:00
|
|
|
Arguments args ->
|
|
|
|
AggregationPredicateArguments
|
|
|
|
<$> fuse
|
|
|
|
( P.field Name._arguments Nothing
|
|
|
|
. P.object (typeGqlName <> Name.__ <> fnGQLName <> Name.__ <> Name._arguments) Nothing
|
2022-09-23 16:48:38 +03:00
|
|
|
<$> collectFieldsNE
|
2022-08-22 18:57:46 +03:00
|
|
|
( args `for` \ArgumentSignature {..} ->
|
2022-09-30 12:35:21 +03:00
|
|
|
P.field argName Nothing <$> fails (tableSelectColumnsPredEnum (== (ColumnScalar argType)) relFunGqlName si relTable)
|
2022-08-22 18:57:46 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
aggPredDistinct <- fuse $ return $ fieldOptionalDefault Name._distinct Nothing False P.boolean
|
|
|
|
let aggPredFunctionName = fnName
|
2022-09-06 07:24:46 +03:00
|
|
|
aggPredPredicate <- fuse $ P.field Name._predicate Nothing <$> lift (comparisonExps @b si (ColumnScalar fnReturnType))
|
2022-08-22 18:57:46 +03:00
|
|
|
aggPredFilter <- fuse $ P.fieldOptional Name._filter Nothing <$> lift (boolExp si relTable)
|
|
|
|
pure $ AggregationPredicate {..}
|
|
|
|
)
|
|
|
|
where
|
|
|
|
-- Input field of the aggregation predicates for one array relation.
|
|
|
|
relAggregateField ::
|
|
|
|
RelInfo b ->
|
|
|
|
G.Name ->
|
|
|
|
G.Name ->
|
2022-09-22 14:13:49 +03:00
|
|
|
(IR.AnnBoolExp b (UnpreparedValue b)) ->
|
2022-08-22 18:57:46 +03:00
|
|
|
(InputFieldsParser n [AggregationPredicate b (UnpreparedValue b)]) ->
|
|
|
|
(InputFieldsParser n (Maybe (AggregationPredicatesImplementation b (UnpreparedValue b))))
|
2022-09-22 14:13:49 +03:00
|
|
|
relAggregateField rel relGqlName typeGqlName rowPermissions =
|
2022-08-22 18:57:46 +03:00
|
|
|
P.fieldOptional (relGqlName <> Name.__ <> Name._aggregate) Nothing
|
|
|
|
. P.object typeGqlName Nothing
|
2022-09-22 14:13:49 +03:00
|
|
|
. fmap (AggregationPredicatesImplementation rel rowPermissions)
|
2022-09-16 17:59:23 +03:00
|
|
|
. ( `P.bindFields`
|
|
|
|
\case
|
|
|
|
[predicate] -> pure predicate
|
|
|
|
_ -> P.parseError "exactly one predicate should be specified"
|
|
|
|
)
|
2022-08-22 18:57:46 +03:00
|
|
|
|
|
|
|
-- Input field for a single aggregation predicate.
|
|
|
|
aggPredicateField ::
|
|
|
|
G.Name ->
|
|
|
|
G.Name ->
|
|
|
|
InputFieldsParser n (AggregationPredicate b (UnpreparedValue b)) ->
|
|
|
|
InputFieldsParser n (Maybe (AggregationPredicate b (UnpreparedValue b)))
|
|
|
|
aggPredicateField fnGQLName typeGqlName =
|
|
|
|
P.fieldOptional fnGQLName Nothing . P.object (typeGqlName <> Name.__ <> fnGQLName) Nothing
|
|
|
|
|
2022-09-23 16:48:38 +03:00
|
|
|
-- 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 xs = MaybeT $ NE.nonEmpty . catMaybes . NE.toList <$> sequenceA (xs <&> 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 ::
|
2022-09-06 19:48:04 +03:00
|
|
|
Functor f =>
|
|
|
|
MaybeT f (NonEmpty (InputFieldsParser n c)) ->
|
|
|
|
MaybeT f (InputFieldsParser n (NonEmpty c))
|
2022-09-23 16:48:38 +03:00
|
|
|
collectFieldsNE = fmap sequenceA
|
2022-08-22 18:57:46 +03:00
|
|
|
|
2022-09-23 16:48:38 +03:00
|
|
|
-- 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 = fmap $ fmap (catMaybes . NE.toList) . sequenceA
|
2022-08-22 18:57:46 +03:00
|
|
|
|
|
|
|
-- Mark a computation as potentially failing.
|
2022-09-06 19:48:04 +03:00
|
|
|
fails :: f (Maybe a) -> MaybeT f a
|
2022-08-22 18:57:46 +03:00
|
|
|
fails = MaybeT
|
|
|
|
|
|
|
|
-- Compose our monad with InputFieldsParser into one fused Applicative that
|
|
|
|
-- acts on the parsed values directly.
|
2022-09-06 19:48:04 +03:00
|
|
|
fuse :: MaybeT f (InputFieldsParser n a) -> Compose (MaybeT f) (InputFieldsParser n) a
|
2022-08-22 18:57:46 +03:00
|
|
|
fuse = Compose
|
|
|
|
|
|
|
|
-- The inverse of 'fuse'.
|
2022-09-06 19:48:04 +03:00
|
|
|
unfuse :: Compose (MaybeT f) (InputFieldsParser n) a -> MaybeT f (InputFieldsParser n a)
|
2022-08-22 18:57:46 +03:00
|
|
|
unfuse = 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 n d a p = fromMaybe a <$> P.fieldOptional n d p
|
|
|
|
|
|
|
|
data FunctionSignature (b :: BackendType) = FunctionSignature
|
|
|
|
{ fnName :: Text,
|
|
|
|
fnGQLName :: G.Name,
|
|
|
|
fnArguments :: ArgumentsSignature b,
|
|
|
|
fnReturnType :: B.ScalarType b
|
|
|
|
}
|
|
|
|
|
|
|
|
data ArgumentsSignature (b :: BackendType)
|
|
|
|
= ArgumentsStar
|
2022-09-23 16:48:38 +03:00
|
|
|
| SingleArgument (B.ScalarType b)
|
2022-08-22 18:57:46 +03:00
|
|
|
| Arguments (NonEmpty (ArgumentSignature b))
|
|
|
|
|
|
|
|
data ArgumentSignature (b :: BackendType) = ArgumentSignature
|
|
|
|
{ argType :: B.ScalarType b,
|
|
|
|
argName :: G.Name
|
|
|
|
}
|