2022-08-07 11:55:49 -06:00
|
|
|
package gql_ev
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-19 12:26:42 -06:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
2022-08-07 11:55:49 -06:00
|
|
|
|
2022-08-19 12:26:42 -06:00
|
|
|
"github.com/sour-is/ev/app/msgbus"
|
|
|
|
"github.com/sour-is/ev/app/salty"
|
|
|
|
"github.com/sour-is/ev/internal/graph/generated"
|
2022-08-13 13:34:13 -06:00
|
|
|
"github.com/sour-is/ev/internal/logz"
|
2022-08-07 11:55:49 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type Resolver struct {
|
2022-08-19 12:26:42 -06:00
|
|
|
msgbus.MsgbusResolver
|
|
|
|
salty.SaltyResolver
|
2022-08-07 11:55:49 -06:00
|
|
|
}
|
|
|
|
|
2022-08-19 12:26:42 -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()
|
|
|
|
|
2022-08-19 12:26:42 -06:00
|
|
|
r := &Resolver{m, s}
|
2022-08-13 13:34:13 -06:00
|
|
|
|
2022-08-19 12:26:42 -06:00
|
|
|
return r, nil
|
2022-08-07 11:55:49 -06:00
|
|
|
}
|
|
|
|
|
2022-08-19 12:26:42 -06:00
|
|
|
// Query returns generated.QueryResolver implementation.
|
|
|
|
func (r *Resolver) Query() generated.QueryResolver { return r }
|
2022-08-13 13:34:13 -06:00
|
|
|
|
2022-08-19 12:26:42 -06:00
|
|
|
// Query returns generated.QueryResolver implementation.
|
|
|
|
func (r *Resolver) Mutation() generated.MutationResolver { return r }
|
2022-08-07 11:55:49 -06:00
|
|
|
|
2022-08-19 12:26:42 -06:00
|
|
|
// Subscription returns generated.SubscriptionResolver implementation.
|
|
|
|
func (r *Resolver) Subscription() generated.SubscriptionResolver { return r }
|
2022-08-07 11:55:49 -06:00
|
|
|
|
2022-08-19 12:26:42 -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
|
|
|
|
}
|
2022-08-19 12:26:42 -06:00
|
|
|
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-09 16:23:33 -06:00
|
|
|
}
|
2022-08-14 10:04:15 -06:00
|
|
|
}
|
|
|
|
|
2022-08-19 12:26:42 -06:00
|
|
|
return h
|
2022-08-14 10:04:15 -06:00
|
|
|
}
|