Skip to content

Commit

Permalink
Add the second step from gqlgen instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
eisenheimjelid committed Mar 3, 2021
1 parent ba80b5b commit 5b23fa7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 20 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ Exec "go run ./server.go" to start GraphQL server
$ go run server.go
2021/03/02 16:29:48 connect to http://localhost:8080/ for GraphQL playground
```

## Step 2. Add code slightly more realistic

Add new `graph/model/todo.go`, regenerate the code and implement the new resolver into `graph/schema.resolvers.go`.

```bash
$ go generate
$ go run server.go
2021/03/02 20:35:39 connect to http://localhost:8080/ for GraphQL playground
```
37 changes: 27 additions & 10 deletions graph/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions graph/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions graph/model/todo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package model

type Todo struct {
ID string `json:"id"`
Text string `json:"text"`
Done bool `json:"done"`
UserID string `json:"user"`
}
1 change: 1 addition & 0 deletions graph/resolver.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package graph

//go:generate go run github.com/99designs/gqlgen
import "github.com/eisenheimjelid/learn-gqlgen/graph/model"

// This file will not be regenerated automatically.
Expand Down
14 changes: 11 additions & 3 deletions graph/schema.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

func (r *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (*model.Todo, error) {
todo := &model.Todo{
Text: input.Text,
ID: fmt.Sprintf("T%d", rand.Intn(100)),
User: &model.User{ID: input.UserID, Name: "user " + input.UserID},
Text: input.Text,
ID: fmt.Sprintf("T%d", rand.Intn(100)),
UserID: input.UserID, // fix this line
}
r.todos = append(r.todos, todo)
return todo, nil
Expand All @@ -26,11 +26,19 @@ func (r *queryResolver) Todos(ctx context.Context) ([]*model.Todo, error) {
return r.todos, nil
}

func (r *todoResolver) User(ctx context.Context, obj *model.Todo) (*model.User, error) {
return &model.User{ID: obj.UserID, Name: "user " + obj.UserID}, nil
}

// Mutation returns generated.MutationResolver implementation.
func (r *Resolver) Mutation() generated.MutationResolver { return &mutationResolver{r} }

// Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }

// Todo returns generated.TodoResolver implementation.
func (r *Resolver) Todo() generated.TodoResolver { return &todoResolver{r} }

type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
type todoResolver struct{ *Resolver }

0 comments on commit 5b23fa7

Please sign in to comment.