mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-17 20:41:49 +03:00
cc6c86aeab
## Description Thanks to #1664, the Metadata API types no longer require a `ToJSON` instance. This PR follows up with a cleanup of the types of the arguments to the metadata API: - whenever possible, it moves those argument types to where they're used (RQL.DDL.*) - it removes all unrequired instances (mostly `ToJSON`) This PR does not attempt to do it for _all_ such argument types. For some of the metadata operations, the type used to describe the argument to the API and used to represent the value in the metadata are one and the same (like for `CreateEndpoint`). Sometimes, the two types are intertwined in complex ways (`RemoteRelationship` and `RemoteRelationshipDef`). In the spirit of only doing uncontroversial cleaning work, this PR only moves types that are not used outside of RQL.DDL. Furthermore, this is a small step towards separating the different types all jumbled together in RQL.Types. ## Notes This PR also improves several `FromJSON` instances to make use of `withObject`, and to use a human readable string instead of a type name in error messages whenever possible. For instance: - before: `expected Object for Object, but encountered X` after: `expected Object for add computed field, but encountered X` - before: `Expecting an object for update query` after: `expected Object for update query, but encountered X` This PR also renames `CreateFunctionPermission` to `FunctionPermissionArgument`, to remove the quite surprising `type DropFunctionPermission = CreateFunctionPermission`. This PR also deletes some dead code, mostly in RQL.DML. This PR also moves a PG-specific source resolving function from DDL.Schema.Source to the only place where it is used: App.hs. https://github.com/hasura/graphql-engine-mono/pull/1844 GitOrigin-RevId: a594521194bb7fe6a111b02a9e099896f9fed59c
141 lines
5.0 KiB
Haskell
141 lines
5.0 KiB
Haskell
-- | The RQL query ('/v2/query')
|
|
|
|
module Hasura.Server.API.V2Query where
|
|
|
|
import Hasura.Prelude
|
|
|
|
import qualified Data.Environment as Env
|
|
import qualified Network.HTTP.Client as HTTP
|
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
|
import Data.Aeson
|
|
import Data.Aeson.Casing
|
|
import Data.Aeson.TH
|
|
|
|
import qualified Hasura.Backends.BigQuery.DDL.RunSQL as BigQuery
|
|
import qualified Hasura.Backends.MSSQL.DDL.RunSQL as MSSQL
|
|
import qualified Hasura.Backends.MySQL.SQL as MySQL
|
|
import qualified Hasura.Backends.Postgres.DDL.RunSQL as Postgres
|
|
import qualified Hasura.Tracing as Tracing
|
|
|
|
import Hasura.Base.Error
|
|
import Hasura.EncJSON
|
|
import Hasura.Metadata.Class
|
|
import Hasura.RQL.DDL.Schema
|
|
import Hasura.RQL.DML.Count
|
|
import Hasura.RQL.DML.Delete
|
|
import Hasura.RQL.DML.Insert
|
|
import Hasura.RQL.DML.Select
|
|
import Hasura.RQL.DML.Types
|
|
import Hasura.RQL.DML.Update
|
|
import Hasura.RQL.Types
|
|
import Hasura.RQL.Types.Run
|
|
import Hasura.Server.Types
|
|
import Hasura.Server.Version (HasVersion)
|
|
import Hasura.Session
|
|
|
|
|
|
data RQLQuery
|
|
= RQInsert !InsertQuery
|
|
| RQSelect !SelectQuery
|
|
| RQUpdate !UpdateQuery
|
|
| RQDelete !DeleteQuery
|
|
| RQCount !CountQuery
|
|
| RQRunSql !Postgres.RunSQL
|
|
| RQMssqlRunSql !MSSQL.MSSQLRunSQL
|
|
| RQCitusRunSql !Postgres.RunSQL
|
|
| RQMysqlRunSql !MySQL.RunSQL
|
|
| RQBigqueryRunSql !BigQuery.BigQueryRunSQL
|
|
| RQBigqueryDatabaseInspection !BigQuery.BigQueryRunSQL
|
|
| RQBulk ![RQLQuery]
|
|
|
|
$(deriveFromJSON
|
|
defaultOptions { constructorTagModifier = snakeCase . drop 2
|
|
, sumEncoding = TaggedObject "type" "args"
|
|
}
|
|
''RQLQuery)
|
|
|
|
|
|
runQuery
|
|
:: ( HasVersion
|
|
, MonadIO m
|
|
, MonadBaseControl IO m
|
|
, Tracing.MonadTrace m
|
|
, MonadMetadataStorage m
|
|
, MonadResolveSource m
|
|
)
|
|
=> Env.Environment
|
|
-> InstanceId
|
|
-> UserInfo
|
|
-> RebuildableSchemaCache
|
|
-> HTTP.Manager
|
|
-> ServerConfigCtx
|
|
-> RQLQuery
|
|
-> m (EncJSON, RebuildableSchemaCache)
|
|
runQuery env instanceId userInfo schemaCache httpManager serverConfigCtx rqlQuery = do
|
|
(metadata, currentResourceVersion) <- fetchMetadata
|
|
result <- runQueryM env rqlQuery & Tracing.interpTraceT \x -> do
|
|
(((js, tracemeta), meta), rsc, ci) <-
|
|
x & runMetadataT metadata
|
|
& runCacheRWT schemaCache
|
|
& peelRun runCtx
|
|
& runExceptT
|
|
& liftEitherM
|
|
pure ((js, rsc, ci, meta), tracemeta)
|
|
withReload currentResourceVersion result
|
|
where
|
|
runCtx = RunCtx userInfo httpManager serverConfigCtx
|
|
|
|
withReload currentResourceVersion (result, updatedCache, invalidations, updatedMetadata) = do
|
|
when (queryModifiesSchema rqlQuery) $ do
|
|
case _sccMaintenanceMode serverConfigCtx of
|
|
MaintenanceModeDisabled -> do
|
|
-- set modified metadata in storage
|
|
newResourceVersion <- setMetadata currentResourceVersion updatedMetadata
|
|
-- notify schema cache sync
|
|
notifySchemaCacheSync newResourceVersion instanceId invalidations
|
|
MaintenanceModeEnabled ->
|
|
throw500 "metadata cannot be modified in maintenance mode"
|
|
pure (result, updatedCache)
|
|
|
|
queryModifiesSchema :: RQLQuery -> Bool
|
|
queryModifiesSchema = \case
|
|
RQInsert _ -> False
|
|
RQSelect _ -> False
|
|
RQUpdate _ -> False
|
|
RQDelete _ -> False
|
|
RQCount _ -> False
|
|
RQRunSql q -> Postgres.isSchemaCacheBuildRequiredRunSQL q
|
|
RQCitusRunSql q -> Postgres.isSchemaCacheBuildRequiredRunSQL q
|
|
RQMssqlRunSql q -> MSSQL.sqlContainsDDLKeyword $ MSSQL._mrsSql q
|
|
RQMysqlRunSql _ -> False
|
|
RQBigqueryRunSql _ -> False
|
|
RQBigqueryDatabaseInspection _ -> False
|
|
RQBulk l -> any queryModifiesSchema l
|
|
|
|
runQueryM
|
|
:: ( HasVersion
|
|
, MonadError QErr m
|
|
, MonadIO m
|
|
, MonadBaseControl IO m
|
|
, UserInfoM m
|
|
, CacheRWM m
|
|
, HasServerConfigCtx m
|
|
, Tracing.MonadTrace m
|
|
, MetadataM m
|
|
)
|
|
=> Env.Environment -> RQLQuery -> m EncJSON
|
|
runQueryM env = \case
|
|
RQInsert q -> runInsert q
|
|
RQSelect q -> runSelect q
|
|
RQUpdate q -> runUpdate q
|
|
RQDelete q -> runDelete q
|
|
RQCount q -> runCount q
|
|
RQRunSql q -> Postgres.runRunSQL @'Vanilla q
|
|
RQMssqlRunSql q -> MSSQL.runSQL q
|
|
RQMysqlRunSql q -> MySQL.runSQL q
|
|
RQCitusRunSql q -> Postgres.runRunSQL @'Citus q
|
|
RQBigqueryRunSql q -> BigQuery.runSQL q
|
|
RQBigqueryDatabaseInspection q -> BigQuery.runDatabaseInspection q
|
|
RQBulk l -> encJFromList <$> indexedMapM (runQueryM env) l
|