This is a HackerNews clone built with GraphQL(gglgen), Go and MySQL.
It is based on the How to GraphQL Tutorial.
- Clone the repo
git clone https://github.com/thegeorgenikhil/hackernews-go-graphql
- Install dependencies
go mod download
- Start a MySQL Docker container using the following command:
docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=dbpass -e MYSQL_DATABASE=hackernews -d mysql:latest
- Run the app
go run server.go
-
Open http://localhost:8080 to view the GraphQL Playground it in the browser.
-
Use http://localhost:8080/graphql to query the GraphQL API.
- Get all links
query GetLinks {
links {
title
address
id
user {
id
name
}
}
}
Response:
{
"data": {
"links": [
{
"title": "GraphQL",
"address": "https://graphql.org/",
"id": "1",
"user": {
"id": "1",
"name": "thegeorgenikhil"
}
},
{
"title": "How to GraphQL",
"address": "https://www.howtographql.com/",
"id": "2",
"user": {
"id": "1",
"name": "thegeorgenikhil"
}
}
]
}
}
- Post a new link
mutation CreateLink($input: NewLink!) {
createLink(input: $input) {
id
title
address
user {
id
name
}
}
}
Variables:
{
"input": {
"title": "GraphQL",
"address": "https://graphql.org/"
}
}
Headers:
{
"Authorization": "<user-token-here>"
}
Response:
{
"data": {
"createLink": {
"id": "1",
"title": "GraphQL",
"address": "https://graphql.org/",
"user": {
"id": "1",
"name": "thegeorgenikhil"
}
}
}
}
- Create a new user
mutation CreateUser($input: NewUser!) {
createUser(input: $input)
}
Variables:
{
"input": {
"username": "user",
"password": "password"
}
}
Response:
{
"data": {
"createUser": "<user-token-here>"
}
}
- Login a user
mutation LoginUser($input: Login!) {
login(input: $input)
}
Variables:
{
"input": {
"username": "user",
"password": "password"
}
}
Response:
{
"data": {
"login": "<user-token-here>"
}
}
- Refresh a user token
mutation RefereshToken($input: RefreshTokenInput!) {
refreshToken(input: $input)
}
Variables:
{
"input": {
"token": "<expired-token-here>"
}
}
Response:
{
"data": {
"refreshToken": "<new-user-token-here>"
}
}