2021-04-22 00:44:37 +03:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
2021-09-24 01:56:37 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-04-01 23:40:31 +03:00
|
|
|
module Hasura.Backends.Postgres.Instances.Transport
|
2021-09-24 01:56:37 +03:00
|
|
|
( runPGMutationTransaction,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.ByteString qualified as B
|
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
|
|
|
import Data.Text.Extended
|
|
|
|
import Database.PG.Query qualified as Q
|
|
|
|
import Hasura.Backends.Postgres.Execute.LiveQuery qualified as PGL
|
|
|
|
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.EncJSON
|
|
|
|
import Hasura.GraphQL.Execute.Backend
|
|
|
|
import Hasura.GraphQL.Execute.LiveQuery.Plan
|
|
|
|
import Hasura.GraphQL.Logging
|
2021-10-29 17:42:07 +03:00
|
|
|
import Hasura.GraphQL.Namespace
|
|
|
|
( RootFieldAlias,
|
|
|
|
RootFieldMap,
|
|
|
|
mkUnNamespacedRootFieldAlias,
|
|
|
|
)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.GraphQL.Transport.Backend
|
|
|
|
import Hasura.GraphQL.Transport.HTTP.Protocol
|
|
|
|
import Hasura.Logging qualified as L
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.Server.Types (RequestId)
|
|
|
|
import Hasura.Session
|
|
|
|
import Hasura.Tracing
|
|
|
|
import Hasura.Tracing qualified as Tracing
|
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-05-21 05:46:58 +03:00
|
|
|
instance
|
2021-09-24 01:56:37 +03:00
|
|
|
( 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-09-24 01:56:37 +03:00
|
|
|
runPGQuery ::
|
|
|
|
( MonadIO m,
|
|
|
|
MonadError QErr m,
|
|
|
|
MonadQueryLog m,
|
|
|
|
MonadTrace m
|
|
|
|
) =>
|
|
|
|
RequestId ->
|
|
|
|
GQLReqUnparsed ->
|
2021-10-29 17:42:07 +03:00
|
|
|
RootFieldAlias ->
|
2021-09-24 01:56:37 +03:00
|
|
|
UserInfo ->
|
|
|
|
L.Logger L.Hasura ->
|
|
|
|
SourceConfig ('Postgres pgKind) ->
|
|
|
|
Tracing.TraceT (Q.TxET QErr IO) EncJSON ->
|
|
|
|
Maybe EQ.PreparedSql ->
|
|
|
|
-- | Also return the time spent in the PG query; for telemetry.
|
|
|
|
m (DiffTime, EncJSON)
|
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-09-24 01:56:37 +03:00
|
|
|
withElapsedTime $
|
|
|
|
trace ("Postgres Query for root field " <>> fieldName) $
|
2021-09-25 06:59:35 +03:00
|
|
|
Tracing.interpTraceT (runQueryTx $ _pscExecCtx sourceConfig) tx
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|
|
|
runPGMutation ::
|
|
|
|
( MonadIO m,
|
|
|
|
MonadError QErr m,
|
|
|
|
MonadQueryLog m,
|
|
|
|
MonadTrace m
|
|
|
|
) =>
|
|
|
|
RequestId ->
|
|
|
|
GQLReqUnparsed ->
|
2021-10-29 17:42:07 +03:00
|
|
|
RootFieldAlias ->
|
2021-09-24 01:56:37 +03:00
|
|
|
UserInfo ->
|
|
|
|
L.Logger L.Hasura ->
|
|
|
|
SourceConfig ('Postgres pgKind) ->
|
|
|
|
Tracing.TraceT (Q.TxET QErr IO) EncJSON ->
|
|
|
|
Maybe EQ.PreparedSql ->
|
|
|
|
m (DiffTime, EncJSON)
|
|
|
|
runPGMutation reqId query fieldName userInfo logger sourceConfig tx _genSql = do
|
2021-02-12 06:04:09 +03:00
|
|
|
-- 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
|
2021-09-24 01:56:37 +03:00
|
|
|
withElapsedTime $
|
|
|
|
trace ("Postgres Mutation for root field " <>> fieldName) $
|
|
|
|
Tracing.interpTraceT
|
|
|
|
( liftEitherM . liftIO . runExceptT
|
|
|
|
. runTx (_pscExecCtx sourceConfig) Q.ReadWrite
|
|
|
|
. withTraceContext ctx
|
|
|
|
. withUserInfo userInfo
|
|
|
|
)
|
|
|
|
tx
|
|
|
|
|
|
|
|
runPGSubscription ::
|
|
|
|
MonadIO m =>
|
|
|
|
SourceConfig ('Postgres pgKind) ->
|
|
|
|
MultiplexedQuery ('Postgres pgKind) ->
|
|
|
|
[(CohortId, CohortVariables)] ->
|
|
|
|
m (DiffTime, Either QErr [(CohortId, B.ByteString)])
|
2021-09-01 20:56:46 +03:00
|
|
|
runPGSubscription sourceConfig query variables =
|
2021-09-24 01:56:37 +03:00
|
|
|
withElapsedTime $
|
|
|
|
runExceptT $
|
|
|
|
runQueryTx (_pscExecCtx sourceConfig) $
|
|
|
|
PGL.executeMultiplexedQuery query variables
|
|
|
|
|
|
|
|
runPGQueryExplain ::
|
|
|
|
forall pgKind m.
|
|
|
|
( MonadIO m,
|
|
|
|
MonadError QErr m
|
|
|
|
) =>
|
|
|
|
DBStepInfo ('Postgres pgKind) ->
|
|
|
|
m EncJSON
|
2021-04-13 14:10:08 +03:00
|
|
|
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-09-24 01:56:37 +03:00
|
|
|
mkQueryLog ::
|
|
|
|
GQLReqUnparsed ->
|
2021-10-29 17:42:07 +03:00
|
|
|
RootFieldAlias ->
|
2021-09-24 01:56:37 +03:00
|
|
|
Maybe EQ.PreparedSql ->
|
|
|
|
RequestId ->
|
|
|
|
QueryLog
|
2021-03-13 17:40:50 +03:00
|
|
|
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-09-24 01:56:37 +03:00
|
|
|
generatedQuery =
|
|
|
|
preparedSql <&> \(EQ.PreparedSql query args) ->
|
|
|
|
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]
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
runPGMutationTransaction ::
|
|
|
|
( MonadIO m,
|
|
|
|
MonadError QErr m,
|
|
|
|
MonadQueryLog m,
|
|
|
|
MonadTrace m
|
|
|
|
) =>
|
|
|
|
RequestId ->
|
|
|
|
GQLReqUnparsed ->
|
|
|
|
UserInfo ->
|
|
|
|
L.Logger L.Hasura ->
|
|
|
|
SourceConfig ('Postgres pgKind) ->
|
2021-10-29 17:42:07 +03:00
|
|
|
RootFieldMap (DBStepInfo ('Postgres pgKind)) ->
|
|
|
|
m (DiffTime, RootFieldMap EncJSON)
|
2021-04-01 23:40:31 +03:00
|
|
|
runPGMutationTransaction reqId query userInfo logger sourceConfig mutations = do
|
2021-10-29 17:42:07 +03:00
|
|
|
logQueryLog logger $ mkQueryLog query (mkUnNamespacedRootFieldAlias $$(G.litName "transaction")) Nothing reqId
|
2021-04-01 23:40:31 +03:00
|
|
|
ctx <- Tracing.currentContext
|
|
|
|
withElapsedTime $ do
|
2021-09-24 01:56:37 +03:00
|
|
|
Tracing.interpTraceT
|
|
|
|
( liftEitherM . liftIO . runExceptT
|
|
|
|
. runTx (_pscExecCtx sourceConfig) Q.ReadWrite
|
|
|
|
. withTraceContext ctx
|
|
|
|
. withUserInfo userInfo
|
|
|
|
)
|
|
|
|
$ flip OMap.traverseWithKey mutations \fieldName dbsi ->
|
|
|
|
trace ("Postgres Mutation for root field " <>> fieldName) $ dbsiAction dbsi
|