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

-- | Schema parsers for logical models
module Hasura.LogicalModel.Schema
  ( getSelPermInfoForLogicalModel,
    buildLogicalModelIR,
    buildLogicalModelPermissions,
    logicalModelSelectionList,
    defaultLogicalModelArgs,
    defaultLogicalModelSelectionSet,
    logicalModelFieldParsers,
  )
where

import Data.HashMap.Strict qualified as HashMap
import Data.HashMap.Strict.InsOrd qualified as InsOrdHashMap
import Data.List.NonEmpty qualified as NE
import Data.Set qualified as S
import Data.Text.Extended
import Data.Text.NonEmpty (mkNonEmptyText)
import Hasura.Base.Error
import Hasura.GraphQL.Parser.Internal.Parser qualified as IP
import Hasura.GraphQL.Schema.Backend
  ( BackendLogicalModelSelectSchema (..),
    MonadBuildSchema,
    columnParser,
    scalarSelectionArgumentsParser,
  )
import Hasura.GraphQL.Schema.BoolExp
import Hasura.GraphQL.Schema.Common
  ( AnnotatedField,
    AnnotatedFields,
    SchemaT,
    SelectArgs,
    getRedactionExprForColumn,
    parsedSelectionsToFields,
    partialSQLExpToUnpreparedValue,
    retrieve,
    scRole,
  )
import Hasura.GraphQL.Schema.OrderBy
import Hasura.GraphQL.Schema.Parser
  ( InputFieldsParser,
    Kind (..),
    Parser,
  )
import Hasura.GraphQL.Schema.Parser qualified as P
import Hasura.GraphQL.Schema.Select (defaultArgsParser)
import Hasura.LogicalModel.Cache (LogicalModelInfo (..))
import Hasura.LogicalModel.Common (getSelPermInfoForLogicalModel)
import Hasura.LogicalModel.IR (LogicalModel (..))
import Hasura.LogicalModel.Types
  ( LogicalModelField (..),
    LogicalModelName (..),
    LogicalModelType (..),
    LogicalModelTypeArray (..),
    LogicalModelTypeReference (..),
    LogicalModelTypeScalar (..),
  )
import Hasura.Name qualified as Name
import Hasura.Prelude
import Hasura.RQL.IR qualified as IR
import Hasura.RQL.Types.Backend (Backend, Column)
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.Common (RelName (..))
import Hasura.RQL.Types.Metadata.Object
import Hasura.RQL.Types.Permission qualified as Permission
import Hasura.RQL.Types.Relationships.Local (Nullable (..))
import Hasura.RQL.Types.Roles (RoleName, adminRoleName)
import Hasura.RQL.Types.Source
import Hasura.RQL.Types.SourceCustomization
import Hasura.Table.Cache (SelPermInfo (..), _permSel)
import Language.GraphQL.Draft.Syntax qualified as G

-- | build select permissions for logical model
logicalModelPermissions ::
  (Backend b) =>
  LogicalModelInfo b ->
  RoleName ->
  Maybe (IR.TablePermG b (IR.UnpreparedValue b))
logicalModelPermissions :: forall (b :: BackendType).
Backend b =>
LogicalModelInfo b
-> RoleName -> Maybe (TablePermG b (UnpreparedValue b))
logicalModelPermissions LogicalModelInfo b
logicalModel RoleName
roleName = do
  RoleName -> LogicalModelInfo b -> Maybe (SelPermInfo b)
forall (b :: BackendType).
Backend b =>
RoleName -> LogicalModelInfo b -> Maybe (SelPermInfo b)
getSelPermInfoForLogicalModel RoleName
roleName LogicalModelInfo b
logicalModel Maybe (SelPermInfo b)
-> (SelPermInfo b -> TablePermG b (UnpreparedValue b))
-> Maybe (TablePermG b (UnpreparedValue b))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \SelPermInfo b
selectPermissions ->
    IR.TablePerm
      { $sel:_tpFilter:TablePerm :: AnnBoolExp b (UnpreparedValue b)
IR._tpFilter = (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))
-> AnnBoolExp 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,
        $sel:_tpLimit:TablePerm :: Maybe Int
IR._tpLimit = SelPermInfo b -> Maybe Int
forall (b :: BackendType). SelPermInfo b -> Maybe Int
spiLimit SelPermInfo b
selectPermissions
      }

-- | turn post-schema cache LogicalModelInfo into IR
buildLogicalModelIR :: LogicalModelInfo b -> LogicalModel b
buildLogicalModelIR :: forall (b :: BackendType). LogicalModelInfo b -> LogicalModel b
buildLogicalModelIR LogicalModelInfo {Maybe Text
RolePermInfoMap b
InsOrdHashMap (Column b) (LogicalModelField b)
LogicalModelName
_lmiName :: LogicalModelName
_lmiFields :: InsOrdHashMap (Column b) (LogicalModelField b)
_lmiDescription :: Maybe Text
_lmiPermissions :: RolePermInfoMap b
_lmiName :: forall (b :: BackendType). LogicalModelInfo b -> LogicalModelName
_lmiFields :: forall (b :: BackendType).
LogicalModelInfo b
-> InsOrdHashMap (Column b) (LogicalModelField b)
_lmiDescription :: forall (b :: BackendType). LogicalModelInfo b -> Maybe Text
_lmiPermissions :: forall (b :: BackendType). LogicalModelInfo b -> RolePermInfoMap b
..} =
  LogicalModel
    { lmName :: LogicalModelName
lmName = LogicalModelName
_lmiName,
      lmFields :: InsOrdHashMap (Column b) (LogicalModelField b)
lmFields = InsOrdHashMap (Column b) (LogicalModelField b)
_lmiFields
    }

-- | top-level select permissions for a logical model
buildLogicalModelPermissions ::
  forall b r m n.
  ( MonadBuildSchema b r m n
  ) =>
  LogicalModelInfo b ->
  SchemaT r m (Maybe (IR.TablePermG b (IR.UnpreparedValue b)))
buildLogicalModelPermissions :: forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
LogicalModelInfo b
-> SchemaT r m (Maybe (TablePermG b (UnpreparedValue b)))
buildLogicalModelPermissions LogicalModelInfo b
logicalModel = do
  RoleName
roleName <- (SchemaContext -> RoleName) -> SchemaT r m RoleName
forall r (m :: * -> *) a b.
(MonadReader r m, Has a r) =>
(a -> b) -> m b
retrieve SchemaContext -> RoleName
scRole

  pure $ LogicalModelInfo b
-> RoleName -> Maybe (TablePermG b (UnpreparedValue b))
forall (b :: BackendType).
Backend b =>
LogicalModelInfo b
-> RoleName -> Maybe (TablePermG b (UnpreparedValue b))
logicalModelPermissions LogicalModelInfo b
logicalModel RoleName
roleName

logicalModelColumnsForRole ::
  RoleName ->
  LogicalModelInfo b ->
  Maybe (Permission.PermColSpec b)
logicalModelColumnsForRole :: forall (b :: BackendType).
RoleName -> LogicalModelInfo b -> Maybe (PermColSpec b)
logicalModelColumnsForRole RoleName
role LogicalModelInfo b
logicalModel =
  if RoleName
role RoleName -> RoleName -> Bool
forall a. Eq a => a -> a -> Bool
== RoleName
adminRoleName
    then -- if admin, assume all columns are OK
      PermColSpec b -> Maybe (PermColSpec b)
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PermColSpec b
forall (b :: BackendType). PermColSpec b
Permission.PCStar
    else -- find list of columns we're allowed to access for this role

      RoleName
-> HashMap RoleName (RolePermInfo b) -> Maybe (RolePermInfo b)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup RoleName
role (LogicalModelInfo b -> HashMap RoleName (RolePermInfo b)
forall (b :: BackendType). LogicalModelInfo b -> RolePermInfoMap b
_lmiPermissions LogicalModelInfo b
logicalModel)
        Maybe (RolePermInfo b)
-> (RolePermInfo b -> Maybe (SelPermInfo b))
-> Maybe (SelPermInfo b)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= RolePermInfo b -> Maybe (SelPermInfo b)
forall (b :: BackendType). RolePermInfo b -> Maybe (SelPermInfo b)
_permSel
        Maybe (SelPermInfo b)
-> (SelPermInfo b -> PermColSpec b) -> Maybe (PermColSpec b)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> [Column b] -> PermColSpec b
forall (b :: BackendType). [Column b] -> PermColSpec b
Permission.PCCols
        ([Column b] -> PermColSpec b)
-> (SelPermInfo b -> [Column b]) -> SelPermInfo b -> PermColSpec b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap (Column b) (AnnRedactionExpPartialSQL b) -> [Column b]
forall k v. HashMap k v -> [k]
HashMap.keys
        (HashMap (Column b) (AnnRedactionExpPartialSQL b) -> [Column b])
-> (SelPermInfo b
    -> HashMap (Column b) (AnnRedactionExpPartialSQL b))
-> SelPermInfo b
-> [Column b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SelPermInfo b -> HashMap (Column b) (AnnRedactionExpPartialSQL b)
forall (b :: BackendType).
SelPermInfo b -> HashMap (Column b) (AnnRedactionExpPartialSQL b)
spiCols

-- just the field parsers themselves
-- we use them (plus relationships etc) to make the Native Query / Stored
-- Procedure types
-- for now this is not memoized so please don't call it from
-- `parseLogicalModelField`, and instead call a top-level parser
logicalModelFieldParsers ::
  forall b r m n.
  ( MonadBuildSchema b r m n
  ) =>
  S.Set RelName ->
  LogicalModelInfo b ->
  SchemaT r m [(IP.FieldParser MetadataObjId n (AnnotatedField b))]
logicalModelFieldParsers :: forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
Set RelName
-> LogicalModelInfo b
-> SchemaT r m [FieldParser MetadataObjId n (AnnotatedField b)]
logicalModelFieldParsers Set RelName
knownRelNames LogicalModelInfo b
logicalModel = do
  RoleName
roleName <- (SchemaContext -> RoleName) -> SchemaT r m RoleName
forall r (m :: * -> *) a b.
(MonadReader r m, Has a r) =>
(a -> b) -> m b
retrieve SchemaContext -> RoleName
scRole

  let selectableColumns :: PermColSpec b
selectableColumns =
        PermColSpec b -> Maybe (PermColSpec b) -> PermColSpec b
forall a. a -> Maybe a -> a
fromMaybe
          ([Column b] -> PermColSpec b
forall (b :: BackendType). [Column b] -> PermColSpec b
Permission.PCCols [Column b]
forall a. Monoid a => a
mempty)
          (RoleName -> LogicalModelInfo b -> Maybe (PermColSpec b)
forall (b :: BackendType).
RoleName -> LogicalModelInfo b -> Maybe (PermColSpec b)
logicalModelColumnsForRole RoleName
roleName LogicalModelInfo b
logicalModel)

  let isSelectable :: Column b -> Bool
isSelectable Column b
column =
        case PermColSpec b
selectableColumns of
          PermColSpec b
Permission.PCStar -> Bool
True
          Permission.PCCols [Column b]
cols -> Column b
column Column b -> [Column b] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Column b]
cols

  -- which columns are we allowed to access given permissions?
  let allowedColumns :: [(Column b, LogicalModelField b)]
allowedColumns =
        ((Column b, LogicalModelField b) -> Bool)
-> [(Column b, LogicalModelField b)]
-> [(Column b, LogicalModelField b)]
forall a. (a -> Bool) -> [a] -> [a]
filter
          (Column b -> Bool
isSelectable (Column b -> Bool)
-> ((Column b, LogicalModelField b) -> Column b)
-> (Column b, LogicalModelField b)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Column b, LogicalModelField b) -> Column b
forall a b. (a, b) -> a
fst)
          (InsOrdHashMap (Column b) (LogicalModelField b)
-> [(Column b, LogicalModelField b)]
forall k v. InsOrdHashMap k v -> [(k, v)]
InsOrdHashMap.toList (LogicalModelInfo b
-> InsOrdHashMap (Column b) (LogicalModelField b)
forall (b :: BackendType).
LogicalModelInfo b
-> InsOrdHashMap (Column b) (LogicalModelField b)
_lmiFields LogicalModelInfo b
logicalModel))

  -- we filter out parsers that return 'Nothing' as those we are no permitted to see.
  [Maybe (FieldParser MetadataObjId n (AnnotatedField b))]
-> [FieldParser MetadataObjId n (AnnotatedField b)]
forall a. [Maybe a] -> [a]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes ([Maybe (FieldParser MetadataObjId n (AnnotatedField b))]
 -> [FieldParser MetadataObjId n (AnnotatedField b)])
-> SchemaT
     r m [Maybe (FieldParser MetadataObjId n (AnnotatedField b))]
-> SchemaT r m [FieldParser MetadataObjId n (AnnotatedField b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Column b, LogicalModelField b)
 -> SchemaT
      r m (Maybe (FieldParser MetadataObjId n (AnnotatedField b))))
-> [(Column b, LogicalModelField b)]
-> SchemaT
     r m [Maybe (FieldParser MetadataObjId n (AnnotatedField b))]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse ((Column b
 -> LogicalModelField b
 -> SchemaT
      r m (Maybe (FieldParser MetadataObjId n (AnnotatedField b))))
-> (Column b, LogicalModelField b)
-> SchemaT
     r m (Maybe (FieldParser MetadataObjId n (AnnotatedField b)))
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Set RelName
-> Column b
-> LogicalModelField b
-> SchemaT
     r m (Maybe (FieldParser MetadataObjId n (AnnotatedField b)))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
Set RelName
-> Column b
-> LogicalModelField b
-> SchemaT
     r m (Maybe (FieldParser MetadataObjId n (AnnotatedField b)))
parseLogicalModelField Set RelName
knownRelNames)) [(Column b, LogicalModelField b)]
allowedColumns

-- | this seems like it works on luck, ie that everything is really just Text
-- underneath
columnToRelName :: forall b. (Backend b) => Column b -> Maybe RelName
columnToRelName :: forall (b :: BackendType). Backend b => Column b -> Maybe RelName
columnToRelName Column b
column =
  NonEmptyText -> RelName
RelName (NonEmptyText -> RelName) -> Maybe NonEmptyText -> Maybe RelName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe NonEmptyText
mkNonEmptyText (Column b -> Text
forall a. ToTxt a => a -> Text
toTxt Column b
column)

-- | parse a single logical model field. Currently the only way to 'fulfil' a
-- non-scalar field is with a relationship that provides the nested
-- object/array information.
parseLogicalModelField ::
  forall b r m n.
  ( MonadBuildSchema b r m n
  ) =>
  S.Set RelName ->
  Column b ->
  LogicalModelField b ->
  SchemaT r m (Maybe (IP.FieldParser MetadataObjId n (AnnotatedField b)))
parseLogicalModelField :: forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
Set RelName
-> Column b
-> LogicalModelField b
-> SchemaT
     r m (Maybe (FieldParser MetadataObjId n (AnnotatedField b)))
parseLogicalModelField Set RelName
knownRelNames Column b
column LogicalModelField b
logimoField = MaybeT
  (SchemaT r m) (FieldParser MetadataObjId n (AnnotatedField b))
-> SchemaT
     r m (Maybe (FieldParser MetadataObjId n (AnnotatedField b)))
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT do
  case LogicalModelField b
logimoField of
    ( LogicalModelField
        { Maybe Text
lmfDescription :: Maybe Text
lmfDescription :: forall (b :: BackendType). LogicalModelField b -> Maybe Text
lmfDescription,
          lmfType :: forall (b :: BackendType).
LogicalModelField b -> LogicalModelType b
lmfType = LogicalModelTypeScalar (LogicalModelTypeScalarC {ScalarType b
lmtsScalar :: ScalarType b
lmtsScalar :: forall (b :: BackendType). LogicalModelTypeScalar b -> ScalarType b
lmtsScalar, Bool
lmtsNullable :: Bool
lmtsNullable :: forall (b :: BackendType). LogicalModelTypeScalar b -> Bool
lmtsNullable})
        }
      ) -> do
        Name
columnName <- Text -> Maybe Name
G.mkName (Column b -> Text
forall a. ToTxt a => a -> Text
toTxt Column b
column) Maybe Name
-> MaybeT (SchemaT r m) Name -> MaybeT (SchemaT r m) Name
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Text -> MaybeT (SchemaT r m) Name
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 (Column b
column Column b -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" is not a valid GraphQL name")

        -- We have not yet worked out what providing permissions here enables
        -- TODO(redactionExp): This should be drawn from SelPermInfo.spiCols
        let redactionExp :: AnnRedactionExp b v
redactionExp = AnnRedactionExp b v
forall (b :: BackendType) v. AnnRedactionExp b v
IR.NoRedaction
            columnType :: ColumnType b
columnType = ScalarType b -> ColumnType b
forall (b :: BackendType). ScalarType b -> ColumnType b
ColumnScalar ScalarType b
lmtsScalar
            pathArg :: InputFieldsParser n (Maybe (ScalarSelectionArguments b))
pathArg = ColumnType b
-> InputFieldsParser n (Maybe (ScalarSelectionArguments b))
forall (b :: BackendType) (n :: * -> *).
(BackendSchema b, MonadParse n) =>
ColumnType b
-> InputFieldsParser n (Maybe (ScalarSelectionArguments b))
forall (n :: * -> *).
MonadParse n =>
ColumnType b
-> InputFieldsParser n (Maybe (ScalarSelectionArguments b))
scalarSelectionArgumentsParser ColumnType b
columnType

        Parser 'Both n (ValueWithOrigin (ColumnValue b))
field <- SchemaT r m (Parser 'Both n (ValueWithOrigin (ColumnValue b)))
-> MaybeT
     (SchemaT r m) (Parser 'Both n (ValueWithOrigin (ColumnValue 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 (SchemaT r m (Parser 'Both n (ValueWithOrigin (ColumnValue b)))
 -> MaybeT
      (SchemaT r m) (Parser 'Both n (ValueWithOrigin (ColumnValue b))))
-> SchemaT r m (Parser 'Both n (ValueWithOrigin (ColumnValue b)))
-> MaybeT
     (SchemaT r m) (Parser 'Both n (ValueWithOrigin (ColumnValue b)))
forall a b. (a -> b) -> a -> b
$ ColumnType b
-> Nullability
-> SchemaT r m (Parser 'Both n (ValueWithOrigin (ColumnValue b)))
forall r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
ColumnType b
-> Nullability
-> SchemaT r m (Parser 'Both n (ValueWithOrigin (ColumnValue b)))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(BackendSchema b, MonadBuildSchema b r m n) =>
ColumnType b
-> Nullability
-> SchemaT r m (Parser 'Both n (ValueWithOrigin (ColumnValue b)))
columnParser ColumnType b
columnType (Bool -> Nullability
G.Nullability Bool
lmtsNullable)

        FieldParser MetadataObjId n (AnnotatedField b)
-> MaybeT
     (SchemaT r m) (FieldParser MetadataObjId n (AnnotatedField b))
forall a. a -> MaybeT (SchemaT r m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
          (FieldParser MetadataObjId n (AnnotatedField b)
 -> MaybeT
      (SchemaT r m) (FieldParser MetadataObjId n (AnnotatedField b)))
-> FieldParser MetadataObjId n (AnnotatedField b)
-> MaybeT
     (SchemaT r m) (FieldParser MetadataObjId n (AnnotatedField b))
forall a b. (a -> b) -> a -> b
$! Name
-> Maybe Description
-> InputFieldsParser n (Maybe (ScalarSelectionArguments b))
-> Parser 'Both n (ValueWithOrigin (ColumnValue b))
-> FieldParser MetadataObjId n (Maybe (ScalarSelectionArguments b))
forall (m :: * -> *) origin a b.
MonadParse m =>
Name
-> Maybe Description
-> InputFieldsParser origin m a
-> Parser origin 'Both m b
-> FieldParser origin m a
P.selection Name
columnName (Text -> Description
G.Description (Text -> Description) -> Maybe Text -> Maybe Description
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
lmfDescription) InputFieldsParser n (Maybe (ScalarSelectionArguments b))
pathArg Parser 'Both n (ValueWithOrigin (ColumnValue b))
field
          FieldParser MetadataObjId n (Maybe (ScalarSelectionArguments b))
-> (Maybe (ScalarSelectionArguments b) -> AnnotatedField b)
-> FieldParser MetadataObjId n (AnnotatedField b)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> Column b
-> ColumnType b
-> AnnRedactionExp b (UnpreparedValue b)
-> Maybe (ScalarSelectionArguments b)
-> AnnotatedField b
forall (backend :: BackendType) v r.
Column backend
-> ColumnType backend
-> AnnRedactionExp backend v
-> Maybe (ScalarSelectionArguments backend)
-> AnnFieldG backend r v
IR.mkAnnColumnField Column b
column ColumnType b
columnType AnnRedactionExp b (UnpreparedValue b)
redactionExp
    ( LogicalModelField
        { lmfType :: forall (b :: BackendType).
LogicalModelField b -> LogicalModelType b
lmfType =
            LogicalModelTypeReference
              (LogicalModelTypeReferenceC {})
        }
      ) -> do
        -- we have no use for logical model object fields atm, but we don't
        -- want to explode if they're set up for relationships (as that used to
        -- be allowed)
        RelName
relName <- forall (b :: BackendType). Backend b => Column b -> Maybe RelName
columnToRelName @b Column b
column Maybe RelName
-> MaybeT (SchemaT r m) RelName -> MaybeT (SchemaT r m) RelName
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Text -> MaybeT (SchemaT r m) RelName
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 (Column b
column Column b -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" is not a valid relationship name")

        if RelName -> Set RelName -> Bool
forall a. Ord a => a -> Set a -> Bool
S.member RelName
relName Set RelName
knownRelNames
          then Maybe (FieldParser MetadataObjId n (AnnotatedField b))
-> MaybeT
     (SchemaT r m) (FieldParser MetadataObjId n (AnnotatedField b))
forall (m :: * -> *) b. Applicative m => Maybe b -> MaybeT m b
hoistMaybe Maybe (FieldParser MetadataObjId n (AnnotatedField b))
forall a. Maybe a
Nothing -- a relationships exists with this name, gently ignore it to ensure backwards compat
          else
            Text
-> MaybeT
     (SchemaT r m) (FieldParser MetadataObjId n (AnnotatedField b))
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500
              ( Text
"Unexpected logical model object field '"
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RelName -> Text
forall a. ToTxt a => a -> Text
toTxt RelName
relName
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"'."
              )
    ( LogicalModelField
        { lmfType :: forall (b :: BackendType).
LogicalModelField b -> LogicalModelType b
lmfType =
            LogicalModelTypeArray
              ( LogicalModelTypeArrayC
                  { lmtaArray :: forall (b :: BackendType).
LogicalModelTypeArray b -> LogicalModelType b
lmtaArray =
                      LogicalModelTypeReference (LogicalModelTypeReferenceC {})
                  }
                )
        }
      ) -> do
        -- we have no use for logical model array fields atm, but we don't
        -- want to explode if they're set up for relationships (as that used to
        -- be allowed)
        RelName
relName <- forall (b :: BackendType). Backend b => Column b -> Maybe RelName
columnToRelName @b Column b
column Maybe RelName
-> MaybeT (SchemaT r m) RelName -> MaybeT (SchemaT r m) RelName
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Text -> MaybeT (SchemaT r m) RelName
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 (Column b
column Column b -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" is not a valid relationship name")

        if RelName -> Set RelName -> Bool
forall a. Ord a => a -> Set a -> Bool
S.member RelName
relName Set RelName
knownRelNames
          then Maybe (FieldParser MetadataObjId n (AnnotatedField b))
-> MaybeT
     (SchemaT r m) (FieldParser MetadataObjId n (AnnotatedField b))
forall (m :: * -> *) b. Applicative m => Maybe b -> MaybeT m b
hoistMaybe Maybe (FieldParser MetadataObjId n (AnnotatedField b))
forall a. Maybe a
Nothing -- a relationships exists with this name, gently ignore it to ensure backwards compat
          else
            Text
-> MaybeT
     (SchemaT r m) (FieldParser MetadataObjId n (AnnotatedField b))
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500
              ( Text
"Unexpected logical model array field '"
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RelName -> Text
forall a. ToTxt a => a -> Text
toTxt RelName
relName
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"'."
              )
    ( LogicalModelField
        { lmfType :: forall (b :: BackendType).
LogicalModelField b -> LogicalModelType b
lmfType =
            LogicalModelTypeArray
              (LogicalModelTypeArrayC {lmtaArray :: forall (b :: BackendType).
LogicalModelTypeArray b -> LogicalModelType b
lmtaArray = LogicalModelTypeScalar LogicalModelTypeScalar b
_})
        }
      ) ->
        Text
-> MaybeT
     (SchemaT r m) (FieldParser MetadataObjId n (AnnotatedField b))
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 Text
"Arrays of scalar types are not currently implemented"
    ( LogicalModelField
        { lmfType :: forall (b :: BackendType).
LogicalModelField b -> LogicalModelType b
lmfType =
            LogicalModelTypeArray
              (LogicalModelTypeArrayC {lmtaArray :: forall (b :: BackendType).
LogicalModelTypeArray b -> LogicalModelType b
lmtaArray = LogicalModelTypeArray LogicalModelTypeArray b
_})
        }
      ) ->
        Text
-> MaybeT
     (SchemaT r m) (FieldParser MetadataObjId n (AnnotatedField b))
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 Text
"Nested arrays are not currently implemented"

defaultLogicalModelSelectionSet ::
  forall b r m n.
  ( MonadBuildSchema b r m n
  ) =>
  LogicalModelInfo b ->
  SchemaT r m (Maybe (Parser 'Output n (AnnotatedFields b)))
defaultLogicalModelSelectionSet :: forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
LogicalModelInfo b
-> SchemaT r m (Maybe (Parser 'Output n (AnnotatedFields b)))
defaultLogicalModelSelectionSet LogicalModelInfo b
logicalModel = MaybeT (SchemaT r m) (Parser 'Output n (AnnotatedFields b))
-> SchemaT r m (Maybe (Parser 'Output n (AnnotatedFields b)))
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT do
  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

  PermColSpec b
selectableColumns <- Maybe (PermColSpec b) -> MaybeT (SchemaT r m) (PermColSpec b)
forall (m :: * -> *) b. Applicative m => Maybe b -> MaybeT m b
hoistMaybe (Maybe (PermColSpec b) -> MaybeT (SchemaT r m) (PermColSpec b))
-> Maybe (PermColSpec b) -> MaybeT (SchemaT r m) (PermColSpec b)
forall a b. (a -> b) -> a -> b
$ RoleName -> LogicalModelInfo b -> Maybe (PermColSpec b)
forall (b :: BackendType).
RoleName -> LogicalModelInfo b -> Maybe (PermColSpec b)
logicalModelColumnsForRole RoleName
roleName LogicalModelInfo b
logicalModel

  let isSelectable :: Column b -> Bool
isSelectable Column b
column =
        case PermColSpec b
selectableColumns of
          PermColSpec b
Permission.PCStar -> Bool
True
          Permission.PCCols [Column b]
cols -> Column b
column Column b -> [Column b] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Column b]
cols

  let fieldName :: Name
fieldName = LogicalModelName -> Name
getLogicalModelName (LogicalModelInfo b -> LogicalModelName
forall (b :: BackendType). LogicalModelInfo b -> LogicalModelName
_lmiName LogicalModelInfo b
logicalModel)

  -- which columns are we allowed to access given permissions?
  let allowedColumns :: [(Column b, LogicalModelField b)]
allowedColumns =
        ((Column b, LogicalModelField b) -> Bool)
-> [(Column b, LogicalModelField b)]
-> [(Column b, LogicalModelField b)]
forall a. (a -> Bool) -> [a] -> [a]
filter
          (Column b -> Bool
isSelectable (Column b -> Bool)
-> ((Column b, LogicalModelField b) -> Column b)
-> (Column b, LogicalModelField b)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Column b, LogicalModelField b) -> Column b
forall a b. (a, b) -> a
fst)
          (InsOrdHashMap (Column b) (LogicalModelField b)
-> [(Column b, LogicalModelField b)]
forall k v. InsOrdHashMap k v -> [(k, v)]
InsOrdHashMap.toList (LogicalModelInfo b
-> InsOrdHashMap (Column b) (LogicalModelField b)
forall (b :: BackendType).
LogicalModelInfo b
-> InsOrdHashMap (Column b) (LogicalModelField b)
_lmiFields LogicalModelInfo b
logicalModel))

  let description :: Maybe Description
description = Text -> Description
G.Description (Text -> Description) -> Maybe Text -> Maybe Description
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LogicalModelInfo b -> Maybe Text
forall (b :: BackendType). LogicalModelInfo b -> Maybe Text
_lmiDescription LogicalModelInfo b
logicalModel

      -- We entirely ignore Relay for now.
      implementsInterfaces :: [Parser MetadataObjId 'Output n Any]
implementsInterfaces = [Parser MetadataObjId 'Output n Any]
forall a. Monoid a => a
mempty

  SchemaT r m (Parser 'Output n (AnnotatedFields b))
-> MaybeT (SchemaT r m) (Parser 'Output n (AnnotatedFields 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 (SchemaT r m (Parser 'Output n (AnnotatedFields b))
 -> MaybeT (SchemaT r m) (Parser 'Output n (AnnotatedFields b)))
-> SchemaT r m (Parser 'Output n (AnnotatedFields b))
-> MaybeT (SchemaT r m) (Parser 'Output n (AnnotatedFields b))
forall a b. (a -> b) -> a -> b
$ Name
-> Name
-> SchemaT r m (Parser 'Output n (AnnotatedFields b))
-> SchemaT r m (Parser 'Output n (AnnotatedFields 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 'defaultLogicalModelSelectionSet Name
fieldName do
    -- we filter out parsers that return 'Nothing' as those we are no permitted to see.
    [FieldParser
   MetadataObjId
   n
   (AnnFieldG
      b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))]
parsers <- [Maybe
   (FieldParser
      MetadataObjId
      n
      (AnnFieldG
         b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b)))]
-> [FieldParser
      MetadataObjId
      n
      (AnnFieldG
         b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))]
forall a. [Maybe a] -> [a]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes ([Maybe
    (FieldParser
       MetadataObjId
       n
       (AnnFieldG
          b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b)))]
 -> [FieldParser
       MetadataObjId
       n
       (AnnFieldG
          b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))])
-> SchemaT
     r
     m
     [Maybe
        (FieldParser
           MetadataObjId
           n
           (AnnFieldG
              b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b)))]
-> SchemaT
     r
     m
     [FieldParser
        MetadataObjId
        n
        (AnnFieldG
           b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Column b, LogicalModelField b)
 -> SchemaT
      r
      m
      (Maybe
         (FieldParser
            MetadataObjId
            n
            (AnnFieldG
               b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b)))))
-> [(Column b, LogicalModelField b)]
-> SchemaT
     r
     m
     [Maybe
        (FieldParser
           MetadataObjId
           n
           (AnnFieldG
              b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b)))]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse ((Column b
 -> LogicalModelField b
 -> SchemaT
      r
      m
      (Maybe
         (FieldParser
            MetadataObjId
            n
            (AnnFieldG
               b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b)))))
-> (Column b, LogicalModelField b)
-> SchemaT
     r
     m
     (Maybe
        (FieldParser
           MetadataObjId
           n
           (AnnFieldG
              b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))))
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Set RelName
-> Column b
-> LogicalModelField b
-> SchemaT
     r
     m
     (Maybe
        (FieldParser
           MetadataObjId
           n
           (AnnFieldG
              b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
Set RelName
-> Column b
-> LogicalModelField b
-> SchemaT
     r m (Maybe (FieldParser MetadataObjId n (AnnotatedField b)))
parseLogicalModelField Set RelName
forall a. Monoid a => a
mempty)) [(Column b, LogicalModelField b)]
allowedColumns

    pure
      $ Name
-> Maybe Description
-> [FieldParser
      MetadataObjId
      n
      (AnnFieldG
         b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))]
-> [Parser MetadataObjId 'Output n Any]
-> Parser
     MetadataObjId
     'Output
     n
     (InsOrdHashMap
        Name
        (ParsedSelection
           (AnnFieldG
              b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))))
forall (m :: * -> *) origin a b.
MonadParse m =>
Name
-> Maybe Description
-> [FieldParser origin m a]
-> [Parser origin 'Output m b]
-> Parser origin 'Output m (InsOrdHashMap Name (ParsedSelection a))
P.selectionSetObject Name
fieldName Maybe Description
description [FieldParser
   MetadataObjId
   n
   (AnnFieldG
      b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))]
parsers [Parser MetadataObjId 'Output n Any]
implementsInterfaces
      Parser
  MetadataObjId
  'Output
  n
  (InsOrdHashMap
     Name
     (ParsedSelection
        (AnnFieldG
           b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))))
-> (InsOrdHashMap
      Name
      (ParsedSelection
         (AnnFieldG
            b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b)))
    -> AnnotatedFields b)
-> Parser 'Output n (AnnotatedFields b)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (Text
 -> AnnFieldG
      b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b))
-> InsOrdHashMap
     Name
     (ParsedSelection
        (AnnFieldG
           b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b)))
-> AnnotatedFields b
forall a.
(Text -> a) -> InsOrdHashMap Name (ParsedSelection a) -> Fields a
parsedSelectionsToFields Text
-> AnnFieldG
     b (RemoteRelationshipField UnpreparedValue) (UnpreparedValue b)
forall (b :: BackendType) r v. Text -> AnnFieldG b r v
IR.AFExpression

logicalModelSelectionList ::
  (MonadBuildSchema b r m n, BackendLogicalModelSelectSchema b) =>
  Nullable ->
  LogicalModelInfo b ->
  SchemaT r m (Maybe (Parser 'Output n (AnnotatedFields b)))
logicalModelSelectionList :: forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(MonadBuildSchema b r m n, BackendLogicalModelSelectSchema b) =>
Nullable
-> LogicalModelInfo b
-> SchemaT r m (Maybe (Parser 'Output n (AnnotatedFields b)))
logicalModelSelectionList Nullable
nullability LogicalModelInfo b
logicalModel =
  (Parser 'Output n (AnnotatedFields b)
 -> Parser 'Output n (AnnotatedFields b))
-> Maybe (Parser 'Output n (AnnotatedFields b))
-> Maybe (Parser 'Output n (AnnotatedFields b))
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Parser 'Output n (AnnotatedFields b)
-> Parser 'Output n (AnnotatedFields b)
nullabilityModifier (Maybe (Parser 'Output n (AnnotatedFields b))
 -> Maybe (Parser 'Output n (AnnotatedFields b)))
-> SchemaT r m (Maybe (Parser 'Output n (AnnotatedFields b)))
-> SchemaT r m (Maybe (Parser 'Output n (AnnotatedFields b)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LogicalModelInfo b
-> SchemaT r m (Maybe (Parser 'Output n (AnnotatedFields b)))
forall r (m :: * -> *) (n :: * -> *).
MonadBuildSourceSchema b r m n =>
LogicalModelInfo b
-> SchemaT r m (Maybe (Parser 'Output n (AnnotatedFields b)))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(BackendLogicalModelSelectSchema b,
 MonadBuildSourceSchema b r m n) =>
LogicalModelInfo b
-> SchemaT r m (Maybe (Parser 'Output n (AnnotatedFields b)))
logicalModelSelectionSet LogicalModelInfo b
logicalModel
  where
    nullabilityModifier :: Parser 'Output n (AnnotatedFields b)
-> Parser 'Output n (AnnotatedFields b)
nullabilityModifier =
      case Nullable
nullability of
        Nullable
Nullable -> Parser 'Output n (AnnotatedFields b)
-> Parser 'Output n (AnnotatedFields b)
forall (m :: * -> *) a. Parser 'Output m a -> Parser 'Output m a
nullableObjectList
        Nullable
NotNullable -> Parser 'Output n (AnnotatedFields b)
-> Parser 'Output n (AnnotatedFields b)
forall (m :: * -> *) a. Parser 'Output m a -> Parser 'Output m a
nonNullableObjectList

    -- \| Converts an output type parser from object_type to [object_type!]!
    nonNullableObjectList :: Parser 'Output m a -> Parser 'Output m a
    nonNullableObjectList :: forall (m :: * -> *) a. Parser 'Output m a -> Parser 'Output m a
nonNullableObjectList =
      Parser MetadataObjId 'Output m a
-> Parser MetadataObjId 'Output m a
forall (m :: * -> *) origin (k :: Kind) a.
Parser origin k m a -> Parser origin k m a
P.nonNullableParser (Parser MetadataObjId 'Output m a
 -> Parser MetadataObjId 'Output m a)
-> (Parser MetadataObjId 'Output m a
    -> Parser MetadataObjId 'Output m a)
-> Parser MetadataObjId 'Output m a
-> Parser MetadataObjId 'Output m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser MetadataObjId 'Output m a
-> Parser MetadataObjId 'Output m a
forall (m :: * -> *) origin a.
Parser origin 'Output m a -> Parser origin 'Output m a
P.multiple (Parser MetadataObjId 'Output m a
 -> Parser MetadataObjId 'Output m a)
-> (Parser MetadataObjId 'Output m a
    -> Parser MetadataObjId 'Output m a)
-> Parser MetadataObjId 'Output m a
-> Parser MetadataObjId 'Output m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser MetadataObjId 'Output m a
-> Parser MetadataObjId 'Output m a
forall (m :: * -> *) origin (k :: Kind) a.
Parser origin k m a -> Parser origin k m a
P.nonNullableParser

    -- \| Converts an output type parser from object_type to [object_type!]
    nullableObjectList :: Parser 'Output m a -> Parser 'Output m a
    nullableObjectList :: forall (m :: * -> *) a. Parser 'Output m a -> Parser 'Output m a
nullableObjectList =
      Parser MetadataObjId 'Output m a
-> Parser MetadataObjId 'Output m a
forall (m :: * -> *) origin a.
Parser origin 'Output m a -> Parser origin 'Output m a
P.multiple (Parser MetadataObjId 'Output m a
 -> Parser MetadataObjId 'Output m a)
-> (Parser MetadataObjId 'Output m a
    -> Parser MetadataObjId 'Output m a)
-> Parser MetadataObjId 'Output m a
-> Parser MetadataObjId 'Output m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser MetadataObjId 'Output m a
-> Parser MetadataObjId 'Output m a
forall (m :: * -> *) origin (k :: Kind) a.
Parser origin k m a -> Parser origin k m a
P.nonNullableParser

-- | Argument to filter rows returned from table selection
-- > where: table_bool_exp
logicalModelWhereArg ::
  forall b r m n.
  ( MonadBuildSchema b r m n,
    AggregationPredicatesSchema b
  ) =>
  LogicalModelInfo b ->
  SchemaT r m (InputFieldsParser n (Maybe (IR.AnnBoolExp b (IR.UnpreparedValue b))))
logicalModelWhereArg :: forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(MonadBuildSchema b r m n, AggregationPredicatesSchema b) =>
LogicalModelInfo b
-> SchemaT
     r
     m
     (InputFieldsParser n (Maybe (AnnBoolExp b (UnpreparedValue b))))
logicalModelWhereArg LogicalModelInfo b
logicalModel = do
  Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
boolExpParser <- LogicalModelInfo b
-> SchemaT r m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(MonadBuildSchema b r m n, AggregationPredicatesSchema b) =>
LogicalModelInfo b
-> SchemaT r m (Parser 'Input n (AnnBoolExp b (UnpreparedValue b)))
logicalModelBoolExp LogicalModelInfo b
logicalModel
  pure
    $ (Maybe (Maybe (AnnBoolExp b (UnpreparedValue b)))
 -> Maybe (AnnBoolExp b (UnpreparedValue b)))
-> InputFieldsParser
     MetadataObjId n (Maybe (Maybe (AnnBoolExp b (UnpreparedValue b))))
-> InputFieldsParser n (Maybe (AnnBoolExp 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 Maybe (Maybe (AnnBoolExp b (UnpreparedValue b)))
-> Maybe (AnnBoolExp b (UnpreparedValue b))
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join
    (InputFieldsParser
   MetadataObjId n (Maybe (Maybe (AnnBoolExp b (UnpreparedValue b))))
 -> InputFieldsParser n (Maybe (AnnBoolExp b (UnpreparedValue b))))
-> InputFieldsParser
     MetadataObjId n (Maybe (Maybe (AnnBoolExp b (UnpreparedValue b))))
-> InputFieldsParser n (Maybe (AnnBoolExp b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ Name
-> Maybe Description
-> Parser
     MetadataObjId 'Input n (Maybe (AnnBoolExp b (UnpreparedValue b)))
-> InputFieldsParser
     MetadataObjId n (Maybe (Maybe (AnnBoolExp 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
whereName Maybe Description
whereDesc
    (Parser
   MetadataObjId 'Input n (Maybe (AnnBoolExp b (UnpreparedValue b)))
 -> InputFieldsParser
      MetadataObjId n (Maybe (Maybe (AnnBoolExp b (UnpreparedValue b)))))
-> Parser
     MetadataObjId 'Input n (Maybe (AnnBoolExp b (UnpreparedValue b)))
-> InputFieldsParser
     MetadataObjId n (Maybe (Maybe (AnnBoolExp b (UnpreparedValue b))))
forall a b. (a -> b) -> a -> b
$ Parser 'Input n (AnnBoolExp b (UnpreparedValue b))
-> Parser
     MetadataObjId 'Input n (Maybe (AnnBoolExp 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 (AnnBoolExp b (UnpreparedValue b))
boolExpParser
  where
    whereName :: Name
whereName = Name
Name._where
    whereDesc :: Maybe Description
whereDesc = 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
"filter the rows returned"

-- | Argument to sort rows returned from table selection
-- > order_by: [table_order_by!]
logicalModelOrderByArg ::
  forall b r m n.
  ( MonadBuildSchema b r m n
  ) =>
  LogicalModelInfo b ->
  SchemaT r m (InputFieldsParser n (Maybe (NonEmpty (IR.AnnotatedOrderByItemG b (IR.UnpreparedValue b)))))
logicalModelOrderByArg :: forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
LogicalModelInfo b
-> SchemaT
     r
     m
     (InputFieldsParser
        n (Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b)))))
logicalModelOrderByArg LogicalModelInfo b
logicalModel = do
  NamingCase
tCase <- (SourceInfo b -> NamingCase) -> SchemaT r m NamingCase
forall r (m :: * -> *) a b.
(MonadReader r m, Has a r) =>
(a -> b) -> m b
retrieve ((SourceInfo b -> NamingCase) -> SchemaT r m NamingCase)
-> (SourceInfo b -> NamingCase) -> SchemaT r m NamingCase
forall a b. (a -> b) -> a -> b
$ ResolvedSourceCustomization -> NamingCase
_rscNamingConvention (ResolvedSourceCustomization -> NamingCase)
-> (SourceInfo b -> ResolvedSourceCustomization)
-> SourceInfo b
-> NamingCase
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (b :: BackendType).
SourceInfo b -> ResolvedSourceCustomization
_siCustomization @b
  Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)]
orderByParser <- LogicalModelInfo b
-> SchemaT
     r m (Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
LogicalModelInfo b
-> SchemaT
     r m (Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)])
logicalModelOrderByExp LogicalModelInfo b
logicalModel
  let orderByName :: Name
orderByName = NamingCase -> Name -> Name
applyFieldNameCaseCust NamingCase
tCase Name
Name._order_by
      orderByDesc :: Maybe Description
orderByDesc = 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
"sort the rows by one or more columns"
  InputFieldsParser
  n (Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b))))
-> SchemaT
     r
     m
     (InputFieldsParser
        n (Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b)))))
forall a. a -> SchemaT r m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputFieldsParser
   n (Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b))))
 -> SchemaT
      r
      m
      (InputFieldsParser
         n
         (Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b))))))
-> InputFieldsParser
     n (Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b))))
-> SchemaT
     r
     m
     (InputFieldsParser
        n (Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b)))))
forall a b. (a -> b) -> a -> b
$ do
    Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]]
maybeOrderByExps <-
      (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 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 (Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]])
-> Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]]
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join
        (InputFieldsParser
   MetadataObjId
   n
   (Maybe (Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]]))
 -> InputFieldsParser
      MetadataObjId
      n
      (Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]]))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]]))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]])
forall a b. (a -> b) -> a -> 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
orderByName Maybe Description
orderByDesc
        (Parser
   MetadataObjId
   'Input
   n
   (Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]])
 -> InputFieldsParser
      MetadataObjId
      n
      (Maybe (Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]])))
-> Parser
     MetadataObjId
     'Input
     n
     (Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]])
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]]))
forall a b. (a -> b) -> a -> b
$ Parser
  MetadataObjId
  '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
   MetadataObjId
   'Input
   n
   [[AnnotatedOrderByItemG b (UnpreparedValue b)]]
 -> Parser
      MetadataObjId
      'Input
      n
      (Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]]))
-> Parser
     MetadataObjId
     'Input
     n
     [[AnnotatedOrderByItemG b (UnpreparedValue b)]]
-> Parser
     MetadataObjId
     'Input
     n
     (Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]])
forall a b. (a -> b) -> a -> b
$ Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)]
-> Parser
     MetadataObjId
     'Input
     n
     [[AnnotatedOrderByItemG b (UnpreparedValue b)]]
forall origin (k :: Kind) (m :: * -> *) a.
(MonadParse m, 'Input <: k) =>
Parser origin k m a -> Parser origin k m [a]
P.list Parser 'Input n [AnnotatedOrderByItemG b (UnpreparedValue b)]
orderByParser
    pure $ Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]]
maybeOrderByExps Maybe [[AnnotatedOrderByItemG b (UnpreparedValue b)]]
-> ([[AnnotatedOrderByItemG b (UnpreparedValue b)]]
    -> Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b))))
-> Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b)))
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [AnnotatedOrderByItemG b (UnpreparedValue b)]
-> Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b)))
forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty ([AnnotatedOrderByItemG b (UnpreparedValue b)]
 -> Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b))))
-> ([[AnnotatedOrderByItemG b (UnpreparedValue b)]]
    -> [AnnotatedOrderByItemG b (UnpreparedValue b)])
-> [[AnnotatedOrderByItemG b (UnpreparedValue b)]]
-> Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[AnnotatedOrderByItemG b (UnpreparedValue b)]]
-> [AnnotatedOrderByItemG b (UnpreparedValue b)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat

-- | Argument to distinct select on columns returned from table selection
-- > distinct_on: [table_select_column!]
logicalModelDistinctArg ::
  forall b r m n.
  ( MonadBuildSchema b r m n
  ) =>
  LogicalModelInfo b ->
  SchemaT r m (InputFieldsParser n (Maybe (NonEmpty (IR.AnnDistinctColumn b (IR.UnpreparedValue b)))))
logicalModelDistinctArg :: forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
LogicalModelInfo b
-> SchemaT
     r
     m
     (InputFieldsParser
        n (Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b)))))
logicalModelDistinctArg LogicalModelInfo b
logicalModel = do
  RoleName
roleName <- (SchemaContext -> RoleName) -> SchemaT r m RoleName
forall r (m :: * -> *) a b.
(MonadReader r m, Has a r) =>
(a -> b) -> m b
retrieve SchemaContext -> RoleName
scRole
  let selectPermissions :: Maybe (SelPermInfo b)
selectPermissions = RoleName -> LogicalModelInfo b -> Maybe (SelPermInfo b)
forall (b :: BackendType).
Backend b =>
RoleName -> LogicalModelInfo b -> Maybe (SelPermInfo b)
getSelPermInfoForLogicalModel RoleName
roleName LogicalModelInfo b
logicalModel

  let name :: Name
name = LogicalModelName -> Name
getLogicalModelName (LogicalModelInfo b -> LogicalModelName
forall (b :: BackendType). LogicalModelInfo b -> LogicalModelName
_lmiName LogicalModelInfo b
logicalModel)

  NamingCase
tCase <- (SourceInfo b -> NamingCase) -> SchemaT r m NamingCase
forall r (m :: * -> *) a b.
(MonadReader r m, Has a r) =>
(a -> b) -> m b
retrieve ((SourceInfo b -> NamingCase) -> SchemaT r m NamingCase)
-> (SourceInfo b -> NamingCase) -> SchemaT r m NamingCase
forall a b. (a -> b) -> a -> b
$ ResolvedSourceCustomization -> NamingCase
_rscNamingConvention (ResolvedSourceCustomization -> NamingCase)
-> (SourceInfo b -> ResolvedSourceCustomization)
-> SourceInfo b
-> NamingCase
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (b :: BackendType).
SourceInfo b -> ResolvedSourceCustomization
_siCustomization @b

  let maybeColumnDefinitions :: Maybe
  (NonEmpty
     (Definition EnumValueInfo,
      AnnDistinctColumn b (UnpreparedValue b)))
maybeColumnDefinitions =
        (Column b
 -> Maybe
      (Definition EnumValueInfo,
       AnnDistinctColumn b (UnpreparedValue b)))
-> [Column b]
-> Maybe
     [(Definition EnumValueInfo,
       AnnDistinctColumn b (UnpreparedValue b))]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Maybe (SelPermInfo b)
-> Column b
-> Maybe
     (Definition EnumValueInfo, AnnDistinctColumn b (UnpreparedValue b))
definitionFromTypeRow Maybe (SelPermInfo b)
selectPermissions) (InsOrdHashMap (Column b) (LogicalModelField b) -> [Column b]
forall k v. InsOrdHashMap k v -> [k]
InsOrdHashMap.keys (LogicalModelInfo b
-> InsOrdHashMap (Column b) (LogicalModelField b)
forall (b :: BackendType).
LogicalModelInfo b
-> InsOrdHashMap (Column b) (LogicalModelField b)
_lmiFields LogicalModelInfo b
logicalModel))
          Maybe
  [(Definition EnumValueInfo,
    AnnDistinctColumn b (UnpreparedValue b))]
-> ([(Definition EnumValueInfo,
      AnnDistinctColumn b (UnpreparedValue b))]
    -> Maybe
         (NonEmpty
            (Definition EnumValueInfo,
             AnnDistinctColumn b (UnpreparedValue b))))
-> Maybe
     (NonEmpty
        (Definition EnumValueInfo,
         AnnDistinctColumn b (UnpreparedValue b)))
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [(Definition EnumValueInfo,
  AnnDistinctColumn b (UnpreparedValue b))]
-> Maybe
     (NonEmpty
        (Definition EnumValueInfo,
         AnnDistinctColumn b (UnpreparedValue b)))
forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty

  case (,) (Name
 -> NonEmpty
      (Definition EnumValueInfo, AnnDistinctColumn b (UnpreparedValue b))
 -> (Name,
     NonEmpty
       (Definition EnumValueInfo,
        AnnDistinctColumn b (UnpreparedValue b))))
-> Maybe Name
-> Maybe
     (NonEmpty
        (Definition EnumValueInfo, AnnDistinctColumn b (UnpreparedValue b))
      -> (Name,
          NonEmpty
            (Definition EnumValueInfo,
             AnnDistinctColumn b (UnpreparedValue b))))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe Name
G.mkName Text
"_enum_name" Maybe
  (NonEmpty
     (Definition EnumValueInfo, AnnDistinctColumn b (UnpreparedValue b))
   -> (Name,
       NonEmpty
         (Definition EnumValueInfo,
          AnnDistinctColumn b (UnpreparedValue b))))
-> Maybe
     (NonEmpty
        (Definition EnumValueInfo,
         AnnDistinctColumn b (UnpreparedValue b)))
-> Maybe
     (Name,
      NonEmpty
        (Definition EnumValueInfo,
         AnnDistinctColumn b (UnpreparedValue b)))
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe
  (NonEmpty
     (Definition EnumValueInfo,
      AnnDistinctColumn b (UnpreparedValue b)))
maybeColumnDefinitions of
    Maybe
  (Name,
   NonEmpty
     (Definition EnumValueInfo,
      AnnDistinctColumn b (UnpreparedValue b)))
Nothing -> Text
-> SchemaT
     r
     m
     (InputFieldsParser
        n (Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b)))))
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 (Text
 -> SchemaT
      r
      m
      (InputFieldsParser
         n (Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b))))))
-> Text
-> SchemaT
     r
     m
     (InputFieldsParser
        n (Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b)))))
forall a b. (a -> b) -> a -> b
$ Text
"Error creating an enum name for logical model " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> LogicalModelName -> Text
forall a. Show a => a -> Text
tshow (LogicalModelInfo b -> LogicalModelName
forall (b :: BackendType). LogicalModelInfo b -> LogicalModelName
_lmiName LogicalModelInfo b
logicalModel)
    Just (Name
enum', NonEmpty
  (Definition EnumValueInfo, AnnDistinctColumn b (UnpreparedValue b))
columnDefinitions) -> do
      let enumName :: Name
enumName = Name
name Name -> Name -> Name
forall a. Semigroup a => a -> a -> a
<> Name
enum'
          description :: Maybe a
description = Maybe a
forall a. Maybe a
Nothing
          columnsEnum :: Maybe
  (Parser
     MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b)))
columnsEnum = Parser
  MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b))
-> Maybe
     (Parser
        MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b)))
forall a. a -> Maybe a
Just (Parser
   MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b))
 -> Maybe
      (Parser
         MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b))))
-> Parser
     MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b))
-> Maybe
     (Parser
        MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b)))
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) origin a.
MonadParse m =>
Name
-> Maybe Description
-> NonEmpty (Definition origin EnumValueInfo, a)
-> Parser origin 'Both m a
P.enum @n Name
enumName Maybe Description
forall a. Maybe a
description NonEmpty
  (Definition EnumValueInfo, AnnDistinctColumn b (UnpreparedValue b))
columnDefinitions
          distinctOnName :: Name
distinctOnName = NamingCase -> Name -> Name
applyFieldNameCaseCust NamingCase
tCase Name
Name._distinct_on
          distinctOnDesc :: Maybe Description
distinctOnDesc = 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
"distinct select on columns"

      InputFieldsParser
  n (Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b))))
-> SchemaT
     r
     m
     (InputFieldsParser
        n (Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b)))))
forall a. a -> SchemaT r m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure do
        Maybe [AnnDistinctColumn b (UnpreparedValue b)]
maybeDistinctOnColumns <-
          Maybe (Maybe [AnnDistinctColumn b (UnpreparedValue b)])
-> Maybe [AnnDistinctColumn b (UnpreparedValue b)]
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join
            (Maybe (Maybe [AnnDistinctColumn b (UnpreparedValue b)])
 -> Maybe [AnnDistinctColumn b (UnpreparedValue b)])
-> (Maybe (Maybe (Maybe [AnnDistinctColumn b (UnpreparedValue b)]))
    -> Maybe (Maybe [AnnDistinctColumn b (UnpreparedValue b)]))
-> Maybe (Maybe (Maybe [AnnDistinctColumn b (UnpreparedValue b)]))
-> Maybe [AnnDistinctColumn b (UnpreparedValue b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Maybe (Maybe [AnnDistinctColumn b (UnpreparedValue b)]))
-> Maybe (Maybe [AnnDistinctColumn b (UnpreparedValue b)])
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join
            (Maybe (Maybe (Maybe [AnnDistinctColumn b (UnpreparedValue b)]))
 -> Maybe [AnnDistinctColumn b (UnpreparedValue b)])
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe (Maybe [AnnDistinctColumn b (UnpreparedValue b)])))
-> InputFieldsParser
     MetadataObjId n (Maybe [AnnDistinctColumn b (UnpreparedValue b)])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe
  (Parser
     MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b)))
-> (Parser
      MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b))
    -> InputFieldsParser
         MetadataObjId
         n
         (Maybe (Maybe [AnnDistinctColumn b (UnpreparedValue b)])))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe (Maybe [AnnDistinctColumn b (UnpreparedValue b)])))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for
              Maybe
  (Parser
     MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b)))
columnsEnum
              (Name
-> Maybe Description
-> Parser
     MetadataObjId
     'Both
     n
     (Maybe [AnnDistinctColumn b (UnpreparedValue b)])
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe [AnnDistinctColumn 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
distinctOnName Maybe Description
distinctOnDesc (Parser
   MetadataObjId
   'Both
   n
   (Maybe [AnnDistinctColumn b (UnpreparedValue b)])
 -> InputFieldsParser
      MetadataObjId
      n
      (Maybe (Maybe [AnnDistinctColumn b (UnpreparedValue b)])))
-> (Parser
      MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b))
    -> Parser
         MetadataObjId
         'Both
         n
         (Maybe [AnnDistinctColumn b (UnpreparedValue b)]))
-> Parser
     MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b))
-> InputFieldsParser
     MetadataObjId
     n
     (Maybe (Maybe [AnnDistinctColumn b (UnpreparedValue b)]))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser
  MetadataObjId 'Both n [AnnDistinctColumn b (UnpreparedValue b)]
-> Parser
     MetadataObjId
     'Both
     n
     (Maybe [AnnDistinctColumn 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
   MetadataObjId 'Both n [AnnDistinctColumn b (UnpreparedValue b)]
 -> Parser
      MetadataObjId
      'Both
      n
      (Maybe [AnnDistinctColumn b (UnpreparedValue b)]))
-> (Parser
      MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b))
    -> Parser
         MetadataObjId 'Both n [AnnDistinctColumn b (UnpreparedValue b)])
-> Parser
     MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b))
-> Parser
     MetadataObjId
     'Both
     n
     (Maybe [AnnDistinctColumn b (UnpreparedValue b)])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser
  MetadataObjId 'Both n (AnnDistinctColumn b (UnpreparedValue b))
-> Parser
     MetadataObjId 'Both n [AnnDistinctColumn b (UnpreparedValue b)]
forall origin (k :: Kind) (m :: * -> *) a.
(MonadParse m, 'Input <: k) =>
Parser origin k m a -> Parser origin k m [a]
P.list)
        pure $ Maybe [AnnDistinctColumn b (UnpreparedValue b)]
maybeDistinctOnColumns Maybe [AnnDistinctColumn b (UnpreparedValue b)]
-> ([AnnDistinctColumn b (UnpreparedValue b)]
    -> Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b))))
-> Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b)))
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [AnnDistinctColumn b (UnpreparedValue b)]
-> Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b)))
forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty
  where
    definitionFromTypeRow :: Maybe (SelPermInfo b) -> Column b -> Maybe (P.Definition P.EnumValueInfo, IR.AnnDistinctColumn b (IR.UnpreparedValue b))
    definitionFromTypeRow :: Maybe (SelPermInfo b)
-> Column b
-> Maybe
     (Definition EnumValueInfo, AnnDistinctColumn b (UnpreparedValue b))
definitionFromTypeRow Maybe (SelPermInfo b)
maybeSelectPermissions Column b
name' = do
      SelPermInfo b
selectPermissions <- Maybe (SelPermInfo b)
maybeSelectPermissions
      Name
columnName <- Text -> Maybe Name
G.mkName (Column b -> Text
forall a. ToTxt a => a -> Text
toTxt Column b
name')

      let definition :: Definition EnumValueInfo
definition =
            P.Definition
              { dName :: Name
dName = Name
columnName,
                dDescription :: Maybe Description
dDescription = Description -> Maybe Description
forall a. a -> Maybe a
Just Description
"column name",
                dOrigin :: Maybe MetadataObjId
dOrigin = Maybe MetadataObjId
forall a. Maybe a
Nothing,
                dDirectives :: [Directive Void]
dDirectives = [Directive Void]
forall a. Monoid a => a
mempty,
                dInfo :: EnumValueInfo
dInfo = EnumValueInfo
P.EnumValueInfo
              }

      let redactionExp :: AnnRedactionExp b (UnpreparedValue b)
redactionExp = AnnRedactionExp b (UnpreparedValue b)
-> Maybe (AnnRedactionExp b (UnpreparedValue b))
-> AnnRedactionExp b (UnpreparedValue b)
forall a. a -> Maybe a -> a
fromMaybe AnnRedactionExp b (UnpreparedValue b)
forall (b :: BackendType) v. AnnRedactionExp b v
IR.NoRedaction (Maybe (AnnRedactionExp b (UnpreparedValue b))
 -> AnnRedactionExp b (UnpreparedValue b))
-> Maybe (AnnRedactionExp b (UnpreparedValue b))
-> AnnRedactionExp b (UnpreparedValue b)
forall a b. (a -> b) -> a -> b
$ SelPermInfo b
-> Column b -> Maybe (AnnRedactionExp b (UnpreparedValue b))
forall (b :: BackendType).
Backend b =>
SelPermInfo b
-> Column b -> Maybe (AnnRedactionExpUnpreparedValue b)
getRedactionExprForColumn SelPermInfo b
selectPermissions Column b
name'

      (Definition EnumValueInfo, AnnDistinctColumn b (UnpreparedValue b))
-> Maybe
     (Definition EnumValueInfo, AnnDistinctColumn b (UnpreparedValue b))
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Definition EnumValueInfo
definition, Column b
-> AnnRedactionExp b (UnpreparedValue b)
-> AnnDistinctColumn b (UnpreparedValue b)
forall (b :: BackendType) v.
Column b -> AnnRedactionExp b v -> AnnDistinctColumn b v
IR.AnnDistinctColumn Column b
name' AnnRedactionExp b (UnpreparedValue b)
redactionExp)

defaultLogicalModelArgs ::
  forall b r m n.
  ( MonadBuildSchema b r m n,
    AggregationPredicatesSchema b
  ) =>
  LogicalModelInfo b ->
  SchemaT r m (InputFieldsParser n (SelectArgs b))
defaultLogicalModelArgs :: forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(MonadBuildSchema b r m n, AggregationPredicatesSchema b) =>
LogicalModelInfo b
-> SchemaT r m (InputFieldsParser n (SelectArgs b))
defaultLogicalModelArgs LogicalModelInfo b
logicalModel = do
  InputFieldsParser n (Maybe (AnnBoolExp b (UnpreparedValue b)))
whereParser <- LogicalModelInfo b
-> SchemaT
     r
     m
     (InputFieldsParser n (Maybe (AnnBoolExp b (UnpreparedValue b))))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
(MonadBuildSchema b r m n, AggregationPredicatesSchema b) =>
LogicalModelInfo b
-> SchemaT
     r
     m
     (InputFieldsParser n (Maybe (AnnBoolExp b (UnpreparedValue b))))
logicalModelWhereArg LogicalModelInfo b
logicalModel
  InputFieldsParser
  n (Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b))))
orderByParser <- LogicalModelInfo b
-> SchemaT
     r
     m
     (InputFieldsParser
        n (Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b)))))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
LogicalModelInfo b
-> SchemaT
     r
     m
     (InputFieldsParser
        n (Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b)))))
logicalModelOrderByArg LogicalModelInfo b
logicalModel
  InputFieldsParser
  n (Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b))))
distinctParser <- LogicalModelInfo b
-> SchemaT
     r
     m
     (InputFieldsParser
        n (Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b)))))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
LogicalModelInfo b
-> SchemaT
     r
     m
     (InputFieldsParser
        n (Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b)))))
logicalModelDistinctArg LogicalModelInfo b
logicalModel

  InputFieldsParser n (Maybe (AnnBoolExp b (UnpreparedValue b)))
-> InputFieldsParser
     n (Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b))))
-> InputFieldsParser
     n (Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b))))
-> SchemaT r m (InputFieldsParser n (SelectArgs b))
forall (b :: BackendType) r (m :: * -> *) (n :: * -> *).
MonadBuildSchema b r m n =>
InputFieldsParser n (Maybe (AnnBoolExp b (UnpreparedValue b)))
-> InputFieldsParser
     n (Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b))))
-> InputFieldsParser
     n (Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b))))
-> SchemaT r m (InputFieldsParser n (SelectArgs b))
defaultArgsParser InputFieldsParser n (Maybe (AnnBoolExp b (UnpreparedValue b)))
whereParser InputFieldsParser
  n (Maybe (NonEmpty (AnnotatedOrderByItemG b (UnpreparedValue b))))
orderByParser InputFieldsParser
  n (Maybe (NonEmpty (AnnDistinctColumn b (UnpreparedValue b))))
distinctParser