2020-07-15 19:15:31 +02:00
|
|
|
{- 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/. -}
|
|
|
|
|
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-06-19 10:53:41 +02:00
|
|
|
import Language.GraphQL.Type
|
2020-05-24 13:51:00 +02:00
|
|
|
import qualified Language.GraphQL.Type.Out as Out
|
2020-07-19 07:36:06 +02:00
|
|
|
import Test.Hspec (Spec, describe, it)
|
|
|
|
import Test.Hspec.GraphQL
|
2019-12-06 22:52:24 +01:00
|
|
|
import Text.RawString.QQ (r)
|
|
|
|
|
2020-05-14 09:17:14 +02:00
|
|
|
experimentalResolver :: Schema IO
|
2020-10-07 05:24:51 +02:00
|
|
|
experimentalResolver = schema queryType Nothing Nothing mempty
|
2020-05-14 09:17:14 +02:00
|
|
|
where
|
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-07-14 19:37:56 +02:00
|
|
|
$ Out.ValueResolver (Out.Field Nothing (Out.NamedScalarType int) mempty)
|
|
|
|
$ pure $ Int 5
|
2019-12-06 22:52:24 +01:00
|
|
|
|
2020-07-19 07:36:06 +02:00
|
|
|
emptyObject :: Aeson.Object
|
|
|
|
emptyObject = HashMap.singleton "data" $ object []
|
2019-12-06 22:52:24 +01:00
|
|
|
|
|
|
|
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
|
2020-07-19 07:36:06 +02:00
|
|
|
actual `shouldResolveTo` emptyObject
|
2019-12-06 22:52:24 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|]
|
2020-07-19 07:36:06 +02:00
|
|
|
expected = HashMap.singleton "data"
|
|
|
|
$ object
|
2019-12-06 22:52:24 +01:00
|
|
|
[ "experimentalField" .= (5 :: Int)
|
|
|
|
]
|
2020-05-14 09:17:14 +02:00
|
|
|
actual <- graphql experimentalResolver sourceQuery
|
2020-07-19 07:36:06 +02:00
|
|
|
actual `shouldResolveTo` expected
|
2019-12-06 22:52:24 +01:00
|
|
|
|
|
|
|
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
|
2020-07-19 07:36:06 +02:00
|
|
|
actual `shouldResolveTo` emptyObject
|
2019-12-06 22:52:24 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:12:19 +02:00
|
|
|
fragment experimentalFragment on Query {
|
2019-12-06 22:52:24 +01:00
|
|
|
experimentalField
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
|
2020-05-14 09:17:14 +02:00
|
|
|
actual <- graphql experimentalResolver sourceQuery
|
2020-07-19 07:36:06 +02:00
|
|
|
actual `shouldResolveTo` emptyObject
|
2019-12-06 22:52:24 +01:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2020-09-04 19:12:19 +02:00
|
|
|
... on Query @skip(if: true) {
|
2019-12-06 22:52:24 +01:00
|
|
|
experimentalField
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
|
2020-05-14 09:17:14 +02:00
|
|
|
actual <- graphql experimentalResolver sourceQuery
|
2020-07-19 07:36:06 +02:00
|
|
|
actual `shouldResolveTo` emptyObject
|