This project aims at building a web app and API using Haskell. The project is built using cabal and Spock, so make sure you have cabal installed before using this (Spock will be installed during the process). Lets dive into the installation process (I am assumiung you have already installed cabal)
The process is pretty simple, you need to follow the following steps to get started:
- Clone the repo using the following command:
git clone [email protected]:paritosh-08/personkeeper.git
- Install using the following command:
cd personkeeper cabal update cabal new-install
Run the following command to run the program:
cabal new-run :personkeeper
Now open your browser and go to http://localhost:3000/ for the webapp, here you can view the people in the database and can add new person into the database using the form.
For API, go to http://localhost:3000/api, here you will get a json output of all the people in the database, you can get specific people on the endpoint: http://localhost:3000/api/[ID]. Also you can add new people by a post request on the API url:
curl -H "Content-Type: application/json" -d '{ "input": {"name": "New Person", "age": 31} }' localhost:3000/api
- https://www.spock.li/tutorials/rest-api
- https://haskell-at-work.com/episodes/2018-04-09-your-first-web-application-with-spock.html
Now we can use the API endpoint with Hasura Actions to expose a GraphQL endpoint where we can use the Spock API.
First we need to start Hasura in Docker (as we have the API exposed in localhost). Go to https://hasura.io/docs/latest/graphql/core/getting-started/docker-simple.html for a simple guide to run Hasura in Docker, or you can just follow along if you have already setup everything.
After setting up Hasura on docker, we need to add the API in Hasura Actions, follow along to add the API in Hasura Actions.
-
Go to Actions tab on the top, you will get something like the following:
-
Now click on
Create
button, you will get the following page, where we will define our Action. -
Now fill the following in the fields:
- Action definition:
type Mutation { api ( name: String! age: Int! ): SampleOutput }
- New types definition:
type SampleOutput { result : String! id : String! }
- Handler:
- If you are on Linux (Ubuntu)
http://172.17.0.1:3000/api
- If you are on Linux (Ubuntu)
Now hit save.
- Action definition:
Now we will run our Hasura Action using a GraphQL mutation. Follow along to do so:
-
First head out to the API tab on the top, you will get something like the following:
-
Now Paste the following GraphQL snippet in the GraphiQL box:
mutation MyMutation { api(age: 2, name: "Baby's Bro") { result } }
-
Hit the play buttion (near the GraphiQL title), you will get something like the following: