-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
resolver.go
78 lines (66 loc) · 1.46 KB
/
resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//go:generate gorunpkg github.com/99designs/gqlgen
package type_system_extension
import (
"context"
"fmt"
)
func NewRootResolver() ResolverRoot {
return &resolver{
todos: []*Todo{
{
ID: "Todo:1",
Text: "Buy a cat food",
State: StateNotYet,
Verified: false,
},
{
ID: "Todo:2",
Text: "Check cat water",
State: StateDone,
Verified: true,
},
{
ID: "Todo:3",
Text: "Check cat meal",
State: StateDone,
Verified: true,
},
},
}
}
type resolver struct {
todos []*Todo
}
func (r *resolver) MyQuery() MyQueryResolver {
return &queryResolver{r}
}
func (r *resolver) MyMutation() MyMutationResolver {
return &mutationResolver{r}
}
type queryResolver struct{ *resolver }
func (r *queryResolver) Todos(ctx context.Context) ([]Todo, error) {
todos := make([]Todo, 0, len(r.todos))
for _, todo := range r.todos {
todos = append(todos, *todo)
}
return todos, nil
}
func (r *queryResolver) Todo(ctx context.Context, id string) (*Todo, error) {
for _, todo := range r.todos {
if todo.ID == id {
return todo, nil
}
}
return nil, nil
}
type mutationResolver struct{ *resolver }
func (r *mutationResolver) CreateTodo(ctx context.Context, todoInput TodoInput) (Todo, error) {
newID := fmt.Sprintf("Todo:%d", len(r.todos)+1)
newTodo := &Todo{
ID: newID,
Text: todoInput.Text,
State: StateNotYet,
}
r.todos = append(r.todos, newTodo)
return *newTodo, nil
}