package graphql

import (
	"context"
	"fmt"

	"github.com/99designs/gqlgen/graphql"
	"github.com/go-playground/validator/v10"
)

var validate = validator.New()

func Binding(ctx context.Context, obj interface{}, next graphql.Resolver, constraint string) (interface{}, error) {
	val, err := next(ctx)
	if err != nil {
		return nil, err
	}

	if err := validate.Var(val, constraint); err != nil {
		return nil, fmt.Errorf("invalid input: %w", err)
	}

	return val, nil
}
