summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2021-08-31 17:29:20 +0200
committerEugen Wissner <belka@caraus.de>2021-08-31 17:29:20 +0200
commitcc8f14f12234453667610563df32a3249731437d (patch)
tree25da72fc9e7d9001e34a898f32c962504a4329c5 /src
parentdd6fdf69f67a84271a3e9f3b5a10a3cdad2ca082 (diff)
downloadgraphql-cc8f14f12234453667610563df32a3249731437d.tar.gz
Provide a custom Show instance for output Value
Diffstat (limited to 'src')
-rw-r--r--src/Language/GraphQL/Type/Definition.hs26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/Language/GraphQL/Type/Definition.hs b/src/Language/GraphQL/Type/Definition.hs
index 6ca77aa..076b38e 100644
--- a/src/Language/GraphQL/Type/Definition.hs
+++ b/src/Language/GraphQL/Type/Definition.hs
@@ -20,10 +20,12 @@ module Language.GraphQL.Type.Definition
import Data.Int (Int32)
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
+import Data.List (intercalate)
import Data.String (IsString(..))
import Data.Text (Text)
import qualified Data.Text as Text
-import Language.GraphQL.AST (Name)
+import Language.GraphQL.AST (Name, escape)
+import Numeric (showFloat)
import Prelude hiding (id)
-- | Represents accordingly typed GraphQL values.
@@ -36,7 +38,27 @@ data Value
| Enum Name
| List [Value] -- ^ Arbitrary nested list.
| Object (HashMap Name Value)
- deriving (Eq, Show)
+ deriving Eq
+
+instance Show Value where
+ showList = mappend . showList'
+ where
+ showList' list = "[" ++ intercalate ", " (show <$> list) ++ "]"
+ show (Int integer) = show integer
+ show (Float float') = showFloat float' mempty
+ show (String text) = "\"" <> Text.foldr (mappend . escape) "\"" text
+ show (Boolean boolean') = show boolean'
+ show Null = "null"
+ show (Enum name) = Text.unpack name
+ show (List list) = show list
+ show (Object fields) = unwords
+ [ "{"
+ , intercalate ", " (HashMap.foldrWithKey showObject [] fields)
+ , "}"
+ ]
+ where
+ showObject key value accumulator =
+ concat [Text.unpack key, ": ", show value] : accumulator
instance IsString Value where
fromString = String . fromString