Hacker News-like GraphQL API
This is a Hacker News-like GraphQL API built with Golang, GORM, gqlgen, and Chi. It provides a simple yet powerful API for creating and managing users and links.
Features
- User Creation: Users can register an account.
- User Login: Users can log in to their accounts.
- Link Creation: Authenticated users can create links.
- Built-in Authentication: The API includes a JWT-based authentication system.
- Query Links: Users can query links either by user or retrieve all links.
Technologies Used
- Golang: The programming language used for the backend.
- GORM: An ORM library for Golang that handles database operations.
- gqlgen: A library for building GraphQL servers in Go.
- Chi: A lightweight router for Go.
Getting Started
To get a local copy up and running, follow these steps.
Prerequisites
- Go (version 1.16 or later)
- PostgreSQL (or another supported database)
Environment Variables
To run the server, you need to configure environment variables. Copy the .env.example
file from the root of the project and rename it to .env
. Update the values as needed:
cp .env.example .env
Installation
-
Clone the repository:
git clone https://github.com/Anuolu-2020/hackernews-api-clone.git
cd your-repo
-
Install the required dependencies:
go mod tidy
-
Set up your database and update the connection string in the configuration.
-
Run the application:
go run server.go
API Endpoints
User
- Register User:
POST /query
- Login User:
POST /query
Links
- Create Link:
POST /query
- Query Links:
POST /query
Example Queries
Register User
mutation create {
createUser(input: { username: "exampleUser", password: "123" })
}
Login User
mutation create {
login(input: { username: "exampleUser", password: "123" })
}
Create Link
mutation create {
createLink(input: { title: "real link!", address: "www.graphql.org" }) {
user {
name
}
}
}
Query Links
query {
links {
title
address
user {
name
}
}
}
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments