Adjust value completion tests

This commit is contained in:
Eugen Wissner 2021-09-01 08:51:20 +02:00
parent c0d41a56ce
commit 233a58094d
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
2 changed files with 45 additions and 15 deletions

View File

@ -142,14 +142,10 @@ instance Exception ResolverException where
fromException = graphQLExceptionFromException fromException = graphQLExceptionFromException
data FieldError data FieldError
= ArgumentTypeError = ResultCoercionError
| MissingArgumentError
| ResultCoercionError
| NullResultError | NullResultError
instance Show FieldError where instance Show FieldError where
show ArgumentTypeError = "Invalid argument type."
show MissingArgumentError = "Required argument not specified."
show ResultCoercionError = "Result coercion failed." show ResultCoercionError = "Result coercion failed."
show NullResultError = "Non-Nullable field resolver returned Null." show NullResultError = "Non-Nullable field resolver returned Null."
@ -175,6 +171,30 @@ instance Exception ValueCompletionException where
toException = graphQLExceptionToException toException = graphQLExceptionToException
fromException = graphQLExceptionFromException 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 data QueryError
= OperationNameRequired = OperationNameRequired
| OperationNotFound String | OperationNotFound String
@ -240,6 +260,7 @@ data Input
| Enum Full.Name | Enum Full.Name
| List [Input] | List [Input]
| Object (HashMap Full.Name Input) | Object (HashMap Full.Name Input)
deriving Show
document :: Full.Document document :: Full.Document
-> ([Full.OperationDefinition], HashMap Full.Name Full.FragmentDefinition) -> ([Full.OperationDefinition], HashMap Full.Name Full.FragmentDefinition)
@ -541,7 +562,9 @@ executeField objectValue fields resolver errorPath =
-> GraphQLException -> GraphQLException
-> ExecutorT m a -> ExecutorT m a
exceptionHandler fieldLocation e = 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 in ExecutorT (lift $ tell $ Seq.singleton newError) >> pure Coerce.null
go fieldName inputArguments = do go fieldName inputArguments = do
let (Out.Field _ fieldType argumentTypes, resolveFunction) = let (Out.Field _ fieldType argumentTypes, resolveFunction) =
@ -681,9 +704,12 @@ coerceArgumentValues argumentDefinitions argumentValues =
in case matchedMap of in case matchedMap of
Just matchedValues -> pure matchedValues Just matchedValues -> pure matchedValues
Nothing Nothing
| Just _ <- HashMap.lookup argumentName argumentValues -> | Just inputValue <- HashMap.lookup argumentName argumentValues
throwFieldError ArgumentTypeError -> throwM
| otherwise -> throwFieldError MissingArgumentError $ InputCoercionException (Text.unpack argumentName) variableType
$ Just inputValue
| otherwise -> throwM
$ InputCoercionException (Text.unpack argumentName) variableType Nothing
matchFieldValues' = Coerce.matchFieldValues coerceArgumentValue matchFieldValues' = Coerce.matchFieldValues coerceArgumentValue
$ Full.node <$> argumentValues $ Full.node <$> argumentValues
coerceArgumentValue inputType (Int integer) = coerceArgumentValue inputType (Int integer) =

View File

@ -235,9 +235,10 @@ spec =
] ]
] ]
executionErrors = pure $ Error executionErrors = pure $ Error
{ message = "Enum value completion failed." { message =
"Value completion error. Expected type !School, found: EXISTENTIALISM."
, locations = [Location 1 17] , locations = [Location 1 17]
, path = [] , path = [Segment "philosopher", Segment "school"]
} }
expected = Response data'' executionErrors expected = Response data'' executionErrors
Right (Right actual) = either (pure . parseError) execute' Right (Right actual) = either (pure . parseError) execute'
@ -251,9 +252,10 @@ spec =
] ]
] ]
executionErrors = pure $ Error executionErrors = pure $ Error
{ message = "Union value completion failed." { message =
"Value completion error. Expected type !Interest, found: { instrument: \"piano\" }."
, locations = [Location 1 17] , locations = [Location 1 17]
, path = [] , path = [Segment "philosopher", Segment "interest"]
} }
expected = Response data'' executionErrors expected = Response data'' executionErrors
Right (Right actual) = either (pure . parseError) execute' Right (Right actual) = either (pure . parseError) execute'
@ -267,9 +269,11 @@ spec =
] ]
] ]
executionErrors = pure $ Error 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] , locations = [Location 1 17]
, path = [] , path = [Segment "philosopher", Segment "majorWork"]
} }
expected = Response data'' executionErrors expected = Response data'' executionErrors
Right (Right actual) = either (pure . parseError) execute' Right (Right actual) = either (pure . parseError) execute'