diff options
| author | Eugen Wissner <belka@caraus.de> | 2020-06-06 21:22:11 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2020-06-06 21:22:11 +0200 |
| commit | 4c9264c12c15d52e40a245b21acaa70f76cc9ba4 (patch) | |
| tree | a0d305c3145dbabef1a91c793de6f52a3d48a402 /tests | |
| parent | 93a04032886976b540f5fdb1417bd085a642f772 (diff) | |
| download | graphql-4c9264c12c15d52e40a245b21acaa70f76cc9ba4.tar.gz | |
Coerce argument values properly
Fixes #44.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/Language/GraphQL/Execute/CoerceSpec.hs | 16 | ||||
| -rw-r--r-- | tests/Test/StarWars/Schema.hs | 21 |
2 files changed, 20 insertions, 17 deletions
diff --git a/tests/Language/GraphQL/Execute/CoerceSpec.hs b/tests/Language/GraphQL/Execute/CoerceSpec.hs index ed8fd63..15180fb 100644 --- a/tests/Language/GraphQL/Execute/CoerceSpec.hs +++ b/tests/Language/GraphQL/Execute/CoerceSpec.hs @@ -6,12 +6,10 @@ module Language.GraphQL.Execute.CoerceSpec import Data.Aeson as Aeson ((.=)) import qualified Data.Aeson as Aeson import qualified Data.Aeson.Types as Aeson -import Data.HashMap.Strict (HashMap) import qualified Data.HashMap.Strict as HashMap import Data.Maybe (isNothing) import Data.Scientific (scientific) import qualified Data.Set as Set -import Language.GraphQL.AST.Document (Name) import Language.GraphQL.Execute.Coerce import Language.GraphQL.Type.Definition import qualified Language.GraphQL.Type.In as In @@ -22,14 +20,6 @@ direction :: EnumType direction = EnumType "Direction" Nothing $ Set.fromList ["NORTH", "EAST", "SOUTH", "WEST"] -coerceInputLiteral :: In.Type -> Value -> Maybe Subs -coerceInputLiteral input value = coerceInputLiterals - (HashMap.singleton "variableName" input) - (HashMap.singleton "variableName" value) - -lookupActual :: Maybe (HashMap Name Value) -> Maybe Value -lookupActual = (HashMap.lookup "variableName" =<<) - singletonInputObject :: In.Type singletonInputObject = In.NamedInputObjectType type' where @@ -39,7 +29,7 @@ singletonInputObject = In.NamedInputObjectType type' spec :: Spec spec = do - describe "ToGraphQL Aeson" $ do + describe "VariableValue Aeson" $ do it "coerces strings" $ let expected = Just (String "asdf") actual = coerceVariableValue @@ -107,7 +97,7 @@ spec = do let expected = Just (Enum "NORTH") actual = coerceInputLiteral (In.NamedEnumType direction) (Enum "NORTH") - in lookupActual actual `shouldBe` expected + in actual `shouldBe` expected it "fails with non-existing enum value" $ let actual = coerceInputLiteral (In.NamedEnumType direction) (Enum "NORTH_EAST") @@ -115,4 +105,4 @@ spec = do it "coerces integers to IDs" $ let expected = Just (String "1234") actual = coerceInputLiteral (In.NamedScalarType id) (Int 1234) - in lookupActual actual `shouldBe` expected + in actual `shouldBe` expected diff --git a/tests/Test/StarWars/Schema.hs b/tests/Test/StarWars/Schema.hs index b30da1a..d787a07 100644 --- a/tests/Test/StarWars/Schema.hs +++ b/tests/Test/StarWars/Schema.hs @@ -11,10 +11,12 @@ import Data.Functor.Identity (Identity) import qualified Data.HashMap.Strict as HashMap import Data.Maybe (catMaybes) import Data.Text (Text) +import qualified Data.Set as Set import Language.GraphQL.Trans import Language.GraphQL.Type.Definition +import qualified Language.GraphQL.Type.In as In import qualified Language.GraphQL.Type.Out as Out -import Language.GraphQL.Type.Schema +import Language.GraphQL.Type.Schema (Schema(..)) import Test.StarWars.Data import Prelude hiding (id) @@ -24,10 +26,17 @@ schema :: Schema Identity schema = Schema { query = queryType, mutation = Nothing } where queryType = Out.ObjectType "Query" Nothing [] $ HashMap.fromList - [ ("hero", Out.Resolver (Out.Field Nothing (Out.NamedObjectType heroObject) mempty) hero) - , ("human", Out.Resolver (Out.Field Nothing (Out.NamedObjectType heroObject) mempty) human) - , ("droid", Out.Resolver (Out.Field Nothing (Out.NamedObjectType droidObject) mempty) droid) + [ ("hero", Out.Resolver heroField hero) + , ("human", Out.Resolver humanField human) + , ("droid", Out.Resolver droidField droid) ] + heroField = Out.Field Nothing (Out.NamedObjectType heroObject) + $ HashMap.singleton "episode" + $ In.Argument Nothing (In.NamedEnumType episodeEnum) Nothing + humanField = Out.Field Nothing (Out.NamedObjectType heroObject) + $ HashMap.singleton "id" + $ In.Argument Nothing (In.NonNullScalarType string) Nothing + droidField = Out.Field Nothing (Out.NamedObjectType droidObject) mempty heroObject :: Out.ObjectType Identity heroObject = Out.ObjectType "Human" Nothing [] $ HashMap.fromList @@ -76,6 +85,10 @@ idField f = do let (Object v') = v pure $ v' HashMap.! f +episodeEnum :: EnumType +episodeEnum = EnumType "Episode" Nothing + $ Set.fromList ["NEW_HOPE", "EMPIRE", "JEDI"] + hero :: ActionT Identity Value hero = do episode <- argument "episode" |
