2019-12-06 22:52:24 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
module Test.DirectiveSpec
|
|
|
|
( spec
|
|
|
|
) where
|
|
|
|
|
2020-05-27 23:18:35 +02:00
|
|
|
import Data.Aeson (object, (.=))
|
|
|
|
import qualified Data.Aeson as Aeson
|
2020-05-10 18:32:58 +02:00
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
2019-12-06 22:52:24 +01:00
|
|
|
import Language.GraphQL
|
2020-05-14 09:17:14 +02:00
|
|
|
import Language.GraphQL.Type.Definition
|
2020-05-24 13:51:00 +02:00
|
|
|
import qualified Language.GraphQL.Type.Out as Out
|
2020-05-14 09:17:14 +02:00
|
|
|
import Language.GraphQL.Type.Schema (Schema(..))
|
2019-12-06 22:52:24 +01:00
|
|
|
import Test.Hspec (Spec, describe, it, shouldBe)
|
|
|
|
import Text.RawString.QQ (r)
|
|
|
|
|
2020-05-14 09:17:14 +02:00
|
|
|
experimentalResolver :: Schema IO
|
|
|
|
experimentalResolver = Schema { query = queryType, mutation = Nothing }
|
|
|
|
where
|
2020-05-27 23:18:35 +02:00
|
|
|
resolver = pure $ Int 5
|
2020-05-26 11:13:55 +02:00
|
|
|
queryType = Out.ObjectType "Query" Nothing []
|
2020-05-14 09:17:14 +02:00
|
|
|
$ HashMap.singleton "experimentalField"
|
2020-06-03 07:20:38 +02:00
|
|
|
$ Out.Resolver (Out.Field Nothing (Out.NamedScalarType int) mempty) resolver
|
2019-12-06 22:52:24 +01:00
|
|
|
|
2020-05-27 23:18:35 +02:00
|
|
|
emptyObject :: Aeson.Value
|
2019-12-06 22:52:24 +01:00
|
|
|
emptyObject = object
|
|
|
|
[ "data" .= object []
|
|
|
|
]
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec =
|
|
|
|
describe "Directive executor" $ do
|
|
|
|
it "should be able to @skip fields" $ do
|
2020-05-14 09:17:14 +02:00
|
|
|
let sourceQuery = [r|
|
2019-12-06 22:52:24 +01:00
|
|
|
{
|
|
|
|
experimentalField @skip(if: true)
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
|
2020-05-14 09:17:14 +02:00
|
|
|
actual <- graphql experimentalResolver sourceQuery
|
2019-12-06 22:52:24 +01:00
|
|
|
actual `shouldBe` emptyObject
|
|
|
|
|
|
|
|
it "should not skip fields if @skip is false" $ do
|
2020-05-14 09:17:14 +02:00
|
|
|
let sourceQuery = [r|
|
2019-12-06 22:52:24 +01:00
|
|
|
{
|
|
|
|
experimentalField @skip(if: false)
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
expected = object
|
|
|
|
[ "data" .= object
|
|
|
|
[ "experimentalField" .= (5 :: Int)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2020-05-14 09:17:14 +02:00
|
|
|
actual <- graphql experimentalResolver sourceQuery
|
2019-12-06 22:52:24 +01:00
|
|
|
actual `shouldBe` expected
|
|
|
|
|
|
|
|
it "should skip fields if @include is false" $ do
|
2020-05-14 09:17:14 +02:00
|
|
|
let sourceQuery = [r|
|
2019-12-06 22:52:24 +01:00
|
|
|
{
|
|
|
|
experimentalField @include(if: false)
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
|
2020-05-14 09:17:14 +02:00
|
|
|
actual <- graphql experimentalResolver sourceQuery
|
2019-12-06 22:52:24 +01:00
|
|
|
actual `shouldBe` emptyObject
|
|
|
|
|
|
|
|
it "should be able to @skip a fragment spread" $ do
|
2020-05-14 09:17:14 +02:00
|
|
|
let sourceQuery = [r|
|
2019-12-06 22:52:24 +01:00
|
|
|
{
|
|
|
|
...experimentalFragment @skip(if: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment experimentalFragment on ExperimentalType {
|
|
|
|
experimentalField
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
|
2020-05-14 09:17:14 +02:00
|
|
|
actual <- graphql experimentalResolver sourceQuery
|
2019-12-06 22:52:24 +01:00
|
|
|
actual `shouldBe` emptyObject
|
|
|
|
|
|
|
|
it "should be able to @skip an inline fragment" $ do
|
2020-05-14 09:17:14 +02:00
|
|
|
let sourceQuery = [r|
|
2019-12-06 22:52:24 +01:00
|
|
|
{
|
|
|
|
... on ExperimentalType @skip(if: true) {
|
|
|
|
experimentalField
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
|
2020-05-14 09:17:14 +02:00
|
|
|
actual <- graphql experimentalResolver sourceQuery
|
2019-12-06 22:52:24 +01:00
|
|
|
actual `shouldBe` emptyObject
|