summaryrefslogtreecommitdiff
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
parentdd6fdf69f67a84271a3e9f3b5a10a3cdad2ca082 (diff)
downloadgraphql-cc8f14f12234453667610563df32a3249731437d.tar.gz
Provide a custom Show instance for output Value
-rw-r--r--CHANGELOG.md6
-rw-r--r--src/Language/GraphQL/Type/Definition.hs26
2 files changed, 30 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4ab72fd..998720a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,11 @@ The format is based on
and this project adheres to
[Haskell Package Versioning Policy](https://pvp.haskell.org/).
+## [Unreleased]
+### Fixed
+- Provide a custom `Show` instance for `Type.Definition.Value` (for error
+ messages).
+
## [1.0.0.0]
### Added
- `Language.GraphQL.Execute.OrderedMap` is a map data structure, that preserves
@@ -443,6 +448,7 @@ and this project adheres to
### Added
- Data types for the GraphQL language.
+[Unreleased]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v1.0.0.0&rev_to=master
[1.0.0.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v1.0.0.0&rev_to=v0.11.1.0
[0.11.1.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v0.11.1.0&rev_to=v0.11.0.0
[0.11.0.0]: https://www.caraus.tech/projects/pub-graphql/repository/23/diff?rev=v0.11.0.0&rev_to=v0.10.0.0
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