2019-07-31 05:40:17 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
2019-07-10 05:57:35 +02:00
|
|
|
module Test.KitchenSinkSpec
|
|
|
|
( spec
|
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.Text.IO as Text.IO
|
2019-08-05 09:00:11 +02:00
|
|
|
import qualified Data.Text.Lazy.IO as Text.Lazy.IO
|
2019-09-27 10:50:38 +02:00
|
|
|
import qualified Data.Text.Lazy as Lazy (Text)
|
2019-11-03 10:42:10 +01:00
|
|
|
import qualified Language.GraphQL.AST.Encoder as Encoder
|
|
|
|
import qualified Language.GraphQL.AST.Parser as Parser
|
2019-07-10 05:57:35 +02:00
|
|
|
import Paths_graphql (getDataFileName)
|
2019-09-27 10:50:38 +02:00
|
|
|
import Test.Hspec (Spec, describe, it)
|
|
|
|
import Test.Hspec.Megaparsec (parseSatisfies)
|
|
|
|
import Text.Megaparsec (parse)
|
2019-07-31 05:40:17 +02:00
|
|
|
import Text.RawString.QQ (r)
|
2019-07-10 05:57:35 +02:00
|
|
|
|
|
|
|
spec :: Spec
|
2019-07-31 05:40:17 +02:00
|
|
|
spec = describe "Kitchen Sink" $ do
|
|
|
|
it "minifies the query" $ do
|
2019-07-27 07:19:21 +02:00
|
|
|
dataFileName <- getDataFileName "tests/data/kitchen-sink.graphql"
|
|
|
|
minFileName <- getDataFileName "tests/data/kitchen-sink.min.graphql"
|
2019-08-05 09:00:11 +02:00
|
|
|
expected <- Text.Lazy.IO.readFile minFileName
|
2019-07-10 05:57:35 +02:00
|
|
|
|
2019-09-27 10:50:38 +02:00
|
|
|
shouldNormalize Encoder.minified dataFileName expected
|
2019-07-31 05:40:17 +02:00
|
|
|
|
|
|
|
it "pretty prints the query" $ do
|
|
|
|
dataFileName <- getDataFileName "tests/data/kitchen-sink.graphql"
|
2019-08-02 13:52:51 +02:00
|
|
|
let expected = [r|query queryName($foo: ComplexType, $site: Site = MOBILE) {
|
2019-08-03 23:57:27 +02:00
|
|
|
whoever123is: node(id: [123, 456]) {
|
|
|
|
id
|
|
|
|
... on User @defer {
|
|
|
|
field2 {
|
|
|
|
id
|
|
|
|
alias: field1(first: 10, after: $foo) @include(if: $foo) {
|
|
|
|
id
|
|
|
|
...frag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-31 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
mutation likeStory {
|
2019-08-03 23:57:27 +02:00
|
|
|
like(story: 123) @defer {
|
|
|
|
story {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
2019-07-31 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 13:52:51 +02:00
|
|
|
fragment frag on Friend {
|
2019-08-03 23:57:27 +02:00
|
|
|
foo(size: $size, bar: $b, obj: {key: "value"})
|
2019-07-31 05:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-08-03 23:57:27 +02:00
|
|
|
unnamed(truthy: true, falsey: false)
|
|
|
|
query
|
2019-07-31 05:40:17 +02:00
|
|
|
}
|
|
|
|
|]
|
|
|
|
|
2019-09-27 10:50:38 +02:00
|
|
|
shouldNormalize Encoder.pretty dataFileName expected
|
|
|
|
|
|
|
|
shouldNormalize :: Encoder.Formatter -> FilePath -> Lazy.Text -> IO ()
|
|
|
|
shouldNormalize formatter dataFileName expected = do
|
|
|
|
actual <- Text.IO.readFile dataFileName
|
|
|
|
parse Parser.document dataFileName actual `parseSatisfies` condition
|
|
|
|
where
|
|
|
|
condition = (expected ==) . Encoder.document formatter
|