Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support monad transformer stacks #7

Open
eviefp opened this issue Aug 25, 2020 · 2 comments
Open

Support monad transformer stacks #7

eviefp opened this issue Aug 25, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@eviefp
Copy link

eviefp commented Aug 25, 2020

While looking into payload, I was trying to write some simple example of an API and rather than use a real DB, I figured I'd use a StateT transformer to keep some state across API calls, but I noticed the exposed functions force handlers to run in Aff.

I assume it should be possible to relax the requirement to MonadAff?

@hoodunit
Copy link
Owner

I've been vaguely thinking about introducing something like Servant's natural transformation API to allow use cases like this. This should be possible.

@hoodunit hoodunit changed the title Monad Transformer Stack Support monad transformer stacks Oct 4, 2020
@hoodunit hoodunit added the enhancement New feature or request label Oct 4, 2020
@roryc89
Copy link

roryc89 commented Apr 13, 2021

In our project, we handle this outside of the payload library, by mapping over the handlers record and running our own monad.

  handlers env = 
    runHandlers env 
      { avatarDelete: avatarDelete :: _ -> ServerM _
      , avatarSignedUrl: avatarSignedUrl :: _ -> ServerM _
      , changeEmail: changeEmail :: _ -> ServerM _
      , hasuraAuthenticate: hasuraAuthenticate :: _ -> ServerM _
      , hasuraAuthenticateAdmin: hasuraAuthenticateAdmin :: _ -> ServerM _
      , hasuraAuthenticateNotVerified: hasuraAuthenticateNotVerified :: _ -> ServerM _
      , signIn: signIn :: _ -> ServerM _
      , signInGoogle: signInGoogle :: _ -> ServerM _
      , signInLinkedin: signInLinkedin :: _ -> ServerM _
      , register: register :: _ -> ServerM _
      , checkUser: checkUser :: _ -> ServerM _
      , renderPdf: renderPdf :: _ -> ServerM _
      , resetPassword: resetPassword :: _ -> ServerM _
      , sendPasswordResetEmail: sendPasswordResetEmail :: _ -> ServerM _
      , sendVerificationEmail: sendVerificationEmail :: _ -> ServerM _
      , verifyEmail: verifyEmail :: _ -> ServerM _
      , hasuraPublic: hasuraPublic :: _ -> ServerM _
      }
...
import Prelude

import Data.Maybe (Maybe(..))
import Data.Symbol (class IsSymbol, SProxy, reflectSymbol)
import Effect.Aff (Aff)
import Env (Env(..))
import Heterogeneous.Mapping (class HMapWithIndex, class MappingWithIndex, hmapWithIndex)
import ServerM (ServerM, runServerM)

data RunHandler
  = RunHandler Env

instance runHandler ::
  ( IsSymbol sym
    ) =>
  MappingWithIndex RunHandler (SProxy sym) (r -> ServerM res) (r -> Aff res) where
  mappingWithIndex (RunHandler (Env env)) sym h req =
    runServerM
      ( Env
          env
            { path = Just $ reflectSymbol sym }
      )
      (h req)

runHandlers ::
  forall serverMs affs.
  HMapWithIndex RunHandler serverMs affs => Env -> serverMs -> affs
runHandlers = hmapWithIndex <<< RunHandler

nsaunders added a commit to nsaunders/purescript-payload that referenced this issue May 30, 2021
nsaunders added a commit to nsaunders/purescript-payload that referenced this issue May 30, 2021
nsaunders added a commit to nsaunders/purescript-payload that referenced this issue Jun 2, 2021
nsaunders added a commit to nsaunders/purescript-payload that referenced this issue Jun 3, 2021
nsaunders added a commit to nsaunders/purescript-payload that referenced this issue Jan 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants