Skip to content

rhzs/gqlgen-todos

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GQLGEN Todo Example

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.

Running The Service

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.

Code Structure

.
├── 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.

Unit Testing

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/                                   |
------------------------------------------------------------------------------------------------------------------------------------------

Credit

Made in Jakarta with love (c) 2023.

About

This is an improved version for Golang GQLGEN TODO Example

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages