diff options
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 |
