{-# OPTIONS_GHC -fno-warn-orphans #-}

module Hasura.Backends.DataConnector.Adapter.Transport () where

--------------------------------------------------------------------------------

import Control.Concurrent.STM
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, AgentLicenseKey (..), runAgentClientT)
import Hasura.Base.Error (QErr (..))
import Hasura.CredentialCache
import Hasura.EncJSON (EncJSON)
import Hasura.GraphQL.Execute.Backend (DBStepInfo (..), OnBaseMonad (..), arResult)
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.RQL.Types.BackendType (BackendType (DataConnector))
import Hasura.SQL.AnyBackend (AnyBackend)
import Hasura.Server.Types (RequestId)
import Hasura.Session (UserInfo)
import Hasura.Tracing qualified as Tracing

--------------------------------------------------------------------------------

instance BackendTransport 'DataConnector where
  runDBQuery :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadError QErr m,
 MonadQueryLog m, MonadExecutionLog m, MonadTrace m) =>
RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig 'DataConnector
-> OnBaseMonad
     (ExecutionMonad 'DataConnector)
     (Maybe (AnyBackend ExecutionStats), EncJSON)
-> Maybe (PreparedQuery 'DataConnector)
-> ResolvedConnectionTemplate 'DataConnector
-> m (DiffTime, EncJSON)
runDBQuery = RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig 'DataConnector
-> OnBaseMonad
     (ExecutionMonad 'DataConnector)
     (Maybe (AnyBackend ExecutionStats), EncJSON)
-> Maybe (PreparedQuery 'DataConnector)
-> ResolvedConnectionTemplate 'DataConnector
-> m (DiffTime, EncJSON)
RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig
-> OnBaseMonad
     AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
-> Maybe DataConnectorPreparedQuery
-> ResolvedConnectionTemplate 'DataConnector
-> m (DiffTime, EncJSON)
forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadError QErr m, MonadTrace m,
 MonadQueryLog m) =>
RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig
-> OnBaseMonad
     AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
-> Maybe DataConnectorPreparedQuery
-> ResolvedConnectionTemplate 'DataConnector
-> m (DiffTime, EncJSON)
runDBQuery'
  runDBQueryExplain :: forall (m :: * -> *).
(MonadIO m, MonadError QErr m, MonadBaseControl IO m,
 MonadTrace m) =>
Maybe (CredentialCache AgentLicenseKey)
-> DBStepInfo 'DataConnector -> m EncJSON
runDBQueryExplain = Maybe (CredentialCache AgentLicenseKey)
-> DBStepInfo 'DataConnector -> m EncJSON
forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadError QErr m,
 MonadTrace m) =>
Maybe (CredentialCache AgentLicenseKey)
-> DBStepInfo 'DataConnector -> m EncJSON
runDBQueryExplain'
  runDBMutation :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadError QErr m,
 MonadQueryLog m, MonadTrace m) =>
RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig 'DataConnector
-> OnBaseMonad (ExecutionMonad 'DataConnector) EncJSON
-> Maybe (PreparedQuery 'DataConnector)
-> ResolvedConnectionTemplate 'DataConnector
-> m (DiffTime, EncJSON)
runDBMutation = RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig 'DataConnector
-> OnBaseMonad (ExecutionMonad 'DataConnector) EncJSON
-> Maybe (PreparedQuery 'DataConnector)
-> ResolvedConnectionTemplate 'DataConnector
-> m (DiffTime, EncJSON)
RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig
-> OnBaseMonad AgentClientT EncJSON
-> Maybe DataConnectorPreparedQuery
-> ResolvedConnectionTemplate 'DataConnector
-> m (DiffTime, EncJSON)
forall (m :: * -> *) a.
(MonadIO m, MonadBaseControl IO m, MonadError QErr m, MonadTrace m,
 MonadQueryLog m) =>
RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig
-> OnBaseMonad AgentClientT a
-> Maybe DataConnectorPreparedQuery
-> ResolvedConnectionTemplate 'DataConnector
-> m (DiffTime, a)
runDBMutation'
  runDBStreamingSubscription :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig 'DataConnector
-> MultiplexedQuery 'DataConnector
-> [(CohortId, CohortVariables)]
-> ResolvedConnectionTemplate 'DataConnector
-> m (DiffTime,
      Either QErr [(CohortId, ByteString, CursorVariableValues)])
runDBStreamingSubscription SourceConfig 'DataConnector
_ MultiplexedQuery 'DataConnector
_ [(CohortId, CohortVariables)]
_ ResolvedConnectionTemplate 'DataConnector
_ =
    IO
  (DiffTime,
   Either QErr [(CohortId, ByteString, CursorVariableValues)])
-> m (DiffTime,
      Either QErr [(CohortId, ByteString, CursorVariableValues)])
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO
   (DiffTime,
    Either QErr [(CohortId, ByteString, CursorVariableValues)])
 -> m (DiffTime,
       Either QErr [(CohortId, ByteString, CursorVariableValues)]))
-> (IOError
    -> IO
         (DiffTime,
          Either QErr [(CohortId, ByteString, CursorVariableValues)]))
-> IOError
-> m (DiffTime,
      Either QErr [(CohortId, ByteString, CursorVariableValues)])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IOError
-> IO
     (DiffTime,
      Either QErr [(CohortId, ByteString, CursorVariableValues)])
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwIO (IOError
 -> m (DiffTime,
       Either QErr [(CohortId, ByteString, CursorVariableValues)]))
-> IOError
-> m (DiffTime,
      Either QErr [(CohortId, ByteString, CursorVariableValues)])
forall a b. (a -> b) -> a -> b
$ String -> IOError
userError String
"runDBStreamingSubscription: not implemented for the Data Connector backend."
  runDBSubscription :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig 'DataConnector
-> MultiplexedQuery 'DataConnector
-> [(CohortId, CohortVariables)]
-> ResolvedConnectionTemplate 'DataConnector
-> m (DiffTime, Either QErr [(CohortId, ByteString)])
runDBSubscription SourceConfig 'DataConnector
_ MultiplexedQuery 'DataConnector
_ [(CohortId, CohortVariables)]
_ ResolvedConnectionTemplate 'DataConnector
_ =
    IO (DiffTime, Either QErr [(CohortId, ByteString)])
-> m (DiffTime, Either QErr [(CohortId, ByteString)])
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (DiffTime, Either QErr [(CohortId, ByteString)])
 -> m (DiffTime, Either QErr [(CohortId, ByteString)]))
-> (IOError -> IO (DiffTime, Either QErr [(CohortId, ByteString)]))
-> IOError
-> m (DiffTime, Either QErr [(CohortId, ByteString)])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IOError -> IO (DiffTime, Either QErr [(CohortId, ByteString)])
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwIO (IOError -> m (DiffTime, Either QErr [(CohortId, ByteString)]))
-> IOError -> m (DiffTime, Either QErr [(CohortId, ByteString)])
forall a b. (a -> b) -> a -> b
$ String -> IOError
userError String
"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 ->
  Maybe (CredentialCache AgentLicenseKey) ->
  SourceConfig ->
  OnBaseMonad AgentClientT (Maybe (AnyBackend HGL.ExecutionStats), EncJSON) ->
  Maybe DataConnectorPreparedQuery ->
  ResolvedConnectionTemplate 'DataConnector ->
  m (DiffTime, EncJSON)
runDBQuery' :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadError QErr m, MonadTrace m,
 MonadQueryLog m) =>
RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig
-> OnBaseMonad
     AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
-> Maybe DataConnectorPreparedQuery
-> ResolvedConnectionTemplate 'DataConnector
-> m (DiffTime, EncJSON)
runDBQuery' RequestId
requestId GQLReqUnparsed
query RootFieldAlias
fieldName UserInfo
_userInfo Logger Hasura
logger Maybe (CredentialCache AgentLicenseKey)
licenseKeyCacheMaybe sourceConfig :: SourceConfig
sourceConfig@SourceConfig {Maybe Int
Maybe Text
Config
Capabilities
Environment
Manager
BaseUrl
DataConnectorName
_scEndpoint :: BaseUrl
_scConfig :: Config
_scTemplate :: Maybe Text
_scCapabilities :: Capabilities
_scManager :: Manager
_scTimeoutMicroseconds :: Maybe Int
_scDataConnectorName :: DataConnectorName
_scEnvironment :: Environment
_scEndpoint :: SourceConfig -> BaseUrl
_scConfig :: SourceConfig -> Config
_scTemplate :: SourceConfig -> Maybe Text
_scCapabilities :: SourceConfig -> Capabilities
_scManager :: SourceConfig -> Manager
_scTimeoutMicroseconds :: SourceConfig -> Maybe Int
_scDataConnectorName :: SourceConfig -> DataConnectorName
_scEnvironment :: SourceConfig -> Environment
..} OnBaseMonad
  AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
action Maybe DataConnectorPreparedQuery
queryRequest ResolvedConnectionTemplate 'DataConnector
_ = do
  Maybe AgentLicenseKey
agentAuthKey <-
    Maybe (CredentialCache AgentLicenseKey)
-> (CredentialCache AgentLicenseKey -> m AgentLicenseKey)
-> m (Maybe AgentLicenseKey)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for Maybe (CredentialCache AgentLicenseKey)
licenseKeyCacheMaybe \CredentialCache AgentLicenseKey
licenseKeyCache -> do
      (AgentLicenseKey
key, STM (STM ())
_requestKeyRefresh) <- IO (AgentLicenseKey, STM (STM ()))
-> m (AgentLicenseKey, STM (STM ()))
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (AgentLicenseKey, STM (STM ()))
 -> m (AgentLicenseKey, STM (STM ())))
-> IO (AgentLicenseKey, STM (STM ()))
-> m (AgentLicenseKey, STM (STM ()))
forall a b. (a -> b) -> a -> b
$ STM (AgentLicenseKey, STM (STM ()))
-> IO (AgentLicenseKey, STM (STM ()))
forall a. STM a -> IO a
atomically (STM (AgentLicenseKey, STM (STM ()))
 -> IO (AgentLicenseKey, STM (STM ())))
-> STM (AgentLicenseKey, STM (STM ()))
-> IO (AgentLicenseKey, STM (STM ()))
forall a b. (a -> b) -> a -> b
$ CredentialCache AgentLicenseKey
-> STM (AgentLicenseKey, STM (STM ()))
forall cred. CredentialCache cred -> STM (cred, STM (STM ()))
getCredential CredentialCache AgentLicenseKey
licenseKeyCache
      -- TODO: If the license key has expired or is otherwise invalid, request a key refresh
      AgentLicenseKey -> m AgentLicenseKey
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AgentLicenseKey
key

  -- TODO: Re-introduce this case statement once we no longer want to
  -- allow CE to attempt GDC requests.
  -- case (_cLicensing _scCapabilities, agentAuthKey) of
  --  (Just _, Nothing) -> throw401 "EE License Key Required."
  --  _ -> do
  m () -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Logger Hasura -> QueryLog -> m ()
forall (m :: * -> *).
MonadQueryLog m =>
Logger Hasura -> QueryLog -> m ()
HGL.logQueryLog Logger Hasura
logger (QueryLog -> m ()) -> QueryLog -> m ()
forall a b. (a -> b) -> a -> b
$ GQLReqUnparsed
-> RootFieldAlias
-> Maybe DataConnectorPreparedQuery
-> RequestId
-> QueryLog
mkQueryLog GQLReqUnparsed
query RootFieldAlias
fieldName Maybe DataConnectorPreparedQuery
queryRequest RequestId
requestId
  m EncJSON -> m (DiffTime, EncJSON)
forall (m :: * -> *) a. MonadIO m => m a -> m (DiffTime, a)
withElapsedTime
    (m EncJSON -> m (DiffTime, EncJSON))
-> (OnBaseMonad
      AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
    -> m EncJSON)
-> OnBaseMonad
     AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
-> m (DiffTime, EncJSON)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> m EncJSON -> m EncJSON
forall (m :: * -> *) a.
(MonadIO m, MonadTrace m) =>
Text -> m a -> m a
Tracing.newSpan (Text
"Data Connector backend query for root field " Text -> RootFieldAlias -> Text
forall t. ToTxt t => Text -> t -> Text
<>> RootFieldAlias
fieldName)
    (m EncJSON -> m EncJSON)
-> (OnBaseMonad
      AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
    -> m EncJSON)
-> OnBaseMonad
     AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
-> m EncJSON
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (m EncJSON -> m () -> m EncJSON
forall a b. m a -> m b -> m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall (b :: BackendType) (m :: * -> *).
(HasSourceConfiguration b, MonadTrace m) =>
SourceConfig b -> m ()
Tracing.attachSourceConfigAttributes @'DataConnector SourceConfig 'DataConnector
SourceConfig
sourceConfig)
    (m EncJSON -> m EncJSON)
-> (OnBaseMonad
      AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
    -> m EncJSON)
-> OnBaseMonad
     AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
-> m EncJSON
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AgentClientT m EncJSON -> AgentClientContext -> m EncJSON)
-> AgentClientContext -> AgentClientT m EncJSON -> m EncJSON
forall a b c. (a -> b -> c) -> b -> a -> c
flip AgentClientT m EncJSON -> AgentClientContext -> m EncJSON
forall (m :: * -> *) a.
AgentClientT m a -> AgentClientContext -> m a
runAgentClientT (Logger Hasura
-> BaseUrl
-> Manager
-> Maybe Int
-> Maybe AgentLicenseKey
-> AgentClientContext
AgentClientContext Logger Hasura
logger BaseUrl
_scEndpoint Manager
_scManager Maybe Int
_scTimeoutMicroseconds Maybe AgentLicenseKey
agentAuthKey)
    (AgentClientT m EncJSON -> m EncJSON)
-> (OnBaseMonad
      AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
    -> AgentClientT m EncJSON)
-> OnBaseMonad
     AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
-> m EncJSON
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OnBaseMonad AgentClientT EncJSON -> AgentClientT m EncJSON
OnBaseMonad AgentClientT EncJSON
-> forall (m :: * -> *).
   (Functor (AgentClientT m), MonadIO m, MonadBaseControl IO m,
    MonadTrace m, MonadError QErr m) =>
   AgentClientT m EncJSON
forall (t :: (* -> *) -> * -> *) a.
OnBaseMonad t a
-> forall (m :: * -> *).
   (Functor (t m), MonadIO m, MonadBaseControl IO m, MonadTrace m,
    MonadError QErr m) =>
   t m a
runOnBaseMonad
    (OnBaseMonad AgentClientT EncJSON -> AgentClientT m EncJSON)
-> (OnBaseMonad
      AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
    -> OnBaseMonad AgentClientT EncJSON)
-> OnBaseMonad
     AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
-> AgentClientT m EncJSON
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Maybe (AnyBackend ExecutionStats), EncJSON) -> EncJSON)
-> OnBaseMonad
     AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
-> OnBaseMonad AgentClientT EncJSON
forall a b.
(a -> b)
-> OnBaseMonad AgentClientT a -> OnBaseMonad AgentClientT b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe (AnyBackend ExecutionStats), EncJSON) -> EncJSON
forall a b. (a, b) -> b
snd
    (OnBaseMonad
   AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
 -> m (DiffTime, EncJSON))
-> OnBaseMonad
     AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
-> m (DiffTime, EncJSON)
forall a b. (a -> b) -> a -> b
$ OnBaseMonad
  AgentClientT (Maybe (AnyBackend ExecutionStats), EncJSON)
action

mkQueryLog ::
  GQLReqUnparsed ->
  RootFieldAlias ->
  Maybe DataConnectorPreparedQuery ->
  RequestId ->
  HGL.QueryLog
mkQueryLog :: GQLReqUnparsed
-> RootFieldAlias
-> Maybe DataConnectorPreparedQuery
-> RequestId
-> QueryLog
mkQueryLog GQLReqUnparsed
gqlQuery RootFieldAlias
fieldName Maybe DataConnectorPreparedQuery
maybeQuery RequestId
requestId =
  GQLReqUnparsed
-> Maybe (RootFieldAlias, GeneratedQuery)
-> RequestId
-> QueryLogKind
-> QueryLog
HGL.QueryLog
    GQLReqUnparsed
gqlQuery
    ((\DataConnectorPreparedQuery
query -> (RootFieldAlias
fieldName, Text -> Value -> GeneratedQuery
HGL.GeneratedQuery (DataConnectorPreparedQuery -> Text
encodePreparedQueryToJsonText DataConnectorPreparedQuery
query) Value
J.Null)) (DataConnectorPreparedQuery -> (RootFieldAlias, GeneratedQuery))
-> Maybe DataConnectorPreparedQuery
-> Maybe (RootFieldAlias, GeneratedQuery)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe DataConnectorPreparedQuery
maybeQuery)
    RequestId
requestId
    -- @QueryLogKindDatabase Nothing@ means that the backend doesn't support connection templates
    (Maybe BackendResolvedConnectionTemplate -> QueryLogKind
HGL.QueryLogKindDatabase Maybe BackendResolvedConnectionTemplate
forall a. Maybe a
Nothing)

runDBQueryExplain' ::
  ( MonadIO m,
    MonadBaseControl IO m,
    MonadError QErr m,
    Tracing.MonadTrace m
  ) =>
  Maybe (CredentialCache AgentLicenseKey) ->
  DBStepInfo 'DataConnector ->
  m EncJSON
runDBQueryExplain' :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadError QErr m,
 MonadTrace m) =>
Maybe (CredentialCache AgentLicenseKey)
-> DBStepInfo 'DataConnector -> m EncJSON
runDBQueryExplain' Maybe (CredentialCache AgentLicenseKey)
licenseKeyCacheMaybe (DBStepInfo SourceName
_ SourceConfig {Maybe Int
Maybe Text
Config
Capabilities
Environment
Manager
BaseUrl
DataConnectorName
_scEndpoint :: SourceConfig -> BaseUrl
_scConfig :: SourceConfig -> Config
_scTemplate :: SourceConfig -> Maybe Text
_scCapabilities :: SourceConfig -> Capabilities
_scManager :: SourceConfig -> Manager
_scTimeoutMicroseconds :: SourceConfig -> Maybe Int
_scDataConnectorName :: SourceConfig -> DataConnectorName
_scEnvironment :: SourceConfig -> Environment
_scEndpoint :: BaseUrl
_scConfig :: Config
_scTemplate :: Maybe Text
_scCapabilities :: Capabilities
_scManager :: Manager
_scTimeoutMicroseconds :: Maybe Int
_scDataConnectorName :: DataConnectorName
_scEnvironment :: Environment
..} Maybe (PreparedQuery 'DataConnector)
_ OnBaseMonad
  (ExecutionMonad 'DataConnector) (ActionResult 'DataConnector)
action ResolvedConnectionTemplate 'DataConnector
_) = do
  Maybe AgentLicenseKey
agentAuthKey <-
    Maybe (CredentialCache AgentLicenseKey)
-> (CredentialCache AgentLicenseKey -> m AgentLicenseKey)
-> m (Maybe AgentLicenseKey)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for Maybe (CredentialCache AgentLicenseKey)
licenseKeyCacheMaybe \CredentialCache AgentLicenseKey
licenseKeyCache -> do
      (AgentLicenseKey
key, STM (STM ())
_requestKeyRefresh) <- IO (AgentLicenseKey, STM (STM ()))
-> m (AgentLicenseKey, STM (STM ()))
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (AgentLicenseKey, STM (STM ()))
 -> m (AgentLicenseKey, STM (STM ())))
-> IO (AgentLicenseKey, STM (STM ()))
-> m (AgentLicenseKey, STM (STM ()))
forall a b. (a -> b) -> a -> b
$ STM (AgentLicenseKey, STM (STM ()))
-> IO (AgentLicenseKey, STM (STM ()))
forall a. STM a -> IO a
atomically (STM (AgentLicenseKey, STM (STM ()))
 -> IO (AgentLicenseKey, STM (STM ())))
-> STM (AgentLicenseKey, STM (STM ()))
-> IO (AgentLicenseKey, STM (STM ()))
forall a b. (a -> b) -> a -> b
$ CredentialCache AgentLicenseKey
-> STM (AgentLicenseKey, STM (STM ()))
forall cred. CredentialCache cred -> STM (cred, STM (STM ()))
getCredential CredentialCache AgentLicenseKey
licenseKeyCache
      -- TODO: If the license key has expired or is otherwise invalid, request a key refresh
      AgentLicenseKey -> m AgentLicenseKey
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AgentLicenseKey
key
  -- TODO: Re-introduce this case statement once we no longer want to
  -- allow CE to attempt GDC requests.
  -- case (_cLicensing _scCapabilities, agentAuthKey) of
  --   (Just _, Nothing) -> throw401 "EE License Key Required."
  --   _ ->
  (AgentClientT m EncJSON -> AgentClientContext -> m EncJSON)
-> AgentClientContext -> AgentClientT m EncJSON -> m EncJSON
forall a b c. (a -> b -> c) -> b -> a -> c
flip AgentClientT m EncJSON -> AgentClientContext -> m EncJSON
forall (m :: * -> *) a.
AgentClientT m a -> AgentClientContext -> m a
runAgentClientT (Logger Hasura
-> BaseUrl
-> Manager
-> Maybe Int
-> Maybe AgentLicenseKey
-> AgentClientContext
AgentClientContext Logger Hasura
nullLogger BaseUrl
_scEndpoint Manager
_scManager Maybe Int
_scTimeoutMicroseconds Maybe AgentLicenseKey
agentAuthKey)
    (AgentClientT m EncJSON -> m EncJSON)
-> (AgentClientT m (ActionResult 'DataConnector)
    -> AgentClientT m EncJSON)
-> AgentClientT m (ActionResult 'DataConnector)
-> m EncJSON
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ActionResult 'DataConnector -> EncJSON)
-> AgentClientT m (ActionResult 'DataConnector)
-> AgentClientT m EncJSON
forall a b. (a -> b) -> AgentClientT m a -> AgentClientT m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ActionResult 'DataConnector -> EncJSON
forall (b :: BackendType). ActionResult b -> EncJSON
arResult
    (AgentClientT m (ActionResult 'DataConnector) -> m EncJSON)
-> AgentClientT m (ActionResult 'DataConnector) -> m EncJSON
forall a b. (a -> b) -> a -> b
$ OnBaseMonad AgentClientT (ActionResult 'DataConnector)
-> forall (m :: * -> *).
   (Functor (AgentClientT m), MonadIO m, MonadBaseControl IO m,
    MonadTrace m, MonadError QErr m) =>
   AgentClientT m (ActionResult 'DataConnector)
forall (t :: (* -> *) -> * -> *) a.
OnBaseMonad t a
-> forall (m :: * -> *).
   (Functor (t m), MonadIO m, MonadBaseControl IO m, MonadTrace m,
    MonadError QErr m) =>
   t m a
runOnBaseMonad OnBaseMonad AgentClientT (ActionResult 'DataConnector)
OnBaseMonad
  (ExecutionMonad 'DataConnector) (ActionResult 'DataConnector)
action

runDBMutation' ::
  ( MonadIO m,
    MonadBaseControl IO m,
    MonadError QErr m,
    Tracing.MonadTrace m,
    HGL.MonadQueryLog m
  ) =>
  RequestId ->
  GQLReqUnparsed ->
  RootFieldAlias ->
  UserInfo ->
  Logger Hasura ->
  Maybe (CredentialCache AgentLicenseKey) ->
  SourceConfig ->
  OnBaseMonad AgentClientT a ->
  Maybe DataConnectorPreparedQuery ->
  ResolvedConnectionTemplate 'DataConnector ->
  m (DiffTime, a)
runDBMutation' :: forall (m :: * -> *) a.
(MonadIO m, MonadBaseControl IO m, MonadError QErr m, MonadTrace m,
 MonadQueryLog m) =>
RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig
-> OnBaseMonad AgentClientT a
-> Maybe DataConnectorPreparedQuery
-> ResolvedConnectionTemplate 'DataConnector
-> m (DiffTime, a)
runDBMutation' RequestId
requestId GQLReqUnparsed
query RootFieldAlias
fieldName UserInfo
_userInfo Logger Hasura
logger Maybe (CredentialCache AgentLicenseKey)
licenseKeyCacheMaybe sourceConfig :: SourceConfig
sourceConfig@SourceConfig {Maybe Int
Maybe Text
Config
Capabilities
Environment
Manager
BaseUrl
DataConnectorName
_scEndpoint :: SourceConfig -> BaseUrl
_scConfig :: SourceConfig -> Config
_scTemplate :: SourceConfig -> Maybe Text
_scCapabilities :: SourceConfig -> Capabilities
_scManager :: SourceConfig -> Manager
_scTimeoutMicroseconds :: SourceConfig -> Maybe Int
_scDataConnectorName :: SourceConfig -> DataConnectorName
_scEnvironment :: SourceConfig -> Environment
_scEndpoint :: BaseUrl
_scConfig :: Config
_scTemplate :: Maybe Text
_scCapabilities :: Capabilities
_scManager :: Manager
_scTimeoutMicroseconds :: Maybe Int
_scDataConnectorName :: DataConnectorName
_scEnvironment :: Environment
..} OnBaseMonad AgentClientT a
action Maybe DataConnectorPreparedQuery
queryRequest ResolvedConnectionTemplate 'DataConnector
_ = do
  Maybe AgentLicenseKey
agentAuthKey <-
    Maybe (CredentialCache AgentLicenseKey)
-> (CredentialCache AgentLicenseKey -> m AgentLicenseKey)
-> m (Maybe AgentLicenseKey)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for Maybe (CredentialCache AgentLicenseKey)
licenseKeyCacheMaybe \CredentialCache AgentLicenseKey
licenseKeyCache -> do
      (AgentLicenseKey
key, STM (STM ())
_requestKeyRefresh) <- IO (AgentLicenseKey, STM (STM ()))
-> m (AgentLicenseKey, STM (STM ()))
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (AgentLicenseKey, STM (STM ()))
 -> m (AgentLicenseKey, STM (STM ())))
-> IO (AgentLicenseKey, STM (STM ()))
-> m (AgentLicenseKey, STM (STM ()))
forall a b. (a -> b) -> a -> b
$ STM (AgentLicenseKey, STM (STM ()))
-> IO (AgentLicenseKey, STM (STM ()))
forall a. STM a -> IO a
atomically (STM (AgentLicenseKey, STM (STM ()))
 -> IO (AgentLicenseKey, STM (STM ())))
-> STM (AgentLicenseKey, STM (STM ()))
-> IO (AgentLicenseKey, STM (STM ()))
forall a b. (a -> b) -> a -> b
$ CredentialCache AgentLicenseKey
-> STM (AgentLicenseKey, STM (STM ()))
forall cred. CredentialCache cred -> STM (cred, STM (STM ()))
getCredential CredentialCache AgentLicenseKey
licenseKeyCache
      -- TODO: If the license key has expired or is otherwise invalid, request a key refresh
      AgentLicenseKey -> m AgentLicenseKey
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AgentLicenseKey
key

  -- TODO: Re-introduce this case statement once we no longer want to
  -- allow CE to attempt GDC requests.
  -- case (_cLicensing _scCapabilities, agentAuthKey) of
  --   (Just _, Nothing) -> throw401 "EE License Key Required."
  --   _ -> do
  m () -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Logger Hasura -> QueryLog -> m ()
forall (m :: * -> *).
MonadQueryLog m =>
Logger Hasura -> QueryLog -> m ()
HGL.logQueryLog Logger Hasura
logger (QueryLog -> m ()) -> QueryLog -> m ()
forall a b. (a -> b) -> a -> b
$ GQLReqUnparsed
-> RootFieldAlias
-> Maybe DataConnectorPreparedQuery
-> RequestId
-> QueryLog
mkQueryLog GQLReqUnparsed
query RootFieldAlias
fieldName Maybe DataConnectorPreparedQuery
queryRequest RequestId
requestId
  m a -> m (DiffTime, a)
forall (m :: * -> *) a. MonadIO m => m a -> m (DiffTime, a)
withElapsedTime
    (m a -> m (DiffTime, a))
-> (OnBaseMonad AgentClientT a -> m a)
-> OnBaseMonad AgentClientT a
-> m (DiffTime, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> m a -> m a
forall (m :: * -> *) a.
(MonadIO m, MonadTrace m) =>
Text -> m a -> m a
Tracing.newSpan (Text
"Data Connector backend mutation for root field " Text -> RootFieldAlias -> Text
forall t. ToTxt t => Text -> t -> Text
<>> RootFieldAlias
fieldName)
    (m a -> m a)
-> (OnBaseMonad AgentClientT a -> m a)
-> OnBaseMonad AgentClientT a
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (m a -> m () -> m a
forall a b. m a -> m b -> m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall (b :: BackendType) (m :: * -> *).
(HasSourceConfiguration b, MonadTrace m) =>
SourceConfig b -> m ()
Tracing.attachSourceConfigAttributes @'DataConnector SourceConfig 'DataConnector
SourceConfig
sourceConfig)
    (m a -> m a)
-> (OnBaseMonad AgentClientT a -> m a)
-> OnBaseMonad AgentClientT a
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AgentClientT m a -> AgentClientContext -> m a)
-> AgentClientContext -> AgentClientT m a -> m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip AgentClientT m a -> AgentClientContext -> m a
forall (m :: * -> *) a.
AgentClientT m a -> AgentClientContext -> m a
runAgentClientT (Logger Hasura
-> BaseUrl
-> Manager
-> Maybe Int
-> Maybe AgentLicenseKey
-> AgentClientContext
AgentClientContext Logger Hasura
logger BaseUrl
_scEndpoint Manager
_scManager Maybe Int
_scTimeoutMicroseconds Maybe AgentLicenseKey
agentAuthKey)
    (AgentClientT m a -> m a)
-> (OnBaseMonad AgentClientT a -> AgentClientT m a)
-> OnBaseMonad AgentClientT a
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OnBaseMonad AgentClientT a -> AgentClientT m a
OnBaseMonad AgentClientT a
-> forall (m :: * -> *).
   (Functor (AgentClientT m), MonadIO m, MonadBaseControl IO m,
    MonadTrace m, MonadError QErr m) =>
   AgentClientT m a
forall (t :: (* -> *) -> * -> *) a.
OnBaseMonad t a
-> forall (m :: * -> *).
   (Functor (t m), MonadIO m, MonadBaseControl IO m, MonadTrace m,
    MonadError QErr m) =>
   t m a
runOnBaseMonad
    (OnBaseMonad AgentClientT a -> m (DiffTime, a))
-> OnBaseMonad AgentClientT a -> m (DiffTime, a)
forall a b. (a -> b) -> a -> b
$ OnBaseMonad AgentClientT a
action