graphql-engine/server/src-lib/Hasura/RQL/DDL/Relationship/Rename.hs
Antoine Leblanc 21254256a1 Improve error messages of Metadata API.
### Description

This PR improves error messages in our metadata API by displaying a message with the name of the failing command and a link to our documentation. Furthermore, it harmonizes our internal uses of `withObject`, to respect the convention of using the Haskell type name, now that the Aeson error message is displayed as an "internal error message".

https://github.com/hasura/graphql-engine-mono/pull/1905

GitOrigin-RevId: e4064ba3290306437aa7e45faa316c60e51bc6b6
2021-09-20 19:50:22 +00:00

63 lines
1.9 KiB
Haskell

module Hasura.RQL.DDL.Relationship.Rename
( RenameRel
, runRenameRel
) where
import Hasura.Prelude
import qualified Data.HashMap.Strict as Map
import Data.Aeson
import Data.Text.Extended
import Hasura.Base.Error
import Hasura.EncJSON
import Hasura.RQL.DDL.Schema (renameRelationshipInMetadata)
import Hasura.RQL.Types
data RenameRel b
= RenameRel
{ _rrSource :: !SourceName
, _rrTable :: !(TableName b)
, _rrName :: !RelName
, _rrNewName :: !RelName
}
instance (Backend b) => FromJSON (RenameRel b) where
parseJSON = withObject "RenameRel" $ \o ->
RenameRel
<$> o .:? "source" .!= defaultSource
<*> o .: "table"
<*> o .: "name"
<*> o .: "new_name"
renameRelP2
:: forall b m
. (QErrM m, CacheRM m, BackendMetadata b)
=> SourceName -> TableName b -> RelName -> RelInfo b -> m MetadataModifier
renameRelP2 source qt newRN relInfo = withNewInconsistentObjsCheck $ do
tabInfo <- askTableCoreInfo @b source qt
-- check for conflicts in fieldInfoMap
case Map.lookup (fromRel newRN) $ _tciFieldInfoMap tabInfo of
Nothing -> return ()
Just _ ->
throw400 AlreadyExists $ "cannot rename relationship " <> oldRN
<<> " to " <> newRN <<> " in table " <> qt <<>
" as a column/relationship with the name already exists"
-- update metadata
execWriterT $ renameRelationshipInMetadata @b source qt oldRN (riType relInfo) newRN
where
oldRN = riName relInfo
runRenameRel
:: forall b m
. (MonadError QErr m, CacheRWM m, MetadataM m, BackendMetadata b)
=> RenameRel b -> m EncJSON
runRenameRel (RenameRel source qt rn newRN) = do
tabInfo <- askTableCoreInfo @b source qt
ri <- askRelType (_tciFieldInfoMap tabInfo) rn ""
withNewInconsistentObjsCheck $
renameRelP2 source qt newRN ri >>= buildSchemaCache
pure successMsg