Skip to content

Commit

Permalink
Support returning nulls from slices
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Mar 13, 2019
1 parent 2b270e4 commit a48c55b
Show file tree
Hide file tree
Showing 17 changed files with 572 additions and 10 deletions.
353 changes: 353 additions & 0 deletions codegen/testserver/generated.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions codegen/testserver/models-gen.go

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

3 changes: 3 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func (r *queryResolver) Panics(ctx context.Context) (*Panics, error) {
func (r *queryResolver) DefaultScalar(ctx context.Context, arg string) (string, error) {
panic("not implemented")
}
func (r *queryResolver) Slices(ctx context.Context) (*Slices, error) {
panic("not implemented")
}
func (r *queryResolver) ValidType(ctx context.Context) (*ValidType, error) {
panic("not implemented")
}
Expand Down
10 changes: 10 additions & 0 deletions codegen/testserver/slices.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extend type Query {
slices: Slices
}

type Slices {
test1: [String]
test2: [String!]
test3: [String]!
test4: [String!]!
}
33 changes: 33 additions & 0 deletions codegen/testserver/slices_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package testserver

import (
"context"
"net/http/httptest"
"testing"

"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/handler"
"github.com/stretchr/testify/require"
)

func TestSlices(t *testing.T) {
resolvers := &Stub{}

srv := httptest.NewServer(handler.GraphQL(NewExecutableSchema(Config{Resolvers: resolvers})))
c := client.New(srv.URL)

t.Run("nulls vs empty slices", func(t *testing.T) {
resolvers.QueryResolver.Slices = func(ctx context.Context) (slices *Slices, e error) {
return &Slices{}, nil
}

var resp struct {
Slices Slices
}
c.MustPost(`query { slices { test1, test2, test3, test4 }}`, &resp)
require.Nil(t, resp.Slices.Test1)
require.Nil(t, resp.Slices.Test2)
require.NotNil(t, resp.Slices.Test3)
require.NotNil(t, resp.Slices.Test4)
})
}
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.

5 changes: 5 additions & 0 deletions codegen/type.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
{{- if $type.SelfMarshalling }}
return v
{{- else if $type.IsSlice }}
{{- if not $type.GQL.NonNull }}
if v == nil {
return graphql.Null
}
{{- end }}
ret := make(graphql.Array, len(v))
{{- if not $type.IsScalar }}
var wg sync.WaitGroup
Expand Down
12 changes: 12 additions & 0 deletions example/chat/generated.go

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

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

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

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

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

12 changes: 12 additions & 0 deletions example/scalars/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 example/selection/generated.go

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

Loading

0 comments on commit a48c55b

Please sign in to comment.