Skip to content

Commit a7c50d4

Browse files
author
Sean Sorrell
committed
compile w/ generics
1 parent ddc1b7a commit a7c50d4

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

graphql.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type Schema struct {
8383
useStringDescriptions bool
8484
disableIntrospection bool
8585
subscribeResolverTimeout time.Duration
86-
visitors map[string]types.DirectiveVisitor
86+
visitors map[string]types.DirectiveVisitor[any,any]
8787
}
8888

8989
func (s *Schema) ASTSchema() *types.Schema {
@@ -172,7 +172,7 @@ func SubscribeResolverTimeout(timeout time.Duration) SchemaOpt {
172172

173173
// DirectiveVisitors allows to pass custom directive visitors that will be able to handle
174174
// your GraphQL schema directives.
175-
func DirectiveVisitors(visitors map[string]types.DirectiveVisitor) SchemaOpt {
175+
func DirectiveVisitors(visitors map[string]types.DirectiveVisitor[any,any]) SchemaOpt {
176176
return func(s *Schema) {
177177
s.visitors = visitors
178178
}

graphql_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,20 @@ type customDirectiveVisitor struct {
5757
beforeWasCalled bool
5858
}
5959

60-
func (v *customDirectiveVisitor) Before(ctx context.Context, directive *types.Directive, input interface{}) error {
60+
func (v *customDirectiveVisitor) Before(ctx context.Context, directive *types.Directive, input string) error {
6161
v.beforeWasCalled = true
6262
return nil
6363
}
6464

65-
func (v *customDirectiveVisitor) After(ctx context.Context, directive *types.Directive, output interface{}) (interface{}, error) {
65+
func (v *customDirectiveVisitor) After(ctx context.Context, directive *types.Directive, output string) (string, error) {
6666
if v.beforeWasCalled == false {
67-
return nil, errors.New("Before directive visitor method wasn't called.")
67+
return "", errors.New("Before directive visitor method wasn't called.")
6868
}
6969

7070
if value, ok := directive.Arguments.Get("customAttribute"); ok {
71-
return fmt.Sprintf("Directive '%s' (with arg '%s') modified result: %s", directive.Name.Name, value.String(), output.(string)), nil
71+
return fmt.Sprintf("Directive '%s' (with arg '%s') modified result: %s", directive.Name.Name, value.String(), output), nil
7272
}
73-
return fmt.Sprintf("Directive '%s' modified result: %s", directive.Name.Name, output.(string)), nil
73+
return fmt.Sprintf("Directive '%s' modified result: %s", directive.Name.Name, output), nil
7474
}
7575

7676
type theNumberResolver struct {
@@ -286,7 +286,7 @@ func TestCustomDirective(t *testing.T) {
286286
hello_html: String! @customDirective
287287
}
288288
`, &helloSnakeResolver1{},
289-
graphql.DirectiveVisitors(map[string]types.DirectiveVisitor{
289+
graphql.DirectiveVisitors(map[string]types.DirectiveVisitor[any,any]{
290290
"customDirective": &customDirectiveVisitor{},
291291
})),
292292
Query: `
@@ -314,7 +314,7 @@ func TestCustomDirective(t *testing.T) {
314314
say_hello(full_name: String!): String! @customDirective(customAttribute: hi)
315315
}
316316
`, &helloSnakeResolver1{},
317-
graphql.DirectiveVisitors(map[string]types.DirectiveVisitor{
317+
graphql.DirectiveVisitors(map[string]types.DirectiveVisitor[any,any]{
318318
"customDirective": &customDirectiveVisitor{},
319319
})),
320320
Query: `
@@ -335,7 +335,7 @@ func TestCustomDirectiveStructFieldResolver(t *testing.T) {
335335
t.Parallel()
336336

337337
schemaOpt := []graphql.SchemaOpt{
338-
graphql.DirectiveVisitors(map[string]types.DirectiveVisitor{
338+
graphql.DirectiveVisitors(map[string]types.DirectiveVisitor[any,any]{
339339
"customDirective": &customDirectiveVisitor{},
340340
}),
341341
graphql.UseFieldResolvers(),

internal/exec/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Request struct {
2525
Logger log.Logger
2626
PanicHandler errors.PanicHandler
2727
SubscribeResolverTimeout time.Duration
28-
Visitors map[string]types.DirectiveVisitor
28+
Visitors map[string]types.DirectiveVisitor[any,any]
2929
}
3030

3131
func (r *Request) handlePanic(ctx context.Context) {

types/directive.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ type DirectiveDefinition struct {
2828

2929
type DirectiveList []*Directive
3030

31-
type DirectiveVisitor interface {
32-
Before(ctx context.Context, directive *Directive, input interface{}) error
33-
After(ctx context.Context, directive *Directive, output interface{}) (interface{}, error)
31+
type DirectiveVisitor[In any, Out any] interface {
32+
Before(ctx context.Context, directive *Directive, input In) error
33+
After(ctx context.Context, directive *Directive, output Out) (Out, error)
3434
}
3535

3636
// Returns the Directive in the DirectiveList by name or nil if not found.

0 commit comments

Comments
 (0)