{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Hasura.Backends.Postgres.Instances.Transport
( runPGMutationTransaction,
)
where
import Control.Monad.Trans.Control
import Data.Aeson qualified as J
import Data.ByteString qualified as B
import Data.HashMap.Strict.InsOrd qualified as InsOrdHashMap
import Data.Text.Extended
import Database.PG.Query qualified as PG
import Hasura.Backends.DataConnector.Agent.Client (AgentLicenseKey)
import Hasura.Backends.Postgres.Connection.MonadTx
import Hasura.Backends.Postgres.Execute.Subscription qualified as PGL
import Hasura.Backends.Postgres.Execute.Types
import Hasura.Backends.Postgres.Instances.Execute qualified as EQ
import Hasura.Backends.Postgres.SQL.Value
import Hasura.Backends.Postgres.Translate.Select (PostgresAnnotatedFieldJSON)
import Hasura.Base.Error
import Hasura.CredentialCache
import Hasura.EncJSON
import Hasura.GraphQL.Execute.Backend
import Hasura.GraphQL.Execute.Subscription.Plan
import Hasura.GraphQL.Logging
import Hasura.GraphQL.Namespace
( RootFieldAlias,
RootFieldMap,
mkUnNamespacedRootFieldAlias,
)
import Hasura.GraphQL.Transport.Backend
import Hasura.GraphQL.Transport.HTTP.Protocol
import Hasura.Logging qualified as L
import Hasura.Name qualified as Name
import Hasura.Prelude
import Hasura.RQL.DDL.ConnectionTemplate (BackendResolvedConnectionTemplate (..), ResolvedConnectionTemplateWrapper (..))
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.BackendTag (HasTag)
import Hasura.RQL.Types.BackendType
import Hasura.SQL.AnyBackend qualified as AB
import Hasura.Server.Types (RequestId)
import Hasura.Session
import Hasura.Tracing
instance
( Backend ('Postgres pgKind),
PostgresAnnotatedFieldJSON pgKind
) =>
BackendTransport ('Postgres pgKind)
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 ('Postgres pgKind)
-> OnBaseMonad
(ExecutionMonad ('Postgres pgKind))
(Maybe (AnyBackend ExecutionStats), EncJSON)
-> Maybe (PreparedQuery ('Postgres pgKind))
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime, EncJSON)
runDBQuery = forall (pgKind :: PostgresKind) (m :: * -> *).
(HasTag ('Postgres pgKind), MonadIO m, MonadBaseControl IO m,
MonadError QErr m, MonadQueryLog m, MonadTrace m) =>
RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig ('Postgres pgKind)
-> OnBaseMonad
(TxET QErr) (Maybe (AnyBackend ExecutionStats), EncJSON)
-> Maybe PreparedSql
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime, EncJSON)
runPGQuery @pgKind
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 ('Postgres pgKind)
-> OnBaseMonad (ExecutionMonad ('Postgres pgKind)) EncJSON
-> Maybe (PreparedQuery ('Postgres pgKind))
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime, EncJSON)
runDBMutation = forall (pgKind :: PostgresKind) (m :: * -> *).
(HasTag ('Postgres pgKind), MonadIO m, MonadBaseControl IO m,
MonadError QErr m, MonadQueryLog m, MonadTrace m) =>
RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig ('Postgres pgKind)
-> OnBaseMonad (TxET QErr) EncJSON
-> Maybe PreparedSql
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime, EncJSON)
runPGMutation @pgKind
runDBSubscription :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig ('Postgres pgKind)
-> MultiplexedQuery ('Postgres pgKind)
-> [(CohortId, CohortVariables)]
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime, Either QErr [(CohortId, ByteString)])
runDBSubscription = SourceConfig ('Postgres pgKind)
-> MultiplexedQuery ('Postgres pgKind)
-> [(CohortId, CohortVariables)]
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime, Either QErr [(CohortId, ByteString)])
SourceConfig ('Postgres Any)
-> MultiplexedQuery ('Postgres Any)
-> [(CohortId, CohortVariables)]
-> ResolvedConnectionTemplate ('Postgres Any)
-> m (DiffTime, Either QErr [(CohortId, ByteString)])
forall (m :: * -> *) (pgKind :: PostgresKind).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig ('Postgres pgKind)
-> MultiplexedQuery ('Postgres pgKind)
-> [(CohortId, CohortVariables)]
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime, Either QErr [(CohortId, ByteString)])
runPGSubscription
runDBStreamingSubscription :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig ('Postgres pgKind)
-> MultiplexedQuery ('Postgres pgKind)
-> [(CohortId, CohortVariables)]
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime,
Either QErr [(CohortId, ByteString, CursorVariableValues)])
runDBStreamingSubscription = SourceConfig ('Postgres pgKind)
-> MultiplexedQuery ('Postgres pgKind)
-> [(CohortId, CohortVariables)]
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime,
Either QErr [(CohortId, ByteString, CursorVariableValues)])
SourceConfig ('Postgres Any)
-> MultiplexedQuery ('Postgres Any)
-> [(CohortId, CohortVariables)]
-> ResolvedConnectionTemplate ('Postgres Any)
-> m (DiffTime,
Either QErr [(CohortId, ByteString, CursorVariableValues)])
forall (m :: * -> *) (pgKind :: PostgresKind).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig ('Postgres pgKind)
-> MultiplexedQuery ('Postgres pgKind)
-> [(CohortId, CohortVariables)]
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime,
Either QErr [(CohortId, ByteString, CursorVariableValues)])
runPGStreamingSubscription
runDBQueryExplain :: forall (m :: * -> *).
(MonadIO m, MonadError QErr m, MonadBaseControl IO m,
MonadTrace m) =>
Maybe (CredentialCache AgentLicenseKey)
-> DBStepInfo ('Postgres pgKind) -> m EncJSON
runDBQueryExplain = Maybe (CredentialCache AgentLicenseKey)
-> DBStepInfo ('Postgres pgKind) -> m EncJSON
forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadError QErr m,
MonadTrace m) =>
Maybe (CredentialCache AgentLicenseKey)
-> DBStepInfo ('Postgres pgKind) -> m EncJSON
runPGQueryExplain
runPGQuery ::
forall pgKind m.
( HasTag ('Postgres pgKind),
MonadIO m,
MonadBaseControl IO m,
MonadError QErr m,
MonadQueryLog m,
MonadTrace m
) =>
RequestId ->
GQLReqUnparsed ->
RootFieldAlias ->
UserInfo ->
L.Logger L.Hasura ->
Maybe (CredentialCache AgentLicenseKey) ->
SourceConfig ('Postgres pgKind) ->
OnBaseMonad (PG.TxET QErr) (Maybe (AB.AnyBackend ExecutionStats), EncJSON) ->
Maybe EQ.PreparedSql ->
ResolvedConnectionTemplate ('Postgres pgKind) ->
m (DiffTime, EncJSON)
runPGQuery :: forall (pgKind :: PostgresKind) (m :: * -> *).
(HasTag ('Postgres pgKind), MonadIO m, MonadBaseControl IO m,
MonadError QErr m, MonadQueryLog m, MonadTrace m) =>
RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig ('Postgres pgKind)
-> OnBaseMonad
(TxET QErr) (Maybe (AnyBackend ExecutionStats), EncJSON)
-> Maybe PreparedSql
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime, EncJSON)
runPGQuery RequestId
reqId GQLReqUnparsed
query RootFieldAlias
fieldName UserInfo
_userInfo Logger Hasura
logger Maybe (CredentialCache AgentLicenseKey)
_ SourceConfig ('Postgres pgKind)
sourceConfig OnBaseMonad
(TxET QErr) (Maybe (AnyBackend ExecutionStats), EncJSON)
tx Maybe PreparedSql
genSql ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate = do
Logger Hasura -> QueryLog -> m ()
forall (m :: * -> *).
MonadQueryLog m =>
Logger Hasura -> QueryLog -> m ()
logQueryLog Logger Hasura
logger (QueryLog -> m ()) -> QueryLog -> m ()
forall a b. (a -> b) -> a -> b
$ GQLReqUnparsed
-> RootFieldAlias
-> Maybe PreparedSql
-> RequestId
-> Maybe (ResolvedConnectionTemplate ('Postgres Any))
-> QueryLog
forall (pgKind :: PostgresKind).
GQLReqUnparsed
-> RootFieldAlias
-> Maybe PreparedSql
-> RequestId
-> Maybe (ResolvedConnectionTemplate ('Postgres pgKind))
-> QueryLog
mkQueryLog GQLReqUnparsed
query RootFieldAlias
fieldName Maybe PreparedSql
genSql RequestId
reqId (Maybe PostgresResolvedConnectionTemplate
ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate Maybe PostgresResolvedConnectionTemplate
-> Maybe PostgresResolvedConnectionTemplate
-> Maybe (Maybe PostgresResolvedConnectionTemplate)
forall a b. a -> Maybe b -> Maybe a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Maybe PostgresResolvedConnectionTemplate
ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate)
m EncJSON -> m (DiffTime, EncJSON)
forall (m :: * -> *) a. MonadIO m => m a -> m (DiffTime, a)
withElapsedTime
(m EncJSON -> m (DiffTime, EncJSON))
-> m EncJSON -> m (DiffTime, EncJSON)
forall a b. (a -> b) -> a -> b
$ Text -> m EncJSON -> m EncJSON
forall (m :: * -> *) a.
(MonadIO m, MonadTrace m) =>
Text -> m a -> m a
newSpan (Text
"Postgres Query for root field " Text -> RootFieldAlias -> Text
forall t. ToTxt t => Text -> t -> Text
<>> RootFieldAlias
fieldName)
(m EncJSON -> m EncJSON) -> m EncJSON -> m EncJSON
forall a b. (a -> b) -> a -> b
$ (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 ()
attachSourceConfigAttributes @('Postgres pgKind) SourceConfig ('Postgres pgKind)
sourceConfig)
(m EncJSON -> m EncJSON) -> m EncJSON -> m EncJSON
forall a b. (a -> b) -> a -> b
$ PGExecCtx -> PGExecFrom -> TxET QErr m EncJSON -> m EncJSON
forall (m :: * -> *) a.
(MonadIO m, MonadBaseControl IO m, MonadError QErr m) =>
PGExecCtx -> PGExecFrom -> TxET QErr m a -> m a
runQueryTx (PGSourceConfig -> PGExecCtx
_pscExecCtx SourceConfig ('Postgres pgKind)
PGSourceConfig
sourceConfig) (Maybe PostgresResolvedConnectionTemplate -> PGExecFrom
GraphQLQuery Maybe PostgresResolvedConnectionTemplate
ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate)
(TxET QErr m EncJSON -> m EncJSON)
-> TxET QErr m EncJSON -> m EncJSON
forall a b. (a -> b) -> a -> b
$ ((Maybe (AnyBackend ExecutionStats), EncJSON) -> EncJSON)
-> TxET QErr m (Maybe (AnyBackend ExecutionStats), EncJSON)
-> TxET QErr m EncJSON
forall a b. (a -> b) -> TxET QErr m a -> TxET QErr m 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
(TxET QErr) (Maybe (AnyBackend ExecutionStats), EncJSON)
-> forall (m :: * -> *).
(Functor (TxET QErr m), MonadIO m, MonadBaseControl IO m,
MonadTrace m, MonadError QErr m) =>
TxET QErr m (Maybe (AnyBackend ExecutionStats), 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
(TxET QErr) (Maybe (AnyBackend ExecutionStats), EncJSON)
tx)
runPGMutation ::
forall pgKind m.
( HasTag ('Postgres pgKind),
MonadIO m,
MonadBaseControl IO m,
MonadError QErr m,
MonadQueryLog m,
MonadTrace m
) =>
RequestId ->
GQLReqUnparsed ->
RootFieldAlias ->
UserInfo ->
L.Logger L.Hasura ->
Maybe (CredentialCache AgentLicenseKey) ->
SourceConfig ('Postgres pgKind) ->
OnBaseMonad (PG.TxET QErr) EncJSON ->
Maybe EQ.PreparedSql ->
ResolvedConnectionTemplate ('Postgres pgKind) ->
m (DiffTime, EncJSON)
runPGMutation :: forall (pgKind :: PostgresKind) (m :: * -> *).
(HasTag ('Postgres pgKind), MonadIO m, MonadBaseControl IO m,
MonadError QErr m, MonadQueryLog m, MonadTrace m) =>
RequestId
-> GQLReqUnparsed
-> RootFieldAlias
-> UserInfo
-> Logger Hasura
-> Maybe (CredentialCache AgentLicenseKey)
-> SourceConfig ('Postgres pgKind)
-> OnBaseMonad (TxET QErr) EncJSON
-> Maybe PreparedSql
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime, EncJSON)
runPGMutation RequestId
reqId GQLReqUnparsed
query RootFieldAlias
fieldName UserInfo
userInfo Logger Hasura
logger Maybe (CredentialCache AgentLicenseKey)
_ SourceConfig ('Postgres pgKind)
sourceConfig OnBaseMonad (TxET QErr) EncJSON
tx Maybe PreparedSql
_genSql ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate = do
Logger Hasura -> QueryLog -> m ()
forall (m :: * -> *).
MonadQueryLog m =>
Logger Hasura -> QueryLog -> m ()
logQueryLog Logger Hasura
logger (QueryLog -> m ()) -> QueryLog -> m ()
forall a b. (a -> b) -> a -> b
$ GQLReqUnparsed
-> RootFieldAlias
-> Maybe PreparedSql
-> RequestId
-> Maybe (ResolvedConnectionTemplate ('Postgres Any))
-> QueryLog
forall (pgKind :: PostgresKind).
GQLReqUnparsed
-> RootFieldAlias
-> Maybe PreparedSql
-> RequestId
-> Maybe (ResolvedConnectionTemplate ('Postgres pgKind))
-> QueryLog
mkQueryLog GQLReqUnparsed
query RootFieldAlias
fieldName Maybe PreparedSql
forall a. Maybe a
Nothing RequestId
reqId (Maybe PostgresResolvedConnectionTemplate
ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate Maybe PostgresResolvedConnectionTemplate
-> Maybe PostgresResolvedConnectionTemplate
-> Maybe (Maybe PostgresResolvedConnectionTemplate)
forall a b. a -> Maybe b -> Maybe a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Maybe PostgresResolvedConnectionTemplate
ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate)
m EncJSON -> m (DiffTime, EncJSON)
forall (m :: * -> *) a. MonadIO m => m a -> m (DiffTime, a)
withElapsedTime
(m EncJSON -> m (DiffTime, EncJSON))
-> m EncJSON -> m (DiffTime, EncJSON)
forall a b. (a -> b) -> a -> b
$ Text -> m EncJSON -> m EncJSON
forall (m :: * -> *) a.
(MonadIO m, MonadTrace m) =>
Text -> m a -> m a
newSpan (Text
"Postgres Mutation for root field " Text -> RootFieldAlias -> Text
forall t. ToTxt t => Text -> t -> Text
<>> RootFieldAlias
fieldName)
(m EncJSON -> m EncJSON) -> m EncJSON -> m EncJSON
forall a b. (a -> b) -> a -> b
$ (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 ()
attachSourceConfigAttributes @('Postgres pgKind) SourceConfig ('Postgres pgKind)
sourceConfig)
(m EncJSON -> m EncJSON) -> m EncJSON -> m EncJSON
forall a b. (a -> b) -> a -> b
$ UserInfo
-> PGExecCtx
-> PGExecTxType
-> PGExecFrom
-> TxET QErr m EncJSON
-> m EncJSON
forall (m :: * -> *) a.
(MonadIO m, MonadBaseControl IO m, MonadError QErr m,
MonadTrace m) =>
UserInfo
-> PGExecCtx -> PGExecTxType -> PGExecFrom -> TxET QErr m a -> m a
runTxWithCtxAndUserInfo UserInfo
userInfo (PGSourceConfig -> PGExecCtx
_pscExecCtx SourceConfig ('Postgres pgKind)
PGSourceConfig
sourceConfig) (TxAccess -> Maybe TxIsolation -> PGExecTxType
Tx TxAccess
PG.ReadWrite Maybe TxIsolation
forall a. Maybe a
Nothing) (Maybe PostgresResolvedConnectionTemplate -> PGExecFrom
GraphQLQuery Maybe PostgresResolvedConnectionTemplate
ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate)
(TxET QErr m EncJSON -> m EncJSON)
-> TxET QErr m EncJSON -> m EncJSON
forall a b. (a -> b) -> a -> b
$ OnBaseMonad (TxET QErr) EncJSON
-> forall (m :: * -> *).
(Functor (TxET QErr m), MonadIO m, MonadBaseControl IO m,
MonadTrace m, MonadError QErr m) =>
TxET QErr 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 (TxET QErr) EncJSON
tx
runPGSubscription ::
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig ('Postgres pgKind) ->
MultiplexedQuery ('Postgres pgKind) ->
[(CohortId, CohortVariables)] ->
ResolvedConnectionTemplate ('Postgres pgKind) ->
m (DiffTime, Either QErr [(CohortId, B.ByteString)])
runPGSubscription :: forall (m :: * -> *) (pgKind :: PostgresKind).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig ('Postgres pgKind)
-> MultiplexedQuery ('Postgres pgKind)
-> [(CohortId, CohortVariables)]
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime, Either QErr [(CohortId, ByteString)])
runPGSubscription SourceConfig ('Postgres pgKind)
sourceConfig MultiplexedQuery ('Postgres pgKind)
query [(CohortId, CohortVariables)]
variables ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate =
m (Either QErr [(CohortId, ByteString)])
-> m (DiffTime, Either QErr [(CohortId, ByteString)])
forall (m :: * -> *) a. MonadIO m => m a -> m (DiffTime, a)
withElapsedTime
(m (Either QErr [(CohortId, ByteString)])
-> m (DiffTime, Either QErr [(CohortId, ByteString)]))
-> m (Either QErr [(CohortId, ByteString)])
-> m (DiffTime, Either QErr [(CohortId, ByteString)])
forall a b. (a -> b) -> a -> b
$ ExceptT QErr m [(CohortId, ByteString)]
-> m (Either QErr [(CohortId, ByteString)])
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT
(ExceptT QErr m [(CohortId, ByteString)]
-> m (Either QErr [(CohortId, ByteString)]))
-> ExceptT QErr m [(CohortId, ByteString)]
-> m (Either QErr [(CohortId, ByteString)])
forall a b. (a -> b) -> a -> b
$ PGExecCtx
-> PGExecFrom
-> TxET QErr (ExceptT QErr m) [(CohortId, ByteString)]
-> ExceptT QErr m [(CohortId, ByteString)]
forall (m :: * -> *) a.
(MonadIO m, MonadBaseControl IO m, MonadError QErr m) =>
PGExecCtx -> PGExecFrom -> TxET QErr m a -> m a
runQueryTx (PGSourceConfig -> PGExecCtx
_pscExecCtx SourceConfig ('Postgres pgKind)
PGSourceConfig
sourceConfig) (Maybe PostgresResolvedConnectionTemplate -> PGExecFrom
GraphQLQuery Maybe PostgresResolvedConnectionTemplate
ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate)
(TxET QErr (ExceptT QErr m) [(CohortId, ByteString)]
-> ExceptT QErr m [(CohortId, ByteString)])
-> TxET QErr (ExceptT QErr m) [(CohortId, ByteString)]
-> ExceptT QErr m [(CohortId, ByteString)]
forall a b. (a -> b) -> a -> b
$ MultiplexedQuery
-> [(CohortId, CohortVariables)]
-> TxET QErr (ExceptT QErr m) [(CohortId, ByteString)]
forall (m :: * -> *).
MonadTx m =>
MultiplexedQuery
-> [(CohortId, CohortVariables)] -> m [(CohortId, ByteString)]
PGL.executeMultiplexedQuery MultiplexedQuery
MultiplexedQuery ('Postgres pgKind)
query [(CohortId, CohortVariables)]
variables
runPGStreamingSubscription ::
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig ('Postgres pgKind) ->
MultiplexedQuery ('Postgres pgKind) ->
[(CohortId, CohortVariables)] ->
ResolvedConnectionTemplate ('Postgres pgKind) ->
m (DiffTime, Either QErr [(CohortId, B.ByteString, CursorVariableValues)])
runPGStreamingSubscription :: forall (m :: * -> *) (pgKind :: PostgresKind).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig ('Postgres pgKind)
-> MultiplexedQuery ('Postgres pgKind)
-> [(CohortId, CohortVariables)]
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> m (DiffTime,
Either QErr [(CohortId, ByteString, CursorVariableValues)])
runPGStreamingSubscription SourceConfig ('Postgres pgKind)
sourceConfig MultiplexedQuery ('Postgres pgKind)
query [(CohortId, CohortVariables)]
variables ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate =
m (Either QErr [(CohortId, ByteString, CursorVariableValues)])
-> m (DiffTime,
Either QErr [(CohortId, ByteString, CursorVariableValues)])
forall (m :: * -> *) a. MonadIO m => m a -> m (DiffTime, a)
withElapsedTime
(m (Either QErr [(CohortId, ByteString, CursorVariableValues)])
-> m (DiffTime,
Either QErr [(CohortId, ByteString, CursorVariableValues)]))
-> m (Either QErr [(CohortId, ByteString, CursorVariableValues)])
-> m (DiffTime,
Either QErr [(CohortId, ByteString, CursorVariableValues)])
forall a b. (a -> b) -> a -> b
$ ExceptT QErr m [(CohortId, ByteString, CursorVariableValues)]
-> m (Either QErr [(CohortId, ByteString, CursorVariableValues)])
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT
(ExceptT QErr m [(CohortId, ByteString, CursorVariableValues)]
-> m (Either QErr [(CohortId, ByteString, CursorVariableValues)]))
-> ExceptT QErr m [(CohortId, ByteString, CursorVariableValues)]
-> m (Either QErr [(CohortId, ByteString, CursorVariableValues)])
forall a b. (a -> b) -> a -> b
$ do
[(CohortId, ByteString, ViaJSON CursorVariableValues)]
res <- PGExecCtx
-> PGExecFrom
-> TxET
QErr
(ExceptT QErr m)
[(CohortId, ByteString, ViaJSON CursorVariableValues)]
-> ExceptT
QErr m [(CohortId, ByteString, ViaJSON CursorVariableValues)]
forall (m :: * -> *) a.
(MonadIO m, MonadBaseControl IO m, MonadError QErr m) =>
PGExecCtx -> PGExecFrom -> TxET QErr m a -> m a
runQueryTx (PGSourceConfig -> PGExecCtx
_pscExecCtx SourceConfig ('Postgres pgKind)
PGSourceConfig
sourceConfig) (Maybe PostgresResolvedConnectionTemplate -> PGExecFrom
GraphQLQuery Maybe PostgresResolvedConnectionTemplate
ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate) (TxET
QErr
(ExceptT QErr m)
[(CohortId, ByteString, ViaJSON CursorVariableValues)]
-> ExceptT
QErr m [(CohortId, ByteString, ViaJSON CursorVariableValues)])
-> TxET
QErr
(ExceptT QErr m)
[(CohortId, ByteString, ViaJSON CursorVariableValues)]
-> ExceptT
QErr m [(CohortId, ByteString, ViaJSON CursorVariableValues)]
forall a b. (a -> b) -> a -> b
$ MultiplexedQuery
-> [(CohortId, CohortVariables)]
-> TxET
QErr
(ExceptT QErr m)
[(CohortId, ByteString, ViaJSON CursorVariableValues)]
forall (m :: * -> *).
MonadTx m =>
MultiplexedQuery
-> [(CohortId, CohortVariables)]
-> m [(CohortId, ByteString, ViaJSON CursorVariableValues)]
PGL.executeStreamingMultiplexedQuery MultiplexedQuery
MultiplexedQuery ('Postgres pgKind)
query [(CohortId, CohortVariables)]
variables
[(CohortId, ByteString, CursorVariableValues)]
-> ExceptT QErr m [(CohortId, ByteString, CursorVariableValues)]
forall a. a -> ExceptT QErr m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(CohortId, ByteString, CursorVariableValues)]
-> ExceptT QErr m [(CohortId, ByteString, CursorVariableValues)])
-> [(CohortId, ByteString, CursorVariableValues)]
-> ExceptT QErr m [(CohortId, ByteString, CursorVariableValues)]
forall a b. (a -> b) -> a -> b
$ [(CohortId, ByteString, ViaJSON CursorVariableValues)]
res [(CohortId, ByteString, ViaJSON CursorVariableValues)]
-> ((CohortId, ByteString, ViaJSON CursorVariableValues)
-> (CohortId, ByteString, CursorVariableValues))
-> [(CohortId, ByteString, CursorVariableValues)]
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (\(CohortId
cohortId, ByteString
cohortRes, ViaJSON CursorVariableValues
cursorVariableVals) -> (CohortId
cohortId, ByteString
cohortRes, ViaJSON CursorVariableValues -> CursorVariableValues
forall a. ViaJSON a -> a
PG.getViaJSON ViaJSON CursorVariableValues
cursorVariableVals))
runPGQueryExplain ::
forall pgKind m.
( MonadIO m,
MonadBaseControl IO m,
MonadError QErr m,
MonadTrace m
) =>
Maybe (CredentialCache AgentLicenseKey) ->
DBStepInfo ('Postgres pgKind) ->
m EncJSON
runPGQueryExplain :: forall (pgKind :: PostgresKind) (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadError QErr m,
MonadTrace m) =>
Maybe (CredentialCache AgentLicenseKey)
-> DBStepInfo ('Postgres pgKind) -> m EncJSON
runPGQueryExplain Maybe (CredentialCache AgentLicenseKey)
_ (DBStepInfo SourceName
_ SourceConfig ('Postgres pgKind)
sourceConfig Maybe (PreparedQuery ('Postgres pgKind))
_ OnBaseMonad
(ExecutionMonad ('Postgres pgKind))
(ActionResult ('Postgres pgKind))
action ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate) =
PGExecCtx -> PGExecFrom -> TxET QErr m EncJSON -> m EncJSON
forall (m :: * -> *) a.
(MonadIO m, MonadBaseControl IO m, MonadError QErr m) =>
PGExecCtx -> PGExecFrom -> TxET QErr m a -> m a
runQueryTx (PGSourceConfig -> PGExecCtx
_pscExecCtx SourceConfig ('Postgres pgKind)
PGSourceConfig
sourceConfig) (Maybe PostgresResolvedConnectionTemplate -> PGExecFrom
GraphQLQuery Maybe PostgresResolvedConnectionTemplate
ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate)
(TxET QErr m EncJSON -> m EncJSON)
-> TxET QErr m EncJSON -> m EncJSON
forall a b. (a -> b) -> a -> b
$ (ActionResult ('Postgres pgKind) -> EncJSON)
-> TxET QErr m (ActionResult ('Postgres pgKind))
-> TxET QErr m EncJSON
forall a b. (a -> b) -> TxET QErr m a -> TxET QErr m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ActionResult ('Postgres pgKind) -> EncJSON
forall (b :: BackendType). ActionResult b -> EncJSON
arResult (OnBaseMonad (TxET QErr) (ActionResult ('Postgres pgKind))
-> forall (m :: * -> *).
(Functor (TxET QErr m), MonadIO m, MonadBaseControl IO m,
MonadTrace m, MonadError QErr m) =>
TxET QErr m (ActionResult ('Postgres pgKind))
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 (TxET QErr) (ActionResult ('Postgres pgKind))
OnBaseMonad
(ExecutionMonad ('Postgres pgKind))
(ActionResult ('Postgres pgKind))
action)
mkQueryLog ::
GQLReqUnparsed ->
RootFieldAlias ->
Maybe EQ.PreparedSql ->
RequestId ->
Maybe (ResolvedConnectionTemplate ('Postgres pgKind)) ->
QueryLog
mkQueryLog :: forall (pgKind :: PostgresKind).
GQLReqUnparsed
-> RootFieldAlias
-> Maybe PreparedSql
-> RequestId
-> Maybe (ResolvedConnectionTemplate ('Postgres pgKind))
-> QueryLog
mkQueryLog GQLReqUnparsed
gqlQuery RootFieldAlias
fieldName Maybe PreparedSql
preparedSql RequestId
requestId Maybe (ResolvedConnectionTemplate ('Postgres pgKind))
resolvedConnectionTemplate =
GQLReqUnparsed
-> Maybe (RootFieldAlias, GeneratedQuery)
-> RequestId
-> QueryLogKind
-> QueryLog
QueryLog GQLReqUnparsed
gqlQuery ((RootFieldAlias
fieldName,) (GeneratedQuery -> (RootFieldAlias, GeneratedQuery))
-> Maybe GeneratedQuery -> Maybe (RootFieldAlias, GeneratedQuery)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe GeneratedQuery
generatedQuery) RequestId
requestId (Maybe BackendResolvedConnectionTemplate -> QueryLogKind
QueryLogKindDatabase (Maybe PostgresResolvedConnectionTemplate
-> BackendResolvedConnectionTemplate
ResolvedConnectionTemplate ('Postgres Any)
-> BackendResolvedConnectionTemplate
forall (pgKind :: PostgresKind).
ResolvedConnectionTemplate ('Postgres pgKind)
-> BackendResolvedConnectionTemplate
mkBackendResolvedConnectionTemplate (Maybe PostgresResolvedConnectionTemplate
-> BackendResolvedConnectionTemplate)
-> Maybe (Maybe PostgresResolvedConnectionTemplate)
-> Maybe BackendResolvedConnectionTemplate
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Maybe PostgresResolvedConnectionTemplate)
Maybe (ResolvedConnectionTemplate ('Postgres pgKind))
resolvedConnectionTemplate))
where
mkBackendResolvedConnectionTemplate ::
ResolvedConnectionTemplate ('Postgres pgKind) ->
BackendResolvedConnectionTemplate
mkBackendResolvedConnectionTemplate :: forall (pgKind :: PostgresKind).
ResolvedConnectionTemplate ('Postgres pgKind)
-> BackendResolvedConnectionTemplate
mkBackendResolvedConnectionTemplate =
AnyBackend ResolvedConnectionTemplateWrapper
-> BackendResolvedConnectionTemplate
BackendResolvedConnectionTemplate (AnyBackend ResolvedConnectionTemplateWrapper
-> BackendResolvedConnectionTemplate)
-> (Maybe PostgresResolvedConnectionTemplate
-> AnyBackend ResolvedConnectionTemplateWrapper)
-> Maybe PostgresResolvedConnectionTemplate
-> BackendResolvedConnectionTemplate
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
i b -> AnyBackend i
AB.mkAnyBackend @('Postgres 'Vanilla) (ResolvedConnectionTemplateWrapper ('Postgres 'Vanilla)
-> AnyBackend ResolvedConnectionTemplateWrapper)
-> (Maybe PostgresResolvedConnectionTemplate
-> ResolvedConnectionTemplateWrapper ('Postgres 'Vanilla))
-> Maybe PostgresResolvedConnectionTemplate
-> AnyBackend ResolvedConnectionTemplateWrapper
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe PostgresResolvedConnectionTemplate
-> ResolvedConnectionTemplateWrapper ('Postgres 'Vanilla)
ResolvedConnectionTemplate ('Postgres 'Vanilla)
-> ResolvedConnectionTemplateWrapper ('Postgres 'Vanilla)
forall (b :: BackendType).
ResolvedConnectionTemplate b -> ResolvedConnectionTemplateWrapper b
ResolvedConnectionTemplateWrapper
generatedQuery :: Maybe GeneratedQuery
generatedQuery =
Maybe PreparedSql
preparedSql Maybe PreparedSql
-> (PreparedSql -> GeneratedQuery) -> Maybe GeneratedQuery
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(EQ.PreparedSql Query
query PrepArgMap
args) ->
Text -> Value -> GeneratedQuery
GeneratedQuery (Query -> Text
PG.getQueryText Query
query) (IntMap Value -> Value
forall a. ToJSON a => a -> Value
J.toJSON (IntMap Value -> Value) -> IntMap Value -> Value
forall a b. (a -> b) -> a -> b
$ PGScalarValue -> Value
pgScalarValueToJson (PGScalarValue -> Value)
-> ((PrepArg, PGScalarValue) -> PGScalarValue)
-> (PrepArg, PGScalarValue)
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PrepArg, PGScalarValue) -> PGScalarValue
forall a b. (a, b) -> b
snd ((PrepArg, PGScalarValue) -> Value) -> PrepArgMap -> IntMap Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PrepArgMap
args)
runPGMutationTransaction ::
forall pgKind m.
( HasTag ('Postgres pgKind),
MonadIO m,
MonadBaseControl IO m,
MonadError QErr m,
MonadQueryLog m,
MonadTrace m
) =>
RequestId ->
GQLReqUnparsed ->
UserInfo ->
L.Logger L.Hasura ->
SourceConfig ('Postgres pgKind) ->
ResolvedConnectionTemplate ('Postgres pgKind) ->
RootFieldMap (DBStepInfo ('Postgres pgKind)) ->
m (DiffTime, RootFieldMap EncJSON)
runPGMutationTransaction :: forall (pgKind :: PostgresKind) (m :: * -> *).
(HasTag ('Postgres pgKind), MonadIO m, MonadBaseControl IO m,
MonadError QErr m, MonadQueryLog m, MonadTrace m) =>
RequestId
-> GQLReqUnparsed
-> UserInfo
-> Logger Hasura
-> SourceConfig ('Postgres pgKind)
-> ResolvedConnectionTemplate ('Postgres pgKind)
-> RootFieldMap (DBStepInfo ('Postgres pgKind))
-> m (DiffTime, RootFieldMap EncJSON)
runPGMutationTransaction RequestId
reqId GQLReqUnparsed
query UserInfo
userInfo Logger Hasura
logger SourceConfig ('Postgres pgKind)
sourceConfig ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate RootFieldMap (DBStepInfo ('Postgres pgKind))
mutations = do
Logger Hasura -> QueryLog -> m ()
forall (m :: * -> *).
MonadQueryLog m =>
Logger Hasura -> QueryLog -> m ()
logQueryLog Logger Hasura
logger (QueryLog -> m ()) -> QueryLog -> m ()
forall a b. (a -> b) -> a -> b
$ GQLReqUnparsed
-> RootFieldAlias
-> Maybe PreparedSql
-> RequestId
-> Maybe (ResolvedConnectionTemplate ('Postgres Any))
-> QueryLog
forall (pgKind :: PostgresKind).
GQLReqUnparsed
-> RootFieldAlias
-> Maybe PreparedSql
-> RequestId
-> Maybe (ResolvedConnectionTemplate ('Postgres pgKind))
-> QueryLog
mkQueryLog GQLReqUnparsed
query (Name -> RootFieldAlias
mkUnNamespacedRootFieldAlias Name
Name._transaction) Maybe PreparedSql
forall a. Maybe a
Nothing RequestId
reqId (Maybe PostgresResolvedConnectionTemplate
ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate Maybe PostgresResolvedConnectionTemplate
-> Maybe PostgresResolvedConnectionTemplate
-> Maybe (Maybe PostgresResolvedConnectionTemplate)
forall a b. a -> Maybe b -> Maybe a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Maybe PostgresResolvedConnectionTemplate
ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate)
m (RootFieldMap EncJSON) -> m (DiffTime, RootFieldMap EncJSON)
forall (m :: * -> *) a. MonadIO m => m a -> m (DiffTime, a)
withElapsedTime
(m (RootFieldMap EncJSON) -> m (DiffTime, RootFieldMap EncJSON))
-> m (RootFieldMap EncJSON) -> m (DiffTime, RootFieldMap EncJSON)
forall a b. (a -> b) -> a -> b
$ UserInfo
-> PGExecCtx
-> PGExecTxType
-> PGExecFrom
-> TxET QErr m (RootFieldMap EncJSON)
-> m (RootFieldMap EncJSON)
forall (m :: * -> *) a.
(MonadIO m, MonadBaseControl IO m, MonadError QErr m,
MonadTrace m) =>
UserInfo
-> PGExecCtx -> PGExecTxType -> PGExecFrom -> TxET QErr m a -> m a
runTxWithCtxAndUserInfo UserInfo
userInfo (PGSourceConfig -> PGExecCtx
_pscExecCtx SourceConfig ('Postgres pgKind)
PGSourceConfig
sourceConfig) (TxAccess -> Maybe TxIsolation -> PGExecTxType
Tx TxAccess
PG.ReadWrite Maybe TxIsolation
forall a. Maybe a
Nothing) (Maybe PostgresResolvedConnectionTemplate -> PGExecFrom
GraphQLQuery Maybe PostgresResolvedConnectionTemplate
ResolvedConnectionTemplate ('Postgres pgKind)
resolvedConnectionTemplate)
(TxET QErr m (RootFieldMap EncJSON) -> m (RootFieldMap EncJSON))
-> TxET QErr m (RootFieldMap EncJSON) -> m (RootFieldMap EncJSON)
forall a b. (a -> b) -> a -> b
$ ((RootFieldAlias
-> DBStepInfo ('Postgres pgKind) -> TxET QErr m EncJSON)
-> RootFieldMap (DBStepInfo ('Postgres pgKind))
-> TxET QErr m (RootFieldMap EncJSON))
-> RootFieldMap (DBStepInfo ('Postgres pgKind))
-> (RootFieldAlias
-> DBStepInfo ('Postgres pgKind) -> TxET QErr m EncJSON)
-> TxET QErr m (RootFieldMap EncJSON)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (RootFieldAlias
-> DBStepInfo ('Postgres pgKind) -> TxET QErr m EncJSON)
-> RootFieldMap (DBStepInfo ('Postgres pgKind))
-> TxET QErr m (RootFieldMap EncJSON)
forall (f :: * -> *) k a b.
Applicative f =>
(k -> a -> f b) -> InsOrdHashMap k a -> f (InsOrdHashMap k b)
InsOrdHashMap.traverseWithKey RootFieldMap (DBStepInfo ('Postgres pgKind))
mutations \RootFieldAlias
fieldName DBStepInfo ('Postgres pgKind)
dbsi ->
Text -> TxET QErr m EncJSON -> TxET QErr m EncJSON
forall (m :: * -> *) a.
(MonadIO m, MonadTrace m) =>
Text -> m a -> m a
newSpan (Text
"Postgres Mutation for root field " Text -> RootFieldAlias -> Text
forall t. ToTxt t => Text -> t -> Text
<>> RootFieldAlias
fieldName)
(TxET QErr m EncJSON -> TxET QErr m EncJSON)
-> TxET QErr m EncJSON -> TxET QErr m EncJSON
forall a b. (a -> b) -> a -> b
$ (TxET QErr m EncJSON -> TxET QErr m () -> TxET QErr m EncJSON
forall a b. TxET QErr m a -> TxET QErr m b -> TxET QErr 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 ()
attachSourceConfigAttributes @('Postgres pgKind) SourceConfig ('Postgres pgKind)
sourceConfig)
(TxET QErr m EncJSON -> TxET QErr m EncJSON)
-> TxET QErr m EncJSON -> TxET QErr m EncJSON
forall a b. (a -> b) -> a -> b
$ (ActionResult ('Postgres pgKind) -> EncJSON)
-> TxET QErr m (ActionResult ('Postgres pgKind))
-> TxET QErr m EncJSON
forall a b. (a -> b) -> TxET QErr m a -> TxET QErr m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ActionResult ('Postgres pgKind) -> EncJSON
forall (b :: BackendType). ActionResult b -> EncJSON
arResult
(TxET QErr m (ActionResult ('Postgres pgKind))
-> TxET QErr m EncJSON)
-> TxET QErr m (ActionResult ('Postgres pgKind))
-> TxET QErr m EncJSON
forall a b. (a -> b) -> a -> b
$ OnBaseMonad (TxET QErr) (ActionResult ('Postgres pgKind))
-> forall (m :: * -> *).
(Functor (TxET QErr m), MonadIO m, MonadBaseControl IO m,
MonadTrace m, MonadError QErr m) =>
TxET QErr m (ActionResult ('Postgres pgKind))
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 (TxET QErr) (ActionResult ('Postgres pgKind))
-> forall (m :: * -> *).
(Functor (TxET QErr m), MonadIO m, MonadBaseControl IO m,
MonadTrace m, MonadError QErr m) =>
TxET QErr m (ActionResult ('Postgres pgKind)))
-> OnBaseMonad (TxET QErr) (ActionResult ('Postgres pgKind))
-> forall (m :: * -> *).
(Functor (TxET QErr m), MonadIO m, MonadBaseControl IO m,
MonadTrace m, MonadError QErr m) =>
TxET QErr m (ActionResult ('Postgres pgKind))
forall a b. (a -> b) -> a -> b
$ DBStepInfo ('Postgres pgKind)
-> OnBaseMonad
(ExecutionMonad ('Postgres pgKind))
(ActionResult ('Postgres pgKind))
forall (b :: BackendType).
DBStepInfo b -> OnBaseMonad (ExecutionMonad b) (ActionResult b)
dbsiAction DBStepInfo ('Postgres pgKind)
dbsi