2022-02-25 19:08:18 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
2022-05-02 08:03:12 +03:00
|
|
|
module Hasura.Backends.DataConnector.Adapter.Execute
|
2022-02-25 19:08:18 +03:00
|
|
|
(
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-04-08 09:48:37 +03:00
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.ByteString.Lazy qualified as BL
|
|
|
|
import Data.Text.Encoding qualified as TE
|
2022-06-02 08:22:44 +03:00
|
|
|
import Data.Text.Extended (toTxt)
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.API qualified as API
|
2022-06-03 11:06:31 +03:00
|
|
|
import Hasura.Backends.DataConnector.Adapter.Types (SourceConfig (_scCapabilities))
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.Agent.Client
|
|
|
|
import Hasura.Backends.DataConnector.IR.Export as IR
|
|
|
|
import Hasura.Backends.DataConnector.IR.Query qualified as IR.Q
|
|
|
|
import Hasura.Backends.DataConnector.Plan qualified as DC
|
2022-04-28 04:51:58 +03:00
|
|
|
import Hasura.Base.Error (Code (..), QErr, throw400, throw500)
|
2022-04-08 09:48:37 +03:00
|
|
|
import Hasura.EncJSON (EncJSON, encJFromJValue)
|
|
|
|
import Hasura.GraphQL.Execute.Backend (BackendExecute (..), DBStepInfo (..), ExplainPlan (..))
|
|
|
|
import Hasura.GraphQL.Namespace qualified as GQL
|
2022-02-25 19:08:18 +03:00
|
|
|
import Hasura.Prelude
|
2022-06-02 08:22:44 +03:00
|
|
|
import Hasura.RQL.Types.Common qualified as RQL
|
2022-04-08 09:48:37 +03:00
|
|
|
import Hasura.SQL.AnyBackend (mkAnyBackend)
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.SQL.Backend (BackendType (DataConnector))
|
2022-04-08 09:48:37 +03:00
|
|
|
import Hasura.Session
|
2022-07-11 11:04:30 +03:00
|
|
|
import Hasura.Tracing (MonadTrace)
|
2022-02-25 19:08:18 +03:00
|
|
|
import Hasura.Tracing qualified as Tracing
|
2022-07-11 11:04:30 +03:00
|
|
|
import Servant.Client.Generic (genericClient)
|
2022-02-25 19:08:18 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-05-02 08:03:12 +03:00
|
|
|
instance BackendExecute 'DataConnector where
|
2022-06-24 09:58:25 +03:00
|
|
|
type PreparedQuery 'DataConnector = IR.Q.QueryRequest
|
2022-05-02 08:03:12 +03:00
|
|
|
type MultiplexedQuery 'DataConnector = Void
|
2022-07-11 11:04:30 +03:00
|
|
|
type ExecutionMonad 'DataConnector = AgentClientT (Tracing.TraceT (ExceptT QErr IO))
|
2022-02-25 19:08:18 +03:00
|
|
|
|
2022-04-08 09:48:37 +03:00
|
|
|
mkDBQueryPlan UserInfo {..} sourceName sourceConfig ir = do
|
2022-06-24 09:58:25 +03:00
|
|
|
queryRequest <- DC.mkPlan _uiSession sourceConfig ir
|
2022-04-08 09:48:37 +03:00
|
|
|
pure
|
|
|
|
DBStepInfo
|
|
|
|
{ dbsiSourceName = sourceName,
|
|
|
|
dbsiSourceConfig = sourceConfig,
|
2022-06-24 09:58:25 +03:00
|
|
|
dbsiPreparedQuery = Just queryRequest,
|
|
|
|
dbsiAction = buildAction sourceName sourceConfig queryRequest
|
2022-04-08 09:48:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mkDBQueryExplain fieldName UserInfo {..} sourceName sourceConfig ir = do
|
2022-06-24 09:58:25 +03:00
|
|
|
queryRequest <- DC.mkPlan _uiSession sourceConfig ir
|
2022-04-08 09:48:37 +03:00
|
|
|
pure $
|
2022-05-02 08:03:12 +03:00
|
|
|
mkAnyBackend @'DataConnector
|
2022-04-08 09:48:37 +03:00
|
|
|
DBStepInfo
|
|
|
|
{ dbsiSourceName = sourceName,
|
|
|
|
dbsiSourceConfig = sourceConfig,
|
2022-06-24 09:58:25 +03:00
|
|
|
dbsiPreparedQuery = Just queryRequest,
|
|
|
|
dbsiAction = pure . encJFromJValue . toExplainPlan fieldName $ queryRequest
|
2022-04-08 09:48:37 +03:00
|
|
|
}
|
2022-02-25 19:08:18 +03:00
|
|
|
mkDBMutationPlan _ _ _ _ _ =
|
2022-05-02 08:03:12 +03:00
|
|
|
throw400 NotSupported "mkDBMutationPlan: not implemented for the Data Connector backend."
|
2022-04-07 17:41:43 +03:00
|
|
|
mkLiveQuerySubscriptionPlan _ _ _ _ _ =
|
2022-05-02 08:03:12 +03:00
|
|
|
throw400 NotSupported "mkLiveQuerySubscriptionPlan: not implemented for the Data Connector backend."
|
2022-04-07 17:41:43 +03:00
|
|
|
mkDBStreamingSubscriptionPlan _ _ _ _ =
|
2022-05-02 08:03:12 +03:00
|
|
|
throw400 NotSupported "mkLiveQuerySubscriptionPlan: not implemented for the Data Connector backend."
|
2022-02-25 19:08:18 +03:00
|
|
|
mkDBRemoteRelationshipPlan _ _ _ _ _ _ _ =
|
2022-05-02 08:03:12 +03:00
|
|
|
throw500 "mkDBRemoteRelationshipPlan: not implemented for the Data Connector backend."
|
2022-03-21 13:39:49 +03:00
|
|
|
mkSubscriptionExplain _ =
|
2022-05-02 08:03:12 +03:00
|
|
|
throw400 NotSupported "mkSubscriptionExplain: not implemented for the Data Connector backend."
|
2022-04-08 09:48:37 +03:00
|
|
|
|
2022-06-24 09:58:25 +03:00
|
|
|
toExplainPlan :: GQL.RootFieldAlias -> IR.Q.QueryRequest -> ExplainPlan
|
|
|
|
toExplainPlan fieldName queryRequest =
|
|
|
|
ExplainPlan fieldName (Just "") (Just [TE.decodeUtf8 $ BL.toStrict $ J.encode $ queryRequest])
|
2022-04-08 09:48:37 +03:00
|
|
|
|
2022-07-11 11:04:30 +03:00
|
|
|
buildAction :: (MonadIO m, MonadTrace m, MonadError QErr m) => RQL.SourceName -> DC.SourceConfig -> IR.Q.QueryRequest -> AgentClientT m EncJSON
|
2022-06-24 09:58:25 +03:00
|
|
|
buildAction sourceName DC.SourceConfig {..} queryRequest = do
|
2022-04-28 04:51:58 +03:00
|
|
|
-- NOTE: Should this check occur during query construction in 'mkPlan'?
|
2022-06-24 09:58:25 +03:00
|
|
|
when (DC.queryHasRelations queryRequest && isNothing (API.cRelationships _scCapabilities)) $
|
2022-04-08 09:48:37 +03:00
|
|
|
throw400 NotSupported "Agents must provide their own dataloader."
|
2022-07-11 11:04:30 +03:00
|
|
|
let API.Routes {..} = genericClient
|
2022-06-24 09:58:25 +03:00
|
|
|
case IR.queryRequestToAPI queryRequest of
|
|
|
|
Right queryRequest' -> do
|
|
|
|
queryResponse <- _query (toTxt sourceName) _scConfig queryRequest'
|
2022-04-28 04:51:58 +03:00
|
|
|
pure $ encJFromJValue queryResponse
|
|
|
|
Left (IR.ExposedLiteral lit) ->
|
|
|
|
throw500 $ "Invalid query constructed: Exposed IR Literal '" <> lit <> "'."
|