Skip to content

Commit

Permalink
add tests for defined types as []byte and []rune
Browse files Browse the repository at this point in the history
  • Loading branch information
mstephano committed Jan 26, 2023
1 parent 8fa26b4 commit 705d919
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 7 deletions.
4 changes: 4 additions & 0 deletions _examples/scalars/.gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ models:
model: "github.com/99designs/gqlgen/codegen/testserver/singlefile.Bytes"
Runes:
model: "github.com/99designs/gqlgen/_examples/scalars/model.Runes"
DefinedTypeBytes:
model: "github.com/99designs/gqlgen/_examples/scalars/external.Bytes"
DefinedTypeRunes:
model: "github.com/99designs/gqlgen/_examples/scalars/external.Runes"
53 changes: 50 additions & 3 deletions _examples/scalars/external/model.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,60 @@
package external

import (
"fmt"
"io"

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

type (
ObjectID int
Manufacturer string // remote named string
Count uint8 // remote named uint8
ObjectID int
Manufacturer string // remote named string
Count uint8 // remote named uint8
ExternalBytes []byte
ExternalRunes []rune
)

const (
ManufacturerTesla Manufacturer = "TESLA"
ManufacturerHonda Manufacturer = "HONDA"
ManufacturerToyota Manufacturer = "TOYOTA"
)

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

func UnmarshalBytes(v interface{}) (ExternalBytes, error) {
switch v := v.(type) {
case string:
return ExternalBytes(v), nil
case *string:
return ExternalBytes(*v), nil
case ExternalBytes:
return v, nil
default:
return nil, fmt.Errorf("%T is not ExternalBytes", v)
}
}

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

func UnmarshalRunes(v interface{}) (ExternalRunes, error) {
switch v := v.(type) {
case string:
return ExternalRunes(v), nil
case *string:
return ExternalRunes(*v), nil
case ExternalRunes:
return v, nil
default:
return nil, fmt.Errorf("%T is not ExternalRunes", v)
}
}
172 changes: 172 additions & 0 deletions _examples/scalars/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 _examples/scalars/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type User struct {
SomeBytes []byte
SomeOtherBytes []byte
SomeRunes []rune
RemoteBytes external.ExternalBytes
RemoteRunes external.ExternalRunes
}

// Point is serialized as a simple array, eg [1, 2]
Expand Down
2 changes: 2 additions & 0 deletions _examples/scalars/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (r *queryResolver) User(ctx context.Context, id external.ObjectID) (*model.
SomeBytes: []byte("abcdef"),
SomeOtherBytes: []byte{97, 98, 99, 100, 101, 102},
SomeRunes: []rune{'H', 'e', 'l', 'l', 'o', ' ', '世', '界'},
RemoteBytes: external.ExternalBytes("fedcba"),
RemoteRunes: external.ExternalRunes{'界', '世', ' ', 'H', 'e', 'l', 'l', 'o'},
}, nil
}

Expand Down
8 changes: 6 additions & 2 deletions _examples/scalars/scalar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type RawUser struct {
SomeBytes string
SomeOtherBytes string
SomeRunes string
RemoteBytes string
RemoteRunes string
}

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

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

err := c.Post(`{ user(id:"=1=") { someBytes someOtherBytes someRunes } }`, &resp)
err := c.Post(`{ user(id:"=1=") { someBytes someOtherBytes someRunes remoteBytes remoteRunes } }`, &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)
require.Equal(t, "fedcba", resp.User.RemoteBytes)
require.Equal(t, "界世 Hello", resp.User.RemoteRunes)
})

t.Run("custom error messages", func(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions _examples/scalars/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type User {
someBytes: Bytes!
someOtherBytes: Bytes!
someRunes: Runes!
remoteBytes: DefinedTypeBytes!
remoteRunes: DefinedTypeRunes!
}

type Address {
Expand All @@ -50,3 +52,5 @@ scalar Banned
scalar DarkMode
scalar Bytes
scalar Runes
scalar DefinedTypeBytes
scalar DefinedTypeRunes
2 changes: 1 addition & 1 deletion integration/testomitempty.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type DefinedTypeFromBasics {
# for example, value of 5.76 comes out as 5.760000228881836, anyone knows how to fix this?
newFloat32: Float32!

# uint64 need a scalar because it is bigger than int64
# uint64 needs a scalar because it is bigger than int64
newUint64: Uint64!
}

Expand Down
2 changes: 1 addition & 1 deletion internal/code/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func similarBasicKind(kind types.BasicKind) types.BasicKind {
switch kind {
case types.Int8, types.Int16:
return types.Int64
case types.Uint, types.Uint8, types.Uint16, types.Uint32: // exclude Uint64: it still needs scalar with custom marshalling/unmarshalling because it is bigger then int64
case types.Uint, types.Uint8, types.Uint16, types.Uint32: // exclude Uint64: it still needs scalar with custom marshalling/unmarshalling because it is bigger than int64
return types.Int64
default:
return kind
Expand Down

0 comments on commit 705d919

Please sign in to comment.