{- This Source Code Form is subject to the terms of the Mozilla Public License,
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
   obtain one at https://mozilla.org/MPL/2.0/. -}

{-# LANGUAGE OverloadedStrings #-}

-- | ToGraphQL and FromGraphQL typeclasses used for user-defined type
-- conversion.
module Language.GraphQL.Class
    ( FromGraphQL(..)
    , ToGraphQL(..)
    ) where

import Data.Foldable (toList)
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Text (Text)
import qualified Data.Text.Read as Text.Read
import Data.Vector (Vector)
import qualified Data.Vector as Vector
import qualified Language.GraphQL.Type as Type

fromGraphQLToIntegral :: Integral a => Type.Value -> Maybe a
fromGraphQLToIntegral :: forall a. Integral a => Value -> Maybe a
fromGraphQLToIntegral (Type.Int Int32
value) = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
value
fromGraphQLToIntegral (Type.String Text
value) =
    case forall a. Integral a => Reader a
Text.Read.decimal Text
value of
        Right (a
converted, Text
"") -> forall a. a -> Maybe a
Just a
converted
        Either String (a, Text)
_conversionError -> forall a. Maybe a
Nothing
fromGraphQLToIntegral Value
_ = forall a. Maybe a
Nothing

-- | Instances of this typeclass can be converted to GraphQL internal
-- representation.
class ToGraphQL a where
    toGraphQL :: a -> Type.Value

instance ToGraphQL Text where
    toGraphQL :: Text -> Value
toGraphQL = Text -> Value
Type.String

instance ToGraphQL Int where
    toGraphQL :: Int -> Value
toGraphQL = Int32 -> Value
Type.Int forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral

instance ToGraphQL Int8 where
    toGraphQL :: Int8 -> Value
toGraphQL = Int32 -> Value
Type.Int forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral

instance ToGraphQL Int16 where
    toGraphQL :: Int16 -> Value
toGraphQL = Int32 -> Value
Type.Int forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral

instance ToGraphQL Int32 where
    toGraphQL :: Int32 -> Value
toGraphQL = Int32 -> Value
Type.Int

instance ToGraphQL Int64 where
    toGraphQL :: Int64 -> Value
toGraphQL = Int32 -> Value
Type.Int forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral

instance ToGraphQL a => ToGraphQL [a] where
    toGraphQL :: [a] -> Value
toGraphQL = [Value] -> Value
Type.List forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. ToGraphQL a => a -> Value
toGraphQL

instance ToGraphQL a => ToGraphQL (Vector a) where
    toGraphQL :: Vector a -> Value
toGraphQL = [Value] -> Value
Type.List forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. ToGraphQL a => a -> Value
toGraphQL

instance ToGraphQL a => ToGraphQL (Maybe a) where
    toGraphQL :: Maybe a -> Value
toGraphQL (Just a
justValue) = forall a. ToGraphQL a => a -> Value
toGraphQL a
justValue
    toGraphQL Maybe a
Nothing = Value
Type.Null

instance ToGraphQL Bool where
    toGraphQL :: Bool -> Value
toGraphQL = Bool -> Value
Type.Boolean

-- | Instances of this typeclass can be used to convert GraphQL internal
-- representation to user-defined type.
class FromGraphQL a where
    fromGraphQL :: Type.Value -> Maybe a

instance FromGraphQL Text where
    fromGraphQL :: Value -> Maybe Text
fromGraphQL (Type.String Text
value) = forall a. a -> Maybe a
Just Text
value
    fromGraphQL Value
_ = forall a. Maybe a
Nothing

instance FromGraphQL Int where
    fromGraphQL :: Value -> Maybe Int
fromGraphQL = forall a. Integral a => Value -> Maybe a
fromGraphQLToIntegral

instance FromGraphQL Int8 where
    fromGraphQL :: Value -> Maybe Int8
fromGraphQL = forall a. Integral a => Value -> Maybe a
fromGraphQLToIntegral

instance FromGraphQL Int16 where
    fromGraphQL :: Value -> Maybe Int16
fromGraphQL = forall a. Integral a => Value -> Maybe a
fromGraphQLToIntegral

instance FromGraphQL Int32 where
    fromGraphQL :: Value -> Maybe Int32
fromGraphQL = forall a. Integral a => Value -> Maybe a
fromGraphQLToIntegral

instance FromGraphQL Int64 where
    fromGraphQL :: Value -> Maybe Int64
fromGraphQL = forall a. Integral a => Value -> Maybe a
fromGraphQLToIntegral

instance FromGraphQL a => FromGraphQL [a] where
    fromGraphQL :: Value -> Maybe [a]
fromGraphQL (Type.List [Value]
value) = forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall a. FromGraphQL a => Value -> Maybe a
fromGraphQL [Value]
value
    fromGraphQL Value
_ = forall a. Maybe a
Nothing

instance FromGraphQL a => FromGraphQL (Vector a) where
    fromGraphQL :: Value -> Maybe (Vector a)
fromGraphQL (Type.List [Value]
value) = forall a. [a] -> Vector a
Vector.fromList
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall a. FromGraphQL a => Value -> Maybe a
fromGraphQL [Value]
value
    fromGraphQL Value
_ = forall a. Maybe a
Nothing

instance FromGraphQL a => FromGraphQL (Maybe a) where
    fromGraphQL :: Value -> Maybe (Maybe a)
fromGraphQL Value
Type.Null = forall a. a -> Maybe a
Just forall a. Maybe a
Nothing
    fromGraphQL Value
value = forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromGraphQL a => Value -> Maybe a
fromGraphQL Value
value

instance FromGraphQL Bool where
    fromGraphQL :: Value -> Maybe Bool
fromGraphQL (Type.Boolean Bool
value) = forall a. a -> Maybe a
Just Bool
value
    fromGraphQL Value
_ = forall a. Maybe a
Nothing