ev/internal/graph/resolver.go

56 lines
1.5 KiB
Go
Raw Normal View History

2022-08-07 11:55:49 -06:00
package graph
import (
"net/http"
"reflect"
"github.com/sour-is/ev/api/gql_ev"
"github.com/sour-is/ev/internal/graph/generated"
)
// This file will not be regenerated automatically.
//
// It serves as dependency injection for your app, add any dependencies you require here.
type Resolver struct {
*gql_ev.Resolver
}
func New(r *gql_ev.Resolver) *Resolver {
return &Resolver{r}
}
// Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }
2022-08-14 10:04:15 -06:00
// Query returns generated.QueryResolver implementation.
func (r *Resolver) Mutation() generated.MutationResolver { return &mutationResolver{r} }
2022-08-07 11:55:49 -06:00
// Subscription returns generated.SubscriptionResolver implementation.
func (r *Resolver) Subscription() generated.SubscriptionResolver { return &subscriptionResolver{r} }
2022-08-07 11:55:49 -06:00
type queryResolver struct{ *Resolver }
2022-08-14 10:04:15 -06:00
type mutationResolver struct{ *Resolver }
type subscriptionResolver struct{ *Resolver }
2022-08-07 11:55:49 -06:00
func (r *Resolver) ChainMiddlewares(h http.Handler) http.Handler {
v := reflect.ValueOf(r) // Get reflected value of *Resolver
v = reflect.Indirect(v) // Get the pointed value (returns a zero value on nil)
n := v.NumField() // Get number of fields to iterate over.
for i := 0; i < n; i++ {
f := v.Field(i)
if !f.CanInterface() { // Skip non-interface types.
continue
}
if iface, ok := f.Interface().(interface {
GetMiddleware() func(http.Handler) http.Handler
}); ok {
h = iface.GetMiddleware()(h) // Append only items that fulfill the interface.
}
}
return h
}