Skip to content

Simple Hackernews clone written in Django for learning GraphQL

License

Notifications You must be signed in to change notification settings

darth-dodo/hackernews-backend

Repository files navigation

reimagined-broccoli

Product Features

  • Listing of all the links (no login required)
  • Link Creation
  • User Creation
  • JWT Based authentication
  • Vote registration and unregistration by logged in Users

Python env

  • The project uses Python 3.7. Use PyEnv to install the required version
  • Python environment in managed through Poetry
  • Install Poetry using instructions present here
  • Install the project virtual env and packages using the command. Make sure you have set the local Python version to 3.7
poetry install
  • Start the project virtual env using the command
poetry shell
  • To check out more details about your virtualenv please run the command
poetry env info
  • In case of questions, please checkout the guide to maintaining virtual envs and python versions with over here

Application Details

API interaction

  • The GraphQL interface can be accessed using the inbuilt Graphene UI present at localhost:8000/grapqhl but it does not support JWT Auth
  • To use GraphQL with JWT Auth checkout Insomnia or Postman

Precommit hooks

  • isort
  • flake8
  • black
  • in built precommit checks

Seed data

  • From the root of the repo, run the following management command to generate the seed data
python manage.py seed_data

GraphQL

  • The GraphQL interface can be accessed at localhost:8000/graphql
  • GraphQL interfaces are of the following types:
    • Query - analogous to DQL statemets
    • Mutations - analogous to DML and DDL statements

Query

  • Fetch Links data
query {
  links{
    id
    url
    linkVotes{
      created
      user{
        username
      }
    }
    postedBy{
      username
    }
  }
}
  • Fetch HNUsers data
query{
  hnUsers{
    bio
    username
    email
    superuserAccess
  }
}
  • Fetch JWT token user Details
# provide the JWT in Auth headers

query{
  me {
    username
    email
  }
}
  • Fetch List of Links with the HN User Information
query {
  links{
    id
    url
    postedBy{
      username
    }
  }
}
  • Fetch List of Votes
query {
  votes{
    created
    link{
      url
    }
    user{
      username
    }
  }
}
  • List of Votes filtered by Vanilla Search using Q operator
query {
  vanillaFilteredLinks(search: "gringotts"){
    url
  }
}
  • Paginate the List of links data by using skip and page size (first):
query {
  paginatedLinks(search: ".com", skip:10, first: 20){
    url
    description
  }
}
query {
  paginatedLinks(search: ".biz", first: 3){
    url
    description
  }
}
  • Using Relay and Graphene to generate Edges and Nodes. Edges are a collection of nodes and Relay allows for pagination functionality out of the box
query {
  relayLinks(first: 3) {
    edges {
      node {
        id
        url
        description
        linkVotes {
          edges {
            node {
              id
              created
              user {
                username
              }
            }
          }
        }
      }
    }
    pageInfo {
      startCursor
      endCursor
    }
  }
}

Mutations

  • Create Link
mutation {
  createLink(
  description: "Hustlers Den",
  url: "https://github.com/darth-dodo/hustlers-den"
  ){
    url
    description
    id
  }
}
mutation {
  createLink(url: "https://github.com/darth-dodo/discuss-it", description: "Phoenix and Elixir clone") {
    id
    url
    description
    postedBy {
      username
      email
    }
  }
}
  • Create HN User
mutation {
  createHnUser(
    bio: "dummy bio"
    email: "[email protected]"
    password: "dummypassword"
    username: "dummy-1"
  ){
    hnUser {
      bio
    }
  }
}
# generate the JWT payload

mutation {
  tokenAuth(username: "dummy-1", password: "dummypassword"){
    payload
  }
}
# generate the JWT token

mutation {
  tokenAuth(username: "dummy-1", password: "dummypassword"){
    token
  }
}
# verify the JWT token

mutation {
  verifyToken(token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImR1bW15LTEiLCJleHAiOjE1ODczMDg5NjksIm9yaWdJYXQiOjE1ODczMDg2Njl9.sBVGWqOnHxWv7f1zeJqb6V-CUZGZ4rxKDEIbqLtsZQY"){
    payload
  }
}
  • Create Vote
mutation {
  createVote(linkId: 4){
    vote {
      created
      user {
        username
      }
      link {
        url
      }
    }
  }
}
  • Register Unvote
mutation {
  registerUnvote(linkId: 4){
    success
  }
}
  • Create a Link using the Relay functionality
mutation {
  relayCreateLink(input: {
    url: "https://github.com/darth-dodo/meal-helper",
    description:  "Meal Plan Management tool"
  }) {
    link {
      id
      created
      url
      description
    }
  }
}

Error Handling

Further Reading

About

Simple Hackernews clone written in Django for learning GraphQL

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages