-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtodo.resolvers.go
60 lines (48 loc) · 1.8 KB
/
todo.resolvers.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
package graph
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
// Code generated by github.com/99designs/gqlgen version v0.17.36
import (
"context"
"example/graph/model"
"github.com/cloudmatelabs/gorm-gqlgen-relay/relay"
customContext "github.com/juunini/gorm-custom-context"
"gorm.io/gorm/clause"
)
// Todos is the resolver for the todos field.
func (r *queryResolver) Todos(ctx context.Context, first *int, after *string, last *int, before *string, orderBy map[string]interface{}, where *model.TodoFilter) (*relay.Connection[model.Todo], error) {
context := customContext.GetContext(ctx)
db := context.Database.Preload("User")
return relay.Paginate[model.Todo](db, where, orderBy, relay.PaginateOption{
First: first,
After: after,
Last: last,
Before: before,
Table: "todos",
PrimaryKey: "id",
})
}
// CreateTodo is the resolver for the createTodo field.
func (r *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (*model.Todo, error) {
context := customContext.GetContext(ctx)
db := context.Database
user := &model.User{
ID: input.UserID,
Name: "test",
}
if err := db.Clauses(clause.OnConflict{DoNothing: true}).Create(user).Error; err != nil {
return nil, err
}
todo := &model.Todo{
Text: input.Text,
UserID: &input.UserID,
}
err := db.Create(todo).Error
return todo, err
}
// Mutation returns MutationResolver implementation.
func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} }
// Query returns QueryResolver implementation.
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }
type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }