summaryrefslogtreecommitdiff
path: root/tests/Test/RootOperationSpec.hs
blob: 08955f3bcef4d7bbe1877bd2966a53bfe9914d24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Test.RootOperationSpec
    ( spec
    ) where

import Data.Aeson ((.=), object)
import qualified Data.HashMap.Strict as HashMap
import Data.List.NonEmpty (NonEmpty(..))
import Language.GraphQL
import qualified Language.GraphQL.Schema as Schema
import Test.Hspec (Spec, describe, it, shouldBe)
import Text.RawString.QQ (r)
import Language.GraphQL.Type.Definition
import Language.GraphQL.Type.Schema

hatType :: ObjectType IO
hatType = ObjectType "Hat"
    $ HashMap.singleton resolverName
    $ Field Nothing (ScalarOutputType int) mempty resolve
  where
    (Schema.Resolver resolverName resolve) =
        Schema.scalar "circumference" $ pure (60 :: Int)

schema :: Schema IO
schema = Schema
    (ObjectType "Query" hatField)
    (Just $ ObjectType "Mutation" incrementField)
  where
    queryResolvers = Schema.resolversToMap $ garment :| []
    mutationResolvers = Schema.resolversToMap $ increment :| []
    garment = Schema.object "garment" $ pure
        [ Schema.scalar "circumference" $ pure (60 :: Int)
        ]
    increment = Schema.scalar "incrementCircumference"
        $ pure (61 :: Int)
    incrementField = Field Nothing (ScalarOutputType int) mempty
        <$> mutationResolvers
    hatField = Field Nothing (ObjectOutputType hatType) mempty
        <$> queryResolvers

spec :: Spec
spec =
    describe "Root operation type" $ do
        it "returns objects from the root resolvers" $ do
            let querySource = [r|
              {
                garment {
                  circumference
                }
              }
            |]
                expected = object
                    [ "data" .= object
                        [ "garment" .= object
                            [ "circumference" .= (60 :: Int)
                            ]
                        ]
                    ]
            actual <- graphql schema querySource
            actual `shouldBe` expected

        it "chooses Mutation" $ do
            let querySource = [r|
              mutation {
                incrementCircumference
              }
            |]
                expected = object
                    [ "data" .= object
                        [ "incrementCircumference" .= (61 :: Int)
                        ]
                    ]
            actual <- graphql schema querySource
            actual `shouldBe` expected