Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doubt about the main example in the tutorial #1799

Open
amkhlv opened this issue Dec 22, 2024 · 1 comment
Open

doubt about the main example in the tutorial #1799

amkhlv opened this issue Dec 22, 2024 · 1 comment

Comments

@amkhlv
Copy link

amkhlv commented Dec 22, 2024

In the More endpoints section of the chapter "Serving an API", we define a type:

type UserAPI2 = "users" :> Get '[JSON] [User]
           :<|> "albert" :> Get '[JSON] User
           :<|> "isaac" :> Get '[JSON] User

and then serve the data:

server2 :: Server UserAPI2
server2 = return users2
     :<|> return albert
     :<|> return isaac

This seems to be error-prone, as I could easily flip, by accident, the two lines :<|> return albert and :<|> return isaac . This would compile fine, leading to great awkwardness at runtime.

Is there a better way of doing this, especially when there are many endpoints?

@amkhlv
Copy link
Author

amkhlv commented Dec 24, 2024

I guess I should use

newtype NewtonType = NewtonType { unNewton : User }
newtype EinsteinType = EinsteinType { unEinstein : User }

and then:

type UserAPI2 = "users" :> Get '[JSON] [User]
           :<|> "albert" :> Get '[JSON] EinsteinType
           :<|> "isaac" :> Get '[JSON] NewtonType

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant