summaryrefslogtreecommitdiff
path: root/src/Language/GraphQL/Trans.hs
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2019-07-02 20:07:26 +0200
committerEugen Wissner <belka@caraus.de>2019-07-02 20:07:26 +0200
commit91679650b5fc387d59925f1c660af62ec3aa4b87 (patch)
tree8646d2c91c9fb25c79462c1f99c9fb8561417392 /src/Language/GraphQL/Trans.hs
parent1017b728d96b9349c50d83f10efbd8d48246beea (diff)
downloadgraphql-91679650b5fc387d59925f1c660af62ec3aa4b87.tar.gz
Introduce monad transformer for resolvers
Now the errors in the resolvers can be handled and 3 tests throwing errors pass now. Another test fail but it requires distinguisching nullable and non-nullable values.
Diffstat (limited to 'src/Language/GraphQL/Trans.hs')
-rw-r--r--src/Language/GraphQL/Trans.hs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/Language/GraphQL/Trans.hs b/src/Language/GraphQL/Trans.hs
new file mode 100644
index 0000000..f0fcefb
--- /dev/null
+++ b/src/Language/GraphQL/Trans.hs
@@ -0,0 +1,35 @@
+module Language.GraphQL.Trans where
+
+import Control.Applicative (Alternative(..))
+import Control.Monad (MonadPlus(..))
+import Control.Monad.IO.Class (MonadIO(..))
+import Control.Monad.Trans.Class (MonadTrans(..))
+import Control.Monad.Trans.Except (ExceptT)
+import Data.Text (Text)
+
+newtype ActionT m a = ActionT { runActionT :: ExceptT Text m a }
+
+instance Functor m => Functor (ActionT m) where
+ fmap f = ActionT . fmap f . runActionT
+
+instance Monad m => Applicative (ActionT m) where
+ pure = ActionT . pure
+ (ActionT f) <*> (ActionT x) = ActionT $ f <*> x
+
+instance Monad m => Monad (ActionT m) where
+ return = pure
+ (ActionT action) >>= f = ActionT $ action >>= runActionT . f
+
+instance MonadTrans ActionT where
+ lift = ActionT . lift
+
+instance MonadIO m => MonadIO (ActionT m) where
+ liftIO = lift . liftIO
+
+instance Monad m => Alternative (ActionT m) where
+ empty = ActionT empty
+ (ActionT x) <|> (ActionT y) = ActionT $ x <|> y
+
+instance Monad m => MonadPlus (ActionT m) where
+ mzero = empty
+ mplus = (<|>)