summaryrefslogtreecommitdiff
path: root/src/Language/GraphQL/Class.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Language/GraphQL/Class.hs')
-rw-r--r--src/Language/GraphQL/Class.hs40
1 files changed, 34 insertions, 6 deletions
diff --git a/src/Language/GraphQL/Class.hs b/src/Language/GraphQL/Class.hs
index 49bae57..8062277 100644
--- a/src/Language/GraphQL/Class.hs
+++ b/src/Language/GraphQL/Class.hs
@@ -24,13 +24,20 @@ import qualified Data.Text as Text
import Data.Time
( Day
, DiffTime
+ , LocalTime(..)
, NominalDiffTime
+ , TimeOfDay(..)
, UTCTime(..)
, showGregorian
, secondsToNominalDiffTime
, secondsToDiffTime
)
-import Data.Time.Format.ISO8601 (formatParseM, iso8601Format, iso8601Show)
+import Data.Time.Format.ISO8601
+ ( ISO8601(..)
+ , formatParseM
+ , iso8601Format
+ , iso8601Show
+ )
fromGraphQLToIntegral :: Integral a => Type.Value -> Maybe a
fromGraphQLToIntegral (Type.Int value) = Just $ fromIntegral value
@@ -40,6 +47,13 @@ fromGraphQLToIntegral (Type.String value) =
_conversionError -> Nothing
fromGraphQLToIntegral _ = Nothing
+iso8601ToGraphQL :: ISO8601 t => t -> Type.Value
+iso8601ToGraphQL = Type.String . Text.pack . iso8601Show
+
+fromGraphQLToISO8601 :: ISO8601 t => Type.Value -> Maybe t
+fromGraphQLToISO8601 (Type.String value') = formatParseM iso8601Format $ Text.unpack value'
+fromGraphQLToISO8601 _ = Nothing
+
-- | Instances of this typeclass can be converted to GraphQL internal
-- representation.
class ToGraphQL a
@@ -133,7 +147,15 @@ instance ToGraphQL NominalDiffTime
instance ToGraphQL UTCTime
where
- toGraphQL = Type.String . Text.pack . iso8601Show
+ toGraphQL = iso8601ToGraphQL
+
+instance ToGraphQL TimeOfDay
+ where
+ toGraphQL = iso8601ToGraphQL
+
+instance ToGraphQL LocalTime
+ where
+ toGraphQL = iso8601ToGraphQL
-- | Instances of this typeclass can be used to convert GraphQL internal
-- representation to user-defined type.
@@ -224,8 +246,7 @@ instance FromGraphQL Scientific
instance FromGraphQL Day
where
- fromGraphQL (Type.String value') = formatParseM iso8601Format $ Text.unpack value'
- fromGraphQL _ = Nothing
+ fromGraphQL = fromGraphQLToISO8601
instance FromGraphQL DiffTime
where
@@ -239,5 +260,12 @@ instance FromGraphQL NominalDiffTime
instance FromGraphQL UTCTime
where
- fromGraphQL (Type.String value') = formatParseM iso8601Format $ Text.unpack value'
- fromGraphQL _ = Nothing
+ fromGraphQL = fromGraphQLToISO8601
+
+instance FromGraphQL TimeOfDay
+ where
+ fromGraphQL = fromGraphQLToISO8601
+
+instance FromGraphQL LocalTime
+ where
+ fromGraphQL = fromGraphQLToISO8601