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

Support returning nulls from slices #619

Merged
merged 1 commit into from
Mar 14, 2019
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
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