forked from OSS/graphql-spice
Add instances for UTCTime
This commit is contained in:
@ -20,6 +20,17 @@ import Data.Vector (Vector)
|
||||
import qualified Data.Vector as Vector
|
||||
import qualified Language.GraphQL.Type as Type
|
||||
import Data.Scientific (Scientific, toRealFloat)
|
||||
import qualified Data.Text as Text
|
||||
import Data.Time
|
||||
( Day
|
||||
, DiffTime
|
||||
, NominalDiffTime
|
||||
, UTCTime(..)
|
||||
, showGregorian
|
||||
, secondsToNominalDiffTime
|
||||
, secondsToDiffTime
|
||||
)
|
||||
import Data.Time.Format.ISO8601 (formatParseM, iso8601Format, iso8601Show)
|
||||
|
||||
fromGraphQLToIntegral :: Integral a => Type.Value -> Maybe a
|
||||
fromGraphQLToIntegral (Type.Int value) = Just $ fromIntegral value
|
||||
@ -108,6 +119,22 @@ instance ToGraphQL Scientific
|
||||
where
|
||||
toGraphQL = Type.Float . toRealFloat
|
||||
|
||||
instance ToGraphQL Day
|
||||
where
|
||||
toGraphQL = Type.String . Text.pack . showGregorian
|
||||
|
||||
instance ToGraphQL DiffTime
|
||||
where
|
||||
toGraphQL = Type.Int . truncate . (realToFrac :: DiffTime -> Double)
|
||||
|
||||
instance ToGraphQL NominalDiffTime
|
||||
where
|
||||
toGraphQL = Type.Int . truncate . (realToFrac :: NominalDiffTime -> Double)
|
||||
|
||||
instance ToGraphQL UTCTime
|
||||
where
|
||||
toGraphQL = Type.String . Text.pack . iso8601Show
|
||||
|
||||
-- | Instances of this typeclass can be used to convert GraphQL internal
|
||||
-- representation to user-defined type.
|
||||
class FromGraphQL a
|
||||
@ -194,3 +221,23 @@ instance FromGraphQL Scientific
|
||||
where
|
||||
fromGraphQL (Type.Float value) = Just $ realToFrac value
|
||||
fromGraphQL _ = Nothing
|
||||
|
||||
instance FromGraphQL Day
|
||||
where
|
||||
fromGraphQL (Type.String value') = formatParseM iso8601Format $ Text.unpack value'
|
||||
fromGraphQL _ = Nothing
|
||||
|
||||
instance FromGraphQL DiffTime
|
||||
where
|
||||
fromGraphQL (Type.Int value') = Just $ secondsToDiffTime $ fromIntegral value'
|
||||
fromGraphQL _ = Nothing
|
||||
|
||||
instance FromGraphQL NominalDiffTime
|
||||
where
|
||||
fromGraphQL (Type.Int value') = Just $ secondsToNominalDiffTime $ fromIntegral value'
|
||||
fromGraphQL _ = Nothing
|
||||
|
||||
instance FromGraphQL UTCTime
|
||||
where
|
||||
fromGraphQL (Type.String value') = formatParseM iso8601Format $ Text.unpack value'
|
||||
fromGraphQL _ = Nothing
|
||||
|
Reference in New Issue
Block a user