deemp / servant-queryparam

Use records to specify query parameters in servant APIs
BSD 3-Clause "New" or "Revised" License
3 stars 1 forks source link

Generating links for RecordParam apis #1

Open mngrm3a opened 2 months ago

mngrm3a commented 2 months ago

Could you provide an example how to generate links for RecordParam apis or help to fix the test code below? I kind of understand the error message but I have no idea how to fix it.

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

module TestApi where

import Data.Aeson (FromJSON, ToJSON)
import GHC.Generics (Generic)
import GHC.TypeLits (Symbol)
import Servant
  ( AsLink,
    GenericMode (type (:-)),
    Get,
    JSON,
    Link,
    allFieldLinks,
    type (:>),
  )
import Servant.Client (ClientM)
import Servant.Client.Generic (AsClientT, genericClient)
import Servant.QueryParam.Client.Record ()
import Servant.QueryParam.Record (RecordParam)
import Servant.QueryParam.Server.Record ()
import Servant.QueryParam.TypeLevel (Eval, Exp)
import Servant.Server.Generic (AsServer)

data TestParams = MkTestParams
  { a :: !(Maybe Int),
    b :: !(Maybe Int)
  }
  deriving (Eq, Show, Generic)

data TestResponse = MkTestResponse
  deriving (Generic)

instance FromJSON TestResponse

instance ToJSON TestResponse

data KeepName :: Symbol -> Exp Symbol

type instance Eval (KeepName sym) = sym

newtype TestApi mode = MkTestApi
  { get :: mode :- RecordParam KeepName TestParams :> Get '[JSON] TestResponse
  }
  deriving (Generic)

server :: TestApi AsServer
server = MkTestApi {get = const $ pure MkTestResponse}

client :: TestApi (AsClientT ClientM)
client = genericClient

link :: TestApi (AsLink Link)
link = allFieldLinks
src/TestApi.hs:57:8: error: [GHC-64725]
    • There is no instance for Servant.Links.HasLink (Int :> ...)
    • In the expression: allFieldLinks
      In an equation for ‘link’: link = allFieldLinks
   |
57 | link = allFieldLinks
   |       
deemp commented 2 months ago

@mngrm3a which GHC version do you use?

mngrm3a commented 2 months ago

9.6.5