Add a few missing instances #9

Merged
belka merged 1 commits from prescientmoon/graphql-spice:master into master 2024-11-19 22:28:50 +01:00

View File

@ -5,6 +5,7 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-} {-# LANGUAGE TupleSections #-}
{-# LANGUAGE FlexibleInstances #-}
-- | ToGraphQL and FromGraphQL typeclasses used for user-defined type -- | ToGraphQL and FromGraphQL typeclasses used for user-defined type
-- conversion. -- conversion.
@ -101,6 +102,10 @@ class ToGraphQL a
where where
toGraphQL :: a -> Type.Value toGraphQL :: a -> Type.Value
instance ToGraphQL Type.Value
where
toGraphQL a = a
instance ToGraphQL Text instance ToGraphQL Text
where where
toGraphQL = Type.String toGraphQL = Type.String
@ -198,12 +203,20 @@ instance ToGraphQL LocalTime
where where
toGraphQL = iso8601ToGraphQL toGraphQL = iso8601ToGraphQL
instance ToGraphQL a => ToGraphQL (HashMap.HashMap Text a)
where
toGraphQL = Type.Object . fmap toGraphQL
-- | 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.
class FromGraphQL a class FromGraphQL a
where where
fromGraphQL :: Type.Value -> Maybe a fromGraphQL :: Type.Value -> Maybe a
instance FromGraphQL Type.Value
where
fromGraphQL = Just
instance FromGraphQL Text instance FromGraphQL Text
where where
fromGraphQL (Type.String value) = Just value fromGraphQL (Type.String value) = Just value
@ -311,6 +324,11 @@ instance FromGraphQL LocalTime
where where
fromGraphQL = fromGraphQLToISO8601 fromGraphQL = fromGraphQLToISO8601
instance FromGraphQL a => FromGraphQL (HashMap.HashMap Text a)
where
fromGraphQL (Type.Object hm) = traverse fromGraphQL hm
fromGraphQL _ = Nothing
stringLE :: Name -> Q Exp stringLE :: Name -> Q Exp
stringLE = litE . stringL . nameBase stringLE = litE . stringL . nameBase