summaryrefslogtreecommitdiff
path: root/tests/Test/FragmentSpec.hs
blob: 189306dfdfc6e43f9c313e7b48b2113efd46c88c (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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Test.FragmentSpec
    ( spec
    ) where

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

size :: Schema.Resolver IO
size = Schema.scalar "size" $ return ("L" :: Text)

circumference :: Schema.Resolver IO
circumference = Schema.scalar "circumference" $ return (60 :: Int)

garment :: Text -> Schema.Resolver IO
garment typeName = Schema.object "garment" $ return
    [ if typeName == "Hat" then circumference else size
    , Schema.scalar "__typename" $ return typeName
    ]

inlineQuery :: Text
inlineQuery = [r|{
  garment {
    ... on Hat {
      circumference
    }
    ... on Shirt {
      size
    }
  }
}|]

spec :: Spec
spec = describe "Inline fragment executor" $ do
    it "chooses the first selection if the type matches" $ do
        actual <- graphql (garment "Hat" :| []) inlineQuery
        let expected = object
                [ "data" .= object
                    [ "garment" .= object
                        [ "circumference" .= (60 :: Int)
                        ]
                    ]
                ]
         in actual `shouldBe` expected

    it "chooses the last selection if the type matches" $ do
        actual <- graphql (garment "Shirt" :| []) inlineQuery
        let expected = object
                [ "data" .= object
                    [ "garment" .= object
                        [ "size" .= ("L" :: Text)
                        ]
                    ]
                ]
         in actual `shouldBe` expected