mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-18 04:51:35 +03:00
6e574f1bbe
## Description ### I want to speak to the `Manager` Oh boy. This PR is both fairly straightforward and overreaching, so let's break it down. For most network access, we need a [`HTTP.Manager`](https://hackage.haskell.org/package/http-client-0.1.0.0/docs/Network-HTTP-Client-Manager.html). It is created only once, at the top level, when starting the engine, and is then threaded through the application to wherever we need to make a network call. As of main, the way we do this is not standardized: most of the GraphQL execution code passes it "manually" as a function argument throughout the code. We also have a custom monad constraint, `HasHttpManagerM`, that describes a monad's ability to provide a manager. And, finally, several parts of the code store the manager in some kind of argument structure, such as `RunT`'s `RunCtx`. This PR's first goal is to harmonize all of this: we always create the manager at the root, and we already have it when we do our very first `runReaderT`. Wouldn't it make sense for the rest of the code to not manually pass it anywhere, to not store it anywhere, but to always rely on the current monad providing it? This is, in short, what this PR does: it implements a constraint on the base monads, so that they provide the manager, and removes most explicit passing from the code. ### First come, first served One way this PR goes a tiny bit further than "just" doing the aforementioned harmonization is that it starts the process of implementing the "Services oriented architecture" roughly outlined in this [draft document](https://docs.google.com/document/d/1FAigqrST0juU1WcT4HIxJxe1iEBwTuBZodTaeUvsKqQ/edit?usp=sharing). Instead of using the existing `HasHTTPManagerM`, this PR revamps it into the `ProvidesNetwork` service. The idea is, again, that we should make all "external" dependencies of the engine, all things that the core of the engine doesn't care about, a "service". This allows us to define clear APIs for features, to choose different implementations based on which version of the engine we're running, harmonizes our many scattered monadic constraints... Which is why this service is called "Network": we can refine it, moving forward, to be the constraint that defines how all network communication is to operate, instead of relying on disparate classes constraint or hardcoded decisions. A comment in the code clarifies this intent. ### Side-effects? In my Haskell? This PR also unavoidably touches some other aspects of the codebase. One such example: it introduces `Hasura.App.AppContext`, named after `HasuraPro.Context.AppContext`: a name for the reader structure at the base level. It also transforms `Handler` from a type alias to a newtype, as `Handler` is where we actually enforce HTTP limits; but without `Handler` being a distinct type, any code path could simply do a `runExceptT $ runReader` and forget to enforce them. (As a rule of thumb, i am starting to consider any straggling `runReaderT` or `runExceptT` as a code smell: we should not stack / unstack monads haphazardly, and every layer should be an opaque `newtype` with a corresponding run function.) ## Further work In several places, i have left TODOs when i have encountered things that suggest that we should do further unrelated cleanups. I'll write down the follow-up steps, either in the aforementioned document or on slack. But, in short, at a glance, in approximate order, we could: - delete `ExecutionCtx` as it is only a subset of `ServerCtx`, and remove one more `runReaderT` call - delete `ServerConfigCtx` as it is only a subset of `ServerCtx`, and remove it from `RunCtx` - remove `ServerCtx` from `HandlerCtx`, and make it part of `AppContext`, or even make it the `AppContext` altogether (since, at least for the OSS version, `AppContext` is there again only a subset) - remove `CacheBuildParams` and `CacheBuild` altogether, as they're just a distinct stack that is a `ReaderT` on top of `IO` that contains, you guessed it, the same thing as `ServerCtx` - move `RunT` out of `RQL.Types` and rename it, since after the previous cleanups **it only contains `UserInfo`**; it could be bundled with the authentication service, made a small implementation detail in `Hasura.Server.Auth` - rename `PGMetadaStorageT` to something a bit more accurate, such as `App`, and enforce its IO base This would significantly simply our complex stack. From there, or in parallel, we can start moving existing dependencies as Services. For the purpose of supporting read replicas entitlement, we could move `MonadResolveSource` to a `SourceResolver` service, as attempted in #7653, and transform `UserAuthenticationM` into a `Authentication` service. PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7736 GitOrigin-RevId: 68cce710eb9e7d752bda1ba0c49541d24df8209f
181 lines
8.6 KiB
Haskell
181 lines
8.6 KiB
Haskell
{-# LANGUAGE Arrows #-}
|
||
|
||
module Hasura.RemoteSchema.SchemaCache.Build
|
||
( buildRemoteSchemas,
|
||
addRemoteSchemaP2Setup,
|
||
)
|
||
where
|
||
|
||
import Control.Arrow.Extended
|
||
import Control.Arrow.Interpret
|
||
import Control.Monad.Trans.Control (MonadBaseControl)
|
||
import Data.Aeson
|
||
import Data.ByteString.Lazy qualified as BL
|
||
import Data.Environment qualified as Env
|
||
import Data.HashMap.Strict.Extended qualified as M
|
||
import Data.Text.Extended
|
||
import Hasura.Base.Error
|
||
import Hasura.GraphQL.RemoteServer
|
||
import Hasura.Incremental qualified as Inc
|
||
import Hasura.Prelude
|
||
import Hasura.RQL.DDL.Schema.Cache.Common
|
||
import Hasura.RQL.DDL.Schema.Cache.Permission
|
||
import Hasura.RQL.Types.Metadata.Object
|
||
import Hasura.RQL.Types.Roles
|
||
import Hasura.RQL.Types.Roles.Internal (CheckPermission (..))
|
||
import Hasura.RQL.Types.SchemaCache
|
||
import Hasura.RQL.Types.SchemaCache.Build
|
||
import Hasura.RemoteSchema.Metadata
|
||
import Hasura.RemoteSchema.SchemaCache.Permission (resolveRoleBasedRemoteSchema)
|
||
import Hasura.RemoteSchema.SchemaCache.Types
|
||
import Hasura.Services
|
||
import Hasura.Session
|
||
import Hasura.Tracing qualified as Tracing
|
||
|
||
-- Resolves a user specified `RemoteSchemaMetadata` into information rich `RemoteSchemaCtx`
|
||
-- However, given the nature of remote relationships, we cannot fully 'resolve' them, so
|
||
-- we resolve of remote relationships as much as possible.
|
||
buildRemoteSchemas ::
|
||
( ArrowChoice arr,
|
||
Inc.ArrowDistribute arr,
|
||
ArrowWriter (Seq (Either InconsistentMetadata MetadataDependency)) arr,
|
||
Inc.ArrowCache m arr,
|
||
MonadIO m,
|
||
MonadBaseControl IO m,
|
||
Eq remoteRelationshipDefinition,
|
||
ToJSON remoteRelationshipDefinition,
|
||
MonadError QErr m,
|
||
ProvidesNetwork m
|
||
) =>
|
||
Env.Environment ->
|
||
( (Inc.Dependency (HashMap RemoteSchemaName Inc.InvalidationKey), OrderedRoles, Maybe (HashMap RemoteSchemaName BL.ByteString)),
|
||
[RemoteSchemaMetadataG remoteRelationshipDefinition]
|
||
)
|
||
`arr` HashMap RemoteSchemaName (PartiallyResolvedRemoteSchemaCtxG remoteRelationshipDefinition, MetadataObject)
|
||
buildRemoteSchemas env =
|
||
buildInfoMapPreservingMetadata _rsmName mkRemoteSchemaMetadataObject buildRemoteSchema
|
||
where
|
||
-- We want to cache this call because it fetches the remote schema over
|
||
-- HTTP, and we don’t want to re-run that if the remote schema definition
|
||
-- hasn’t changed.
|
||
buildRemoteSchema = Inc.cache proc ((invalidationKeys, orderedRoles, storedIntrospection), remoteSchema@(RemoteSchemaMetadata name defn _comment permissions relationships)) -> do
|
||
Inc.dependOn -< Inc.selectKeyD name invalidationKeys
|
||
remoteSchemaContextParts <-
|
||
(|
|
||
withRecordInconsistency
|
||
( liftEitherA <<< bindA
|
||
-< runExceptT
|
||
case M.lookup name =<< storedIntrospection of
|
||
Nothing -> noopTrace $ addRemoteSchemaP2Setup env name defn
|
||
Just rawIntro -> do
|
||
rsDef <- validateRemoteSchemaDef env defn
|
||
(ir, rsi) <- stitchRemoteSchema rawIntro name rsDef
|
||
pure (ir, rawIntro, rsi)
|
||
)
|
||
|) (mkRemoteSchemaMetadataObject remoteSchema)
|
||
case remoteSchemaContextParts of
|
||
Nothing -> returnA -< Nothing
|
||
Just (introspection, rawIntrospection, remoteSchemaInfo) -> do
|
||
-- we then resolve permissions
|
||
resolvedPermissions <- buildRemoteSchemaPermissions -< ((name, introspection, orderedRoles), fmap (name,) permissions)
|
||
-- resolve remote relationships
|
||
let transformedRelationships = relationships <&> \RemoteSchemaTypeRelationships {..} -> PartiallyResolvedRemoteRelationship _rstrsName <$> _rstrsRelationships
|
||
remoteSchemaContext =
|
||
RemoteSchemaCtx
|
||
{ _rscName = name,
|
||
_rscIntroOriginal = introspection,
|
||
_rscInfo = remoteSchemaInfo,
|
||
_rscRawIntrospectionResult = rawIntrospection,
|
||
_rscPermissions = resolvedPermissions,
|
||
_rscRemoteRelationships = transformedRelationships
|
||
}
|
||
returnA -< Just remoteSchemaContext
|
||
|
||
-- TODO continue propagating MonadTrace up calls so that we can get tracing
|
||
-- for remote schema introspection. This will require modifying CacheBuild.
|
||
noopTrace = Tracing.runTraceTWithReporter Tracing.noReporter Tracing.sampleNever "buildSchemaCacheRule"
|
||
|
||
mkRemoteSchemaMetadataObject remoteSchema =
|
||
MetadataObject (MORemoteSchema (_rsmName remoteSchema)) (toJSON remoteSchema)
|
||
|
||
-- | Resolves a RemoteSchemaPermission metadata object into a 'GraphQL schema'.
|
||
buildRemoteSchemaPermissions ::
|
||
( ArrowChoice arr,
|
||
Inc.ArrowDistribute arr,
|
||
ArrowWriter (Seq (Either InconsistentMetadata MetadataDependency)) arr,
|
||
ArrowKleisli m arr,
|
||
MonadError QErr m
|
||
) =>
|
||
-- this ridiculous duplication of [(RemoteSchemaName, RemoteSchemaPermissionMetadata)]
|
||
-- instead of just [RemoteSchemaName] is because buildInfoMap doesn't pass `e` to the
|
||
-- mkMetadataObject function. However, that change is very invasive.
|
||
((RemoteSchemaName, IntrospectionResult, OrderedRoles), [(RemoteSchemaName, RemoteSchemaPermissionMetadata)]) `arr` M.HashMap RoleName IntrospectionResult
|
||
buildRemoteSchemaPermissions = proc ((remoteSchemaName, originalIntrospection, orderedRoles), permissions) -> do
|
||
metadataPermissionsMap <- do
|
||
buildInfoMap (_rspmRole . snd) mkRemoteSchemaPermissionMetadataObject buildRemoteSchemaPermission
|
||
-<
|
||
(originalIntrospection, permissions)
|
||
-- convert to the intermediate form `CheckPermission` whose `Semigroup`
|
||
-- instance is used to combine permissions
|
||
let metadataCheckPermissionsMap = CPDefined <$> metadataPermissionsMap
|
||
allRolesUnresolvedPermissionsMap <-
|
||
bindA
|
||
-<
|
||
foldM
|
||
( \accumulatedRolePermMap (Role roleName (ParentRoles parentRoles)) -> do
|
||
rolePermission <- onNothing (M.lookup roleName accumulatedRolePermMap) $ do
|
||
parentRolePermissions <-
|
||
for (toList parentRoles) $ \role ->
|
||
onNothing (M.lookup role accumulatedRolePermMap) $
|
||
throw500 $
|
||
"remote schema permissions: bad ordering of roles, could not find the permission of role: " <>> role
|
||
let combinedPermission = sconcat <$> nonEmpty parentRolePermissions
|
||
pure $ fromMaybe CPUndefined combinedPermission
|
||
pure $ M.insert roleName rolePermission accumulatedRolePermMap
|
||
)
|
||
metadataCheckPermissionsMap
|
||
(_unOrderedRoles orderedRoles)
|
||
-- traverse through `allRolesUnresolvedPermissionsMap` to record any inconsistencies (if exists)
|
||
resolvedPermissions <-
|
||
interpretWriter
|
||
-< for (M.toList allRolesUnresolvedPermissionsMap) \(roleName, checkPermission) -> do
|
||
let inconsistentRoleEntity = InconsistentRemoteSchemaPermission remoteSchemaName
|
||
resolvedCheckPermission <- resolveCheckPermission checkPermission roleName inconsistentRoleEntity
|
||
return (roleName, resolvedCheckPermission)
|
||
returnA -< catMaybes $ M.fromList resolvedPermissions
|
||
where
|
||
buildRemoteSchemaPermission = proc (originalIntrospection, (remoteSchemaName, remoteSchemaPerm)) -> do
|
||
let RemoteSchemaPermissionMetadata roleName defn _ = remoteSchemaPerm
|
||
metadataObject = mkRemoteSchemaPermissionMetadataObject (remoteSchemaName, remoteSchemaPerm)
|
||
schemaObject = SORemoteSchemaPermission remoteSchemaName roleName
|
||
providedSchemaDoc = _rspdSchema defn
|
||
addPermContext err = "in remote schema permission for role " <> roleName <<> ": " <> err
|
||
(|
|
||
withRecordInconsistency
|
||
( do
|
||
(resolvedSchemaIntrospection, dependency) <-
|
||
liftEitherA <<< bindA
|
||
-<
|
||
runExceptT $ modifyErr addPermContext $ resolveRoleBasedRemoteSchema roleName remoteSchemaName originalIntrospection providedSchemaDoc
|
||
recordDependencies -< (metadataObject, schemaObject, pure dependency)
|
||
returnA -< resolvedSchemaIntrospection
|
||
)
|
||
|) metadataObject
|
||
|
||
mkRemoteSchemaPermissionMetadataObject ::
|
||
(RemoteSchemaName, RemoteSchemaPermissionMetadata) ->
|
||
MetadataObject
|
||
mkRemoteSchemaPermissionMetadataObject (rsName, (RemoteSchemaPermissionMetadata roleName defn _)) =
|
||
let objectId = MORemoteSchemaPermissions rsName roleName
|
||
in MetadataObject objectId $ toJSON defn
|
||
|
||
addRemoteSchemaP2Setup ::
|
||
(QErrM m, MonadIO m, ProvidesNetwork m, Tracing.MonadTrace m) =>
|
||
Env.Environment ->
|
||
RemoteSchemaName ->
|
||
RemoteSchemaDef ->
|
||
m (IntrospectionResult, BL.ByteString, RemoteSchemaInfo)
|
||
addRemoteSchemaP2Setup env name def = do
|
||
rsi <- validateRemoteSchemaDef env def
|
||
fetchRemoteSchema env name rsi
|