2022-11-23 13:51:55 -07:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"runtime/debug"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/99designs/gqlgen/graphql"
|
|
|
|
"github.com/99designs/gqlgen/graphql/handler"
|
|
|
|
"github.com/99designs/gqlgen/graphql/handler/extension"
|
|
|
|
"github.com/99designs/gqlgen/graphql/handler/lru"
|
|
|
|
"github.com/99designs/gqlgen/graphql/handler/transport"
|
|
|
|
"github.com/vektah/gqlparser/v2/gqlerror"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/ravilushqa/otelgqlgen"
|
|
|
|
|
2023-02-26 22:33:01 -07:00
|
|
|
"go.sour.is/ev/internal/lg"
|
|
|
|
"go.sour.is/ev/pkg/gql/graphiql"
|
|
|
|
"go.sour.is/ev/pkg/gql/playground"
|
2022-11-23 13:51:55 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type BaseResolver interface {
|
|
|
|
ExecutableSchema() graphql.ExecutableSchema
|
|
|
|
BaseResolver() IsResolver
|
|
|
|
}
|
|
|
|
|
|
|
|
type Resolver[T BaseResolver] struct {
|
2023-01-09 13:09:58 -07:00
|
|
|
res T
|
|
|
|
CheckOrigin func(r *http.Request) bool
|
2022-11-23 13:51:55 -07:00
|
|
|
}
|
|
|
|
type IsResolver interface {
|
|
|
|
IsResolver()
|
|
|
|
}
|
|
|
|
|
2023-01-09 13:09:58 -07:00
|
|
|
var defaultCheckOrign = func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
func New[T BaseResolver](ctx context.Context, base T, resolvers ...IsResolver) (*Resolver[T], error) {
|
|
|
|
_, span := lg.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
2023-01-09 13:09:58 -07:00
|
|
|
noop := reflect.ValueOf(base.BaseResolver())
|
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
v := reflect.ValueOf(base)
|
|
|
|
v = reflect.Indirect(v)
|
|
|
|
|
|
|
|
outer:
|
|
|
|
for _, idx := range reflect.VisibleFields(v.Type()) {
|
|
|
|
field := v.FieldByIndex(idx.Index)
|
|
|
|
|
|
|
|
for i := range resolvers {
|
|
|
|
rs := reflect.ValueOf(resolvers[i])
|
|
|
|
|
|
|
|
if field.IsNil() && rs.Type().Implements(field.Type()) {
|
2023-01-09 11:30:02 -07:00
|
|
|
// log.Print("found ", field.Type().Name())
|
2022-11-23 13:51:55 -07:00
|
|
|
span.AddEvent(fmt.Sprint("found ", field.Type().Name()))
|
|
|
|
field.Set(rs)
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 11:30:02 -07:00
|
|
|
// log.Print(fmt.Sprint("default ", field.Type().Name()))
|
2022-11-23 13:51:55 -07:00
|
|
|
span.AddEvent(fmt.Sprint("default ", field.Type().Name()))
|
|
|
|
field.Set(noop)
|
|
|
|
}
|
|
|
|
|
2023-01-09 13:09:58 -07:00
|
|
|
return &Resolver[T]{res: base, CheckOrigin: defaultCheckOrign}, nil
|
2022-11-23 13:51:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver[T]) Resolver() T {
|
|
|
|
return r.res
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChainMiddlewares will check all embeded resolvers for a GetMiddleware func and add to handler.
|
|
|
|
func (r *Resolver[T]) ChainMiddlewares(h http.Handler) http.Handler {
|
2023-01-09 13:09:58 -07:00
|
|
|
v := reflect.ValueOf(r.Resolver()) // Get reflected value of *Resolver
|
|
|
|
v = reflect.Indirect(v) // Get the pointed value (returns a zero value on nil)
|
|
|
|
for _, idx := range reflect.VisibleFields(v.Type()) {
|
|
|
|
field := v.FieldByIndex(idx.Index)
|
|
|
|
// log.Print("middleware ", field.Type().Name())
|
|
|
|
|
|
|
|
if !field.CanInterface() { // Skip non-interface types.
|
2022-11-23 13:51:55 -07:00
|
|
|
continue
|
|
|
|
}
|
2023-01-09 13:09:58 -07:00
|
|
|
if iface, ok := field.Interface().(interface {
|
2022-11-23 13:51:55 -07:00
|
|
|
GetMiddleware() func(http.Handler) http.Handler
|
|
|
|
}); ok {
|
|
|
|
h = iface.GetMiddleware()(h) // Append only items that fulfill the interface.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver[T]) RegisterHTTP(mux *http.ServeMux) {
|
2023-01-09 13:09:58 -07:00
|
|
|
gql := NewServer(r.Resolver().ExecutableSchema(), r.CheckOrigin)
|
2022-11-23 13:51:55 -07:00
|
|
|
gql.SetRecoverFunc(NoopRecover)
|
|
|
|
gql.Use(otelgqlgen.Middleware())
|
|
|
|
mux.Handle("/gql", lg.Htrace(r.ChainMiddlewares(gql), "gql"))
|
2023-01-09 13:09:58 -07:00
|
|
|
mux.Handle("/graphiql", graphiql.Handler("GraphiQL playground", "/gql"))
|
2022-11-23 13:51:55 -07:00
|
|
|
mux.Handle("/playground", playground.Handler("GraphQL playground", "/gql"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func NoopRecover(ctx context.Context, err interface{}) error {
|
|
|
|
if err, ok := err.(string); ok && err == "not implemented" {
|
|
|
|
return gqlerror.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
fmt.Fprintln(os.Stderr)
|
|
|
|
debug.PrintStack()
|
|
|
|
|
|
|
|
return gqlerror.Errorf("internal system error")
|
|
|
|
}
|
|
|
|
|
2023-01-09 13:09:58 -07:00
|
|
|
func NewServer(es graphql.ExecutableSchema, checkOrigin func(*http.Request) bool) *handler.Server {
|
2022-11-23 13:51:55 -07:00
|
|
|
srv := handler.New(es)
|
|
|
|
|
|
|
|
srv.AddTransport(transport.Websocket{
|
|
|
|
Upgrader: websocket.Upgrader{
|
2023-01-09 13:09:58 -07:00
|
|
|
CheckOrigin: checkOrigin,
|
2022-11-23 13:51:55 -07:00
|
|
|
},
|
|
|
|
KeepAlivePingInterval: 10 * time.Second,
|
|
|
|
})
|
|
|
|
srv.AddTransport(transport.Options{})
|
|
|
|
srv.AddTransport(transport.GET{})
|
|
|
|
srv.AddTransport(transport.POST{})
|
|
|
|
srv.AddTransport(transport.MultipartForm{})
|
|
|
|
|
|
|
|
srv.SetQueryCache(lru.New(1000))
|
|
|
|
|
|
|
|
srv.Use(extension.Introspection{})
|
|
|
|
srv.Use(extension.AutomaticPersistedQuery{
|
|
|
|
Cache: lru.New(100),
|
|
|
|
})
|
|
|
|
|
|
|
|
return srv
|
|
|
|
}
|