Code Monkey home page Code Monkey logo

go-graphql-hackernews's Introduction

go-graphql-hackernews

Project Setup

Initialize go modules file:

go mod init github.com/masudur-rahman/hackernews

Initialize a gqlgen project

go get -d github.com/99designs/gqlgen
go run github.com/99designs/gqlgen init

Generate Go codes for the graphql schemas

go run github.com/99designs/gqlgen generate

Sometimes gqlgen generate doesn't work properly. You may need to run the followings

 printf '// +build tools\npackage tools\nimport (_ "github.com/99designs/gqlgen"\n _ "github.com/99designs/gqlgen/graphql/introspection")' | gofmt > tools.go\n\ngo mod tidy
 rm -rf vendor
 go run github.com/99designs/gqlgen generate
 go mod tidy && go mod vendor

Note: If you are getting validation failed: packages.Load error. It may occur, because gqlgen uses todo project as starter template. To get rid of this error, edit graph/schema.resolvers.go file and delete functions CreateTodo and Todos. Now run the command again.

Run MySQL Server from docker

docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=mysql -e MYSQL_DATABASE=hackernews -d mysql:latest

It will start a MySQL Server with a root:mysql user and a database named hackernews

To terminate the mysql docker, just run the following command

docker rm mysql -f

Exec into database

docker exec -it mysql bash

> mysql -u root -p
>> mysql # password

# Basic pre commands
> show databases;
> use hackernews;
> show tables;
> describe Users;

Models and Migrations

We need to create migrations for our app so every time our app runs it creates tables it needs to work properly, we are going to use golang-migrate package. Create a folder structure for our database files in the project root directory:

go-graphql-hackernews
--internal
----pkg
------db
--------migrations
----------mysql
Install go mysql driver and golang-migrate packages then create migrations:
go get -u github.com/go-sql-driver/mysql
go build -tags 'mysql' -ldflags="-X main.Version=1.0.0" -o $GOPATH/bin/migrate github.com/golang-migrate/migrate/v4/cmd/migrate/
cd internal/pkg/db/migrations/
migrate create -ext sql -dir mysql -seq create_users_table
migrate create -ext sql -dir mysql -seq create_links_table

migrate command will create two files for each migration ending with .up and .down; up is responsible for applying migration and down is responsible for reversing it. Open 000001_create_users_table.up.sql and add table for our users:

CREATE TABLE IF NOT EXISTS Users(
    ID INT NOT NULL UNIQUE AUTO_INCREMENT,
    Username VARCHAR (127) NOT NULL UNIQUE,
    Password VARCHAR (127) NOT NULL,
    PRIMARY KEY (ID)
)

in 000002_create_links_table.up.sql:

CREATE TABLE IF NOT EXISTS Links(
    ID INT NOT NULL UNIQUE AUTO_INCREMENT,
    Title VARCHAR (255) ,
    Address VARCHAR (255) ,
    UserID INT ,
    FOREIGN KEY (UserID) REFERENCES Users(ID) ,
    PRIMARY KEY (ID)
)

We need one table for saving links and one table for saving users.

If you want to migrate our tables from the CLI, then run the following migrate command from the project root directory

migrate -database mysql://root:dbpass@/hackernews -path internal/pkg/db/migrations/mysql up

NB: The migration is added to the starting of the GraphQL Server. So, we don't need to run it manually.

Start the GraphQL Server

go run server/server.go

Now open http://localhost:8080 to Experience the hackernews server

You will find a GraphQL Playground here. You can test the defined queries and mutations here. Here's some example queries and mutations.

mutation createUser {
  createUser(input: {username: "masud", password: "password"})
}
mutation login {
  login(input: {username: "masud", password: "password"})
}
mutation createLink {
  createLink(input: {title: "something", address: "somewhere"}) {
    title
    address
    id
    user {
      name
    }
  }
}
  • With a Additional Request Header
    {
        "Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2Njg3ODgyODAsInVzZXJuYW1lIjoibWFzdWQifQ.3G0dggzOSXimbszXsD1yrTGlXgANhGyWrbf2S9vPHRU"
    }
query links {
  links {
    title
    address,
    id
    user {
      name
    }
  }
}

go-graphql-hackernews's People

Contributors

masudur-rahman avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.