/
schema.go
105 lines (87 loc) · 1.98 KB
/
schema.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package examples
import (
"errors"
graphql "github.com/graph-gophers/graphql-go"
"io/ioutil"
)
func ReadSchemaFile() (string, error) {
rawSchema, err := ioutil.ReadFile("./examples/schema.gql")
if err != nil {
return "", err
}
return string(rawSchema), nil
}
type todoItem struct {
ID graphql.ID
Description string
Type string
IsDone bool
}
var todos = []*todoItem{
{
ID: "0b6afc2b-f554-418d-b6b8-6c45e712177c",
Description: "Prepare proposal for project A",
Type: "WORK",
IsDone: false,
},
{
ID: "5085f9be-f272-421b-8644-87b56d4df0d2",
Description: "Pay for invoice 94456712",
Type: "WORK",
IsDone: false,
},
{
ID: "2acfe612-41e1-4a62-88f0-5a6abd932e79",
Description: "Learn setting up GraphQL server with golang",
Type: "PERSONAL",
IsDone: false,
},
{
ID: "cce3f263-9c60-4dc9-88d5-ef052724b6ab",
Description: "Buy toilet paper",
Type: "FAMILY",
IsDone: false,
},
}
type todoItemResolver struct {
t *todoItem
}
func (r *todoItemResolver) ID() graphql.ID {
return r.t.ID
}
func (r *todoItemResolver) Description() string {
return r.t.Description
}
func (r *todoItemResolver) Type() string {
return r.t.Type
}
func (r *todoItemResolver) IsDone() bool {
return r.t.IsDone
}
type Resolver struct {}
func (r *Resolver) PendingTodo() []*todoItemResolver {
var result []*todoItemResolver
for _, item := range todos {
if !item.IsDone {
result = append(result, &todoItemResolver{item})
}
}
return result
}
func (r* Resolver) Todo(args struct { ID graphql.ID }) (*todoItemResolver, error) {
for _, item := range todos {
if item.ID == args.ID {
return &todoItemResolver{item}, nil
}
}
return nil, errors.New("not found")
}
func (r *Resolver) MarkItemDone(args struct { ID graphql.ID }) (*todoItemResolver, error) {
for _, item := range todos {
if item.ID == args.ID {
item.IsDone = true
return &todoItemResolver{item}, nil
}
}
return nil, errors.New("not found")
}