Skip to content

Commit

Permalink
Merge pull request #1243 from 99designs/nilable-nullable-unnmarshal
Browse files Browse the repository at this point in the history
Remove a bunch of unneeded nil checks from non-nullable graphql type unmarshalling
  • Loading branch information
lwc authored Jul 10, 2020
2 parents 3e59a10 + b81138d commit 8494028
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 112 deletions.
105 changes: 81 additions & 24 deletions codegen/testserver/generated.go

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

18 changes: 18 additions & 0 deletions codegen/testserver/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,22 @@ func TestInput(t *testing.T) {
require.EqualError(t, err, `http 422: {"errors":[{"message":"Expected type String!, found 1.","locations":[{"line":1,"column":32}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}},{"message":"Expected type String!, found 2.","locations":[{"line":1,"column":35}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`)
require.Nil(t, resp.DirectiveArg)
})

t.Run("when input slice nullable", func(t *testing.T) {
resolvers.QueryResolver.InputNullableSlice = func(ctx context.Context, arg []string) (b bool, e error) {
return arg == nil, nil
}

var resp struct {
InputNullableSlice bool
}
var err error
err = c.Post(`query { inputNullableSlice(arg: null) }`, &resp)
require.NoError(t, err)
require.True(t, resp.InputNullableSlice)

err = c.Post(`query { inputNullableSlice(arg: []) }`, &resp)
require.NoError(t, err)
require.False(t, resp.InputNullableSlice)
})
}
4 changes: 4 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func (r *queryResolver) InputSlice(ctx context.Context, arg []string) (bool, err
panic("not implemented")
}

func (r *queryResolver) InputNullableSlice(ctx context.Context, arg []string) (bool, error) {
panic("not implemented")
}

func (r *queryResolver) ShapeUnion(ctx context.Context) (ShapeUnion, error) {
panic("not implemented")
}
Expand Down
1 change: 1 addition & 0 deletions codegen/testserver/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Query {
user(id: Int!): User!
nullableArg(arg: Int = 123): String
inputSlice(arg: [String!]!): Boolean!
inputNullableSlice(arg: [String!]): Boolean!
shapeUnion: ShapeUnion!
autobind: Autobind
deprecatedField: String! @deprecated(reason: "test deprecated directive")
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.

2 changes: 1 addition & 1 deletion codegen/type.gotpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{- range $type := .ReferencedTypes }}
{{ with $type.UnmarshalFunc }}
func (ec *executionContext) {{ . }}(ctx context.Context, v interface{}) ({{ $type.GO | ref }}, error) {
{{- if $type.IsNilable }}
{{- if and $type.IsNilable (not $type.GQL.NonNull) }}
if v == nil { return nil, nil }
{{- end }}
{{- if $type.IsPtr }}
Expand Down
3 changes: 0 additions & 3 deletions example/chat/generated.go

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

3 changes: 0 additions & 3 deletions example/config/generated.go

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

3 changes: 0 additions & 3 deletions example/dataloader/generated.go

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

9 changes: 0 additions & 9 deletions example/federation/accounts/graph/generated/generated.go

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

9 changes: 0 additions & 9 deletions example/federation/products/graph/generated/generated.go

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

9 changes: 0 additions & 9 deletions example/federation/reviews/graph/generated/generated.go

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

Loading

0 comments on commit 8494028

Please sign in to comment.