summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2021-06-27 13:42:58 +0200
committerEugen Wissner <belka@caraus.de>2021-06-27 13:42:58 +0200
commitb580d1a98880749c1473c11b790d3ec622fe00ad (patch)
tree5249738121e0cf8aba3f5176e96622ff939e0520 /tests
parentc601ccb4add2ca4f351af8513457c068f553b619 (diff)
downloadgraphql-b580d1a98880749c1473c11b790d3ec622fe00ad.tar.gz
Attach the field location to resolver exceptions
Diffstat (limited to 'tests')
-rw-r--r--tests/Language/GraphQL/ExecuteSpec.hs40
1 files changed, 36 insertions, 4 deletions
diff --git a/tests/Language/GraphQL/ExecuteSpec.hs b/tests/Language/GraphQL/ExecuteSpec.hs
index 1f8770b..d14eb9d 100644
--- a/tests/Language/GraphQL/ExecuteSpec.hs
+++ b/tests/Language/GraphQL/ExecuteSpec.hs
@@ -8,13 +8,15 @@ module Language.GraphQL.ExecuteSpec
( spec
) where
-import Control.Exception (SomeException)
+import Control.Exception (Exception(..), SomeException)
+import Control.Monad.Catch (throwM)
import Data.Aeson ((.=))
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (emptyObject)
import Data.Conduit
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
+import Data.Typeable (cast)
import Language.GraphQL.AST (Document, Location(..), Name)
import Language.GraphQL.AST.Parser (document)
import Language.GraphQL.Error
@@ -28,6 +30,15 @@ import Test.Hspec (Spec, context, describe, it, shouldBe)
import Text.Megaparsec (parse)
import Text.RawString.QQ (r)
+data PhilosopherException = PhilosopherException
+ deriving Show
+
+instance Exception PhilosopherException where
+ toException = toException. ResolverException
+ fromException e = do
+ ResolverException resolverException <- fromException e
+ cast resolverException
+
philosopherSchema :: Schema (Either SomeException)
philosopherSchema =
schemaWithTypes Nothing queryType Nothing subscriptionRoot extraTypes mempty
@@ -40,14 +51,21 @@ philosopherSchema =
queryType :: Out.ObjectType (Either SomeException)
queryType = Out.ObjectType "Query" Nothing []
- $ HashMap.singleton "philosopher"
- $ ValueResolver philosopherField
- $ pure $ Object mempty
+ $ HashMap.fromList
+ [ ("philosopher", ValueResolver philosopherField philosopherResolver)
+ , ("genres", ValueResolver genresField genresResolver)
+ ]
where
philosopherField =
Out.Field Nothing (Out.NonNullObjectType philosopherType)
$ HashMap.singleton "id"
$ In.Argument Nothing (In.NamedScalarType id) Nothing
+ philosopherResolver = pure $ Object mempty
+ genresField =
+ let fieldType = Out.ListType $ Out.NonNullScalarType string
+ in Out.Field Nothing fieldType HashMap.empty
+ genresResolver :: Resolve (Either SomeException)
+ genresResolver = throwM PhilosopherException
musicType :: Out.ObjectType (Either SomeException)
musicType = Out.ObjectType "Music" Nothing []
@@ -288,6 +306,20 @@ spec =
$ parse document "" "{ philosopher(id: \"1\") { century } }"
in actual `shouldBe` expected
+ it "gives location information for failed result coercion" $
+ let data'' = Aeson.object
+ [ "genres" .= Aeson.Null
+ ]
+ executionErrors = pure $ Error
+ { message = "PhilosopherException"
+ , locations = [Location 1 3]
+ , path = []
+ }
+ expected = Response data'' executionErrors
+ Right (Right actual) = either (pure . parseError) execute'
+ $ parse document "" "{ genres }"
+ in actual `shouldBe` expected
+
context "Subscription" $
it "subscribes" $
let data'' = Aeson.object