haskell-servant / haskell-servant/servant
How to define a type for polymorphic endpoint?
- Dominant language
- Haskell
- Stars
- 2k
- Forks
- 427
- Avg merge
- 2d 23h
- Merged PRs (30d)
- 5
Description
Hi,
I suspect the following usecase is not supported by servant well.
I define Servant API type to talk to Gitlab API. E.g. List merge request endpoint returns MRs in different states so there are different fields. I would like to define independent Haskell datas.
MRs can be filtered by state through QueryParam.
- I cannot set value for QueryParam in types.
- I cannot append unescaped string ("?state=merged") to generated URL
- I cannot define endpoint returning values of different types depending on QueryParam value
Workaround is to define 2 Types for the same endpoint and take care of proper values for "synthetic" argument to make its value consistent with result type.
```
data MrState = MrStateMerged | MrStateClosed deriving (Show, Read, Eq, Ord)
type PidCap = Capture "pid" GitLabProjectId
type MrStateParam = QueryParam "state" MrState
type ListMergedMrs = "projects" :> PidCap :> "merge_requests" :> MrStateParam :> Get '[JSON] [GlMergedMr]
type ListClosedMrs = "projects" :> PidCap :> "merge_requests" :> MrStateParam :> Get '[JSON] [GlClosedMr]
api :: Proxy GitLabApiV4
api = Proxy
-- generated API is unsafe
listMergedMrs' :: GitLabProjectId -> Maybe MrState -> ClientM [GlMergedMr]
listClosedMrs' :: GitLabProjectId -> Maybe MrState -> ClientM [GlClosedMr]
listMergedMrs' :<|> listClosedMrs' = client api
-- safe wrappers
listMergedMrs pid = listMergedMrs' pid (Just MrStateMerged)
listClosedMrs pid = listClosedMrs' pid (Just MrStateClosed)
```
Contributor guide
Research direction
Start by examining the Servant API type shown in the issue and how client api generates listMergedMrs' and listClosedMrs'. Clarify whether the desired result is typed QueryParam values, URL construction, or return types selected by the parameter. Done should provide a decided, type-safe approach for polymorphic endpoints or document why the existing workaround is required.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- haskell
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100