2021-02-12 06:04:09 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2021-04-22 00:44:37 +03:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-04-01 23:40:31 +03:00
|
|
|
module Hasura.Backends.Postgres.Instances.Transport
|
|
|
|
( runPGMutationTransaction
|
|
|
|
) where
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
2021-03-13 17:40:50 +03:00
|
|
|
import qualified Data.Aeson as J
|
2021-02-20 16:45:49 +03:00
|
|
|
import qualified Data.ByteString as B
|
2021-04-01 23:40:31 +03:00
|
|
|
import qualified Data.HashMap.Strict.InsOrd as OMap
|
2021-02-20 16:45:49 +03:00
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-02-20 16:45:49 +03:00
|
|
|
import Control.Monad.Morph (hoist)
|
2021-02-12 06:04:09 +03:00
|
|
|
import Data.Text.Extended
|
|
|
|
|
2021-02-20 16:45:49 +03:00
|
|
|
import qualified Hasura.Backends.Postgres.Execute.LiveQuery as PGL
|
2021-06-11 06:26:50 +03:00
|
|
|
import qualified Hasura.Backends.Postgres.Instances.Execute as EQ
|
2021-02-20 16:45:49 +03:00
|
|
|
import qualified Hasura.Logging as L
|
|
|
|
import qualified Hasura.Tracing as Tracing
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-03-13 17:40:50 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.Value
|
2021-05-21 05:46:58 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.Select (PostgresAnnotatedFieldJSON)
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2021-02-12 06:04:09 +03:00
|
|
|
import Hasura.EncJSON
|
2021-02-20 16:45:49 +03:00
|
|
|
import Hasura.GraphQL.Execute.Backend
|
|
|
|
import Hasura.GraphQL.Execute.LiveQuery.Plan
|
2021-03-13 17:40:50 +03:00
|
|
|
import Hasura.GraphQL.Logging
|
2021-02-12 06:04:09 +03:00
|
|
|
import Hasura.GraphQL.Transport.Backend
|
|
|
|
import Hasura.GraphQL.Transport.HTTP.Protocol
|
|
|
|
import Hasura.RQL.Types
|
2021-02-20 16:45:49 +03:00
|
|
|
import Hasura.Server.Types (RequestId)
|
2021-02-12 06:04:09 +03:00
|
|
|
import Hasura.Session
|
|
|
|
import Hasura.Tracing
|
|
|
|
|
|
|
|
|
2021-05-21 05:46:58 +03:00
|
|
|
instance
|
|
|
|
( Backend ('Postgres pgKind)
|
|
|
|
, PostgresAnnotatedFieldJSON pgKind
|
|
|
|
) => BackendTransport ('Postgres pgKind) where
|
|
|
|
runDBQuery = runPGQuery
|
|
|
|
runDBMutation = runPGMutation
|
2021-02-20 16:45:49 +03:00
|
|
|
runDBSubscription = runPGSubscription
|
2021-04-13 14:10:08 +03:00
|
|
|
runDBQueryExplain = runPGQueryExplain
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
|
2021-02-12 06:04:09 +03:00
|
|
|
runPGQuery
|
|
|
|
:: ( MonadIO m
|
|
|
|
, MonadError QErr m
|
|
|
|
, MonadQueryLog m
|
|
|
|
, MonadTrace m
|
|
|
|
)
|
|
|
|
=> RequestId
|
|
|
|
-> GQLReqUnparsed
|
|
|
|
-> G.Name
|
|
|
|
-> UserInfo
|
|
|
|
-> L.Logger L.Hasura
|
2021-04-22 00:44:37 +03:00
|
|
|
-> SourceConfig ('Postgres pgKind)
|
2021-09-15 23:45:49 +03:00
|
|
|
-> Tracing.TraceT (Q.TxET QErr IO) EncJSON
|
2021-06-11 06:26:50 +03:00
|
|
|
-> Maybe EQ.PreparedSql
|
2021-02-12 06:04:09 +03:00
|
|
|
-> m (DiffTime, EncJSON)
|
|
|
|
-- ^ Also return the time spent in the PG query; for telemetry.
|
2021-09-06 17:29:44 +03:00
|
|
|
runPGQuery reqId query fieldName _userInfo logger sourceConfig tx genSql = do
|
2021-02-12 06:04:09 +03:00
|
|
|
-- log the generated SQL and the graphql query
|
2021-03-13 17:40:50 +03:00
|
|
|
logQueryLog logger $ mkQueryLog query fieldName genSql reqId
|
2021-02-12 06:04:09 +03:00
|
|
|
withElapsedTime $ trace ("Postgres Query for root field " <>> fieldName) $
|
2021-09-06 17:29:44 +03:00
|
|
|
Tracing.interpTraceT id $ hoist (runQueryTx $ _pscExecCtx sourceConfig) tx
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
runPGMutation
|
|
|
|
:: ( MonadIO m
|
|
|
|
, MonadError QErr m
|
|
|
|
, MonadQueryLog m
|
|
|
|
, MonadTrace m
|
|
|
|
)
|
|
|
|
=> RequestId
|
|
|
|
-> GQLReqUnparsed
|
|
|
|
-> G.Name
|
|
|
|
-> UserInfo
|
|
|
|
-> L.Logger L.Hasura
|
2021-04-22 00:44:37 +03:00
|
|
|
-> SourceConfig ('Postgres pgKind)
|
2021-09-15 23:45:49 +03:00
|
|
|
-> Tracing.TraceT (Q.TxET QErr IO) EncJSON
|
2021-06-11 06:26:50 +03:00
|
|
|
-> Maybe EQ.PreparedSql
|
2021-02-12 06:04:09 +03:00
|
|
|
-> m (DiffTime, EncJSON)
|
|
|
|
-- ^ Also return 'Mutation' when the operation was a mutation, and the time
|
|
|
|
-- spent in the PG query; for telemetry.
|
|
|
|
runPGMutation reqId query fieldName userInfo logger sourceConfig tx _genSql = do
|
|
|
|
-- log the graphql query
|
2021-03-13 17:40:50 +03:00
|
|
|
logQueryLog logger $ mkQueryLog query fieldName Nothing reqId
|
2021-02-12 06:04:09 +03:00
|
|
|
ctx <- Tracing.currentContext
|
|
|
|
withElapsedTime $ trace ("Postgres Mutation for root field " <>> fieldName) $
|
|
|
|
Tracing.interpTraceT (
|
|
|
|
liftEitherM . liftIO . runExceptT
|
2021-09-15 23:45:49 +03:00
|
|
|
. runTx (_pscExecCtx sourceConfig) Q.ReadWrite
|
2021-02-12 06:04:09 +03:00
|
|
|
. withTraceContext ctx
|
|
|
|
. withUserInfo userInfo
|
2021-04-01 23:40:31 +03:00
|
|
|
) tx
|
2021-02-20 16:45:49 +03:00
|
|
|
|
|
|
|
runPGSubscription
|
2021-09-01 20:56:46 +03:00
|
|
|
:: MonadIO m
|
2021-04-22 00:44:37 +03:00
|
|
|
=> SourceConfig ('Postgres pgKind)
|
|
|
|
-> MultiplexedQuery ('Postgres pgKind)
|
2021-02-20 16:45:49 +03:00
|
|
|
-> [(CohortId, CohortVariables)]
|
|
|
|
-> m (DiffTime, Either QErr [(CohortId, B.ByteString)])
|
2021-09-01 20:56:46 +03:00
|
|
|
runPGSubscription sourceConfig query variables =
|
|
|
|
withElapsedTime
|
2021-02-20 16:45:49 +03:00
|
|
|
$ runExceptT
|
|
|
|
$ runQueryTx (_pscExecCtx sourceConfig)
|
|
|
|
$ PGL.executeMultiplexedQuery query variables
|
2021-03-13 17:40:50 +03:00
|
|
|
|
2021-04-13 14:10:08 +03:00
|
|
|
runPGQueryExplain
|
2021-04-22 00:44:37 +03:00
|
|
|
:: forall pgKind m
|
2021-04-13 14:10:08 +03:00
|
|
|
. ( MonadIO m
|
|
|
|
, MonadError QErr m
|
|
|
|
)
|
2021-04-22 00:44:37 +03:00
|
|
|
=> DBStepInfo ('Postgres pgKind)
|
2021-04-13 14:10:08 +03:00
|
|
|
-> m EncJSON
|
|
|
|
runPGQueryExplain (DBStepInfo _ sourceConfig _ action) =
|
|
|
|
-- All Postgres transport functions use the same monad stack: the ExecutionMonad defined in the
|
|
|
|
-- matching instance of BackendExecute. However, Explain doesn't need tracing! Rather than
|
|
|
|
-- introducing a separate "ExplainMonad", we simply use @runTraceTWithReporter@ to remove the
|
|
|
|
-- TraceT.
|
|
|
|
runQueryTx (_pscExecCtx sourceConfig) $ runTraceTWithReporter noReporter "explain" $ action
|
|
|
|
|
2021-03-13 17:40:50 +03:00
|
|
|
|
|
|
|
mkQueryLog
|
|
|
|
:: GQLReqUnparsed
|
|
|
|
-> G.Name
|
2021-06-11 06:26:50 +03:00
|
|
|
-> Maybe EQ.PreparedSql
|
2021-03-13 17:40:50 +03:00
|
|
|
-> RequestId
|
|
|
|
-> QueryLog
|
|
|
|
mkQueryLog gqlQuery fieldName preparedSql requestId =
|
2021-04-28 20:38:05 +03:00
|
|
|
QueryLog gqlQuery ((fieldName,) <$> generatedQuery) requestId QueryLogKindDatabase
|
2021-03-13 17:40:50 +03:00
|
|
|
where
|
2021-06-11 06:26:50 +03:00
|
|
|
generatedQuery = preparedSql <&> \(EQ.PreparedSql query args) ->
|
2021-03-13 17:40:50 +03:00
|
|
|
GeneratedQuery (Q.getQueryText query) (J.toJSON $ pgScalarValueToJson . snd <$> args)
|
2021-04-01 23:40:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- ad-hoc transaction optimisation
|
|
|
|
-- see Note [Backwards-compatible transaction optimisation]
|
|
|
|
|
|
|
|
runPGMutationTransaction
|
|
|
|
:: ( MonadIO m
|
|
|
|
, MonadError QErr m
|
|
|
|
, MonadQueryLog m
|
|
|
|
, MonadTrace m
|
|
|
|
)
|
|
|
|
=> RequestId
|
|
|
|
-> GQLReqUnparsed
|
|
|
|
-> UserInfo
|
|
|
|
-> L.Logger L.Hasura
|
2021-04-22 00:44:37 +03:00
|
|
|
-> SourceConfig ('Postgres pgKind)
|
|
|
|
-> InsOrdHashMap G.Name (DBStepInfo ('Postgres pgKind))
|
2021-04-01 23:40:31 +03:00
|
|
|
-> m (DiffTime, InsOrdHashMap G.Name EncJSON)
|
|
|
|
runPGMutationTransaction reqId query userInfo logger sourceConfig mutations = do
|
|
|
|
logQueryLog logger $ mkQueryLog query $$(G.litName "transaction") Nothing reqId
|
|
|
|
ctx <- Tracing.currentContext
|
|
|
|
withElapsedTime $ do
|
|
|
|
Tracing.interpTraceT (
|
|
|
|
liftEitherM . liftIO . runExceptT
|
2021-09-15 23:45:49 +03:00
|
|
|
. runTx (_pscExecCtx sourceConfig) Q.ReadWrite
|
2021-04-01 23:40:31 +03:00
|
|
|
. withTraceContext ctx
|
|
|
|
. withUserInfo userInfo
|
|
|
|
) $ flip OMap.traverseWithKey mutations \fieldName dbsi ->
|
|
|
|
trace ("Postgres Mutation for root field " <>> fieldName) $ dbsiAction dbsi
|