From 62cf943b876c9006bf14e37001a2ee2fe4ed4b1a Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 29 Jun 2023 19:18:35 +0200 Subject: [PATCH] Add LocalTime and TimeOfDay instances --- CHANGELOG.md | 2 +- graphql-spice.cabal | 6 +++--- src/Language/GraphQL/Class.hs | 40 +++++++++++++++++++++++++++++------ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb189ba..a6f2a7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ and this project adheres to - `ToGraphQL` and `FromGraphQL` instances for `Word` types, `Float`, `Double`, and `Scientific`. - `ToGraphQL` and `FromGraphQL` instances for `Day`, `DiffTime`, - `NominalDiffTime`, and `UTCTime`. + `NominalDiffTime`, `UTCTime`, `LocalTime` and `TimeOfDay`. - `Resolver`: Export `ServerException`. - `Resolver.defaultResolver`: Throw `FieldNotResolvedException` if the requested field is not in the parent object. diff --git a/graphql-spice.cabal b/graphql-spice.cabal index 2ca4923..b719c62 100644 --- a/graphql-spice.cabal +++ b/graphql-spice.cabal @@ -32,7 +32,7 @@ library hs-source-dirs: src ghc-options: -Wall build-depends: - aeson ^>= 2.0.3, + aeson >= 2.0.3 && < 2.3, base >= 4.7 && < 5, conduit ^>= 1.3.4, containers ^>= 0.6.2, @@ -43,8 +43,8 @@ library scientific ^>= 0.3.7, text >= 1.2 && < 3, time >= 1.11.1, - transformers ^>= 0.5.6, - vector ^>= 0.12.3, + transformers >= 0.5.6 && < 0.7, + vector >= 0.12 && < 0.14, unordered-containers ^>= 0.2.16 default-language: Haskell2010 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