shufo / go-graphql-boilerplate

A boilerplate for creating GraphQL server with Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

go-graphql-boilerplate

This repository contains a boilerplate code for creating GraphQL server with golang

Environment

  • Go 1.12
  • docker, docker-compose

Installation

Install Go 1.12

  • apt-get install golang-1.12
  • or goenv

Docker

Initialize project

$ git clone https://github.com/shufo/go-graphql-boilerplate
$ cd go-graphql-boilerplate
$ make up

Wait for containers running up

Dependencies

Name Category Description
gqlgen GraphQL Generate graphql boilercode. To configure schemas, please edit gqlgen.yml and schemas in schemas directory.
chi Router A http router. route settings: server/router.go
sqlboiler ORM Generates model files from database.
casbin Authorization RBAC based authorization.
jwt-auth JWT A authentication middleware used by chi.
dataloaden N+1 query Reduce queries on N+1 query situation.
testify Testing Testing framework
realize Task runner Build files by watching project files
sql-migrate Migration Database migration by sql files.
go-i18n i18n Internationalization
ozzo-validation Validation Validates user inputs
Go Modules Dependency Management Manage dependencies by go.mod

Workflows

  1. Add migration files (eg. CREATE TABLE, ALTER TABLE) to migrations directory.
  2. Generate models by sqlboiler (automatically generated by realize. or manual: $ make models)
  3. Edit GraphQL Schema (in schemas directory)
  4. Generate GraphQL boilerplate by gqlgen (automatically generated by realize. or manual: $ make gqlgen)
  5. Write resolvers for GraphQL
  6. Write tests for resolvers
  7. Check if tests will pass ($ make test)

How to

Edit DB schemas

Add migration files to migrations directory.

-- +migrate Up
CREATE TABLE ~~
-- +migrate Down
DROP TABLE ~
~~

Add GraphQL Query

Edit schema/query.graphql

todo(id: Int): Todo!

Wait until graph/generated/generated.go generated.

Then add GraphQL resolvers.

Add GraphQL Mutation

Edit schema/mutation.graphql

createTodo(input: CreateTodoInput!): Todo!

Wait until graph/generated/generated.go generated.

Add 3rd party libraries

Run go get command on Local machine.

go get github.com/stretchr/testify

Trouble Shooting

If you get a permission error on go.mod editing, set write permission to go.mod and go.sum file.

sudo chmod +w go.mod go.sum

Test

$ make test

About

A boilerplate for creating GraphQL server with Go


Languages

Language:Go 91.7%Language:Makefile 7.9%Language:Dockerfile 0.4%