Skip to content

Commit

Permalink
Merge pull request #1480 from wilhelmeek/double-bubble
Browse files Browse the repository at this point in the history
Bubble Null from List Element to Nearest Nullable Ancestor
  • Loading branch information
lwc authored Mar 12, 2021
2 parents 997da42 + 1fac78e commit 58e3225
Show file tree
Hide file tree
Showing 20 changed files with 1,278 additions and 16 deletions.
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

0 comments on commit 58e3225

Please sign in to comment.