Skip to content

Commit

Permalink
Merge pull request #637 from 99designs/fix-is-slice
Browse files Browse the repository at this point in the history
Fix Mapping Custom Scalar to Slice
  • Loading branch information
Mathew Byrne authored Mar 31, 2019
2 parents 7b533df + 515f225 commit ef2e51b
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 3 deletions.
6 changes: 3 additions & 3 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ func (ref *TypeReference) Elem() *TypeReference {
}
}

if s, isSlice := ref.GO.(*types.Slice); isSlice {
if ref.IsSlice() {
return &TypeReference{
GO: s.Elem(),
GO: ref.GO.(*types.Slice).Elem(),
GQL: ref.GQL.Elem,
CastType: ref.CastType,
Definition: ref.Definition,
Expand All @@ -221,7 +221,7 @@ func (t *TypeReference) IsNilable() bool {

func (t *TypeReference) IsSlice() bool {
_, isSlice := t.GO.(*types.Slice)
return isSlice
return t.GQL.Elem != nil && isSlice
}

func (t *TypeReference) IsNamed() bool {
Expand Down
27 changes: 27 additions & 0 deletions codegen/testserver/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package testserver

import (
"fmt"
"io"

"github.com/99designs/gqlgen/graphql"
)

func MarshalBytes(b []byte) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = fmt.Fprintf(w, "%q", string(b))
})
}

func UnmarshalBytes(v interface{}) ([]byte, error) {
switch v := v.(type) {
case string:
return []byte(v), nil
case *string:
return []byte(*v), nil
case []byte:
return v, nil
default:
return nil, fmt.Errorf("%T is not []byte", v)
}
}
61 changes: 61 additions & 0 deletions codegen/testserver/generated.go

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

2 changes: 2 additions & 0 deletions codegen/testserver/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ models:
oldFoo: { fieldName: foo, resolver: true }
FallbackToStringEncoding:
model: "github.com/99designs/gqlgen/codegen/testserver.FallbackToStringEncoding"
Bytes:
model: "github.com/99designs/gqlgen/codegen/testserver.Bytes"
3 changes: 3 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ func (r *queryResolver) DefaultScalar(ctx context.Context, arg string) (string,
func (r *queryResolver) Slices(ctx context.Context) (*Slices, error) {
panic("not implemented")
}
func (r *queryResolver) ScalarSlice(ctx context.Context) ([]byte, error) {
panic("not implemented")
}
func (r *queryResolver) Fallback(ctx context.Context, arg FallbackToStringEncoding) (FallbackToStringEncoding, error) {
panic("not implemented")
}
Expand Down
3 changes: 3 additions & 0 deletions codegen/testserver/slices.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extend type Query {
slices: Slices
scalarSlice: Bytes!
}

type Slices {
Expand All @@ -8,3 +9,5 @@ type Slices {
test3: [String]!
test4: [String!]!
}

scalar Bytes
12 changes: 12 additions & 0 deletions codegen/testserver/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ func TestSlices(t *testing.T) {
require.NotNil(t, resp.Slices.Test3)
require.NotNil(t, resp.Slices.Test4)
})

t.Run("custom scalars to slices work", func(t *testing.T) {
resolvers.QueryResolver.ScalarSlice = func(ctx context.Context) ([]byte, error) {
return []byte("testing"), nil
}

var resp struct {
ScalarSlice string
}
c.MustPost(`query { scalarSlice }`, &resp)
require.Equal(t, "testing", resp.ScalarSlice)
})
}
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.

0 comments on commit ef2e51b

Please sign in to comment.