diff options
| author | Eugen Wissner <belka@caraus.de> | 2023-06-26 16:50:14 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2023-06-26 16:50:14 +0200 |
| commit | 36f45861ded111c2cbe69cb28e068583a4b9030f (patch) | |
| tree | 287d25929f7b01da7eed3b1ce00d26d89cfa7310 /src/Language/GraphQL/Class.hs | |
| parent | f90feb488d718c57509375c9627f912b8826c914 (diff) | |
| download | graphql-spice-36f45861ded111c2cbe69cb28e068583a4b9030f.tar.gz | |
Add instances for UTCTime
Diffstat (limited to 'src/Language/GraphQL/Class.hs')
| -rw-r--r-- | src/Language/GraphQL/Class.hs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/Language/GraphQL/Class.hs b/src/Language/GraphQL/Class.hs index cce1bae..49bae57 100644 --- a/src/Language/GraphQL/Class.hs +++ b/src/Language/GraphQL/Class.hs @@ -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 |
