Skip to content

Commit

Permalink
fix #2524 basic alias Byte was not binded properly
Browse files Browse the repository at this point in the history
  • Loading branch information
mstephano committed Jan 26, 2023
1 parent e6114a2 commit 8fa26b4
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 2 deletions.
4 changes: 4 additions & 0 deletions _examples/scalars/.gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ models:
model: github.com/99designs/gqlgen/_examples/scalars/model.Banned
DarkMode:
model: github.com/99designs/gqlgen/_examples/scalars/model.Preferences
Bytes:
model: "github.com/99designs/gqlgen/codegen/testserver/singlefile.Bytes"
Runes:
model: "github.com/99designs/gqlgen/_examples/scalars/model.Runes"
2 changes: 1 addition & 1 deletion _examples/scalars/external/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package external
type (
ObjectID int
Manufacturer string // remote named string
Count uint32 // remote named uint32
Count uint8 // remote named uint8
)

const (
Expand Down
238 changes: 238 additions & 0 deletions _examples/scalars/generated.go

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

22 changes: 22 additions & 0 deletions _examples/scalars/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type User struct {
Children uint
Cars external.Count
Weddings Sum
SomeBytes []byte
SomeOtherBytes []byte
SomeRunes []rune
}

// Point is serialized as a simple array, eg [1, 2]
Expand Down Expand Up @@ -204,3 +207,22 @@ func UnmarshalPreferences(v interface{}) (*Prefs, error) {
}
return &Prefs{DarkMode: tmp}, nil
}

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

func UnmarshalRunes(v interface{}) ([]rune, error) {
switch v := v.(type) {
case string:
return []rune(v), nil
case *string:
return []rune(*v), nil
case []rune:
return v, nil
default:
return nil, fmt.Errorf("%T is not []rune", v)
}
}
3 changes: 3 additions & 0 deletions _examples/scalars/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func (r *queryResolver) User(ctx context.Context, id external.ObjectID) (*model.
Children: 3,
Cars: 5,
Weddings: 2,
SomeBytes: []byte("abcdef"),
SomeOtherBytes: []byte{97, 98, 99, 100, 101, 102},
SomeRunes: []rune{'H', 'e', 'l', 'l', 'o', ' ', '世', '界'},
}, nil
}

Expand Down
13 changes: 13 additions & 0 deletions _examples/scalars/scalar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type RawUser struct {
Children int
Cars int
Weddings int
SomeBytes string
SomeOtherBytes string
SomeRunes string
}

func TestScalars(t *testing.T) {
Expand Down Expand Up @@ -90,6 +93,16 @@ func TestScalars(t *testing.T) {
require.Equal(t, 2, resp.User.Weddings)
})

t.Run("basic alias byte and rune", func(t *testing.T) {
var resp struct{ User RawUser }

err := c.Post(`{ user(id:"=1=") { someBytes someOtherBytes someRunes } }`, &resp)
require.NoError(t, err)
require.Equal(t, "abcdef", resp.User.SomeBytes)
require.Equal(t, "abcdef", resp.User.SomeOtherBytes)
require.Equal(t, "Hello 世界", resp.User.SomeRunes)
})

t.Run("custom error messages", func(t *testing.T) {
var resp struct{ Search []RawUser }

Expand Down
Loading

0 comments on commit 8fa26b4

Please sign in to comment.