Skip to content

Commit

Permalink
Merge pull request #838 from 99designs/fix-directive-nil
Browse files Browse the repository at this point in the history
fix directives return nil handling
  • Loading branch information
vektah authored Aug 19, 2019
2 parents 8590ede + f33e09e commit 7e643fd
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 91 deletions.
8 changes: 3 additions & 5 deletions codegen/field.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,12 @@
if err != nil {
return nil, err
}
if tmp == nil {
return nil, nil
}
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
5 changes: 5 additions & 0 deletions codegen/testserver/directive.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extend type Query {
directiveInput(arg: InputDirectives!): String
directiveInputType(arg: InnerInput! @custom): String
directiveObject: ObjectDirectives
directiveObjectWithCustomGoModel: ObjectDirectivesWithCustomGoModel
directiveFieldDef(ret: String!): String! @length(min: 1, message: "not valid")
directiveField: String
directiveDouble: String @directive1 @directive2
Expand All @@ -36,3 +37,7 @@ type ObjectDirectives {
text: String! @length(min: 0, max: 7, message: "not valid")
nullableText: String @toNull
}

type ObjectDirectivesWithCustomGoModel {
nullableText: String @toNull
}
19 changes: 19 additions & 0 deletions codegen/testserver/directive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ func TestDirectives(t *testing.T) {
}, nil
}

resolvers.QueryResolver.DirectiveObjectWithCustomGoModel = func(ctx context.Context) (*ObjectDirectivesWithCustomGoModel, error) {
s := "Ok"
return &ObjectDirectivesWithCustomGoModel{
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 @@ -350,5 +357,17 @@ func TestDirectives(t *testing.T) {
require.Equal(t, "Ok", resp.DirectiveObject.Text)
require.True(t, resp.DirectiveObject.NullableText == nil)
})
t.Run("when directive returns nil & custom go field is not nilable", func(t *testing.T) {
var resp struct {
DirectiveObjectWithCustomGoModel *struct {
NullableText *string
}
}

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

require.Nil(t, err)
require.True(t, resp.DirectiveObjectWithCustomGoModel.NullableText == nil)
})
})
}
Loading

0 comments on commit 7e643fd

Please sign in to comment.