{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Data.Morpheus.Types.GQLWrapper
( EncodeWrapper (..),
DecodeWrapper (..),
DecodeWrapperConstraint,
)
where
import qualified Data.List.NonEmpty as NonEmpty
import Data.Morpheus.App.Internal.Resolving
( ResolverValue,
SubscriptionField (..),
mkList,
mkNull,
)
import Data.Morpheus.Types.Internal.AST
( GQLError,
ValidValue,
Value (..),
msg,
)
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import Data.Vector (Vector)
import qualified Data.Vector as Vector
import Relude
class EncodeWrapper (wrapper :: Type -> Type) where
encodeWrapper ::
(Monad m) =>
(a -> m (ResolverValue m)) ->
wrapper a ->
m (ResolverValue m)
withList ::
( EncodeWrapper f,
Monad m
) =>
(a -> f b) ->
(b -> m (ResolverValue m)) ->
a ->
m (ResolverValue m)
withList :: (a -> f b)
-> (b -> m (ResolverValue m)) -> a -> m (ResolverValue m)
withList a -> f b
f b -> m (ResolverValue m)
encodeValue = (b -> m (ResolverValue m)) -> f b -> m (ResolverValue m)
forall (wrapper :: * -> *) (m :: * -> *) a.
(EncodeWrapper wrapper, Monad m) =>
(a -> m (ResolverValue m)) -> wrapper a -> m (ResolverValue m)
encodeWrapper b -> m (ResolverValue m)
encodeValue (f b -> m (ResolverValue m))
-> (a -> f b) -> a -> m (ResolverValue m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> f b
f
instance EncodeWrapper Maybe where
encodeWrapper :: (a -> m (ResolverValue m)) -> Maybe a -> m (ResolverValue m)
encodeWrapper = m (ResolverValue m)
-> (a -> m (ResolverValue m)) -> Maybe a -> m (ResolverValue m)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ResolverValue m -> m (ResolverValue m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ResolverValue m
forall (m :: * -> *). ResolverValue m
mkNull)
instance EncodeWrapper [] where
encodeWrapper :: (a -> m (ResolverValue m)) -> [a] -> m (ResolverValue m)
encodeWrapper a -> m (ResolverValue m)
encodeValue = ([ResolverValue m] -> ResolverValue m)
-> m [ResolverValue m] -> m (ResolverValue m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [ResolverValue m] -> ResolverValue m
forall (m :: * -> *). [ResolverValue m] -> ResolverValue m
mkList (m [ResolverValue m] -> m (ResolverValue m))
-> ([a] -> m [ResolverValue m]) -> [a] -> m (ResolverValue m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m (ResolverValue m)) -> [a] -> m [ResolverValue m]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> m (ResolverValue m)
encodeValue
instance EncodeWrapper NonEmpty where
encodeWrapper :: (a -> m (ResolverValue m)) -> NonEmpty a -> m (ResolverValue m)
encodeWrapper = (NonEmpty a -> [a])
-> (a -> m (ResolverValue m)) -> NonEmpty a -> m (ResolverValue m)
forall (f :: * -> *) (m :: * -> *) a b.
(EncodeWrapper f, Monad m) =>
(a -> f b)
-> (b -> m (ResolverValue m)) -> a -> m (ResolverValue m)
withList NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
instance EncodeWrapper Seq where
encodeWrapper :: (a -> m (ResolverValue m)) -> Seq a -> m (ResolverValue m)
encodeWrapper = (Seq a -> [a])
-> (a -> m (ResolverValue m)) -> Seq a -> m (ResolverValue m)
forall (f :: * -> *) (m :: * -> *) a b.
(EncodeWrapper f, Monad m) =>
(a -> f b)
-> (b -> m (ResolverValue m)) -> a -> m (ResolverValue m)
withList Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
instance EncodeWrapper Vector where
encodeWrapper :: (a -> m (ResolverValue m)) -> Vector a -> m (ResolverValue m)
encodeWrapper = (Vector a -> [a])
-> (a -> m (ResolverValue m)) -> Vector a -> m (ResolverValue m)
forall (f :: * -> *) (m :: * -> *) a b.
(EncodeWrapper f, Monad m) =>
(a -> f b)
-> (b -> m (ResolverValue m)) -> a -> m (ResolverValue m)
withList Vector a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
instance EncodeWrapper Set where
encodeWrapper :: (a -> m (ResolverValue m)) -> Set a -> m (ResolverValue m)
encodeWrapper = (Set a -> [a])
-> (a -> m (ResolverValue m)) -> Set a -> m (ResolverValue m)
forall (f :: * -> *) (m :: * -> *) a b.
(EncodeWrapper f, Monad m) =>
(a -> f b)
-> (b -> m (ResolverValue m)) -> a -> m (ResolverValue m)
withList Set a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
instance EncodeWrapper SubscriptionField where
encodeWrapper :: (a -> m (ResolverValue m))
-> SubscriptionField a -> m (ResolverValue m)
encodeWrapper a -> m (ResolverValue m)
encode (SubscriptionField forall e (m :: * -> *) v.
(a ~ Resolver SUBSCRIPTION e m v) =>
Channel e
_ a
res) = a -> m (ResolverValue m)
encode a
res
type family DecodeWrapperConstraint (f :: Type -> Type) a :: Constraint where
DecodeWrapperConstraint Set a = (Ord a)
DecodeWrapperConstraint f a = ()
class DecodeWrapper (f :: Type -> Type) where
decodeWrapper ::
(Monad m, DecodeWrapperConstraint f a) =>
(ValidValue -> m a) ->
ValidValue ->
ExceptT GQLError m (f a)
instance DecodeWrapper Maybe where
decodeWrapper :: (ValidValue -> m a) -> ValidValue -> ExceptT GQLError m (Maybe a)
decodeWrapper ValidValue -> m a
_ ValidValue
Null = Maybe a -> ExceptT GQLError m (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
decodeWrapper ValidValue -> m a
decode ValidValue
x = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a)
-> ExceptT GQLError m a -> ExceptT GQLError m (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a -> ExceptT GQLError m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ValidValue -> m a
decode ValidValue
x)
instance DecodeWrapper [] where
decodeWrapper :: (ValidValue -> m a) -> ValidValue -> ExceptT GQLError m [a]
decodeWrapper ValidValue -> m a
decode (List [ValidValue]
li) = m [a] -> ExceptT GQLError m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> ExceptT GQLError m [a])
-> m [a] -> ExceptT GQLError m [a]
forall a b. (a -> b) -> a -> b
$ (ValidValue -> m a) -> [ValidValue] -> m [a]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ValidValue -> m a
decode [ValidValue]
li
decodeWrapper ValidValue -> m a
_ ValidValue
isType = m (Either GQLError [a]) -> ExceptT GQLError m [a]
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either GQLError [a]) -> ExceptT GQLError m [a])
-> m (Either GQLError [a]) -> ExceptT GQLError m [a]
forall a b. (a -> b) -> a -> b
$ Either GQLError [a] -> m (Either GQLError [a])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either GQLError [a] -> m (Either GQLError [a]))
-> Either GQLError [a] -> m (Either GQLError [a])
forall a b. (a -> b) -> a -> b
$ GQLError -> Either GQLError [a]
forall a b. a -> Either a b
Left (GQLError -> ValidValue -> GQLError
forall (s :: Stage). GQLError -> Value s -> GQLError
typeMismatch GQLError
"List" ValidValue
isType)
instance DecodeWrapper NonEmpty where
decodeWrapper :: (ValidValue -> m a)
-> ValidValue -> ExceptT GQLError m (NonEmpty a)
decodeWrapper = ([a] -> Either GQLError (NonEmpty a))
-> (ValidValue -> m a)
-> ValidValue
-> ExceptT GQLError m (NonEmpty a)
forall (m :: * -> *) a (rList :: * -> *).
Monad m =>
([a] -> Either GQLError (rList a))
-> (ValidValue -> m a)
-> ValidValue
-> ExceptT GQLError m (rList a)
withRefinedList (Either GQLError (NonEmpty a)
-> (NonEmpty a -> Either GQLError (NonEmpty a))
-> Maybe (NonEmpty a)
-> Either GQLError (NonEmpty a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (GQLError -> Either GQLError (NonEmpty a)
forall a b. a -> Either a b
Left GQLError
"Expected a NonEmpty list") NonEmpty a -> Either GQLError (NonEmpty a)
forall a b. b -> Either a b
Right (Maybe (NonEmpty a) -> Either GQLError (NonEmpty a))
-> ([a] -> Maybe (NonEmpty a))
-> [a]
-> Either GQLError (NonEmpty a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Maybe (NonEmpty a)
forall a. [a] -> Maybe (NonEmpty a)
NonEmpty.nonEmpty)
instance DecodeWrapper Seq where
decodeWrapper :: (ValidValue -> m a) -> ValidValue -> ExceptT GQLError m (Seq a)
decodeWrapper ValidValue -> m a
decode = ([a] -> Seq a)
-> ExceptT GQLError m [a] -> ExceptT GQLError m (Seq a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> Seq a
forall a. [a] -> Seq a
Seq.fromList (ExceptT GQLError m [a] -> ExceptT GQLError m (Seq a))
-> (ValidValue -> ExceptT GQLError m [a])
-> ValidValue
-> ExceptT GQLError m (Seq a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ValidValue -> m a) -> ValidValue -> ExceptT GQLError m [a]
forall (f :: * -> *) (m :: * -> *) a.
(DecodeWrapper f, Monad m, DecodeWrapperConstraint f a) =>
(ValidValue -> m a) -> ValidValue -> ExceptT GQLError m (f a)
decodeWrapper ValidValue -> m a
decode
instance DecodeWrapper Vector where
decodeWrapper :: (ValidValue -> m a) -> ValidValue -> ExceptT GQLError m (Vector a)
decodeWrapper ValidValue -> m a
decode = ([a] -> Vector a)
-> ExceptT GQLError m [a] -> ExceptT GQLError m (Vector a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> Vector a
forall a. [a] -> Vector a
Vector.fromList (ExceptT GQLError m [a] -> ExceptT GQLError m (Vector a))
-> (ValidValue -> ExceptT GQLError m [a])
-> ValidValue
-> ExceptT GQLError m (Vector a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ValidValue -> m a) -> ValidValue -> ExceptT GQLError m [a]
forall (f :: * -> *) (m :: * -> *) a.
(DecodeWrapper f, Monad m, DecodeWrapperConstraint f a) =>
(ValidValue -> m a) -> ValidValue -> ExceptT GQLError m (f a)
decodeWrapper ValidValue -> m a
decode
instance DecodeWrapper Set where
decodeWrapper :: (ValidValue -> m a) -> ValidValue -> ExceptT GQLError m (Set a)
decodeWrapper ValidValue -> m a
decode ValidValue
value = do
[a]
listVal <- (ValidValue -> m a) -> ValidValue -> ExceptT GQLError m [a]
forall (f :: * -> *) (m :: * -> *) a.
(DecodeWrapper f, Monad m, DecodeWrapperConstraint f a) =>
(ValidValue -> m a) -> ValidValue -> ExceptT GQLError m (f a)
decodeWrapper ValidValue -> m a
decode ValidValue
value
Set a -> [a] -> ExceptT GQLError m (Set a)
forall (l :: * -> *) (m :: * -> *) a b.
(Foldable l, Monad m) =>
Set a -> l b -> ExceptT GQLError m (Set a)
haveSameSize ([a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList [a]
listVal) [a]
listVal
haveSameSize ::
( Foldable l,
Monad m
) =>
Set a ->
l b ->
ExceptT GQLError m (Set a)
haveSameSize :: Set a -> l b -> ExceptT GQLError m (Set a)
haveSameSize Set a
setVal l b
listVal
| Set a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Set a
setVal Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== l b -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length l b
listVal = Set a -> ExceptT GQLError m (Set a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Set a
setVal
| Bool
otherwise = m (Either GQLError (Set a)) -> ExceptT GQLError m (Set a)
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either GQLError (Set a)) -> ExceptT GQLError m (Set a))
-> m (Either GQLError (Set a)) -> ExceptT GQLError m (Set a)
forall a b. (a -> b) -> a -> b
$ Either GQLError (Set a) -> m (Either GQLError (Set a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either GQLError (Set a) -> m (Either GQLError (Set a)))
-> Either GQLError (Set a) -> m (Either GQLError (Set a))
forall a b. (a -> b) -> a -> b
$ GQLError -> Either GQLError (Set a)
forall a b. a -> Either a b
Left (String -> GQLError
forall a. IsString a => String -> a
fromString (String
"Expected a List without duplicates, found " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall b a. (Show a, IsString b) => a -> b
show (l b -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length l b
listVal Int -> Int -> Int
forall a. Num a => a -> a -> a
- Set a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Set a
setVal) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" duplicates"))
withRefinedList ::
Monad m =>
([a] -> Either GQLError (rList a)) ->
(ValidValue -> m a) ->
ValidValue ->
ExceptT GQLError m (rList a)
withRefinedList :: ([a] -> Either GQLError (rList a))
-> (ValidValue -> m a)
-> ValidValue
-> ExceptT GQLError m (rList a)
withRefinedList [a] -> Either GQLError (rList a)
refiner ValidValue -> m a
decode (List [ValidValue]
li) = do
[a]
listRes <- m [a] -> ExceptT GQLError m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((ValidValue -> m a) -> [ValidValue] -> m [a]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ValidValue -> m a
decode [ValidValue]
li)
case [a] -> Either GQLError (rList a)
refiner [a]
listRes of
Left GQLError
err -> m (Either GQLError (rList a)) -> ExceptT GQLError m (rList a)
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either GQLError (rList a)) -> ExceptT GQLError m (rList a))
-> m (Either GQLError (rList a)) -> ExceptT GQLError m (rList a)
forall a b. (a -> b) -> a -> b
$ Either GQLError (rList a) -> m (Either GQLError (rList a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either GQLError (rList a) -> m (Either GQLError (rList a)))
-> Either GQLError (rList a) -> m (Either GQLError (rList a))
forall a b. (a -> b) -> a -> b
$ GQLError -> Either GQLError (rList a)
forall a b. a -> Either a b
Left (GQLError -> ValidValue -> GQLError
forall (s :: Stage). GQLError -> Value s -> GQLError
typeMismatch GQLError
err ([ValidValue] -> ValidValue
forall (stage :: Stage). [Value stage] -> Value stage
List [ValidValue]
li))
Right rList a
value -> rList a -> ExceptT GQLError m (rList a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure rList a
value
withRefinedList [a] -> Either GQLError (rList a)
_ ValidValue -> m a
_ ValidValue
isType = m (Either GQLError (rList a)) -> ExceptT GQLError m (rList a)
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either GQLError (rList a)) -> ExceptT GQLError m (rList a))
-> m (Either GQLError (rList a)) -> ExceptT GQLError m (rList a)
forall a b. (a -> b) -> a -> b
$ Either GQLError (rList a) -> m (Either GQLError (rList a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either GQLError (rList a) -> m (Either GQLError (rList a)))
-> Either GQLError (rList a) -> m (Either GQLError (rList a))
forall a b. (a -> b) -> a -> b
$ GQLError -> Either GQLError (rList a)
forall a b. a -> Either a b
Left (GQLError -> ValidValue -> GQLError
forall (s :: Stage). GQLError -> Value s -> GQLError
typeMismatch GQLError
"List" ValidValue
isType)
typeMismatch :: GQLError -> Value s -> GQLError
typeMismatch :: GQLError -> Value s -> GQLError
typeMismatch GQLError
text Value s
jsType =
GQLError
"Type mismatch! expected:" GQLError -> GQLError -> GQLError
forall a. Semigroup a => a -> a -> a
<> GQLError -> GQLError
forall a. Msg a => a -> GQLError
msg GQLError
text GQLError -> GQLError -> GQLError
forall a. Semigroup a => a -> a -> a
<> GQLError
", got: "
GQLError -> GQLError -> GQLError
forall a. Semigroup a => a -> a -> a
<> Value s -> GQLError
forall a. Msg a => a -> GQLError
msg Value s
jsType