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`, - `ToGraphQL` and `FromGraphQL` instances for `Word` types, `Float`, `Double`,
and `Scientific`. and `Scientific`.
- `ToGraphQL` and `FromGraphQL` instances for `Day`, `DiffTime`, - `ToGraphQL` and `FromGraphQL` instances for `Day`, `DiffTime`,
`NominalDiffTime`, and `UTCTime`. `NominalDiffTime`, `UTCTime`, `LocalTime` and `TimeOfDay`.
- `Resolver`: Export `ServerException`. - `Resolver`: Export `ServerException`.
- `Resolver.defaultResolver`: Throw `FieldNotResolvedException` if the requested - `Resolver.defaultResolver`: Throw `FieldNotResolvedException` if the requested
field is not in the parent object. field is not in the parent object.

View File

@ -32,7 +32,7 @@ library
hs-source-dirs: src hs-source-dirs: src
ghc-options: -Wall ghc-options: -Wall
build-depends: build-depends:
aeson ^>= 2.0.3, aeson >= 2.0.3 && < 2.3,
base >= 4.7 && < 5, base >= 4.7 && < 5,
conduit ^>= 1.3.4, conduit ^>= 1.3.4,
containers ^>= 0.6.2, containers ^>= 0.6.2,
@ -43,8 +43,8 @@ library
scientific ^>= 0.3.7, scientific ^>= 0.3.7,
text >= 1.2 && < 3, text >= 1.2 && < 3,
time >= 1.11.1, time >= 1.11.1,
transformers ^>= 0.5.6, transformers >= 0.5.6 && < 0.7,
vector ^>= 0.12.3, vector >= 0.12 && < 0.14,
unordered-containers ^>= 0.2.16 unordered-containers ^>= 0.2.16
default-language: Haskell2010 default-language: Haskell2010

View File

@ -24,13 +24,20 @@ import qualified Data.Text as Text
import Data.Time import Data.Time
( Day ( Day
, DiffTime , DiffTime
, LocalTime(..)
, NominalDiffTime , NominalDiffTime
, TimeOfDay(..)
, UTCTime(..) , UTCTime(..)
, showGregorian , showGregorian
, secondsToNominalDiffTime , secondsToNominalDiffTime
, secondsToDiffTime , 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 :: Integral a => Type.Value -> Maybe a
fromGraphQLToIntegral (Type.Int value) = Just $ fromIntegral value fromGraphQLToIntegral (Type.Int value) = Just $ fromIntegral value
@ -40,6 +47,13 @@ fromGraphQLToIntegral (Type.String value) =
_conversionError -> Nothing _conversionError -> Nothing
fromGraphQLToIntegral _ = 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 -- | Instances of this typeclass can be converted to GraphQL internal
-- representation. -- representation.
class ToGraphQL a class ToGraphQL a
@ -133,7 +147,15 @@ instance ToGraphQL NominalDiffTime
instance ToGraphQL UTCTime instance ToGraphQL UTCTime
where 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 -- | Instances of this typeclass can be used to convert GraphQL internal
-- representation to user-defined type. -- representation to user-defined type.
@ -224,8 +246,7 @@ instance FromGraphQL Scientific
instance FromGraphQL Day instance FromGraphQL Day
where where
fromGraphQL (Type.String value') = formatParseM iso8601Format $ Text.unpack value' fromGraphQL = fromGraphQLToISO8601
fromGraphQL _ = Nothing
instance FromGraphQL DiffTime instance FromGraphQL DiffTime
where where
@ -239,5 +260,12 @@ instance FromGraphQL NominalDiffTime
instance FromGraphQL UTCTime instance FromGraphQL UTCTime
where where
fromGraphQL (Type.String value') = formatParseM iso8601Format $ Text.unpack value' fromGraphQL = fromGraphQLToISO8601
fromGraphQL _ = Nothing
instance FromGraphQL TimeOfDay
where
fromGraphQL = fromGraphQLToISO8601
instance FromGraphQL LocalTime
where
fromGraphQL = fromGraphQLToISO8601