Skip to content

Commit

Permalink
add example and test field directive
Browse files Browse the repository at this point in the history
  • Loading branch information
asamusev committed Jun 25, 2019
1 parent 526beec commit a58ecfe
Show file tree
Hide file tree
Showing 8 changed files with 566 additions and 912 deletions.
33 changes: 32 additions & 1 deletion codegen/testserver/directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ func TestDirectives(t *testing.T) {
return &s, nil
}

resolvers.QueryResolver.DirectiveField = func(ctx context.Context) (*string, error) {
if s, ok := ctx.Value("request_id").(*string); ok {
return s, nil
}

return nil, nil
}

srv := httptest.NewServer(
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{
if message == nil {
return fmt.Errorf(msg)
}
return fmt.Errorf(*message)
Expand Down Expand Up @@ -104,6 +112,9 @@ func TestDirectives(t *testing.T) {
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))
},
},
}),
handler.ResolverMiddleware(func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
Expand Down Expand Up @@ -169,6 +180,26 @@ func TestDirectives(t *testing.T) {
require.Equal(t, "Ok", *resp.DirectiveArg)
})
})
t.Run("field directives", func(t *testing.T) {
t.Run("add field directive", func(t *testing.T) {
var resp struct {
DirectiveField string
}

c.MustPost(`query { directiveField@logged(id:"testes_id") }`, &resp)

require.Equal(t, resp.DirectiveField, `testes_id`)
})
t.Run("without field directive", func(t *testing.T) {
var resp struct {
DirectiveField *string
}

c.MustPost(`query { directiveField }`, &resp)

require.Nil(t, resp.DirectiveField)
})
})
t.Run("input field directives", func(t *testing.T) {
t.Run("when function errors on directives", func(t *testing.T) {
var resp struct {
Expand Down
Loading

0 comments on commit a58ecfe

Please sign in to comment.