{-# 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 (..),
)
import Data.Morpheus.Types.Internal.AST
( Message,
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 :: * -> *) 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
ResNull)
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
ResList (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 :: * -> *) a :: Constraint where
DecodeWrapperConstraint Set a = (Ord a)
DecodeWrapperConstraint f a = ()
class DecodeWrapper (f :: * -> *) where
decodeWrapper ::
(Monad m, DecodeWrapperConstraint f a) =>
(ValidValue -> m a) ->
ValidValue ->
ExceptT Message m (f a)
instance DecodeWrapper Maybe where
decodeWrapper :: (ValidValue -> m a) -> ValidValue -> ExceptT Message m (Maybe a)
decodeWrapper ValidValue -> m a
_ ValidValue
Null = Maybe a -> ExceptT Message 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 Message m a -> ExceptT Message m (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a -> ExceptT Message 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 Message m [a]
decodeWrapper ValidValue -> m a
decode (List [ValidValue]
li) = m [a] -> ExceptT Message m [a]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m [a] -> ExceptT Message m [a]) -> m [a] -> ExceptT Message 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 Message [a]) -> ExceptT Message m [a]
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either Message [a]) -> ExceptT Message m [a])
-> m (Either Message [a]) -> ExceptT Message m [a]
forall a b. (a -> b) -> a -> b
$ Either Message [a] -> m (Either Message [a])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Message [a] -> m (Either Message [a]))
-> Either Message [a] -> m (Either Message [a])
forall a b. (a -> b) -> a -> b
$ Message -> Either Message [a]
forall a b. a -> Either a b
Left (Message -> ValidValue -> Message
forall (s :: Stage). Message -> Value s -> Message
typeMismatch Message
"List" ValidValue
isType)
instance DecodeWrapper NonEmpty where
decodeWrapper :: (ValidValue -> m a) -> ValidValue -> ExceptT Message m (NonEmpty a)
decodeWrapper = ([a] -> Either Message (NonEmpty a))
-> (ValidValue -> m a)
-> ValidValue
-> ExceptT Message m (NonEmpty a)
forall (m :: * -> *) a (rList :: * -> *).
Monad m =>
([a] -> Either Message (rList a))
-> (ValidValue -> m a) -> ValidValue -> ExceptT Message m (rList a)
withRefinedList (Either Message (NonEmpty a)
-> (NonEmpty a -> Either Message (NonEmpty a))
-> Maybe (NonEmpty a)
-> Either Message (NonEmpty a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Message -> Either Message (NonEmpty a)
forall a b. a -> Either a b
Left Message
"Expected a NonEmpty list") NonEmpty a -> Either Message (NonEmpty a)
forall a b. b -> Either a b
Right (Maybe (NonEmpty a) -> Either Message (NonEmpty a))
-> ([a] -> Maybe (NonEmpty a))
-> [a]
-> Either Message (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 Message m (Seq a)
decodeWrapper ValidValue -> m a
decode = ([a] -> Seq a)
-> ExceptT Message m [a] -> ExceptT Message 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 Message m [a] -> ExceptT Message m (Seq a))
-> (ValidValue -> ExceptT Message m [a])
-> ValidValue
-> ExceptT Message m (Seq a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ValidValue -> m a) -> ValidValue -> ExceptT Message m [a]
forall (f :: * -> *) (m :: * -> *) a.
(DecodeWrapper f, Monad m, DecodeWrapperConstraint f a) =>
(ValidValue -> m a) -> ValidValue -> ExceptT Message m (f a)
decodeWrapper ValidValue -> m a
decode
instance DecodeWrapper Vector where
decodeWrapper :: (ValidValue -> m a) -> ValidValue -> ExceptT Message m (Vector a)
decodeWrapper ValidValue -> m a
decode = ([a] -> Vector a)
-> ExceptT Message m [a] -> ExceptT Message 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 Message m [a] -> ExceptT Message m (Vector a))
-> (ValidValue -> ExceptT Message m [a])
-> ValidValue
-> ExceptT Message m (Vector a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ValidValue -> m a) -> ValidValue -> ExceptT Message m [a]
forall (f :: * -> *) (m :: * -> *) a.
(DecodeWrapper f, Monad m, DecodeWrapperConstraint f a) =>
(ValidValue -> m a) -> ValidValue -> ExceptT Message m (f a)
decodeWrapper ValidValue -> m a
decode
instance DecodeWrapper Set where
decodeWrapper :: (ValidValue -> m a) -> ValidValue -> ExceptT Message m (Set a)
decodeWrapper ValidValue -> m a
decode ValidValue
value = do
[a]
listVal <- (ValidValue -> m a) -> ValidValue -> ExceptT Message m [a]
forall (f :: * -> *) (m :: * -> *) a.
(DecodeWrapper f, Monad m, DecodeWrapperConstraint f a) =>
(ValidValue -> m a) -> ValidValue -> ExceptT Message m (f a)
decodeWrapper ValidValue -> m a
decode ValidValue
value
Set a -> [a] -> ExceptT Message m (Set a)
forall (l :: * -> *) (m :: * -> *) a b.
(Foldable l, Monad m) =>
Set a -> l b -> ExceptT Message 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 Message m (Set a)
haveSameSize :: Set a -> l b -> ExceptT Message 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 Message m (Set a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Set a
setVal
| Bool
otherwise = m (Either Message (Set a)) -> ExceptT Message m (Set a)
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either Message (Set a)) -> ExceptT Message m (Set a))
-> m (Either Message (Set a)) -> ExceptT Message m (Set a)
forall a b. (a -> b) -> a -> b
$ Either Message (Set a) -> m (Either Message (Set a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Message (Set a) -> m (Either Message (Set a)))
-> Either Message (Set a) -> m (Either Message (Set a))
forall a b. (a -> b) -> a -> b
$ Message -> Either Message (Set a)
forall a b. a -> Either a b
Left (String -> Message
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
- l b -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length l b
listVal) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" duplicates"))
withRefinedList ::
Monad m =>
([a] -> Either Message (rList a)) ->
(ValidValue -> m a) ->
ValidValue ->
ExceptT Message m (rList a)
withRefinedList :: ([a] -> Either Message (rList a))
-> (ValidValue -> m a) -> ValidValue -> ExceptT Message m (rList a)
withRefinedList [a] -> Either Message (rList a)
refiner ValidValue -> m a
decode (List [ValidValue]
li) = do
[a]
listRes <- m [a] -> ExceptT Message 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 Message (rList a)
refiner [a]
listRes of
Left Message
err -> m (Either Message (rList a)) -> ExceptT Message m (rList a)
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either Message (rList a)) -> ExceptT Message m (rList a))
-> m (Either Message (rList a)) -> ExceptT Message m (rList a)
forall a b. (a -> b) -> a -> b
$ Either Message (rList a) -> m (Either Message (rList a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Message (rList a) -> m (Either Message (rList a)))
-> Either Message (rList a) -> m (Either Message (rList a))
forall a b. (a -> b) -> a -> b
$ Message -> Either Message (rList a)
forall a b. a -> Either a b
Left (Message -> ValidValue -> Message
forall (s :: Stage). Message -> Value s -> Message
typeMismatch Message
err ([ValidValue] -> ValidValue
forall (stage :: Stage). [Value stage] -> Value stage
List [ValidValue]
li))
Right rList a
value -> rList a -> ExceptT Message m (rList a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure rList a
value
withRefinedList [a] -> Either Message (rList a)
_ ValidValue -> m a
_ ValidValue
isType = m (Either Message (rList a)) -> ExceptT Message m (rList a)
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either Message (rList a)) -> ExceptT Message m (rList a))
-> m (Either Message (rList a)) -> ExceptT Message m (rList a)
forall a b. (a -> b) -> a -> b
$ Either Message (rList a) -> m (Either Message (rList a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Message (rList a) -> m (Either Message (rList a)))
-> Either Message (rList a) -> m (Either Message (rList a))
forall a b. (a -> b) -> a -> b
$ Message -> Either Message (rList a)
forall a b. a -> Either a b
Left (Message -> ValidValue -> Message
forall (s :: Stage). Message -> Value s -> Message
typeMismatch Message
"List" ValidValue
isType)
typeMismatch :: Message -> Value s -> Message
typeMismatch :: Message -> Value s -> Message
typeMismatch Message
text Value s
jsType =
Message
"Type mismatch! expected:" Message -> Message -> Message
forall a. Semigroup a => a -> a -> a
<> Message -> Message
forall a. Msg a => a -> Message
msg Message
text Message -> Message -> Message
forall a. Semigroup a => a -> a -> a
<> Message
", got: "
Message -> Message -> Message
forall a. Semigroup a => a -> a -> a
<> Value s -> Message
forall a. Msg a => a -> Message
msg Value s
jsType