After I watched good talk of George Wilson (Next Level MTL https://github.com/gwils/next-level-mtl-with-classy-optics/blob/master/Slides.pdf) I try to create application which uses MTL style design and decided to use servant, it looks like this library doesn't fit well to such design.
Below code doesn't compile because I am not able to transform m into Handler.
getItems :: (MonadIO m, MonadReader r m, HasNetworkConfig r) => m [Item]
getItems =
return [Item "foo" "bar"]
mkApp :: Application
mkApp = serve itemApi getItems
You can find complete example here: https://github.com/paweln1986/ServantMTLStackOverflowExample
Is it possible to use any monad with servant? How to achieve this? I tried to use hoistServer without success. Do you have any idea what I miss here?
Compilation error:
• No instance for (MonadReader r0 Handler)
arising from a use of ‘getItems’
• In the second argument of ‘serve’, namely ‘getItems’
In the expression: serve itemApi getItems
In an equation for ‘mkApp’: mkApp = serve itemApi getItems
|
40 | mkApp = serve itemApi getItems
| ^^^^^^^^
Shorter example:
type ReaderAPI = "ep1" :> Get '[JSON] Int :<|> "ep2" :> Get '[JSON] String :<|> Raw :<|> EmptyAPI
readerApi = Proxy :: Proxy ReaderAPI
readerServer :: (MonadIO m, HasNetworkConfig r, MonadReader r m) => ServerT ReaderAPI (AppT m)
readerServer = return 1797 :<|> view (networkConfig . host) :<|> Tagged (error "raw server") :<|> emptyServer
nt x = return undefined
mainServer = hoistServer readerApi nt readerServer :: Server ReaderAPI
This gives me below compilation error
• Ambiguous type variable ‘m0’ arising from a use of ‘readerServer’
prevents the constraint ‘(MonadIO m0)’ from being solved.
Probable fix: use a type annotation to specify what ‘m0’ should be.
These potential instances exist:
instance [safe] MonadIO IO -- Defined in ‘Control.Monad.IO.Class’
instance MonadIO m => MonadIO (AppT m) -- Defined in ‘Types’
instance MonadIO Handler
-- Defined in ‘Servant.Server.Internal.Handler’
...plus 18 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the third argument of ‘hoistServer’, namely ‘readerServer’
In the expression:
hoistServer readerApi nt readerServer :: Server ReaderAPI
In an equation for ‘mainServer’:
mainServer
= hoistServer readerApi nt readerServer :: Server ReaderAPI
|
64 | mainServer = hoistServer readerApi nt readerServer :: Server ReaderAPI
| ^^^^^^^^^^^^
m
inmainServer
's definition; for example, toReaderT R IO
(with some suitableR
)? – Cactus