Skip to content

Commit

Permalink
add NewDefaultServer
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Nov 5, 2019
1 parent 1b57bc3 commit 40f0886
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
14 changes: 5 additions & 9 deletions graphql/handler/apollotracing/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type (
ApolloTracing struct{}
Tracer struct{}

TracingExtension struct {
mu sync.Mutex
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion graphql/handler/apollotracing/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
5 changes: 3 additions & 2 deletions graphql/handler/extension/complexity_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package extension
package extension_test

import (
"context"
Expand All @@ -8,14 +8,15 @@ 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"
)

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
}
Expand Down
27 changes: 27 additions & 0 deletions graphql/handler/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 40f0886

Please sign in to comment.