forked from OSS/graphql
Provide error information for variable definitions
This commit is contained in:
parent
f527b61a3d
commit
d7422e46ca
@ -43,19 +43,40 @@ data Response = Response
|
|||||||
data QueryError
|
data QueryError
|
||||||
= OperationNameRequired
|
= OperationNameRequired
|
||||||
| OperationNotFound String
|
| OperationNotFound String
|
||||||
| CoercionError
|
| CoercionError Full.VariableDefinition
|
||||||
|
| UnknownInputType Full.VariableDefinition
|
||||||
|
|
||||||
instance Show QueryError where
|
queryError :: QueryError -> Error
|
||||||
show OperationNameRequired = "Operation name is required."
|
queryError OperationNameRequired =
|
||||||
show (OperationNotFound operationName) =
|
Error{ message = "Operation name is required.", locations = [], path = [] }
|
||||||
concat ["Operation \"", operationName, "\" not found."]
|
queryError (OperationNotFound operationName) =
|
||||||
show CoercionError = "Coercion error."
|
let queryErrorMessage = concat
|
||||||
|
[ "Operation \""
|
||||||
|
, operationName
|
||||||
|
, "\" not found."
|
||||||
|
]
|
||||||
|
in Error{ message = queryErrorMessage, locations = [], path = [] }
|
||||||
|
queryError (CoercionError variableDefinition) =
|
||||||
|
let Full.VariableDefinition variableName _ _ location = variableDefinition
|
||||||
|
queryErrorMessage = concat
|
||||||
|
[ "Failed to coerce the variable \""
|
||||||
|
, Text.unpack variableName
|
||||||
|
, "\"."
|
||||||
|
]
|
||||||
|
in Error{ message = queryErrorMessage, locations = [location], path = [] }
|
||||||
|
queryError (UnknownInputType variableDefinition) =
|
||||||
|
let Full.VariableDefinition variableName variableTypeName _ location = variableDefinition
|
||||||
|
queryErrorMessage = concat
|
||||||
|
[ "Variable \""
|
||||||
|
, Text.unpack variableName
|
||||||
|
, "\" has unknown type \""
|
||||||
|
, show variableTypeName
|
||||||
|
, "\"."
|
||||||
|
]
|
||||||
|
in Error{ message = queryErrorMessage, locations = [location], path = [] }
|
||||||
|
|
||||||
respondWithQueryError :: QueryError -> Response
|
respondWithQueryError :: QueryError -> Response
|
||||||
respondWithQueryError queryError
|
respondWithQueryError = Response mempty . pure . queryError
|
||||||
= Response mempty
|
|
||||||
$ pure
|
|
||||||
$ Error{ message = show queryError, locations = [], path = [] }
|
|
||||||
|
|
||||||
-- operationName selectionSet location
|
-- operationName selectionSet location
|
||||||
data Operation = Operation
|
data Operation = Operation
|
||||||
@ -91,7 +112,7 @@ executeRequest _schema sourceDocument operationName _variableValues _initialValu
|
|||||||
let transformedDocument = document sourceDocument
|
let transformedDocument = document sourceDocument
|
||||||
operation = getOperation transformedDocument operationName
|
operation = getOperation transformedDocument operationName
|
||||||
in case operation of
|
in case operation of
|
||||||
Left queryError -> pure $ respondWithQueryError queryError
|
Left queryError' -> pure $ respondWithQueryError queryError'
|
||||||
Right (Operation Full.Query _ _ _ _) -> executeQuery
|
Right (Operation Full.Query _ _ _ _) -> executeQuery
|
||||||
Right (Operation Full.Mutation _ _ _ _) -> executeMutation
|
Right (Operation Full.Mutation _ _ _ _) -> executeMutation
|
||||||
Right (Operation Full.Subscription _ _ _ _) -> subscribe
|
Right (Operation Full.Subscription _ _ _ _) -> subscribe
|
||||||
@ -124,22 +145,24 @@ coerceVariableValues :: Coerce.VariableValue a
|
|||||||
-> Either QueryError Type.Subs
|
-> Either QueryError Type.Subs
|
||||||
coerceVariableValues types operationDefinition' variableValues =
|
coerceVariableValues types operationDefinition' variableValues =
|
||||||
let Operation _ _ variableDefinitions _ _ = operationDefinition'
|
let Operation _ _ variableDefinitions _ _ = operationDefinition'
|
||||||
in maybe (Left CoercionError) Right
|
in foldr forEach (Right HashMap.empty) variableDefinitions
|
||||||
$ foldr forEach (Just HashMap.empty) variableDefinitions
|
|
||||||
where
|
where
|
||||||
forEach variableDefinition coercedValues = do
|
forEach variableDefinition (Right coercedValues) =
|
||||||
let Full.VariableDefinition variableName variableTypeName defaultValue _ =
|
let Full.VariableDefinition variableName variableTypeName defaultValue _ =
|
||||||
variableDefinition
|
variableDefinition
|
||||||
let defaultValue' = constValue . Full.node <$> defaultValue
|
defaultValue' = constValue . Full.node <$> defaultValue
|
||||||
variableType <- Type.Internal.lookupInputType variableTypeName types
|
in case Type.Internal.lookupInputType variableTypeName types of
|
||||||
|
Just variableType ->
|
||||||
Coerce.matchFieldValues
|
maybe (Left $ CoercionError variableDefinition) Right
|
||||||
|
$ Coerce.matchFieldValues
|
||||||
coerceVariableValue'
|
coerceVariableValue'
|
||||||
variableValues
|
variableValues
|
||||||
variableName
|
variableName
|
||||||
variableType
|
variableType
|
||||||
defaultValue'
|
defaultValue'
|
||||||
coercedValues
|
$ Just coercedValues
|
||||||
|
Nothing -> Left $ UnknownInputType variableDefinition
|
||||||
|
forEach _ coercedValuesOrError = coercedValuesOrError
|
||||||
coerceVariableValue' variableType value'
|
coerceVariableValue' variableType value'
|
||||||
= Coerce.coerceVariableValue variableType value'
|
= Coerce.coerceVariableValue variableType value'
|
||||||
>>= Coerce.coerceInputLiteral variableType
|
>>= Coerce.coerceInputLiteral variableType
|
||||||
|
Loading…
Reference in New Issue
Block a user