From d690d22ce89891e990ec275ea9dc196d17decb44 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sat, 27 Jul 2019 07:19:21 +0200 Subject: [PATCH] Test the encoder with the unminified document --- CHANGELOG.md | 4 +++ src/Language/GraphQL/Encoder.hs | 38 ++++++++++++++++------------- tests/Test/KitchenSinkSpec.hs | 8 +++--- tests/data/kitchen-sink.min.graphql | 8 +++++- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f8249f..2d50dcd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,11 +4,15 @@ All notable changes to this project will be documented in this file. ## [Unreleased] ### Added - `executeWithName` executes an operation with the given name. +- Export `Language.GraphQL.Encoder.definition`. ### Changed - `Operation` includes now possible operation name which allows to support documents with multiple operations. +### Removed +- Unused `Language.GraphQL.Encoder.spaced`. + ## [0.4.0.0] - 2019-07-23 ### Added - Support for mutations. diff --git a/src/Language/GraphQL/Encoder.hs b/src/Language/GraphQL/Encoder.hs index c315091..42ce333 100644 --- a/src/Language/GraphQL/Encoder.hs +++ b/src/Language/GraphQL/Encoder.hs @@ -1,24 +1,31 @@ {-# LANGUAGE OverloadedStrings #-} -- | This module defines a printer for the @GraphQL@ language. module Language.GraphQL.Encoder - ( document - , spaced + ( definition + , document ) where import Data.Foldable (fold) import Data.Monoid ((<>)) import qualified Data.List.NonEmpty as NonEmpty (toList) -import Data.Text (Text, cons, intercalate, pack, snoc) +import Data.Text (Text, pack) +import qualified Data.Text as Text import Language.GraphQL.AST --- * Document - +-- | Converts a 'Document' into a string. document :: Document -> Text -document defs = (`snoc` '\n') . mconcat . NonEmpty.toList $ definition <$> defs +document defs = Text.intercalate "\n" + . NonEmpty.toList + $ definition <$> defs +-- | Converts a 'Definition' into a string. definition :: Definition -> Text -definition (DefinitionOperation x) = operationDefinition x -definition (DefinitionFragment x) = fragmentDefinition x +definition x = Text.snoc (encodeDefinition x) '\n' + where + encodeDefinition (DefinitionOperation operation) + = operationDefinition operation + encodeDefinition (DefinitionFragment fragment) + = fragmentDefinition fragment operationDefinition :: OperationDefinition -> Text operationDefinition (OperationSelectionSet sels) = selectionSet sels @@ -60,7 +67,7 @@ selection (SelectionFragmentSpread x) = fragmentSpread x field :: Field -> Text field (Field alias name args dirs selso) = - optempty (`snoc` ':') (fold alias) + optempty (`Text.snoc` ':') (fold alias) <> name <> optempty arguments args <> optempty directives dirs @@ -146,11 +153,8 @@ nonNullType (NonNullTypeList x) = listType x <> "!" -- * Internal -spaced :: Text -> Text -spaced = cons '\SP' - between :: Char -> Char -> Text -> Text -between open close = cons open . (`snoc` close) +between open close = Text.cons open . (`Text.snoc` close) parens :: Text -> Text parens = between '(' ')' @@ -165,16 +169,16 @@ quotes :: Text -> Text quotes = between '"' '"' spaces :: (a -> Text) -> [a] -> Text -spaces f = intercalate "\SP" . fmap f +spaces f = Text.intercalate "\SP" . fmap f parensCommas :: (a -> Text) -> [a] -> Text -parensCommas f = parens . intercalate "," . fmap f +parensCommas f = parens . Text.intercalate "," . fmap f bracketsCommas :: (a -> Text) -> [a] -> Text -bracketsCommas f = brackets . intercalate "," . fmap f +bracketsCommas f = brackets . Text.intercalate "," . fmap f bracesCommas :: (a -> Text) -> [a] -> Text -bracesCommas f = braces . intercalate "," . fmap f +bracesCommas f = braces . Text.intercalate "," . fmap f optempty :: (Eq a, Monoid a, Monoid b) => (a -> b) -> a -> b optempty f xs = if xs == mempty then mempty else f xs diff --git a/tests/Test/KitchenSinkSpec.hs b/tests/Test/KitchenSinkSpec.hs index 99a00f7..f9f9395 100644 --- a/tests/Test/KitchenSinkSpec.hs +++ b/tests/Test/KitchenSinkSpec.hs @@ -20,10 +20,12 @@ import Text.Megaparsec ( errorBundlePretty spec :: Spec spec = describe "Kitchen Sink" $ it "prints the query" $ do - dataFileName <- getDataFileName "tests/data/kitchen-sink.min.graphql" - expected <- Text.IO.readFile dataFileName + dataFileName <- getDataFileName "tests/data/kitchen-sink.graphql" + minFileName <- getDataFileName "tests/data/kitchen-sink.min.graphql" + actual <- Text.IO.readFile dataFileName + expected <- Text.IO.readFile minFileName either (expectationFailure . errorBundlePretty) (flip shouldBe expected . Encoder.document) - $ parse Parser.document dataFileName expected + $ parse Parser.document dataFileName actual diff --git a/tests/data/kitchen-sink.min.graphql b/tests/data/kitchen-sink.min.graphql index 4f8553d..c39e6ba 100644 --- a/tests/data/kitchen-sink.min.graphql +++ b/tests/data/kitchen-sink.min.graphql @@ -1 +1,7 @@ -query queryName($foo:ComplexType,$site:Site=MOBILE){whoever123is:node(id:[123,456]){id,... on User@defer{field2{id,alias:field1(first:10,after:$foo)@include(if:$foo){id,...frag}}}}}mutation likeStory{like(story:123)@defer{story{id}}}fragment frag on Friend{foo(size:$size,bar:$b,obj:{key:"value"})} +query queryName($foo:ComplexType,$site:Site=MOBILE){whoever123is:node(id:[123,456]){id,... on User@defer{field2{id,alias:field1(first:10,after:$foo)@include(if:$foo){id,...frag}}}}} + +mutation likeStory{like(story:123)@defer{story{id}}} + +fragment frag on Friend{foo(size:$size,bar:$b,obj:{key:"value"})} + +{unnamed(truthy:true,falsey:false),query}