Skip to content

Commit

Permalink
Convert todo example to new resolver syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Jul 5, 2018
1 parent b16e842 commit 9f6ff0c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion example/todo/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func main() {
http.Handle("/", handler.Playground("Todo", "/query"))
http.Handle("/query", handler.GraphQL(
todo.MakeExecutableSchema(todo.New()),
todo.NewExecutableSchema(todo.New()),
handler.RecoverFunc(func(ctx context.Context, err interface{}) error {
// send this panic somewhere
log.Print(err)
Expand Down
40 changes: 25 additions & 15 deletions example/todo/todo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ import (
"github.com/mitchellh/mapstructure"
)

type todoResolver struct {
todos []Todo
lastID int
}

func New() *todoResolver {
return &todoResolver{
func New() *resolvers {
return &resolvers{
todos: []Todo{
{ID: 1, Text: "A todo not to forget", Done: false},
{ID: 2, Text: "This is the most important", Done: false},
Expand All @@ -26,7 +21,22 @@ func New() *todoResolver {
}
}

func (r *todoResolver) MyQuery_todo(ctx context.Context, id int) (*Todo, error) {
type resolvers struct {
todos []Todo
lastID int
}

func (r *resolvers) MyQuery() MyQueryResolver {
return (*QueryResolver)(r)
}

func (r *resolvers) MyMutation() MyMutationResolver {
return (*MutationResolver)(r)
}

type QueryResolver resolvers

func (r *QueryResolver) Todo(ctx context.Context, id int) (*Todo, error) {
time.Sleep(220 * time.Millisecond)

if id == 666 {
Expand All @@ -41,18 +51,20 @@ func (r *todoResolver) MyQuery_todo(ctx context.Context, id int) (*Todo, error)
return nil, errors.New("not found")
}

func (r *todoResolver) MyQuery_lastTodo(ctx context.Context) (*Todo, error) {
func (r *QueryResolver) LastTodo(ctx context.Context) (*Todo, error) {
if len(r.todos) == 0 {
return nil, errors.New("not found")
}
return &r.todos[len(r.todos)-1], nil
}

func (r *todoResolver) MyQuery_todos(ctx context.Context) ([]Todo, error) {
func (r *QueryResolver) Todos(ctx context.Context) ([]Todo, error) {
return r.todos, nil
}

func (r *todoResolver) MyMutation_createTodo(ctx context.Context, todo TodoInput) (Todo, error) {
type MutationResolver resolvers

func (r *MutationResolver) CreateTodo(ctx context.Context, todo TodoInput) (Todo, error) {
newID := r.id()

newTodo := Todo{
Expand All @@ -69,9 +81,7 @@ func (r *todoResolver) MyMutation_createTodo(ctx context.Context, todo TodoInput
return newTodo, nil
}

// this example uses a map instead of a struct for the change set. this scales updating keys on large objects where
// most properties are optional, and if unspecified the existing value should be kept.
func (r *todoResolver) MyMutation_updateTodo(ctx context.Context, id int, changes map[string]interface{}) (*Todo, error) {
func (r *MutationResolver) UpdateTodo(ctx context.Context, id int, changes map[string]interface{}) (*Todo, error) {
var affectedTodo *Todo

for i := 0; i < len(r.todos); i++ {
Expand All @@ -93,7 +103,7 @@ func (r *todoResolver) MyMutation_updateTodo(ctx context.Context, id int, change
return affectedTodo, nil
}

func (r *todoResolver) id() int {
func (r *MutationResolver) id() int {
r.lastID++
return r.lastID
}
2 changes: 1 addition & 1 deletion example/todo/todo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestTodo(t *testing.T) {
srv := httptest.NewServer(handler.GraphQL(MakeExecutableSchema(New())))
srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(New())))
c := client.New(srv.URL)

t.Run("create a new todo", func(t *testing.T) {
Expand Down

0 comments on commit 9f6ff0c

Please sign in to comment.