github.com/jepp2078/gqlgen@v0.7.2/example/config/resolver.go (about) 1 //go:generate gorunpkg github.com/99designs/gqlgen 2 3 package config 4 5 import ( 6 "context" 7 "fmt" 8 ) 9 10 func New() Config { 11 c := Config{ 12 Resolvers: &Resolver{ 13 todos: []Todo{ 14 {DatabaseID: 1, Description: "A todo not to forget", Done: false}, 15 {DatabaseID: 2, Description: "This is the most important", Done: false}, 16 {DatabaseID: 3, Description: "Please do this or else", Done: false}, 17 }, 18 nextID: 3, 19 }, 20 } 21 return c 22 } 23 24 type Resolver struct { 25 todos []Todo 26 nextID int 27 } 28 29 func (r *Resolver) Mutation() MutationResolver { 30 return &mutationResolver{r} 31 } 32 func (r *Resolver) Query() QueryResolver { 33 return &queryResolver{r} 34 } 35 func (r *Resolver) Todo() TodoResolver { 36 return &todoResolver{r} 37 } 38 39 type mutationResolver struct{ *Resolver } 40 41 func (r *mutationResolver) CreateTodo(ctx context.Context, input NewTodo) (Todo, error) { 42 newID := r.nextID 43 r.nextID++ 44 45 newTodo := Todo{ 46 DatabaseID: newID, 47 Description: input.Text, 48 } 49 50 r.todos = append(r.todos, newTodo) 51 52 return newTodo, nil 53 } 54 55 type queryResolver struct{ *Resolver } 56 57 func (r *queryResolver) Todos(ctx context.Context) ([]Todo, error) { 58 return r.todos, nil 59 } 60 61 type todoResolver struct{ *Resolver } 62 63 func (r *todoResolver) ID(ctx context.Context, obj *Todo) (string, error) { 64 if obj.ID != "" { 65 return obj.ID, nil 66 } 67 68 obj.ID = fmt.Sprintf("TODO:%d", obj.DatabaseID) 69 70 return obj.ID, nil 71 }