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 --buildOpen 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/bashTo generate graphql files.
gqlgen generateOn docker
~/go/bin/gqlgen generateOut of docker
A Database PostgreSQL is needed, configurations for it in docker/local.env
gin --port=8080 #or ~/go/bin/gin --port=8080With VScode and SGBD on Docker
docker-compose -f docker-compose-db.yml upOpen 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.htmlAll 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-golangSee: go.mod
echo '' >> main.gopackage main
import (
"log"
)
func main() {
log.Printf("Hello World!")
}Run to see you first hello world
go run main.gogo get github.com/codegangsta/ginUpdate 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 initTo generate graphql files.
gqlgen generateOn docker
~/go/bin/gqlgen generateInstall GORM
go get -u gorm.io/gorm && \
go get -u gorm.io/driver/postgresStart project with Hot Reload
~/go/bin/ginDefault start project
go run main.goOpen on you best browser: http://localhost:8085
With VScode and SGBD on Docker
docker-compose -f docker-compose-db.yml upgo test -v ./src/tests/With code coverage
go test -cover -coverprofile=c.out ./src/tests/
go tool cover -html=c.out -o coverage.html