package deferexample

// 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.70-dev

import (
	"context"
	"crypto/rand"
	"fmt"
	"math/big"
	"time"
)

// CreateTodo is the resolver for the createTodo field.
func (r *mutationResolver) CreateTodo(ctx context.Context, input NewTodo) (*Todo, error) {
	randNumber, _ := rand.Int(rand.Reader, big.NewInt(100))
	todo := &Todo{
		Text:   input.Text,
		ID:     fmt.Sprintf("T%d", randNumber),
		userID: input.UserID,
	}

	r.mu.Lock()
	r.todos = append(r.todos, todo)
	r.mu.Unlock()

	return todo, nil
}

// Todos is the resolver for the todos field.
func (r *queryResolver) Todos(ctx context.Context) ([]*Todo, error) {
	r.mu.RLock()
	defer r.mu.RUnlock()
	return r.todos, nil
}

// User is the resolver for the user field on Todos. Having this
// separate resolver makes the field "deferrable," ie GQLGen will handle
// an operation with an @defer directive applied to the given field as a
// separately-delivered response, and depending on the logic applied in
// the transport can deliver them in the same response or serially.
func (r *todoResolver) User(ctx context.Context, t *Todo) (*User, error) {
	time.Sleep(time.Millisecond * 10)
	return &User{
		ID:   t.userID,
		Name: fmt.Sprintf("User %v", t.userID),
	}, nil
}

// Mutation returns MutationResolver implementation.
func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} }

// Query returns QueryResolver implementation.
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }

// Todo returns TodoResolver implementation.
func (r *Resolver) Todo() TodoResolver { return &todoResolver{r} }

type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
type todoResolver struct{ *Resolver }
