forked from 99designs/gqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
71 lines (55 loc) · 1.34 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
//go:generate gorunpkg github.com/99designs/gqlgen
package config
import (
"context"
"fmt"
)
func New() Config {
c := Config{
Resolvers: &Resolver{
todos: []Todo{
{DatabaseID: 1, Description: "A todo not to forget", Done: false},
{DatabaseID: 2, Description: "This is the most important", Done: false},
{DatabaseID: 3, Description: "Please do this or else", Done: false},
},
nextID: 3,
},
}
return c
}
type Resolver struct {
todos []Todo
nextID int
}
func (r *Resolver) Mutation() MutationResolver {
return &mutationResolver{r}
}
func (r *Resolver) Query() QueryResolver {
return &queryResolver{r}
}
func (r *Resolver) Todo() TodoResolver {
return &todoResolver{r}
}
type mutationResolver struct{ *Resolver }
func (r *mutationResolver) CreateTodo(ctx context.Context, input NewTodo) (Todo, error) {
newID := r.nextID
r.nextID++
newTodo := Todo{
DatabaseID: newID,
Description: input.Text,
}
r.todos = append(r.todos, newTodo)
return newTodo, nil
}
type queryResolver struct{ *Resolver }
func (r *queryResolver) Todos(ctx context.Context) ([]Todo, error) {
return r.todos, nil
}
type todoResolver struct{ *Resolver }
func (r *todoResolver) ID(ctx context.Context, obj *Todo) (string, error) {
if obj.ID != "" {
return obj.ID, nil
}
obj.ID = fmt.Sprintf("TODO:%d", obj.DatabaseID)
return obj.ID, nil
}