Skip to content

Commit

Permalink
refactor unimplemented directive handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Aug 17, 2019
1 parent 5c644a6 commit 1f7ed0d
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 0 deletions.
12 changes: 12 additions & 0 deletions codegen/directives.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
}
{{- end }}
{{- end }}
if ec.directives.{{$directive.Name|ucFirst}} == nil {
return nil, errors.New("directive {{$directive.Name}} is not implemented")
}
return ec.directives.{{$directive.Name|ucFirst}}({{$directive.ResolveArgs $in $i }})
}
{{ end -}}
Expand All @@ -34,6 +37,9 @@
{{- end }}
n := next
next = func(ctx context.Context) (interface{}, error) {
if ec.directives.{{$directive.Name|ucFirst}} == nil {
return nil, errors.New("directive {{$directive.Name}} is not implemented")
}
return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})
}
{{- end }}
Expand Down Expand Up @@ -81,6 +87,9 @@ func (ec *executionContext) _subscriptionMiddleware(ctx context.Context, obj *as
{{- end }}
n := next
next = func(ctx context.Context) (interface{}, error) {
if ec.directives.{{$directive.Name|ucFirst}} == nil {
return nil, errors.New("directive {{$directive.Name}} is not implemented")
}
return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})
}
{{- end }}
Expand Down Expand Up @@ -121,6 +130,9 @@ func (ec *executionContext) _subscriptionMiddleware(ctx context.Context, obj *as
{{- end }}
n := next
next = func(ctx context.Context) (interface{}, error) {
if ec.directives.{{$directive.Name|ucFirst}} == nil {
return nil, errors.New("directive {{$directive.Name}} is not implemented")
}
return ec.directives.{{$directive.Name|ucFirst}}({{$directive.CallArgs}})
}
{{- end }}
Expand Down
2 changes: 2 additions & 0 deletions codegen/testserver/directive.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ directive @logged(id: UUID!) on FIELD
directive @toNull on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | FIELD_DEFINITION
directive @directive1 on FIELD_DEFINITION
directive @directive2 on FIELD_DEFINITION
directive @unimplemented on FIELD_DEFINITION

extend type Query {
directiveArg(arg: String! @length(min:1, max: 255, message: "invalid length")): String
Expand All @@ -16,6 +17,7 @@ extend type Query {
directiveFieldDef(ret: String!): String! @length(min: 1, message: "not valid")
directiveField: String
directiveDouble: String @directive1 @directive2
directiveUnimplemented: String @unimplemented
}

input InputDirectives {
Expand Down
16 changes: 16 additions & 0 deletions codegen/testserver/directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func TestDirectives(t *testing.T) {
return &s, nil
}

resolvers.QueryResolver.DirectiveUnimplemented = func(ctx context.Context) (*string, error) {
s := "Ok"
return &s, nil
}

srv := httptest.NewServer(
handler.GraphQL(
NewExecutableSchema(Config{
Expand Down Expand Up @@ -137,6 +142,7 @@ func TestDirectives(t *testing.T) {
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) {
Expand Down Expand Up @@ -227,6 +233,16 @@ func TestDirectives(t *testing.T) {
require.Equal(t, "Ok", resp.DirectiveDouble)
})

t.Run("directive is not implemented", func(t *testing.T) {
var resp struct {
DirectiveUnimplemented string
}

err := c.Post(`query { directiveUnimplemented }`, &resp)

require.EqualError(t, err, `[{"message":"directive unimplemented is not implemented","path":["directiveUnimplemented"]}]`)
})

t.Run("ok", func(t *testing.T) {
var resp struct {
DirectiveFieldDef string
Expand Down
119 changes: 119 additions & 0 deletions codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ func (r *queryResolver) DirectiveField(ctx context.Context) (*string, error) {
func (r *queryResolver) DirectiveDouble(ctx context.Context) (*string, error) {
panic("not implemented")
}
func (r *queryResolver) DirectiveUnimplemented(ctx context.Context) (*string, error) {
panic("not implemented")
}
func (r *queryResolver) MapStringInterface(ctx context.Context, in map[string]interface{}) (map[string]interface{}, error) {
panic("not implemented")
}
Expand Down
4 changes: 4 additions & 0 deletions codegen/testserver/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1f7ed0d

Please sign in to comment.