From 40f088681b169ef1249258f164b70f37ae7b66b6 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 5 Nov 2019 17:55:32 +1100 Subject: [PATCH] add NewDefaultServer --- graphql/handler/apollotracing/tracer.go | 14 ++++------ graphql/handler/apollotracing/tracer_test.go | 2 +- graphql/handler/extension/complexity_test.go | 5 ++-- graphql/handler/server.go | 27 ++++++++++++++++++++ 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/graphql/handler/apollotracing/tracer.go b/graphql/handler/apollotracing/tracer.go index 9ac9ad16031..5e6d1211edf 100644 --- a/graphql/handler/apollotracing/tracer.go +++ b/graphql/handler/apollotracing/tracer.go @@ -9,7 +9,7 @@ import ( ) type ( - ApolloTracing struct{} + Tracer struct{} TracingExtension struct { mu sync.Mutex @@ -39,14 +39,10 @@ type ( } ) -var _ graphql.ResponseInterceptor = ApolloTracing{} -var _ graphql.FieldInterceptor = ApolloTracing{} +var _ graphql.ResponseInterceptor = Tracer{} +var _ graphql.FieldInterceptor = Tracer{} -func New() graphql.HandlerExtension { - return &ApolloTracing{} -} - -func (a ApolloTracing) InterceptField(ctx context.Context, next graphql.Resolver) (res interface{}, err error) { +func (a Tracer) InterceptField(ctx context.Context, next graphql.Resolver) (res interface{}, err error) { rc := graphql.GetOperationContext(ctx) td, ok := graphql.GetExtension(ctx, "tracing").(*TracingExtension) if !ok { @@ -75,7 +71,7 @@ func (a ApolloTracing) InterceptField(ctx context.Context, next graphql.Resolver return next(ctx) } -func (a ApolloTracing) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response { +func (a Tracer) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response { rc := graphql.GetOperationContext(ctx) start := rc.Stats.OperationStart diff --git a/graphql/handler/apollotracing/tracer_test.go b/graphql/handler/apollotracing/tracer_test.go index e2e3a93ccdf..a905e7ffdd2 100644 --- a/graphql/handler/apollotracing/tracer_test.go +++ b/graphql/handler/apollotracing/tracer_test.go @@ -17,7 +17,7 @@ import ( func TestApolloTracing(t *testing.T) { h := testserver.New() h.AddTransport(transport.POST{}) - h.Use(apollotracing.New()) + h.Use(apollotracing.Tracer{}) resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }"}`) assert.Equal(t, http.StatusOK, resp.Code, resp.Body.String()) diff --git a/graphql/handler/extension/complexity_test.go b/graphql/handler/extension/complexity_test.go index 90465f4f81a..9ee1b398bd4 100644 --- a/graphql/handler/extension/complexity_test.go +++ b/graphql/handler/extension/complexity_test.go @@ -1,4 +1,4 @@ -package extension +package extension_test import ( "context" @@ -8,6 +8,7 @@ import ( "testing" "github.com/99designs/gqlgen/graphql" + "github.com/99designs/gqlgen/graphql/handler/extension" "github.com/99designs/gqlgen/graphql/handler/testserver" "github.com/99designs/gqlgen/graphql/handler/transport" "github.com/stretchr/testify/require" @@ -15,7 +16,7 @@ import ( func TestHandlerComplexity(t *testing.T) { h := testserver.New() - h.Use(ComplexityLimit(func(ctx context.Context, rc *graphql.OperationContext) int { + h.Use(extension.ComplexityLimit(func(ctx context.Context, rc *graphql.OperationContext) int { if rc.RawQuery == "{ ok: name }" { return 4 } diff --git a/graphql/handler/server.go b/graphql/handler/server.go index 9ef6e234d84..ae2680dce1b 100644 --- a/graphql/handler/server.go +++ b/graphql/handler/server.go @@ -5,8 +5,13 @@ import ( "encoding/json" "fmt" "net/http" + "time" "github.com/99designs/gqlgen/graphql" + "github.com/99designs/gqlgen/graphql/handler/apollotracing" + "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/gqlerror" ) @@ -34,6 +39,28 @@ func New(es graphql.ExecutableSchema) *Server { return s } +func NewDefaultServer(es graphql.ExecutableSchema) *Server { + srv := New(es) + + srv.AddTransport(transport.Websocket{ + KeepAlivePingInterval: 10 * time.Second, + }) + srv.AddTransport(transport.Options{}) + srv.AddTransport(transport.GET{}) + srv.AddTransport(transport.POST{}) + srv.AddTransport(transport.MultipartForm{}) + + srv.SetQueryCache(lru.New(100)) + + srv.Use(extension.Introspection{}) + srv.Use(extension.AutomaticPersistedQuery{ + Cache: lru.New(100), + }) + srv.Use(apollotracing.Tracer{}) + + return srv +} + func (s *Server) AddTransport(transport graphql.Transport) { s.transports = append(s.transports, transport) }