{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
module Data.Morpheus.Execution.Server.Interpreter
( Interpreter(..)
)
where
import Data.Aeson ( encode )
import Data.ByteString ( ByteString )
import qualified Data.ByteString.Lazy.Char8 as LB
( ByteString
, fromStrict
, toStrict
)
import Data.Text ( Text )
import qualified Data.Text.Lazy as LT
( Text
, fromStrict
, toStrict
)
import Data.Text.Lazy.Encoding ( decodeUtf8
, encodeUtf8
)
import Control.Monad.IO.Class ( MonadIO() )
import Data.Morpheus.Execution.Server.Resolve
( RootResCon
, byteStringIO
, statefulResolver
, statelessResolver
, streamResolver
, coreResolver
)
import Data.Morpheus.Execution.Subscription.ClientRegister
( GQLState )
import Data.Morpheus.Types.Internal.Resolving
( GQLRootResolver(..)
, ResponseStream
)
import Data.Morpheus.Types.IO ( GQLRequest
, GQLResponse
)
class Interpreter k m e where
interpreter ::
Monad m
=> (RootResCon m e query mut sub) =>
GQLRootResolver m e query mut sub -> k
type StateLess m a = a -> m a
instance Interpreter (GQLRequest -> m GQLResponse) m e where
interpreter = statelessResolver
instance Interpreter (StateLess m LB.ByteString) m e where
interpreter root = byteStringIO (statelessResolver root)
instance Interpreter (StateLess m LT.Text) m e where
interpreter root request =
decodeUtf8 <$> interpreter root (encodeUtf8 request)
instance Interpreter (StateLess m ByteString) m e where
interpreter root request =
LB.toStrict <$> interpreter root (LB.fromStrict request)
instance Interpreter (StateLess m Text) m e where
interpreter root request =
LT.toStrict <$> interpreter root (LT.fromStrict request)
type WSPub m e a = GQLState m e -> a -> m a
instance MonadIO m => Interpreter (WSPub m e LB.ByteString) m e where
interpreter root state = statefulResolver state (coreResolver root)
instance MonadIO m => Interpreter (WSPub m e LT.Text) m e where
interpreter root state request =
decodeUtf8 <$> interpreter root state (encodeUtf8 request)
instance MonadIO m => Interpreter (WSPub m e ByteString) m e where
interpreter root state request =
LB.toStrict <$> interpreter root state (LB.fromStrict request)
instance MonadIO m => Interpreter (WSPub m e Text) m e where
interpreter root state request =
LT.toStrict <$> interpreter root state (LT.fromStrict request)
type WSSub m e a = a -> ResponseStream e m a
instance Interpreter (GQLRequest -> ResponseStream e m LB.ByteString) m e where
interpreter root request = encode <$> streamResolver root request
instance Interpreter (WSSub m e LB.ByteString) m e where
interpreter root = byteStringIO (streamResolver root)
instance Interpreter (WSSub m e LT.Text) m e where
interpreter root request =
decodeUtf8 <$> interpreter root (encodeUtf8 request)
instance Interpreter (WSSub m e ByteString) m e where
interpreter root request =
LB.toStrict <$> interpreter root (LB.fromStrict request)
instance Interpreter (WSSub m e Text) m e where
interpreter root request =
LT.toStrict <$> interpreter root (LT.fromStrict request)