2023-04-19 12:03:36 +03:00
|
|
|
module Hasura.LogicalModel.Common
|
2023-02-15 20:55:06 +03:00
|
|
|
( toFieldInfo,
|
2023-04-17 14:30:10 +03:00
|
|
|
columnsFromFields,
|
2023-04-25 14:58:15 +03:00
|
|
|
logicalModelFieldsToFieldInfo,
|
2023-02-15 20:55:06 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2023-04-25 14:58:15 +03:00
|
|
|
import Data.Bifunctor (bimap)
|
2023-04-26 18:42:13 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2023-03-22 19:34:30 +03:00
|
|
|
import Data.HashMap.Strict.InsOrd qualified as InsOrd
|
2023-02-15 20:55:06 +03:00
|
|
|
import Data.Text.Extended (ToTxt (toTxt))
|
2023-04-19 12:03:36 +03:00
|
|
|
import Hasura.LogicalModel.Types (LogicalModelField (..))
|
2023-04-13 19:10:38 +03:00
|
|
|
import Hasura.NativeQuery.Types (NullableScalarType (..))
|
2023-02-15 20:55:06 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types.Backend (Backend (..))
|
2023-04-25 14:58:15 +03:00
|
|
|
import Hasura.RQL.Types.Column (ColumnInfo (..), ColumnMutability (..), ColumnType (..), fromCol)
|
|
|
|
import Hasura.RQL.Types.Table (FieldInfo (..), FieldInfoMap)
|
2023-02-15 20:55:06 +03:00
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
|
2023-04-17 14:30:10 +03:00
|
|
|
columnsFromFields ::
|
2023-04-19 12:03:36 +03:00
|
|
|
InsOrd.InsOrdHashMap k (LogicalModelField b) ->
|
2023-04-17 14:30:10 +03:00
|
|
|
InsOrd.InsOrdHashMap k (NullableScalarType b)
|
|
|
|
columnsFromFields =
|
|
|
|
InsOrd.mapMaybe
|
|
|
|
( \case
|
2023-04-19 12:03:36 +03:00
|
|
|
LogicalModelScalarField
|
|
|
|
{ lmfType = nstType,
|
|
|
|
lmfNullable = nstNullable,
|
|
|
|
lmfDescription = nstDescription
|
2023-04-17 14:30:10 +03:00
|
|
|
} ->
|
|
|
|
Just (NullableScalarType {..})
|
|
|
|
_ -> Nothing
|
|
|
|
)
|
|
|
|
|
2023-04-04 15:45:20 +03:00
|
|
|
toFieldInfo :: forall b. (Backend b) => InsOrd.InsOrdHashMap (Column b) (NullableScalarType b) -> Maybe [FieldInfo b]
|
|
|
|
toFieldInfo fields =
|
2023-02-15 20:55:06 +03:00
|
|
|
traverseWithIndex
|
2023-04-19 12:03:36 +03:00
|
|
|
(\i -> fmap FIColumn . logicalModelToColumnInfo i)
|
2023-04-04 15:45:20 +03:00
|
|
|
(InsOrd.toList fields)
|
2023-02-15 20:55:06 +03:00
|
|
|
|
2023-04-25 14:58:15 +03:00
|
|
|
traverseWithIndex :: (Applicative m) => (Int -> aa -> m bb) -> [aa] -> m [bb]
|
|
|
|
traverseWithIndex f = zipWithM f [0 ..]
|
|
|
|
|
|
|
|
logicalModelToColumnInfo :: forall b. (Backend b) => Int -> (Column b, NullableScalarType b) -> Maybe (ColumnInfo b)
|
|
|
|
logicalModelToColumnInfo i (column, NullableScalarType {..}) = do
|
|
|
|
name <- G.mkName (toTxt column)
|
|
|
|
pure $
|
|
|
|
ColumnInfo
|
|
|
|
{ ciColumn = column,
|
|
|
|
ciName = name,
|
|
|
|
ciPosition = i,
|
|
|
|
ciType = ColumnScalar nstType,
|
|
|
|
ciIsNullable = nstNullable,
|
|
|
|
ciDescription = G.Description <$> nstDescription,
|
|
|
|
ciMutability = ColumnMutability {_cmIsInsertable = False, _cmIsUpdatable = False}
|
|
|
|
}
|
|
|
|
|
|
|
|
logicalModelFieldsToFieldInfo ::
|
|
|
|
forall b.
|
|
|
|
(Backend b) =>
|
|
|
|
InsOrd.InsOrdHashMap (Column b) (LogicalModelField b) ->
|
|
|
|
FieldInfoMap (FieldInfo b)
|
|
|
|
logicalModelFieldsToFieldInfo =
|
2023-04-26 18:42:13 +03:00
|
|
|
HashMap.fromList
|
2023-04-25 14:58:15 +03:00
|
|
|
. fmap (bimap (fromCol @b) FIColumn)
|
|
|
|
. fromMaybe mempty
|
|
|
|
. traverseWithIndex
|
|
|
|
(\i (column, lmf) -> (,) column <$> logicalModelToColumnInfo i (column, lmf))
|
|
|
|
. InsOrd.toList
|
|
|
|
. columnsFromFields
|