haskell-servant / servant

Main repository for the servant libraries — DSL for describing, serving, querying, mocking, documenting web applications and more!
https://docs.servant.dev/
1.8k stars 407 forks source link

Making query parameters optionals in allLinks/safeLink with `NamedRoute`s #1674

Closed maxime-didier closed 11 months ago

maxime-didier commented 1 year ago

There currently does not seem to be a clean way to generate links without query parameters. I tried using safeLink, but there's a missing type family instance of IsElem for NamedRoutes.

There's a partial workaround of using ToServantApi to remove the NamedRoute, but it won't work for nested NamedRoutes. Ie, like this:

{-# LANGUAGE AllowAmbiguousTypes #-}
myApiSafeLink :: forall endpoint. (IsElem endpoint (ToServantApi MyAPI), HasLink endpoint) => MkLink endpoint a
myApiSafeLink = safeLink' someFunction (Proxy @(ToServantApi MyAPI)) (Proxy @endpoint)