2022-04-28 04:51:58 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
|
2022-05-02 08:03:12 +03:00
|
|
|
module Hasura.Backends.DataConnector.IR.Export
|
2022-04-28 04:51:58 +03:00
|
|
|
( QueryError (..),
|
2022-06-24 09:58:25 +03:00
|
|
|
queryRequestToAPI,
|
2022-04-28 04:51:58 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2022-06-24 09:58:25 +03:00
|
|
|
import Autodocodec.Extended (ValueWrapper (..))
|
2022-04-28 04:51:58 +03:00
|
|
|
import Data.Aeson (ToJSON)
|
2022-06-08 18:31:28 +03:00
|
|
|
import Data.Aeson.KeyMap (fromHashMapText)
|
2022-06-24 09:58:25 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.API qualified as API
|
|
|
|
import Hasura.Backends.DataConnector.IR.Query qualified as IR.Q
|
2022-04-28 04:51:58 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Witch qualified
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-06-02 05:06:45 +03:00
|
|
|
data QueryError = ExposedLiteral Text
|
2022-04-28 04:51:58 +03:00
|
|
|
deriving stock (Generic)
|
|
|
|
deriving anyclass (ToJSON)
|
|
|
|
|
2022-06-24 09:58:25 +03:00
|
|
|
queryRequestToAPI :: IR.Q.QueryRequest -> Either QueryError API.QueryRequest
|
|
|
|
queryRequestToAPI IR.Q.QueryRequest {..} = do
|
|
|
|
query <- queryToAPI _qrQuery
|
|
|
|
pure $
|
|
|
|
API.QueryRequest
|
|
|
|
{ _qrTable = Witch.from _qrTable,
|
|
|
|
_qrTableRelationships =
|
|
|
|
( \(sourceTableName, relationships) ->
|
|
|
|
API.TableRelationships
|
|
|
|
{ _trSourceTable = Witch.from sourceTableName,
|
|
|
|
_trRelationships = HashMap.mapKeys Witch.from $ Witch.from <$> relationships
|
|
|
|
}
|
|
|
|
)
|
|
|
|
<$> HashMap.toList _qrTableRelationships,
|
|
|
|
_qrQuery = query
|
|
|
|
}
|
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
queryToAPI :: IR.Q.Query -> Either QueryError API.Query
|
|
|
|
queryToAPI IR.Q.Query {..} = do
|
2022-06-24 09:58:25 +03:00
|
|
|
fields' <- traverse fieldToAPI _qFields
|
2022-04-28 04:51:58 +03:00
|
|
|
pure $
|
|
|
|
API.Query
|
2022-06-24 09:58:25 +03:00
|
|
|
{ _qFields = fromHashMapText fields',
|
|
|
|
_qLimit = _qLimit,
|
|
|
|
_qOffset = _qOffset,
|
|
|
|
_qWhere = fmap Witch.from _qWhere,
|
|
|
|
_qOrderBy = nonEmpty $ fmap Witch.from _qOrderBy
|
2022-04-28 04:51:58 +03:00
|
|
|
}
|
|
|
|
|
2022-06-24 09:58:25 +03:00
|
|
|
fieldToAPI :: IR.Q.Field -> Either QueryError API.Field
|
|
|
|
fieldToAPI = \case
|
|
|
|
IR.Q.ColumnField contents -> Right . API.ColumnField . ValueWrapper $ Witch.from contents
|
|
|
|
IR.Q.RelField contents -> API.RelField <$> rcToAPI contents
|
|
|
|
IR.Q.LiteralField lit -> Left $ ExposedLiteral lit
|
2022-04-28 04:51:58 +03:00
|
|
|
|
2022-06-24 09:58:25 +03:00
|
|
|
rcToAPI :: IR.Q.RelationshipField -> Either QueryError API.RelationshipField
|
|
|
|
rcToAPI IR.Q.RelationshipField {..} = do
|
|
|
|
query <- queryToAPI _rfQuery
|
|
|
|
pure $
|
|
|
|
API.RelationshipField
|
|
|
|
{ _rfRelationship = Witch.from _rfRelationship,
|
|
|
|
_rfQuery = query
|
|
|
|
}
|