Add LocalTime and TimeOfDay instances

This commit is contained in:
Eugen Wissner 2023-06-29 19:18:35 +02:00
parent 36f45861de
commit 62cf943b87
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
3 changed files with 38 additions and 10 deletions

View File

@ -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.

View File

@ -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

View File

@ -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