~sircmpwn/gqlgen

ref: c84d29a1c940 gqlgen/example/type-system-extension/resolver.go -rw-r--r-- 1.3 KiB
c84d29a1Drew DeVault codegen: add PGP to common initialisms 1 year, 2 months ago
                                                                                
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
//go:generate go run ../../testdata/gqlgen.go

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) {
	return r.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
}