Coder Social home page Coder Social logo

go-graphql-api-boilerplate's Introduction

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
}

Initial Migration

$ go run ./migrations/init.go

or with docker

$ 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 Model

Run the server

$ go run server.go

or with docker

$ docker build -t go-graphql-api .
$ docker run -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: "[email protected]"
    password: "12345678"
    firstName: "graphql"
    lastName: "go"
  ) {
    ok
    error
    user {
      id
      email
      firstName
      lastName
      bio
      avatar
      createdAt
      updatedAt
    }
  }
}

Sign In

mutation {
  signIn(email: "[email protected]", password: "12345678") {
    ok
    error
    token
  }
}

Change a Password

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

Change a Profile

mutation {
  changeProfile(userID: 1, 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
  • Using Configuration file for DB & JWT secret_key
  • Making schema automation

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.