Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rhzs/gqlgen-todos

This is an improved version for Golang GQLGEN TODO Example
https://github.com/rhzs/gqlgen-todos

dependency-injection golang golang-examples graphql

Last synced: 26 days ago
JSON representation

This is an improved version for Golang GQLGEN TODO Example

Awesome Lists containing this project

README

        

# 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

```text
.
├── 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`:

```text
------------------------------------------------------------------------------------------------------------------------------------------
| 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.