This service is to demonstrate how we can develop GraphQL service in Golang. This example emphasize on how we can structure our Golang code with GraphQL, use dependency injection pattern, and follow Effective Go approach.
To run the service, you can run it directly with go run cmd/main.go
.
Don't worry about not be able to resolve dependencies, you don't have to download dependencies. It will always be there (committed).
If you wish to regenerate the generated file, you can use go run github.com/99designs/gqlgen generate
.
.
├── README.md
├── cmd
│ └── main.go
├── go.mod
├── go.sum
├── gqlgen.yml
├── graph
│ ├── model
│ ├── schema.graphqls
│ └── z_graph.go
├── internal
│ ├── mutation
│ ├── query
│ ├── resolver
│ └── storage
├── tools.go
└── vendor
├── github.com
├── golang.org
├── gopkg.in
└── modules.txt
12 directories, 9 files
internal
folder is created and inside is declaration of GQL handler for mutation
, query
, and resolver
.
The storage
package used for connecting to database / cache / file storage.
The initialization happened in cmd/main.go
. In main.go
, we define how we can inject TODO Storage
to GraphQL resolver. Later, TODO Storage
used by mutation
and query
resolver.
We use testify
and mockery
for unit testing approach. This may not be the right taste for some people. For me, this is the robust and easiest way to do.
Testing Coverage Results using package-cover
:
------------------------------------------------------------------------------------------------------------------------------------------
| Branch | Dir | |
| Cov% | Cov | Stmts | Cov% | Cov | Stmts | Package |
------------------------------------------------------------------------------------------------------------------------------------------
| 100.00 | 0 | 0 | 100.00 | 0 | 0 | github.com/rhzs/gqlgen-todos/graph/ |
| 100.00 | 0 | 0 | 100.00 | 0 | 0 | github.com/rhzs/gqlgen-todos/graph/model/ |
| 100.00 | 5 | 5 | 100.00 | 5 | 5 | github.com/rhzs/gqlgen-todos/internal/mutation/ |
| 100.00 | 2 | 2 | 100.00 | 2 | 2 | github.com/rhzs/gqlgen-todos/internal/query/ |
| 100.00 | 3 | 3 | 100.00 | 3 | 3 | github.com/rhzs/gqlgen-todos/internal/resolver/ |
| 100.00 | 3 | 3 | 100.00 | 3 | 3 | github.com/rhzs/gqlgen-todos/internal/storage/ |
------------------------------------------------------------------------------------------------------------------------------------------
Made in Jakarta with love (c) 2023.