Skip to content

paritosh-08/personkeeper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Webapp and API using Haskell

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)

Installation

The process is pretty simple, you need to follow the following steps to get started:

Steps:

  1. Clone the repo using the following command:
    git clone [email protected]:paritosh-08/personkeeper.git
  2. Install using the following command:
    cd personkeeper
    cabal update
    cabal new-install

Run the program

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

References

  1. https://www.spock.li/tutorials/rest-api
  2. https://haskell-at-work.com/episodes/2018-04-09-your-first-web-application-with-spock.html

Integrating API with Hasura

Now we can use the API endpoint with Hasura Actions to expose a GraphQL endpoint where we can use the Spock API.

Start Hasura in Docker

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.

Setup Hasura Actions

After setting up Hasura on docker, we need to add the API in Hasura Actions, follow along to add the API in Hasura Actions.

  1. Go to Actions tab on the top, you will get something like the following: Hasura Actions Page

  2. Now click on Create button, you will get the following page, where we will define our Action. Add a new Action

  3. Now fill the following in the fields:

    1. Action definition:
      type Mutation {
          api (
              name: String!
              age: Int!
          ): SampleOutput
      }
    2. New types definition:
      type SampleOutput {
          result : String!
          id : String!
      }
    3. Handler:
      1. If you are on Linux (Ubuntu)
        http://172.17.0.1:3000/api

    Now hit save.

Run GraphQL

Now we will run our Hasura Action using a GraphQL mutation. Follow along to do so:

  1. First head out to the API tab on the top, you will get something like the following: Hasura API Page

  2. Now Paste the following GraphQL snippet in the GraphiQL box:

    mutation MyMutation {
        api(age: 2, name: "Baby's Bro") {
            result
        }
    }
  3. Hit the play buttion (near the GraphiQL title), you will get something like the following: Hasura Actions output

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published