-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.go
126 lines (105 loc) · 2.62 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"github.com/mjarkk/yarql"
)
// QueryRoot defines the entry point for all graphql queries
type QueryRoot struct{}
// MethodRoot defines the entry for all method graphql queries
type MethodRoot struct{}
// Node defines the required relay Node interface
// ref: https://relay.dev/docs/guides/graphql-server-specification/
type Node interface {
ResolveId() (uint, yarql.AttrIsID)
}
// Todo respresents a todo entry
type Todo struct {
ID uint `gq:"-"` // ignored because of (Todo).ResolveId()
Title string
Done bool
}
var _ = yarql.Implements((*Node)(nil), Todo{})
// ResolveId implements the Node interface
func (u Todo) ResolveId() (uint, yarql.AttrIsID) {
return u.ID, 0
}
var todoIdx = uint(3)
var todos = []Todo{
{ID: 1, Title: "Get groceries", Done: false},
{ID: 2, Title: "Make TODO app", Done: true},
}
// ResolveTodos returns all todos
func (QueryRoot) ResolveTodos() []Todo {
return todos
}
// GetTodoArgs are the arguments for the ResolveTodo
type GetTodoArgs struct {
ID uint `gq:"id,id"` // rename field to id and label field to have ID type
}
// ResolveTodo returns a todo by id
func (q QueryRoot) ResolveTodo(args GetTodoArgs) *Todo {
for _, todo := range todos {
if todo.ID == args.ID {
return &todo
}
}
return nil
}
// CreateTodoArgs are the arguments for the ResolveCreateTodo
type CreateTodoArgs struct {
Title string
}
// ResolveCreateTodo creates a new todo
func (m MethodRoot) ResolveCreateTodo(args CreateTodoArgs) Todo {
todo := Todo{
ID: todoIdx,
Title: fmt.Sprint(args.Title), // Copy title
Done: false,
}
todos = append(todos, todo)
todoIdx++
return todo
}
// UpdateTodoArgs are the arguments for the ResolveUpdateTodo
type UpdateTodoArgs struct {
ID uint `gq:"id,id"` // rename field to id and label field to have ID type
Title *string
Done *bool
}
// ResolveUpdateTodo updates a todo
func (m MethodRoot) ResolveUpdateTodo(args UpdateTodoArgs) (Todo, error) {
idx := -1
for i, todo := range todos {
if todo.ID == args.ID {
idx = i
break
}
}
if idx == -1 {
return Todo{}, fmt.Errorf("todo with id %d not found", args.ID)
}
todo := todos[idx]
if args.Title != nil {
todo.Title = *args.Title
}
if args.Done != nil {
todo.Done = *args.Done
}
todos[idx] = todo
return todo, nil
}
// ResolveDeleteTodo deletes a todo
func (m MethodRoot) ResolveDeleteTodo(args GetTodoArgs) ([]Todo, error) {
idx := -1
for i, todo := range todos {
if todo.ID == args.ID {
idx = i
break
}
}
if idx == -1 {
return nil, fmt.Errorf("todo with id %d not found", args.ID)
}
todos = append(todos[:idx], todos[idx+1:]...)
return todos, nil
}