ev/api/gql_ev/resolver.go

56 lines
1.5 KiB
Go
Raw Normal View History

2022-08-07 11:55:49 -06:00
package gql_ev
import (
"context"
"net/http"
"reflect"
2022-08-07 11:55:49 -06:00
"github.com/sour-is/ev/app/msgbus"
"github.com/sour-is/ev/app/salty"
"github.com/sour-is/ev/internal/graph/generated"
"github.com/sour-is/ev/internal/logz"
2022-08-07 11:55:49 -06:00
)
type Resolver struct {
msgbus.MsgbusResolver
salty.SaltyResolver
2022-08-07 11:55:49 -06:00
}
func New(ctx context.Context, m msgbus.MsgbusResolver, s salty.SaltyResolver) (*Resolver, error) {
_, span := logz.Span(ctx)
2022-08-16 16:06:25 -06:00
defer span.End()
r := &Resolver{m, s}
return r, nil
2022-08-07 11:55:49 -06:00
}
// Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return r }
// Query returns generated.QueryResolver implementation.
func (r *Resolver) Mutation() generated.MutationResolver { return r }
2022-08-07 11:55:49 -06:00
// Subscription returns generated.SubscriptionResolver implementation.
func (r *Resolver) Subscription() generated.SubscriptionResolver { return r }
2022-08-07 11:55:49 -06:00
// ChainMiddlewares will check all embeded resolvers for a GetMiddleware func and add to handler.
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.
2022-08-07 11:55:49 -06:00
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.
}
2022-08-14 10:04:15 -06:00
}
return h
2022-08-14 10:04:15 -06:00
}