Skip to content

Commit 5b85e93

Browse files
authored
fix #2524 basic alias Byte was not binded properly (#2528)
* fix #2524 basic alias `Byte` was not binded properly * add tests for defined types as []byte and []rune
1 parent 49ac94f commit 5b85e93

File tree

9 files changed

+526
-6
lines changed

9 files changed

+526
-6
lines changed

_examples/scalars/.gqlgen.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ models:
1818
model: github.com/99designs/gqlgen/_examples/scalars/model.Banned
1919
DarkMode:
2020
model: github.com/99designs/gqlgen/_examples/scalars/model.Preferences
21+
Bytes:
22+
model: "github.com/99designs/gqlgen/codegen/testserver/singlefile.Bytes"
23+
Runes:
24+
model: "github.com/99designs/gqlgen/_examples/scalars/model.Runes"
25+
DefinedTypeBytes:
26+
model: "github.com/99designs/gqlgen/_examples/scalars/external.Bytes"
27+
DefinedTypeRunes:
28+
model: "github.com/99designs/gqlgen/_examples/scalars/external.Runes"
Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,60 @@
11
package external
22

3+
import (
4+
"fmt"
5+
"io"
6+
7+
"github.com/99designs/gqlgen/graphql"
8+
)
9+
310
type (
4-
ObjectID int
5-
Manufacturer string // remote named string
6-
Count uint32 // remote named uint32
11+
ObjectID int
12+
Manufacturer string // remote named string
13+
Count uint8 // remote named uint8
14+
ExternalBytes []byte
15+
ExternalRunes []rune
716
)
817

918
const (
1019
ManufacturerTesla Manufacturer = "TESLA"
1120
ManufacturerHonda Manufacturer = "HONDA"
1221
ManufacturerToyota Manufacturer = "TOYOTA"
1322
)
23+
24+
func MarshalBytes(b ExternalBytes) graphql.Marshaler {
25+
return graphql.WriterFunc(func(w io.Writer) {
26+
_, _ = fmt.Fprintf(w, "%q", string(b))
27+
})
28+
}
29+
30+
func UnmarshalBytes(v interface{}) (ExternalBytes, error) {
31+
switch v := v.(type) {
32+
case string:
33+
return ExternalBytes(v), nil
34+
case *string:
35+
return ExternalBytes(*v), nil
36+
case ExternalBytes:
37+
return v, nil
38+
default:
39+
return nil, fmt.Errorf("%T is not ExternalBytes", v)
40+
}
41+
}
42+
43+
func MarshalRunes(r ExternalRunes) graphql.Marshaler {
44+
return graphql.WriterFunc(func(w io.Writer) {
45+
_, _ = fmt.Fprintf(w, "%q", string(r))
46+
})
47+
}
48+
49+
func UnmarshalRunes(v interface{}) (ExternalRunes, error) {
50+
switch v := v.(type) {
51+
case string:
52+
return ExternalRunes(v), nil
53+
case *string:
54+
return ExternalRunes(*v), nil
55+
case ExternalRunes:
56+
return v, nil
57+
default:
58+
return nil, fmt.Errorf("%T is not ExternalRunes", v)
59+
}
60+
}

0 commit comments

Comments
 (0)