mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-22 23:11:41 +03:00
dec8579db8
### Description Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad. However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase. To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint. To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way. PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789 GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
115 lines
4.1 KiB
Haskell
115 lines
4.1 KiB
Haskell
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
module Hasura.Backends.DataConnector.Adapter.Transport () where
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
import Control.Exception.Safe (throwIO)
|
|
import Control.Monad.Trans.Control
|
|
import Data.Aeson qualified as J
|
|
import Data.Text.Extended ((<>>))
|
|
import Hasura.Backends.DataConnector.Adapter.Execute (DataConnectorPreparedQuery (..), encodePreparedQueryToJsonText)
|
|
import Hasura.Backends.DataConnector.Adapter.Types (SourceConfig (..))
|
|
import Hasura.Backends.DataConnector.Agent.Client (AgentClientContext (..), AgentClientT, runAgentClientT)
|
|
import Hasura.Base.Error (QErr)
|
|
import Hasura.EncJSON (EncJSON)
|
|
import Hasura.GraphQL.Execute.Backend (DBStepInfo (..), OnBaseMonad (..))
|
|
import Hasura.GraphQL.Logging qualified as HGL
|
|
import Hasura.GraphQL.Namespace (RootFieldAlias)
|
|
import Hasura.GraphQL.Transport.Backend (BackendTransport (..))
|
|
import Hasura.GraphQL.Transport.HTTP.Protocol (GQLReqUnparsed)
|
|
import Hasura.Logging (Hasura, Logger, nullLogger)
|
|
import Hasura.Prelude
|
|
import Hasura.RQL.Types.Backend (ResolvedConnectionTemplate)
|
|
import Hasura.SQL.Backend (BackendType (DataConnector))
|
|
import Hasura.Server.Types (RequestId)
|
|
import Hasura.Session (UserInfo)
|
|
import Hasura.Tracing qualified as Tracing
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
instance BackendTransport 'DataConnector where
|
|
runDBQuery = runDBQuery'
|
|
runDBQueryExplain = runDBQueryExplain'
|
|
runDBMutation = runDBMutation'
|
|
runDBStreamingSubscription _ _ _ _ =
|
|
liftIO . throwIO $ userError "runDBStreamingSubscription: not implemented for the Data Connector backend."
|
|
runDBSubscription _ _ _ _ =
|
|
liftIO . throwIO $ userError "runDBSubscription: not implemented for the Data Connector backend."
|
|
|
|
runDBQuery' ::
|
|
( MonadIO m,
|
|
MonadBaseControl IO m,
|
|
MonadError QErr m,
|
|
Tracing.MonadTrace m,
|
|
HGL.MonadQueryLog m
|
|
) =>
|
|
RequestId ->
|
|
GQLReqUnparsed ->
|
|
RootFieldAlias ->
|
|
UserInfo ->
|
|
Logger Hasura ->
|
|
SourceConfig ->
|
|
OnBaseMonad AgentClientT a ->
|
|
Maybe DataConnectorPreparedQuery ->
|
|
ResolvedConnectionTemplate 'DataConnector ->
|
|
m (DiffTime, a)
|
|
runDBQuery' requestId query fieldName _userInfo logger SourceConfig {..} action queryRequest _ = do
|
|
void $ HGL.logQueryLog logger $ mkQueryLog query fieldName queryRequest requestId
|
|
withElapsedTime
|
|
. Tracing.trace ("Data Connector backend query for root field " <>> fieldName)
|
|
. flip runAgentClientT (AgentClientContext logger _scEndpoint _scManager _scTimeoutMicroseconds)
|
|
. runOnBaseMonad
|
|
$ action
|
|
|
|
mkQueryLog ::
|
|
GQLReqUnparsed ->
|
|
RootFieldAlias ->
|
|
Maybe DataConnectorPreparedQuery ->
|
|
RequestId ->
|
|
HGL.QueryLog
|
|
mkQueryLog gqlQuery fieldName maybeQuery requestId =
|
|
HGL.QueryLog
|
|
gqlQuery
|
|
((\query -> (fieldName, HGL.GeneratedQuery (encodePreparedQueryToJsonText query) J.Null)) <$> maybeQuery)
|
|
requestId
|
|
-- @QueryLogKindDatabase Nothing@ means that the backend doesn't support connection templates
|
|
(HGL.QueryLogKindDatabase Nothing)
|
|
|
|
runDBQueryExplain' ::
|
|
( MonadIO m,
|
|
MonadBaseControl IO m,
|
|
MonadError QErr m,
|
|
Tracing.MonadTrace m
|
|
) =>
|
|
DBStepInfo 'DataConnector ->
|
|
m EncJSON
|
|
runDBQueryExplain' (DBStepInfo _ SourceConfig {..} _ action _) =
|
|
flip runAgentClientT (AgentClientContext nullLogger _scEndpoint _scManager _scTimeoutMicroseconds) $
|
|
runOnBaseMonad action
|
|
|
|
runDBMutation' ::
|
|
( MonadIO m,
|
|
MonadBaseControl IO m,
|
|
MonadError QErr m,
|
|
Tracing.MonadTrace m,
|
|
HGL.MonadQueryLog m
|
|
) =>
|
|
RequestId ->
|
|
GQLReqUnparsed ->
|
|
RootFieldAlias ->
|
|
UserInfo ->
|
|
Logger Hasura ->
|
|
SourceConfig ->
|
|
OnBaseMonad AgentClientT a ->
|
|
Maybe DataConnectorPreparedQuery ->
|
|
ResolvedConnectionTemplate 'DataConnector ->
|
|
m (DiffTime, a)
|
|
runDBMutation' requestId query fieldName _userInfo logger SourceConfig {..} action queryRequest _ = do
|
|
void $ HGL.logQueryLog logger $ mkQueryLog query fieldName queryRequest requestId
|
|
withElapsedTime
|
|
. Tracing.trace ("Data Connector backend mutation for root field " <>> fieldName)
|
|
. flip runAgentClientT (AgentClientContext logger _scEndpoint _scManager _scTimeoutMicroseconds)
|
|
. runOnBaseMonad
|
|
$ action
|