77 lines
2.6 KiB
Go
77 lines
2.6 KiB
Go
package resolvers
|
|
|
|
// 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.73
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"git.farahty.com/nimer/go-mongo/generated"
|
|
"git.farahty.com/nimer/go-mongo/models"
|
|
todoService "git.farahty.com/nimer/go-mongo/services/todo"
|
|
userService "git.farahty.com/nimer/go-mongo/services/user"
|
|
redis "github.com/redis/go-redis/v9"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
// CreateTodo is the resolver for the createTodo field.
|
|
func (r *mutationResolver) CreateTodo(ctx context.Context, input models.CreateTodoInput) (*models.Todo, error) {
|
|
obj, err := todoService.Create(ctx, input)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if objJson, err := json.Marshal(obj); err == nil {
|
|
if client, ok := r.Redis.(*redis.Client); ok {
|
|
client.Publish(ctx, "NEW_TODO_EVENT", objJson)
|
|
}
|
|
}
|
|
|
|
return obj, nil
|
|
}
|
|
|
|
// Todos is the resolver for the todos field.
|
|
func (r *queryResolver) Todos(ctx context.Context) ([]*models.Todo, error) {
|
|
return todoService.Find(ctx)
|
|
}
|
|
|
|
// Todo is the resolver for the todo field.
|
|
func (r *queryResolver) Todo(ctx context.Context, id primitive.ObjectID) (*models.Todo, error) {
|
|
return todoService.FindByID(ctx, id)
|
|
}
|
|
|
|
// OnTodo is the resolver for the onTodo field.
|
|
func (r *subscriptionResolver) OnTodo(ctx context.Context) (<-chan *models.Todo, error) {
|
|
if client, ok := r.Redis.(*redis.Client); ok {
|
|
return Subscribe[models.Todo](ctx, client, "NEW_TODO_EVENT")
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
// CreatedBy is the resolver for the createdBy field.
|
|
func (r *todoResolver) CreatedBy(ctx context.Context, obj *models.Todo) (*models.User, error) {
|
|
return userService.FindById(ctx, obj.CreatedByID)
|
|
}
|
|
|
|
// UpdatedBy is the resolver for the updatedBy field.
|
|
func (r *todoResolver) UpdatedBy(ctx context.Context, obj *models.Todo) (*models.User, error) {
|
|
return userService.FindById(ctx, obj.UpdatedByID)
|
|
}
|
|
|
|
// Owner is the resolver for the owner field.
|
|
func (r *todoResolver) Owner(ctx context.Context, obj *models.Todo) (*models.User, error) {
|
|
return userService.FindById(ctx, obj.OwnerID)
|
|
}
|
|
|
|
// Subscription returns generated.SubscriptionResolver implementation.
|
|
func (r *Resolver) Subscription() generated.SubscriptionResolver { return &subscriptionResolver{r} }
|
|
|
|
// Todo returns generated.TodoResolver implementation.
|
|
func (r *Resolver) Todo() generated.TodoResolver { return &todoResolver{r} }
|
|
|
|
type subscriptionResolver struct{ *Resolver }
|
|
type todoResolver struct{ *Resolver }
|