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 for named routes in servant #71

Closed
guaraqe opened this issue Apr 7, 2023 · 8 comments
Closed

Support for named routes in servant #71

guaraqe opened this issue Apr 7, 2023 · 8 comments

Comments

@guaraqe
Copy link

guaraqe commented Apr 7, 2023

Are there plans to add support for named routes in servant?

It would be really nice, since the name of the generated functions could be defined by the developers, and functions corresponding to the same route would have the same name in both Haskell and Elm.

@ilyakooo0
Copy link
Collaborator

An obvious issue is that records fields can have duplicate names accross different modules. This would mean that we would still need to speciy the path (possibly of other names) in the names of the generated functions.

@ilyakooo0
Copy link
Collaborator

This is also blocked by servant-foreign not supporting this, so you should probably create an issue there.

@guaraqe
Copy link
Author

guaraqe commented Apr 10, 2023

I see, thanks, I created an issue there: haskell-servant/servant#1673

@TheOddler
Copy link

Looks like in the latest version of servant.foreign (0.16 as of writing) there is now support for this, though it still just uses the generated names (which tbh I'm fine with).

Version 0.16 isn't part of any stack lts yet though, so what I did was copy this into my code:

-- Taken from https://hackage.haskell.org/package/servant-foreign-0.16/docs/src/Servant.Foreign.Internal.html#line-518
-- But that is only available on servant-foreign 0.16, which is not in any stackage lts yet
instance HasForeign lang ftype (ToServantApi r) => HasForeign lang ftype (NamedRoutes r) where
  type Foreign ftype (NamedRoutes r) = Foreign ftype (ToServantApi r)

  foreignFor lang ftype Proxy req =
    foreignFor lang ftype (Proxy :: Proxy (ToServantApi r)) req

And then updated my generateElmModuleWith ... to:

  generateElmModuleWith
    ...
    (Proxy :: Proxy (ToServantApi Web.ApiRoutes))

(Added the ToServantApi there too essentially).

@blackheaven
Copy link

I'm not sure I get it, I have this error:

    • Expected kind ‘Type -> Type’, but ‘API’ has kind ‘Type’
    • In the first argument of ‘ToServantApi’, namely ‘API’
      In the first argument of ‘Proxy’, namely ‘(ToServantApi API)’
      In an expression type signature: Proxy (ToServantApi API)
   |
42 |     (Proxy :: Proxy (ToServantApi API))
   |                                   ^^^

Any idea what's going on?

@TheOddler
Copy link

My guess is that you're missing the mode parameter on you API? My ApiRoutes looks like this:

data ApiRoutes mode = ApiRoutes
  { ping :: mode :- "ping" :> Get '[JSON] String,
    ...
  }
  deriving (Generic)

I also have type Api = NamedRoutes ApiRoutes but that's not the one I used ToServantApi on.

@blackheaven
Copy link

Sorry for that, thanks a lot!

@domenkozar
Copy link
Collaborator

This is fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants