2

我正在尝试在 Servant 中编写 API,并且正在编写许多如下所示的处理程序:

getTableA :: Handler [ARecord]
getTableA = do
    xs <- runDB (selectList [] [])
    let records = map (\(Entity _ v) -> v) xs
    return records

getTableB :: Handler [BRecord]
getTableB = do
    xs <- runDB (selectList [] [])
    let records = map (\(Entity _ v) -> v) xs
    return records

getTableC :: Handler [CRecord]
getTableC = do
    xs <- runDB (selectList [] [])
    let records = map (\(Entity _ v) -> v) xs
    return records

等等。

我希望能够拥有更多类似的东西

getTable :: Handler [a]
getTable = do
    xs <- runDB (selectList [] [])
    let records = map (\(Entity _ v) -> v) xs
    return records

但是我得到了错误

Couldn't match expected type ‘SqlBackend’
            with actual type ‘PersistEntityBackend val’
Relevant bindings include
  getAll :: Handler [val] (bound at src/Novation/Handler.hs:101:1)
In the first argument of ‘runDB’, namely ‘(selectList [] [])’
In a stmt of a 'do' block: xs <- runDB (selectList [] [])
In the expression:
  do { xs <- runDB (selectList [] []);
       let records = map (\ (Entity _ v) -> ...) xs;
       return records }

我希望最终我能够拥有这样的东西

server :: Server MyAPI
server = getTable :<|> getTable :<|> getTable

type MyAPI = 
         "tableA" :> Get '[JSON] [ARecord]
    :<|> "tableB" :> Get '[JSON] [BRecord]
    :<|> "tableC" :> Get '[JSON] [CRecord]

代替

server :: Server MyAPI
server = getTableA :<|> getTableB :<|> getTableC
4

0 回答 0