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