forked from OSS/graphql-spice
Add LocalTime and TimeOfDay instances
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user