summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2021-08-16 16:34:07 +0200
committerEugen Wissner <belka@caraus.de>2021-08-31 17:30:04 +0200
commit38ec439e9f50f4858c36d5584b8b610d6ec39eeb (patch)
tree6f698d472f73369005db032c4aef0054ece1b1b7
parentdd996570c2829e929ae59d3517a9fb82ad1f1ff3 (diff)
downloadgraphql-38ec439e9f50f4858c36d5584b8b610d6ec39eeb.tar.gz
Handle query errors on invalid operations
-rw-r--r--src/Language/GraphQL/Executor.hs24
1 files changed, 19 insertions, 5 deletions
diff --git a/src/Language/GraphQL/Executor.hs b/src/Language/GraphQL/Executor.hs
index 97447f5..45bf1cb 100644
--- a/src/Language/GraphQL/Executor.hs
+++ b/src/Language/GraphQL/Executor.hs
@@ -34,7 +34,18 @@ data Response = Response
data QueryError
= OperationNameRequired
- | OperationNotFound
+ | OperationNotFound String
+
+instance Show QueryError where
+ show OperationNameRequired = "Operation name is required."
+ show (OperationNotFound operationName) =
+ concat ["Operation \"", operationName, "\" not found."]
+
+respondWithQueryError :: QueryError -> Response
+respondWithQueryError queryError
+ = Response mempty
+ $ pure
+ $ Error{ message = show queryError, locations = [], path = [] }
-- operationName selectionSet location
data Operation = Operation
@@ -68,13 +79,16 @@ executeRequest :: Type.Internal.Schema IO
-> IO Response
executeRequest _schema sourceDocument operationName _variableValues _initialValue =
let transformedDocument = document sourceDocument
- _operation = getOperation transformedDocument operationName
- in pure $ Response mempty mempty
+ operation = getOperation transformedDocument operationName
+ in case operation of
+ Left queryError -> pure $ respondWithQueryError queryError
+ Right _ -> pure $ Response mempty mempty
getOperation :: [Operation] -> Maybe String -> Either QueryError Operation
getOperation [operation] Nothing = Right operation
-getOperation operations (Just givenOperationName) =
- maybe (Left OperationNotFound) Right $ find findOperationByName operations
+getOperation operations (Just givenOperationName)
+ = maybe (Left $ OperationNotFound givenOperationName) Right
+ $ find findOperationByName operations
where
findOperationByName (Operation _ (Just operationName) _ _ _) =
givenOperationName == operationName