Go API with echo, gorm, and GraphQL
A simple tutorial of building a Go API with echo, gorm, and GraphQL.
--
GraphQL has been around these days and is usually described as a new type in the field of API technologies. Even though you’ve been constructing and using RESTful API’s for quite some time, you can find it simple and useful.
In this article, we’re going to highlight how to set up GraphQL API in Go with some dependencies.
Example Repo
Here is a link to the codebase in full for reference:
Dependencies
Some dependencies are required to grab in this post so install them:
go get github.com/oxequa/realize
go get github.com/labstack/echo
go get github.com/jinzhu/gorm
go get github.com/jinzhu/gorm/dialects/mysql
go get bitbucket.org/liamstask/goose/cmd/goose
go get github.com/graphql-go/graphql
go get github.com/graphql-go/handler
Setup MySQL with Docker
Getting started with it quickly, we use Docker and setup mysql 8.0
.
Add a docker-compose.yml
in your root project:
version: '3'
services:
mysql:
image: mysql:8.0
volumes:
- mysqldata:/var/lib/mysql
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_0900_as_ci --default-authentication-plugin=mysql_native_password
environment:
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
volumes:
mysqldata:
driver: local
And run it:
docker-compose up
Set up an echo server
Let’s set up an echo server with a very simple API endpoint.
Create main.go
:
package main
import (
"golang-with-echo-gorm-graphql-example/handler"
"log"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func main() {
e := echo.New()…