私は 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