go-graphql-api-boilerplate

A Boilerplate of GraphQL API built in Go + graphql-go + gorm

Github stars Tracking Chart

Go GraphQL API Boilerplate

Stacks

Features

  • User Sign Up & Sign In
  • Change a Password, Profile

How to Run

Initialize DB

  1. Create a database
postgres=# CREATE DATABASE go;
  1. Create a user as owner of database
postgres=# CREATE USER go WITH ENCRYPTED PASSWORD 'go';

postgres=# ALTER DATABASE go OWNER TO go;
  1. Grant all privileges to user for the database
postgres=# GRANT ALL PRIVILEGES ON DATABASE go TO go;
  1. Configure the db in db.go
// ConnectDB : connecting DB
func ConnectDB() (*DB, error) {
	db, err := gorm.Open("postgres", "host=localhost port=5432 user=go dbname=go password=go sslmode=disable")

	if err != nil {
		panic(err)
	}

	return &DB{db}, nil
}

or with Docker

host address should be edited to host.docker.internal to connect a host interface.

// ConnectDB : connecting DB
func ConnectDB() (*DB, error) {
	db, err := gorm.Open("postgres", "host=host.docker.internal port=5432 user=go dbname=go password=go sslmode=disable")

	if err != nil {
		panic(err)
	}

	return &DB{db}, nil
}

Initial Migration

$ go run ./migrations/init.go

or with Docker

$ docker build -t go-graphql-api .
$ docker run --rm go-graphql-api migrate

This will generate the users table in the database as per the User Model declared in ./model/user.go

Run the server

$ go run server.go

or with Docker

$ docker run --rm -d -p 8080:8080 go-graphql-api

GraphQL Playground

Connect to http://localhost:8080

Authentication : JWT

You need to set the Http request headers Authorization: {JWT_token}

Usage

Sign Up

mutation {
  signUp(
    email: "test@test.com"
    password: "12345678"
    firstName: "graphql"
    lastName: "go"
  ) {
    ok
    error
    user {
      id
      email
      firstName
      lastName
      bio
      avatar
      createdAt
      updatedAt
    }
  }
}

Sign In

mutation {
  signIn(email: "test@test.com", password: "12345678") {
    ok
    error
    token
  }
}

Change a Password

mutation {
  changePassword(password: "87654321") {
    ok
    error
    user {
      id
      email
      firstName
      lastName
      bio
      avatar
      createdAt
      updatedAt
    }
  }
}

Change a Profile

mutation {
  changeProfile(bio: "Go developer", avatar: "go-developer.png") {
    ok
    error
    user {
      id
      email
      firstName
      lastName
      bio
      avatar
      createdAt
      updatedAt
    }
  }
}

Get my profile

query {
  getMyProfile {
    ok
    error
    user {
      id
      email
      firstName
      lastName
      bio
      avatar
      createdAt
      updatedAt
    }
  }
}

Next to do

  • Sign-Up
  • Query the profile with implementing context.Context
  • Sign-In with JWT
  • Change the password
  • Change the profile
  • Merging *.graphql files to a schema with packr
  • Using Configuration file for DB & JWT secret_key

Main metrics

Overview
Name With Owneropenatx/atxserver2
Primary LanguageHTML
Program languageGo (Language Count: 5)
Platform
License:MIT License
所有者活动
Created At2019-01-15 08:42:33
Pushed At2023-03-28 07:30:06
Last Commit At2021-08-10 10:01:02
Release Count7
Last Release Namev0.2.2 (Posted on )
First Release Namev0.1.0 (Posted on )
用户参与
Stargazers Count1.4k
Watchers Count50
Fork Count439
Commits Count156
Has Issues Enabled
Issues Count89
Issue Open Count61
Pull Requests Count6
Pull Requests Open Count3
Pull Requests Close Count1
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private