2021-02-12 06:04:09 +03:00
|
|
|
module Hasura.GraphQL.Execute.Backend where
|
|
|
|
|
2021-03-10 10:26:22 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
2021-06-11 06:26:50 +03:00
|
|
|
import qualified Data.Aeson as J
|
|
|
|
import qualified Data.Aeson.Casing as J
|
|
|
|
import qualified Data.Aeson.Ordered as JO
|
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
|
|
|
import qualified Network.HTTP.Types as HTTP
|
|
|
|
|
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
|
|
|
import Data.Kind (Type)
|
2021-07-29 11:29:12 +03:00
|
|
|
import Data.SqlCommenter
|
|
|
|
import Data.Tagged
|
2021-03-10 10:26:22 +03:00
|
|
|
import Data.Text.Extended
|
2021-09-22 13:43:05 +03:00
|
|
|
import Data.Text.NonEmpty (mkNonEmptyTextUnsafe)
|
2021-03-10 10:26:22 +03:00
|
|
|
|
2021-06-11 06:26:50 +03:00
|
|
|
import qualified Hasura.GraphQL.Transport.HTTP.Protocol as GH
|
|
|
|
import qualified Hasura.SQL.AnyBackend as AB
|
2021-03-10 10:26:22 +03:00
|
|
|
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2021-03-10 10:26:22 +03:00
|
|
|
import Hasura.EncJSON
|
2021-06-11 06:26:50 +03:00
|
|
|
import Hasura.GraphQL.Execute.Action.Types (ActionExecutionPlan)
|
2021-03-10 10:26:22 +03:00
|
|
|
import Hasura.GraphQL.Execute.LiveQuery.Plan
|
2021-06-11 06:26:50 +03:00
|
|
|
import Hasura.GraphQL.Execute.RemoteJoin.Types
|
|
|
|
import Hasura.GraphQL.Parser hiding (Type)
|
2021-07-29 11:29:12 +03:00
|
|
|
import Hasura.QueryTags
|
2021-06-11 06:26:50 +03:00
|
|
|
import Hasura.RQL.IR
|
2021-04-13 10:00:43 +03:00
|
|
|
import Hasura.RQL.Types.Action
|
2021-03-10 10:26:22 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
2021-09-22 13:43:05 +03:00
|
|
|
import Hasura.RQL.Types.Column (ColumnType, fromCol)
|
2021-04-01 23:40:31 +03:00
|
|
|
import Hasura.RQL.Types.Common
|
2021-07-29 11:29:12 +03:00
|
|
|
import Hasura.RQL.Types.QueryTags (QueryTagsSourceConfig)
|
2021-03-10 10:26:22 +03:00
|
|
|
import Hasura.RQL.Types.RemoteSchema
|
|
|
|
import Hasura.SQL.Backend
|
2021-06-11 06:26:50 +03:00
|
|
|
import Hasura.Server.Version (HasVersion)
|
2021-03-10 10:26:22 +03:00
|
|
|
import Hasura.Session
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-07-29 11:29:12 +03:00
|
|
|
import Hasura.Metadata.Class
|
|
|
|
import Hasura.Tracing (TraceT)
|
|
|
|
|
2021-09-15 23:45:49 +03:00
|
|
|
import qualified Database.PG.Query as Q
|
2021-07-29 11:29:12 +03:00
|
|
|
import Hasura.RQL.DDL.Schema.Cache (CacheRWT)
|
|
|
|
import Hasura.RQL.Types.Run (RunT (..))
|
|
|
|
import Hasura.RQL.Types.SchemaCache.Build (MetadataT (..))
|
|
|
|
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
-- | This typeclass enacapsulates how a given backend translates a root field into an execution
|
|
|
|
-- plan. For now, each root field maps to one execution step, but in the future, when we have
|
|
|
|
-- a client-side dataloader, each root field might translate into a multi-step plan.
|
2021-02-20 16:45:49 +03:00
|
|
|
class ( Backend b
|
|
|
|
, ToTxt (MultiplexedQuery b)
|
|
|
|
, Monad (ExecutionMonad b)
|
|
|
|
) => BackendExecute (b :: BackendType) where
|
2021-02-12 06:04:09 +03:00
|
|
|
-- generated query information
|
2021-02-20 16:45:49 +03:00
|
|
|
type PreparedQuery b :: Type
|
|
|
|
type MultiplexedQuery b :: Type
|
|
|
|
type ExecutionMonad b :: Type -> Type
|
2021-02-12 06:04:09 +03:00
|
|
|
|
|
|
|
-- execution plan generation
|
|
|
|
mkDBQueryPlan
|
2021-07-29 11:29:12 +03:00
|
|
|
:: forall m
|
|
|
|
. ( MonadError QErr m
|
|
|
|
, MonadQueryTags m
|
|
|
|
)
|
2021-06-11 06:26:50 +03:00
|
|
|
=> UserInfo
|
2021-04-01 23:40:31 +03:00
|
|
|
-> SourceName
|
2021-02-12 06:04:09 +03:00
|
|
|
-> SourceConfig b
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> QueryDB b (Const Void) (UnpreparedValue b)
|
2021-07-29 11:29:12 +03:00
|
|
|
-> QueryTagsComment
|
2021-06-11 06:26:50 +03:00
|
|
|
-> m (DBStepInfo b)
|
2021-02-12 06:04:09 +03:00
|
|
|
mkDBMutationPlan
|
|
|
|
:: forall m
|
|
|
|
. ( MonadError QErr m
|
|
|
|
, HasVersion
|
2021-07-29 11:29:12 +03:00
|
|
|
, MonadQueryTags m
|
2021-02-12 06:04:09 +03:00
|
|
|
)
|
2021-06-11 06:26:50 +03:00
|
|
|
=> UserInfo
|
2021-02-12 06:04:09 +03:00
|
|
|
-> Bool
|
2021-04-01 23:40:31 +03:00
|
|
|
-> SourceName
|
2021-02-12 06:04:09 +03:00
|
|
|
-> SourceConfig b
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> MutationDB b (Const Void) (UnpreparedValue b)
|
2021-07-29 11:29:12 +03:00
|
|
|
-> QueryTagsComment
|
2021-06-11 06:26:50 +03:00
|
|
|
-> m (DBStepInfo b)
|
2021-02-20 16:45:49 +03:00
|
|
|
mkDBSubscriptionPlan
|
|
|
|
:: forall m
|
|
|
|
. ( MonadError QErr m
|
|
|
|
, MonadIO m
|
|
|
|
)
|
|
|
|
=> UserInfo
|
2021-04-01 23:40:31 +03:00
|
|
|
-> SourceName
|
2021-02-20 16:45:49 +03:00
|
|
|
-> SourceConfig b
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> InsOrdHashMap G.Name (QueryDB b (Const Void) (UnpreparedValue b))
|
2021-07-29 11:29:12 +03:00
|
|
|
-> QueryTagsComment
|
2021-02-20 16:45:49 +03:00
|
|
|
-> m (LiveQueryPlan b (MultiplexedQuery b))
|
2021-04-13 14:10:08 +03:00
|
|
|
mkDBQueryExplain
|
|
|
|
:: forall m
|
|
|
|
. ( MonadError QErr m
|
|
|
|
)
|
|
|
|
=> G.Name
|
|
|
|
-> UserInfo
|
|
|
|
-> SourceName
|
|
|
|
-> SourceConfig b
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
-> QueryDB b (Const Void) (UnpreparedValue b)
|
2021-04-13 14:10:08 +03:00
|
|
|
-> m (AB.AnyBackend DBStepInfo)
|
|
|
|
mkLiveQueryExplain
|
|
|
|
:: ( MonadError QErr m
|
|
|
|
, MonadIO m
|
|
|
|
, MonadBaseControl IO m
|
|
|
|
)
|
|
|
|
=> LiveQueryPlan b (MultiplexedQuery b)
|
|
|
|
-> m LiveQueryPlanExplanation
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-09-22 13:43:05 +03:00
|
|
|
mkDBRemoteRelationshipPlan
|
|
|
|
:: forall m
|
|
|
|
. ( MonadError QErr m
|
|
|
|
, MonadQueryTags m
|
|
|
|
)
|
|
|
|
=> UserInfo
|
|
|
|
-> SourceName
|
|
|
|
-> SourceConfig b
|
|
|
|
-> NonEmpty J.Object
|
|
|
|
-- ^ List of json objects, each of which becomes a row of the table.
|
|
|
|
-> HashMap FieldName (Column b, ScalarType b)
|
|
|
|
-- ^ The above objects have this schema.
|
|
|
|
-> FieldName
|
|
|
|
-- ^ This is a field name from the lhs that *has* to be selected in the
|
|
|
|
-- response along with the relationship.
|
|
|
|
-> (FieldName, SourceRelationshipSelection b (Const Void) UnpreparedValue)
|
|
|
|
-> m (DBStepInfo b)
|
|
|
|
|
|
|
|
-- | This is a helper function to convert a remote source's relationship to a
|
|
|
|
-- normal relationship to a temporary table. This function can be used to
|
|
|
|
-- implement executeRemoteRelationship function in databases which support
|
|
|
|
-- constructing a temporary table for a list of json objects.
|
|
|
|
convertRemoteSourceRelationship
|
|
|
|
:: forall b. (Backend b)
|
|
|
|
=> HashMap (Column b) (Column b)
|
|
|
|
-- ^ Join columns for the relationship
|
|
|
|
-> SelectFromG b (UnpreparedValue b)
|
|
|
|
-- ^ The LHS of the join, this is the expression which selects from json
|
|
|
|
-- objects
|
|
|
|
-> Column b
|
|
|
|
-- ^ This is the __argument__ id column, that needs to be added to the response
|
|
|
|
-- This is used by by the remote joins processing logic to convert the
|
|
|
|
-- response from upstream to join indices
|
|
|
|
-> ColumnType b
|
|
|
|
-- ^ This is the type of the __argument__ id column
|
|
|
|
-> (FieldName, SourceRelationshipSelection b (Const Void) UnpreparedValue)
|
|
|
|
-- ^ The relationship column and its name (how it should be selected in the
|
|
|
|
-- response)
|
|
|
|
-> QueryDB b (Const Void) (UnpreparedValue b)
|
|
|
|
convertRemoteSourceRelationship columnMapping selectFrom argumentIdColumn argumentIdColumnType
|
|
|
|
(relationshipName, relationship) =
|
|
|
|
QDBMultipleRows simpleSelect
|
|
|
|
where
|
|
|
|
-- TODO: FieldName should have also been a wrapper around NonEmptyText
|
|
|
|
relName = RelName $ mkNonEmptyTextUnsafe $ getFieldNameTxt relationshipName
|
|
|
|
|
|
|
|
relationshipField = case relationship of
|
|
|
|
SourceRelationshipObject s ->
|
|
|
|
AFObjectRelation $ AnnRelationSelectG relName columnMapping s
|
|
|
|
SourceRelationshipArray s ->
|
|
|
|
AFArrayRelation $ ASSimple $ AnnRelationSelectG relName columnMapping s
|
|
|
|
SourceRelationshipArrayAggregate s ->
|
|
|
|
AFArrayRelation $ ASAggregate $ AnnRelationSelectG relName columnMapping s
|
|
|
|
|
|
|
|
argumentIdField =
|
|
|
|
( fromCol @b argumentIdColumn
|
|
|
|
, AFColumn $
|
|
|
|
AnnColumnField { _acfColumn = argumentIdColumn
|
|
|
|
, _acfType = argumentIdColumnType
|
|
|
|
, _acfAsText = False
|
|
|
|
, _acfOp = Nothing
|
|
|
|
, _acfCaseBoolExpression = Nothing
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
simpleSelect =
|
|
|
|
AnnSelectG { _asnFields = [argumentIdField, (relationshipName, relationshipField)]
|
|
|
|
, _asnFrom = selectFrom
|
|
|
|
, _asnPerm = TablePerm annBoolExpTrue Nothing
|
|
|
|
, _asnArgs = noSelectArgs
|
|
|
|
, _asnStrfyNum = False
|
|
|
|
}
|
|
|
|
|
2021-04-01 23:40:31 +03:00
|
|
|
data DBStepInfo b = DBStepInfo
|
|
|
|
{ dbsiSourceName :: SourceName
|
|
|
|
, dbsiSourceConfig :: SourceConfig b
|
|
|
|
, dbsiPreparedQuery :: Maybe (PreparedQuery b)
|
|
|
|
, dbsiAction :: ExecutionMonad b EncJSON
|
|
|
|
}
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-04-13 14:10:08 +03:00
|
|
|
|
|
|
|
-- | The result of an explain query: for a given root field (denoted by its name): the generated SQL
|
|
|
|
-- query, and the detailed explanation obtained from the database (if any). We mostly use this type
|
|
|
|
-- as an intermediary step, and immediately tranform any value we obtain into an equivalent JSON
|
|
|
|
-- representation.
|
|
|
|
data ExplainPlan
|
|
|
|
= ExplainPlan
|
|
|
|
{ _fpField :: !G.Name
|
|
|
|
, _fpSql :: !(Maybe Text)
|
|
|
|
, _fpPlan :: !(Maybe [Text])
|
|
|
|
} deriving (Show, Eq, Generic)
|
|
|
|
|
|
|
|
instance J.ToJSON ExplainPlan where
|
|
|
|
toJSON = J.genericToJSON $ J.aesonPrefix J.camelCase
|
|
|
|
|
|
|
|
|
2021-02-12 06:04:09 +03:00
|
|
|
-- | One execution step to processing a GraphQL query (e.g. one root field).
|
2021-02-20 16:45:49 +03:00
|
|
|
data ExecutionStep where
|
2021-06-11 06:26:50 +03:00
|
|
|
-- | A query to execute against the database
|
2021-02-12 06:04:09 +03:00
|
|
|
ExecStepDB
|
2021-03-15 16:02:58 +03:00
|
|
|
:: HTTP.ResponseHeaders
|
|
|
|
-> AB.AnyBackend DBStepInfo
|
2021-06-11 06:26:50 +03:00
|
|
|
-> Maybe RemoteJoins
|
2021-02-20 16:45:49 +03:00
|
|
|
-> ExecutionStep
|
2021-06-11 06:26:50 +03:00
|
|
|
-- | Execute an action
|
2021-02-12 06:04:09 +03:00
|
|
|
ExecStepAction
|
2021-06-11 06:26:50 +03:00
|
|
|
:: ActionExecutionPlan
|
|
|
|
-> ActionsInfo
|
|
|
|
-> Maybe RemoteJoins
|
2021-02-20 16:45:49 +03:00
|
|
|
-> ExecutionStep
|
2021-06-11 06:26:50 +03:00
|
|
|
-- | A graphql query to execute against a remote schema
|
2021-02-12 06:04:09 +03:00
|
|
|
ExecStepRemote
|
|
|
|
:: !RemoteSchemaInfo
|
2021-07-30 14:33:06 +03:00
|
|
|
-> !RemoteResultCustomizer
|
2021-02-12 06:04:09 +03:00
|
|
|
-> !GH.GQLReqOutgoing
|
2021-02-20 16:45:49 +03:00
|
|
|
-> ExecutionStep
|
2021-06-11 06:26:50 +03:00
|
|
|
-- | Output a plain JSON object
|
2021-02-12 06:04:09 +03:00
|
|
|
ExecStepRaw
|
2021-05-19 19:37:47 +03:00
|
|
|
:: JO.Value
|
2021-02-20 16:45:49 +03:00
|
|
|
-> ExecutionStep
|
2021-02-12 06:04:09 +03:00
|
|
|
|
2021-03-13 17:40:50 +03:00
|
|
|
|
2021-02-12 06:04:09 +03:00
|
|
|
-- | The series of steps that need to be executed for a given query. For now, those steps are all
|
|
|
|
-- independent. In the future, when we implement a client-side dataloader and generalized joins,
|
|
|
|
-- this will need to be changed into an annotated tree.
|
2021-02-20 16:45:49 +03:00
|
|
|
type ExecutionPlan = InsOrdHashMap G.Name ExecutionStep
|
2021-07-29 11:29:12 +03:00
|
|
|
|
|
|
|
class (Monad m) => MonadQueryTags m where
|
|
|
|
-- | Creates Query Tags. These are appended to the Generated SQL.
|
|
|
|
-- Helps users to use native database monitoring tools to get some 'application-context'.
|
|
|
|
createQueryTags
|
|
|
|
:: (Maybe QueryTagsSourceConfig) -> [Attribute] -> Tagged m Text
|
|
|
|
|
|
|
|
instance (MonadQueryTags m) => MonadQueryTags (ReaderT r m) where
|
|
|
|
createQueryTags qtSourceConfig attr = retag (createQueryTags @m qtSourceConfig attr) :: Tagged (ReaderT r m) Text
|
|
|
|
|
|
|
|
instance (MonadQueryTags m) => MonadQueryTags (ExceptT e m) where
|
|
|
|
createQueryTags qtSourceConfig attr = retag (createQueryTags @m qtSourceConfig attr) :: Tagged (ExceptT e m) Text
|
|
|
|
|
|
|
|
instance (MonadQueryTags m) => MonadQueryTags (TraceT m) where
|
|
|
|
createQueryTags qtSourceConfig attr = retag (createQueryTags @m qtSourceConfig attr) :: Tagged (TraceT m) Text
|
|
|
|
|
|
|
|
instance (MonadQueryTags m) => MonadQueryTags (MetadataStorageT m) where
|
|
|
|
createQueryTags qtSourceConfig attr = retag (createQueryTags @m qtSourceConfig attr) :: Tagged (MetadataStorageT m) Text
|
|
|
|
|
2021-09-15 23:45:49 +03:00
|
|
|
instance (MonadQueryTags m) => MonadQueryTags (Q.TxET QErr m) where
|
|
|
|
createQueryTags qtSourceConfig attr = retag (createQueryTags @m qtSourceConfig attr) :: Tagged (Q.TxET QErr m) Text
|
2021-07-29 11:29:12 +03:00
|
|
|
|
|
|
|
instance (MonadQueryTags m) => MonadQueryTags (MetadataT m) where
|
|
|
|
createQueryTags qtSourceConfig attr = retag (createQueryTags @m qtSourceConfig attr) :: Tagged (MetadataT m) Text
|
|
|
|
|
|
|
|
instance (MonadQueryTags m) => MonadQueryTags (CacheRWT m) where
|
|
|
|
createQueryTags qtSourceConfig attr = retag (createQueryTags @m qtSourceConfig attr) :: Tagged (CacheRWT m) Text
|
|
|
|
|
|
|
|
instance (MonadQueryTags m) => MonadQueryTags (RunT m) where
|
|
|
|
createQueryTags qtSourceConfig attr = retag (createQueryTags @m qtSourceConfig attr) :: Tagged (RunT m) Text
|