graphql-engine/server/src-lib/Hasura/RQL/DDL/Endpoint.hs
Antoine Leblanc e99f9a2f57 Remove MetadataStorageT, clean up error handling.
## Description

This PR removes `MetadataStorageT`, and cleans up all top-level error handling. In short: this PR changes `MonadMetadataStorage` to explicitly return a bunch of `Either QErr a`, instead of relying on the stack providing a `MonadError QErr`. Since we implement that class on the base monad *below any ExceptT*, this removes a lot of very complicated instances that make assumptions about the shape of the stack.

On the back of this, we can remove several layers of ExceptT from the core of the code, including the one in `RunT`, which allows us to remove several instances of `liftEitherM . runExceptT`.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7689
GitOrigin-RevId: 97d600154d690f58c0b93fb4cc2d30fd383fd8b8
2023-02-03 01:05:09 +00:00

64 lines
1.7 KiB
Haskell

module Hasura.RQL.DDL.Endpoint
( runCreateEndpoint,
runDropEndpoint,
dropEndpointInMetadata,
)
where
import Data.HashMap.Strict.InsOrd qualified as OMap
import Data.Text.Extended
import Hasura.Base.Error
import Hasura.EncJSON
import Hasura.Prelude
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.Endpoint
import Hasura.RQL.Types.Metadata
import Hasura.RQL.Types.Metadata.Object
import Hasura.RQL.Types.SchemaCache.Build
runCreateEndpoint ::
( MonadError QErr m,
CacheRWM m,
MetadataM m
) =>
CreateEndpoint ->
m EncJSON
runCreateEndpoint endpoint@EndpointMetadata {..} = do
endpointsMap <- _metaRestEndpoints <$> getMetadata
OMap.lookup _ceName endpointsMap `for_` \_ ->
throw400 AlreadyExists $
"Endpoint with name: " <> toTxt _ceName <> " already exists"
withNewInconsistentObjsCheck $
buildSchemaCacheFor (MOEndpoint _ceName) $
MetadataModifier $
metaRestEndpoints %~ OMap.insert _ceName endpoint
return successMsg
runDropEndpoint ::
( MonadError QErr m,
CacheRWM m,
MetadataM m
) =>
DropEndpoint ->
m EncJSON
runDropEndpoint DropEndpoint {..} = do
checkExists _deName
withNewInconsistentObjsCheck $
buildSchemaCache $
dropEndpointInMetadata _deName
return successMsg
dropEndpointInMetadata :: EndpointName -> MetadataModifier
dropEndpointInMetadata name =
MetadataModifier $ metaRestEndpoints %~ OMap.delete name
checkExists :: (MetadataM m, MonadError QErr m) => EndpointName -> m ()
checkExists name = do
endpointsMap <- _metaRestEndpoints <$> getMetadata
void $
onNothing (OMap.lookup name endpointsMap) $
throw400 NotExists $
"endpoint with name: " <> toTxt name <> " does not exist"