From 233a58094da9f5e02b4000dbef2c183d65a8017d Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Wed, 1 Sep 2021 08:51:20 +0200 Subject: [PATCH] Adjust value completion tests --- src/Language/GraphQL/Execute.hs | 44 +++++++++++++++++++++------ tests/Language/GraphQL/ExecuteSpec.hs | 16 ++++++---- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/Language/GraphQL/Execute.hs b/src/Language/GraphQL/Execute.hs index 1ccdee0..3321152 100644 --- a/src/Language/GraphQL/Execute.hs +++ b/src/Language/GraphQL/Execute.hs @@ -142,14 +142,10 @@ instance Exception ResolverException where fromException = graphQLExceptionFromException data FieldError - = ArgumentTypeError - | MissingArgumentError - | ResultCoercionError + = ResultCoercionError | NullResultError instance Show FieldError where - show ArgumentTypeError = "Invalid argument type." - show MissingArgumentError = "Required argument not specified." show ResultCoercionError = "Result coercion failed." show NullResultError = "Non-Nullable field resolver returned Null." @@ -175,6 +171,30 @@ instance Exception ValueCompletionException where toException = graphQLExceptionToException fromException = graphQLExceptionFromException +data InputCoercionException = InputCoercionException String In.Type (Maybe (Full.Node Input)) + +instance Show InputCoercionException where + show (InputCoercionException argumentName argumentType Nothing) = concat + [ "Required argument \"" + , argumentName + , "\" of type " + , show argumentType + , " not specified." + ] + show (InputCoercionException argumentName argumentType (Just givenValue)) = concat + [ "Argument \"" + , argumentName + , "\" has invalid type. Expected type " + , show argumentType + , ", found: " + , show givenValue + , "." + ] + +instance Exception InputCoercionException where + toException = graphQLExceptionToException + fromException = graphQLExceptionFromException + data QueryError = OperationNameRequired | OperationNotFound String @@ -240,6 +260,7 @@ data Input | Enum Full.Name | List [Input] | Object (HashMap Full.Name Input) + deriving Show document :: Full.Document -> ([Full.OperationDefinition], HashMap Full.Name Full.FragmentDefinition) @@ -541,7 +562,9 @@ executeField objectValue fields resolver errorPath = -> GraphQLException -> ExecutorT m a exceptionHandler fieldLocation e = - let newError = Error (Text.pack $ displayException e) [fieldLocation] errorPath + let newError = Error (Text.pack $ displayException e) [fieldLocation] + $ reverse + $ fieldsSegment fields : errorPath in ExecutorT (lift $ tell $ Seq.singleton newError) >> pure Coerce.null go fieldName inputArguments = do let (Out.Field _ fieldType argumentTypes, resolveFunction) = @@ -681,9 +704,12 @@ coerceArgumentValues argumentDefinitions argumentValues = in case matchedMap of Just matchedValues -> pure matchedValues Nothing - | Just _ <- HashMap.lookup argumentName argumentValues -> - throwFieldError ArgumentTypeError - | otherwise -> throwFieldError MissingArgumentError + | Just inputValue <- HashMap.lookup argumentName argumentValues + -> throwM + $ InputCoercionException (Text.unpack argumentName) variableType + $ Just inputValue + | otherwise -> throwM + $ InputCoercionException (Text.unpack argumentName) variableType Nothing matchFieldValues' = Coerce.matchFieldValues coerceArgumentValue $ Full.node <$> argumentValues coerceArgumentValue inputType (Int integer) = diff --git a/tests/Language/GraphQL/ExecuteSpec.hs b/tests/Language/GraphQL/ExecuteSpec.hs index d14eb9d..fd10787 100644 --- a/tests/Language/GraphQL/ExecuteSpec.hs +++ b/tests/Language/GraphQL/ExecuteSpec.hs @@ -235,9 +235,10 @@ spec = ] ] executionErrors = pure $ Error - { message = "Enum value completion failed." + { message = + "Value completion error. Expected type !School, found: EXISTENTIALISM." , locations = [Location 1 17] - , path = [] + , path = [Segment "philosopher", Segment "school"] } expected = Response data'' executionErrors Right (Right actual) = either (pure . parseError) execute' @@ -251,9 +252,10 @@ spec = ] ] executionErrors = pure $ Error - { message = "Union value completion failed." + { message = + "Value completion error. Expected type !Interest, found: { instrument: \"piano\" }." , locations = [Location 1 17] - , path = [] + , path = [Segment "philosopher", Segment "interest"] } expected = Response data'' executionErrors Right (Right actual) = either (pure . parseError) execute' @@ -267,9 +269,11 @@ spec = ] ] executionErrors = pure $ Error - { message = "Interface value completion failed." + { message + = "Value completion error. Expected type !Work, found:\ + \ { title: \"Also sprach Zarathustra: Ein Buch f\252r Alle und Keinen\" }." , locations = [Location 1 17] - , path = [] + , path = [Segment "philosopher", Segment "majorWork"] } expected = Response data'' executionErrors Right (Right actual) = either (pure . parseError) execute'