Skip to content

Commit

Permalink
Merge pull request #768 from 99designs/fix-ptr-from-directive
Browse files Browse the repository at this point in the history
Fix pointer returns from directive
  • Loading branch information
vektah committed Jul 8, 2019
2 parents 414a4d3 + 6f3d731 commit bf2cc90
Show file tree
Hide file tree
Showing 9 changed files with 340 additions and 12 deletions.
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

0 comments on commit bf2cc90

Please sign in to comment.