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

Bubble Null from List Element to Nearest Nullable Ancestor #1480

Merged
merged 6 commits into from
Mar 12, 2021
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
302 changes: 298 additions & 4 deletions codegen/testserver/generated.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions codegen/testserver/nulls.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extend type Query {
errorBubble: Error
errorBubbleList: [Error!]
errorList: [Error]
errors: Errors
valid: String!
}
Expand Down
34 changes: 32 additions & 2 deletions codegen/testserver/nulls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ func TestNullBubbling(t *testing.T) {
resolvers.QueryResolver.Valid = func(ctx context.Context) (s string, e error) {
return "Ok", nil
}

resolvers.QueryResolver.Errors = func(ctx context.Context) (errors *Errors, e error) {
return &Errors{}, nil
}
resolvers.QueryResolver.ErrorBubble = func(ctx context.Context) (i *Error, e error) {
return &Error{ID: "E1234"}, nil
}
resolvers.QueryResolver.ErrorBubbleList = func(ctx context.Context) (i []*Error, e error) {
return []*Error{nil}, nil
}
resolvers.QueryResolver.ErrorList = func(ctx context.Context) (i []*Error, e error) {
return []*Error{nil}, nil
}

c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})))

Expand Down Expand Up @@ -68,6 +73,31 @@ func TestNullBubbling(t *testing.T) {
require.Equal(t, "Ok", resp.Valid)
})

t.Run("when list element is null", func(t *testing.T) {
var resp struct {
Valid string
ErrorList []*struct{}
}
err := c.Post(`query { valid, errorList { id } }`, &resp)

require.Nil(t, err)
require.Equal(t, len(resp.ErrorList), 1)
require.Nil(t, resp.ErrorList[0])
require.Equal(t, "Ok", resp.Valid)
})

t.Run("when non-null list element is null", func(t *testing.T) {
var resp struct {
Valid string
ErrorBubbleList []*struct{}
}
err := c.Post(`query { valid, errorBubbleList { id } }`, &resp)

require.EqualError(t, err, `[{"message":"must not be null","path":["errorBubbleList",0]}]`)
require.Nil(t, resp.ErrorBubbleList)
require.Equal(t, "Ok", resp.Valid)
})

t.Run("null args", func(t *testing.T) {
var resp struct {
NullableArg *string
Expand All @@ -90,7 +120,7 @@ func TestNullBubbling(t *testing.T) {
resolvers.ErrorsResolver.D = func(ctx context.Context, obj *Errors) (i *Error, e error) { return nil, nil }
resolvers.ErrorsResolver.E = func(ctx context.Context, obj *Errors) (i *Error, e error) { return nil, nil }

err := c.Post(`{ errors {
err := c.Post(`{ errors {
a { id },
b { id },
c { id },
Expand Down
8 changes: 8 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ func (r *queryResolver) ErrorBubble(ctx context.Context) (*Error, error) {
panic("not implemented")
}

func (r *queryResolver) ErrorBubbleList(ctx context.Context) ([]*Error, error) {
panic("not implemented")
}

func (r *queryResolver) ErrorList(ctx context.Context) ([]*Error, error) {
panic("not implemented")
}

func (r *queryResolver) Errors(ctx context.Context) (*Errors, error) {
panic("not implemented")
}
Expand Down
8 changes: 4 additions & 4 deletions codegen/testserver/slices.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ extend type Query {
}

type Slices {
test1: [String]
test2: [String!]
test3: [String]!
test4: [String!]!
test1: [String]
test2: [String!]
test3: [String]!
test4: [String!]!
}

scalar Bytes
8 changes: 8 additions & 0 deletions codegen/testserver/stub.go

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

19 changes: 13 additions & 6 deletions codegen/type.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@
ctx := graphql.WithFieldContext(ctx, fc)
f := func(i int) {
defer func() {
if r := recover(); r != nil {
ec.Error(ctx, ec.Recover(ctx, r))
ret = nil
}
}()
if r := recover(); r != nil {
ec.Error(ctx, ec.Recover(ctx, r))
ret = nil
}
}()
if !isLen1 {
defer wg.Done()
}
Expand All @@ -107,9 +107,16 @@
}
{{ else }}
ret[i] = ec.{{ $type.Elem.MarshalFunc }}(ctx, sel, v[i])
{{- end}}
{{- end }}
}
{{ if not $type.IsScalar }} wg.Wait() {{ end }}
{{ if $type.Elem.GQL.NonNull }}
for _, e := range ret {
if e == graphql.Null {
return graphql.Null
}
}
{{ end }}
return ret
{{- else }}
{{- if $type.IsNilable }}
Expand Down
63 changes: 63 additions & 0 deletions example/chat/generated.go

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

Loading