Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pointer returns from directive #768

Merged
merged 1 commit into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions codegen/args.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func (ec *executionContext) {{ $name }}(ctx context.Context, rawArgs map[string]
}
if data, ok := tmp.({{ $arg.TypeReference.GO | ref }}) ; ok {
arg{{$i}} = data
{{- if $arg.TypeReference.IsNilable }}
} else if tmp == nil {
arg{{$i}} = nil
{{- end }}
} else {
return nil, fmt.Errorf(`unexpected type %T from directive, should be {{ $arg.TypeReference.GO }}`, tmp)
}
Expand Down
5 changes: 5 additions & 0 deletions codegen/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
if data, ok := tmp.({{ .TypeReference.GO | ref }}) ; ok {
return data, nil
}
{{- if .TypeReference.IsNilable -}}
else if tmp == nil {
return nil, nil
}
{{- end }}
return nil, fmt.Errorf(`unexpected type %T from directive, should be {{ .TypeReference.GO }}`, tmp)
{{- else -}}
ctx = rctx // use context from middleware stack in children
Expand Down
4 changes: 4 additions & 0 deletions codegen/input.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
}
if data, ok := tmp.({{ $field.TypeReference.GO | ref }}) ; ok {
it.{{$field.GoFieldName}} = data
{{- if $field.TypeReference.IsNilable }}
} else if tmp == nil {
it.{{$field.GoFieldName}} = nil
{{- end }}
} else {
return it, fmt.Errorf(`unexpected type %T from directive, should be {{ $field.TypeReference.GO }}`, tmp)
}
Expand Down
10 changes: 9 additions & 1 deletion codegen/testserver/directive.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ directive @length(min: Int!, max: Int, message: String) on ARGUMENT_DEFINITION |
directive @range(min: Int = 0, max: Int) on ARGUMENT_DEFINITION
directive @custom on ARGUMENT_DEFINITION
directive @logged(id: UUID!) on FIELD
directive @toNull on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION | FIELD_DEFINITION

extend type Query {
directiveArg(arg: String! @length(min:1, max: 255, message: "invalid length")): String
directiveNullableArg(arg: Int @range(min:0), arg2: Int @range): String
directiveNullableArg(arg: Int @range(min:0), arg2: Int @range, arg3: String @toNull): String
directiveInputNullable(arg: InputDirectives): String
directiveInput(arg: InputDirectives!): String
directiveInputType(arg: InnerInput! @custom): String
directiveObject: ObjectDirectives
directiveFieldDef(ret: String!): String! @length(min: 1, message: "not valid")
directiveField: String
}

input InputDirectives {
text: String! @length(min: 0, max: 7, message: "not valid")
nullableText: String @toNull
inner: InnerDirectives!
innerNullable: InnerDirectives
thirdParty: ThirdParty @length(min: 0, max: 7)
Expand All @@ -23,3 +26,8 @@ input InputDirectives {
input InnerDirectives {
message: String! @length(min: 1, message: "not valid")
}

type ObjectDirectives {
text: String! @length(min: 0, max: 7, message: "not valid")
nullableText: String @toNull
}
31 changes: 29 additions & 2 deletions codegen/testserver/directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestDirectives(t *testing.T) {
return &s, nil
}

resolvers.QueryResolver.DirectiveNullableArg = func(ctx context.Context, arg *int, arg2 *int) (i *string, e error) {
resolvers.QueryResolver.DirectiveNullableArg = func(ctx context.Context, arg *int, arg2 *int, arg3 *string) (*string, error) {
s := "Ok"
return &s, nil
}
Expand All @@ -39,6 +39,14 @@ func TestDirectives(t *testing.T) {
return &s, nil
}

resolvers.QueryResolver.DirectiveObject = func(ctx context.Context) (*ObjectDirectives, error) {
s := "Ok"
return &ObjectDirectives{
Text: s,
NullableText: &s,
}, nil
}

resolvers.QueryResolver.DirectiveField = func(ctx context.Context) (*string, error) {
if s, ok := ctx.Value("request_id").(*string); ok {
return s, nil
Expand Down Expand Up @@ -115,6 +123,9 @@ func TestDirectives(t *testing.T) {
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
},
},
}),
handler.ResolverMiddleware(func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) {
Expand Down Expand Up @@ -271,7 +282,7 @@ func TestDirectives(t *testing.T) {
DirectiveInputNullable *string
}

err := c.Post(`query { directiveInputNullable(arg: {text:"23",inner:{message:"1"},innerNullable:{message:"success"}}) }`, &resp)
err := c.Post(`query { directiveInputNullable(arg: {text:"23",nullableText:"23",inner:{message:"1"},innerNullable:{message:"success"}}) }`, &resp)

require.Nil(t, err)
require.Equal(t, "Ok", *resp.DirectiveInputNullable)
Expand All @@ -287,4 +298,20 @@ func TestDirectives(t *testing.T) {
require.Equal(t, "Ok", *resp.DirectiveInputType)
})
})
t.Run("object field directives", func(t *testing.T) {
t.Run("when function success", func(t *testing.T) {
var resp struct {
DirectiveObject *struct {
Text string
NullableText *string
}
}

err := c.Post(`query { directiveObject{ text nullableText } }`, &resp)

require.Nil(t, err)
require.Equal(t, "Ok", resp.DirectiveObject.Text)
require.True(t, resp.DirectiveObject.NullableText == nil)
})
})
}
Loading