diff options
| author | Eugen Wissner <belka@caraus.de> | 2021-11-20 07:20:31 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2021-11-20 07:20:31 +0100 |
| commit | fdd627bf5d7ae7a78f716ffb2245710e5e200477 (patch) | |
| tree | d8af1fe82a38782c4313cc8d0a0a9a261bb3750b /tests/Language/GraphQL/DirectiveSpec.hs | |
| parent | b387c10d750783822e0ce40fb813355f132c50c9 (diff) | |
| download | graphql-spice-fdd627bf5d7ae7a78f716ffb2245710e5e200477.tar.gz | |
Move functional tests
Diffstat (limited to 'tests/Language/GraphQL/DirectiveSpec.hs')
| -rw-r--r-- | tests/Language/GraphQL/DirectiveSpec.hs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/Language/GraphQL/DirectiveSpec.hs b/tests/Language/GraphQL/DirectiveSpec.hs new file mode 100644 index 0000000..40f6215 --- /dev/null +++ b/tests/Language/GraphQL/DirectiveSpec.hs @@ -0,0 +1,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 +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 |
