Example made with Golang, Gin Framework, GraphQL, Resful, Authenticate with JWT and much more.
A example of bank transaction, made with GOlang, Gin Framework, Restful and GraphQL.
- Endpoints
- Insomnia
- Get Started
- Get Started - without docker
- Run Debug with VScode and DB in docker
- Unity Test and Test Integration
- Step By Step
- Commands
- Technologies
- References
Endpoint | Method | Action |
---|---|---|
/graphql | POST | |
/graphiql | GET | |
/users | GET | Index |
/users | POST | Store |
/users | PATCH | Update |
/users/:id | DELETE | Destroy |
/users/:id | GET | Show |
If you use Insomnia, just import a insomnia-v4.yaml
Just one command
docker-compose up --build
Open on you best browser: http://localhost:8085
GraphQL Playground: http://localhost:8085/gaphiql
To see a database open adminer on: http://localhost:8086
Configurations for connect on database see: /docker/local.env IMPORTANT! If you not have a docker see: Get Started - without docker
├── config - (configuration)
├── controllers (RESTfull methods)
├── core (Base methos for controllers, models and services)
├── database - (database configuration, migrate and seed data)
├── graphql
│ ├── generated - A package that only contains the generated runtime
│ │ └── generated.go
│ ├── model - A package for all your graph models, generated or otherwise
│ │ └── models_gen.go
│ ├── resolver.go - The root graph resolver type. This file wont get regenerated
│ ├── schema.graphqls - Some schema. You can split the schema into as many graphql files as you like
│ └── schema.resolvers.go - the resolver implementation for schema.graphql
├── models (ORM models based on Database tables)
├── routes (files with routes)
├── services (with business rules and intermediate a database)controlling the generated code.
├── go.mod
├── go.sum
├── gqlgen.yml - The gqlgen config file, knobs for
└── main.go - The entry point to your app. Customize it however you see fit
Enter on docker container to exec any command.
docker exec -it labbankgo-api /bin/bash
To generate graphql files.
gqlgen generate
On docker
~/go/bin/gqlgen generate
Out of docker
A Database PostgreSQL is needed, configurations for it in docker/local.env
gin --port=8080 #or ~/go/bin/gin --port=8080
With VScode and SGBD on Docker
docker-compose -f docker-compose-db.yml up
Open your debug on VScode and run "Launch file"
go test -v ./src/tests/
With code coverage
go test -cover -coverprofile=c.out ./src/tests/
go tool cover -html=c.out -o coverage.html
All steps necessary to make this example project.
All libs in Golang have a module name. This is for a package manage for Golang, like a npm/yarm for nodeJS, gradlew/mavem for Java, compose for PHP, mix for Elixir and many more.
go mod init github.com/ruyjfs/example-golang
See: go.mod
echo '' >> main.go
package main
import (
"log"
)
func main() {
log.Printf("Hello World!")
}
Run to see you first hello world
go run main.go
go get github.com/codegangsta/gin
Update a file main.go
with this example code
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func setupRouter() *gin.Engine {
r := gin.Default()
r.GET("/:name", func(c *gin.Context) {
name := c.Params.ByName("name")
c.String(http.StatusOK, "Hello World", name)
})
return r
}
func main() {
r := setupRouter()
r.Run(":8080")
}
go run github.com/99designs/gqlgen init
To generate graphql files.
gqlgen generate
On docker
~/go/bin/gqlgen generate
Install GORM
go get -u gorm.io/gorm && \
go get -u gorm.io/driver/postgres
Start project with Hot Reload
~/go/bin/gin
Default start project
go run main.go
Open on you best browser: http://localhost:8085
With VScode and SGBD on Docker
docker-compose -f docker-compose-db.yml up
go test -v ./src/tests/
With code coverage
go test -cover -coverprofile=c.out ./src/tests/
go tool cover -html=c.out -o coverage.html