summaryrefslogtreecommitdiff
path: root/tests/Language/GraphQL/DirectiveSpec.hs
blob: 6727c509d0063b8b67342709038620f0cbf8df3a (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
{- This Source Code Form is subject to the terms of the Mozilla Public License,
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
   obtain one at https://mozilla.org/MPL/2.0/. -}

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Language.GraphQL.DirectiveSpec
    ( spec
    ) where

import Data.Aeson (object, (.=))
import qualified Data.Aeson as Aeson
import qualified Data.HashMap.Strict as HashMap
import Language.GraphQL.Foundation
import Language.GraphQL.TH
import Language.GraphQL.Type
import qualified Language.GraphQL.Type.Out as Out
import Test.Hspec (Spec, describe, it)
import Test.Hspec.GraphQL

experimentalResolver :: Schema IO
experimentalResolver = schema queryType Nothing Nothing mempty
  where
    queryType = Out.ObjectType "Query" Nothing []
        $ HashMap.singleton "experimentalField"
        $ Out.ValueResolver (Out.Field Nothing (Out.NamedScalarType int) mempty)
        $ pure $ Int 5

emptyObject :: Aeson.Object
emptyObject = HashMap.singleton "data" $ object []

spec :: Spec
spec =
    describe "Directive executor" $ do
        it "should be able to @skip fields" $ do
            let sourceQuery = [gql|
              {
                experimentalField @skip(if: true)
              }
            |]

            actual <- graphql experimentalResolver sourceQuery
            actual `shouldResolveTo` emptyObject

        it "should not skip fields if @skip is false" $ do
            let sourceQuery = [gql|
              {
                experimentalField @skip(if: false)
              }
            |]
                expected = HashMap.singleton "data"
                    $ object
                        [ "experimentalField" .= (5 :: Int)
                        ]
            actual <- graphql experimentalResolver sourceQuery
            actual `shouldResolveTo` expected

        it "should skip fields if @include is false" $ do
            let sourceQuery = [gql|
              {
                experimentalField @include(if: false)
              }
            |]

            actual <- graphql experimentalResolver sourceQuery
            actual `shouldResolveTo` emptyObject

        it "should be able to @skip a fragment spread" $ do
            let sourceQuery = [gql|
              {
                ...experimentalFragment @skip(if: true)
              }

              fragment experimentalFragment on Query {
                experimentalField
              }
            |]

            actual <- graphql experimentalResolver sourceQuery
            actual `shouldResolveTo` emptyObject

        it "should be able to @skip an inline fragment" $ do
            let sourceQuery = [gql|
              {
                ... on Query @skip(if: true) {
                  experimentalField
                }
              }
            |]

            actual <- graphql experimentalResolver sourceQuery
            actual `shouldResolveTo` emptyObject