Skip to content

Commit

Permalink
remove all references to deprecated handler package
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Nov 11, 2019
1 parent dc62234 commit e5cc6ed
Show file tree
Hide file tree
Showing 48 changed files with 319 additions and 298 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ linters:
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unused
- varcheck
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/urfave/cli"

// Required since otherwise dep will prune away these unused packages before codegen has a chance to run
_ "github.com/99designs/gqlgen/graphql/handler"
_ "github.com/99designs/gqlgen/handler"
)

Expand Down
11 changes: 8 additions & 3 deletions codegen/testserver/complexity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
"testing"

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/handler"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/stretchr/testify/require"
)

func TestComplexityCollisions(t *testing.T) {
resolvers := &Stub{}

c := client.New(handler.GraphQL(NewExecutableSchema(Config{Resolvers: resolvers})))
srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))

c := client.New(srv)

resolvers.QueryResolver.Overlapping = func(ctx context.Context) (fields *OverlappingFields, e error) {
return &OverlappingFields{
Expand Down Expand Up @@ -48,7 +51,9 @@ func TestComplexityFuncs(t *testing.T) {
cfg.Complexity.OverlappingFields.Foo = func(childComplexity int) int { return 1000 }
cfg.Complexity.OverlappingFields.NewFoo = func(childComplexity int) int { return 5 }

c := client.New(handler.GraphQL(NewExecutableSchema(cfg), handler.ComplexityLimit(10)))
srv := handler.NewDefaultServer(NewExecutableSchema(cfg))
srv.Use(extension.FixedComplexityLimit(10))
c := client.New(srv)

resolvers.QueryResolver.Overlapping = func(ctx context.Context) (fields *OverlappingFields, e error) {
return &OverlappingFields{
Expand Down
179 changes: 89 additions & 90 deletions codegen/testserver/directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/handler"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -85,96 +85,95 @@ func TestDirectives(t *testing.T) {
resolvers.SubscriptionResolver.DirectiveUnimplemented = func(ctx context.Context) (<-chan *string, error) {
return okchan()
}
srv := handler.NewDefaultServer(NewExecutableSchema(Config{
Resolvers: resolvers,
Directives: DirectiveRoot{
Length: func(ctx context.Context, obj interface{}, next graphql.Resolver, min int, max *int, message *string) (interface{}, error) {
e := func(msg string) error {
if message == nil {
return fmt.Errorf(msg)
}
return fmt.Errorf(*message)
}
res, err := next(ctx)
if err != nil {
return nil, err
}

s := res.(string)
if len(s) < min {
return nil, e("too short")
}
if max != nil && len(s) > *max {
return nil, e("too long")
}
return res, nil
},
Range: func(ctx context.Context, obj interface{}, next graphql.Resolver, min *int, max *int) (interface{}, error) {
res, err := next(ctx)
if err != nil {
return nil, err
}

switch res := res.(type) {
case int:
if min != nil && res < *min {
return nil, fmt.Errorf("too small")
}
if max != nil && res > *max {
return nil, fmt.Errorf("too large")
}
return next(ctx)

case int64:
if min != nil && int(res) < *min {
return nil, fmt.Errorf("too small")
}
if max != nil && int(res) > *max {
return nil, fmt.Errorf("too large")
}
return next(ctx)

case *int:
if min != nil && *res < *min {
return nil, fmt.Errorf("too small")
}
if max != nil && *res > *max {
return nil, fmt.Errorf("too large")
}
return next(ctx)
}
return nil, fmt.Errorf("unsupported type %T", res)
},
Custom: func(ctx context.Context, obj interface{}, next graphql.Resolver) (interface{}, error) {
return next(ctx)
},
Logged: func(ctx context.Context, obj interface{}, next graphql.Resolver, id string) (interface{}, error) {
return next(context.WithValue(ctx, "request_id", &id))
},
ToNull: func(ctx context.Context, obj interface{}, next graphql.Resolver) (interface{}, error) {
return nil, nil
},
Directive1: func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
return next(ctx)
},
Directive2: func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
return next(ctx)
},
Unimplemented: nil,
},
}))

srv.AroundFields(func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
path, _ := ctx.Value("path").([]int)
return next(context.WithValue(ctx, "path", append(path, 1)))
})

srv.AroundFields(func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
path, _ := ctx.Value("path").([]int)
return next(context.WithValue(ctx, "path", append(path, 2)))
})

srv :=
handler.GraphQL(
NewExecutableSchema(Config{
Resolvers: resolvers,
Directives: DirectiveRoot{
Length: func(ctx context.Context, obj interface{}, next graphql.Resolver, min int, max *int, message *string) (interface{}, error) {
e := func(msg string) error {
if message == nil {
return fmt.Errorf(msg)
}
return fmt.Errorf(*message)
}
res, err := next(ctx)
if err != nil {
return nil, err
}

s := res.(string)
if len(s) < min {
return nil, e("too short")
}
if max != nil && len(s) > *max {
return nil, e("too long")
}
return res, nil
},
Range: func(ctx context.Context, obj interface{}, next graphql.Resolver, min *int, max *int) (interface{}, error) {
res, err := next(ctx)
if err != nil {
return nil, err
}

switch res := res.(type) {
case int:
if min != nil && res < *min {
return nil, fmt.Errorf("too small")
}
if max != nil && res > *max {
return nil, fmt.Errorf("too large")
}
return next(ctx)

case int64:
if min != nil && int(res) < *min {
return nil, fmt.Errorf("too small")
}
if max != nil && int(res) > *max {
return nil, fmt.Errorf("too large")
}
return next(ctx)

case *int:
if min != nil && *res < *min {
return nil, fmt.Errorf("too small")
}
if max != nil && *res > *max {
return nil, fmt.Errorf("too large")
}
return next(ctx)
}
return nil, fmt.Errorf("unsupported type %T", res)
},
Custom: func(ctx context.Context, obj interface{}, next graphql.Resolver) (interface{}, error) {
return next(ctx)
},
Logged: func(ctx context.Context, obj interface{}, next graphql.Resolver, id string) (interface{}, error) {
return next(context.WithValue(ctx, "request_id", &id))
},
ToNull: func(ctx context.Context, obj interface{}, next graphql.Resolver) (interface{}, error) {
return nil, nil
},
Directive1: func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
return next(ctx)
},
Directive2: func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
return next(ctx)
},
Unimplemented: nil,
},
}),
handler.ResolverMiddleware(func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
path, _ := ctx.Value("path").([]int)
return next(context.WithValue(ctx, "path", append(path, 1)))
}),
handler.ResolverMiddleware(func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
path, _ := ctx.Value("path").([]int)
return next(context.WithValue(ctx, "path", append(path, 2)))
}),
)
c := client.New(srv)

t.Run("arg directives", func(t *testing.T) {
Expand Down
12 changes: 3 additions & 9 deletions codegen/testserver/generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@ package testserver

import (
"context"
"net/http"
"reflect"
"testing"

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/handler"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/stretchr/testify/require"
)

func TestGeneratedResolversAreValid(t *testing.T) {
http.Handle("/query", handler.GraphQL(NewExecutableSchema(Config{
Resolvers: &Resolver{},
})))
}

func TestForcedResolverFieldIsPointer(t *testing.T) {
field, ok := reflect.TypeOf((*ForcedResolverResolver)(nil)).Elem().MethodByName("Field")
require.True(t, ok)
Expand All @@ -39,7 +32,8 @@ func TestUnionFragments(t *testing.T) {
return &Circle{Radius: 32}, nil
}

c := client.New(handler.GraphQL(NewExecutableSchema(Config{Resolvers: resolvers})))
srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))
c := client.New(srv)

t.Run("inline fragment on union", func(t *testing.T) {
var resp struct {
Expand Down
6 changes: 3 additions & 3 deletions codegen/testserver/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"testing"

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/handler"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/stretchr/testify/require"
)

func TestInput(t *testing.T) {
resolvers := &Stub{}

c := client.New(handler.GraphQL(NewExecutableSchema(Config{Resolvers: resolvers})))
srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))
c := client.New(srv)

t.Run("when function errors on directives", func(t *testing.T) {
resolvers.QueryResolver.InputSlice = func(ctx context.Context, arg []string) (b bool, e error) {
Expand Down
4 changes: 2 additions & 2 deletions codegen/testserver/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/handler"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/stretchr/testify/require"
)

Expand All @@ -24,7 +24,7 @@ func TestInterfaces(t *testing.T) {
return nil, nil
}

srv := handler.GraphQL(
srv := handler.NewDefaultServer(
NewExecutableSchema(Config{
Resolvers: resolvers,
Directives: DirectiveRoot{
Expand Down
28 changes: 13 additions & 15 deletions codegen/testserver/introspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import (

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/introspection"
"github.com/99designs/gqlgen/handler"
"github.com/stretchr/testify/require"
)

func TestIntrospection(t *testing.T) {
t.Run("disabled", func(t *testing.T) {
t.Run("disabled when creating your own server", func(t *testing.T) {
resolvers := &Stub{}

c := client.New(handler.GraphQL(
NewExecutableSchema(Config{Resolvers: resolvers}),
handler.IntrospectionEnabled(false),
))
srv := handler.New(NewExecutableSchema(Config{Resolvers: resolvers}))
srv.AddTransport(transport.POST{})
c := client.New(srv)

var resp interface{}
err := c.Post(introspection.Query, &resp)
Expand All @@ -28,7 +28,7 @@ func TestIntrospection(t *testing.T) {
t.Run("enabled by default", func(t *testing.T) {
resolvers := &Stub{}

c := client.New(handler.GraphQL(
c := client.New(handler.NewDefaultServer(
NewExecutableSchema(Config{Resolvers: resolvers}),
))

Expand Down Expand Up @@ -65,14 +65,12 @@ func TestIntrospection(t *testing.T) {
t.Run("disabled by middleware", func(t *testing.T) {
resolvers := &Stub{}

c := client.New(handler.GraphQL(
NewExecutableSchema(Config{Resolvers: resolvers}),
handler.RequestMiddleware(func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
graphql.GetOperationContext(ctx).DisableIntrospection = true

return next(ctx)
}),
))
srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))
srv.AroundOperations(func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
graphql.GetOperationContext(ctx).DisableIntrospection = true
return next(ctx)
})
c := client.New(srv)

var resp interface{}
err := c.Post(introspection.Query, &resp)
Expand Down
4 changes: 2 additions & 2 deletions codegen/testserver/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/handler"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/stretchr/testify/require"
)

Expand All @@ -21,7 +21,7 @@ func TestMaps(t *testing.T) {
return in.Map, nil
}

c := client.New(handler.GraphQL(
c := client.New(handler.NewDefaultServer(
NewExecutableSchema(Config{Resolvers: resolver}),
))
t.Run("unset", func(t *testing.T) {
Expand Down
Loading

0 comments on commit e5cc6ed

Please sign in to comment.