2021-08-04 14:42:24 +03:00
|
|
|
-- | Planning MySQL queries and subscriptions.
|
2021-10-18 19:04:30 +03:00
|
|
|
module Hasura.Backends.MySQL.Plan
|
|
|
|
( planQuery,
|
|
|
|
queryToActionForest,
|
|
|
|
)
|
|
|
|
where
|
2021-08-04 14:42:24 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Control.Monad.Validate
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.ByteString.Lazy (toStrict)
|
|
|
|
import Data.Text.Extended
|
2021-10-18 19:04:30 +03:00
|
|
|
import Data.Tree
|
|
|
|
import Hasura.Backends.MySQL.DataLoader.Plan qualified as DataLoader
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Backends.MySQL.FromIr
|
|
|
|
import Hasura.Backends.MySQL.Types
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.GraphQL.Parser qualified as GraphQL
|
|
|
|
import Hasura.Prelude hiding (first)
|
|
|
|
import Hasura.RQL.IR
|
|
|
|
import Hasura.RQL.Types.Column qualified as RQL
|
|
|
|
import Hasura.SQL.Backend
|
|
|
|
import Hasura.Session
|
2021-08-04 14:42:24 +03:00
|
|
|
|
2021-10-18 19:04:30 +03:00
|
|
|
-- | Plan the query and then produce a forest of actions for the executor.
|
|
|
|
queryToActionForest ::
|
|
|
|
MonadError QErr m =>
|
|
|
|
UserInfo ->
|
|
|
|
QueryDB 'MySQL (Const Void) (GraphQL.UnpreparedValue 'MySQL) ->
|
|
|
|
m (DataLoader.HeadAndTail, Forest DataLoader.PlannedAction)
|
|
|
|
queryToActionForest userInfo qrf = do
|
|
|
|
select <- planQuery (_uiSession userInfo) qrf
|
|
|
|
let (!headAndTail, !plannedActionsList) =
|
|
|
|
DataLoader.runPlan
|
|
|
|
(DataLoader.planSelectHeadAndTail Nothing Nothing select)
|
|
|
|
!actionsForest = DataLoader.actionsForest id plannedActionsList
|
|
|
|
pure (headAndTail, actionsForest)
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
planQuery ::
|
|
|
|
MonadError QErr m =>
|
|
|
|
SessionVariables ->
|
|
|
|
QueryDB 'MySQL (Const Void) (GraphQL.UnpreparedValue 'MySQL) ->
|
|
|
|
m Select
|
2021-08-04 14:42:24 +03:00
|
|
|
planQuery sessionVariables queryDB = do
|
|
|
|
rootField <- traverse (prepareValueQuery sessionVariables) queryDB
|
|
|
|
sel <-
|
|
|
|
runValidate (runFromIr (fromRootField rootField))
|
2021-09-24 01:56:37 +03:00
|
|
|
`onLeft` (throw400 NotSupported . tshow)
|
2021-08-04 14:42:24 +03:00
|
|
|
pure $
|
|
|
|
sel
|
|
|
|
|
|
|
|
-- | Prepare a value without any query planning; we just execute the
|
|
|
|
-- query with the values embedded.
|
2021-09-24 01:56:37 +03:00
|
|
|
prepareValueQuery ::
|
|
|
|
MonadError QErr m =>
|
|
|
|
SessionVariables ->
|
|
|
|
GraphQL.UnpreparedValue 'MySQL ->
|
|
|
|
m Expression
|
2021-08-04 14:42:24 +03:00
|
|
|
prepareValueQuery sessionVariables =
|
|
|
|
\case
|
|
|
|
GraphQL.UVLiteral x -> pure x
|
|
|
|
GraphQL.UVSession -> pure $ ValueExpression $ BinaryValue $ toStrict $ J.encode sessionVariables
|
2021-09-24 01:56:37 +03:00
|
|
|
GraphQL.UVParameter _ RQL.ColumnValue {..} -> pure $ ValueExpression cvValue
|
2021-08-04 14:42:24 +03:00
|
|
|
GraphQL.UVSessionVar _typ sessionVariable -> do
|
2021-09-24 01:56:37 +03:00
|
|
|
value <-
|
|
|
|
getSessionVariableValue sessionVariable sessionVariables
|
|
|
|
`onNothing` throw400 NotFound ("missing session variable: " <>> sessionVariable)
|
2021-08-04 14:42:24 +03:00
|
|
|
pure $ ValueExpression $ TextValue value
|