Skip to content

Commit 60ab3c8

Browse files
committed
move packer.Unmarshaler interface to decode.Unmarshaler, so the methods are actually visible
1 parent 04aa634 commit 60ab3c8

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

decode/decode.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package decode
2+
3+
// Unmarshaler defines the api of Go types mapped to custom GraphQL scalar types
4+
type Unmarshaler interface {
5+
// ImplementsGraphQLType maps the implementing custom Go type
6+
// to the GraphQL scalar type in the schema.
7+
ImplementsGraphQLType(name string) bool
8+
// UnmarshalGraphQL is the custom unmarshaler for the implementing type
9+
//
10+
// This function will be called whenever you use the
11+
// custom GraphQL scalar type as an input
12+
UnmarshalGraphQL(input interface{}) error
13+
}

graphql.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/graph-gophers/graphql-go/errors"
1111
"github.com/graph-gophers/graphql-go/internal/common"
1212
"github.com/graph-gophers/graphql-go/internal/exec"
13-
"github.com/graph-gophers/graphql-go/internal/exec/packer"
1413
"github.com/graph-gophers/graphql-go/internal/exec/resolvable"
1514
"github.com/graph-gophers/graphql-go/internal/exec/selected"
1615
"github.com/graph-gophers/graphql-go/internal/query"
@@ -21,9 +20,6 @@ import (
2120
"github.com/graph-gophers/graphql-go/trace"
2221
)
2322

24-
// Unmarshaler defines the public api of Go types mapped to custom GraphQL scalar types
25-
type Unmarshaler = packer.Unmarshaler
26-
2723
// ParseSchema parses a GraphQL schema and attaches the given root resolver. It returns an error if
2824
// the Go type signature of the resolvers does not match the schema. If nil is passed as the
2925
// resolver, then the schema can not be executed, but it may be inspected (e.g. with ToJSON).

internal/exec/packer/packer.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"reflect"
77
"strings"
88

9+
"github.com/graph-gophers/graphql-go/decode"
910
"github.com/graph-gophers/graphql-go/errors"
1011
"github.com/graph-gophers/graphql-go/internal/common"
1112
"github.com/graph-gophers/graphql-go/internal/schema"
@@ -115,7 +116,7 @@ func (b *Builder) makePacker(schemaType common.Type, reflectType reflect.Type) (
115116
}
116117

117118
func (b *Builder) makeNonNullPacker(schemaType common.Type, reflectType reflect.Type) (packer, error) {
118-
if u, ok := reflect.New(reflectType).Interface().(Unmarshaler); ok {
119+
if u, ok := reflect.New(reflectType).Interface().(decode.Unmarshaler); ok {
119120
if !u.ImplementsGraphQLType(schemaType.String()) {
120121
return nil, fmt.Errorf("can not unmarshal %s into %s", schemaType, reflectType)
121122
}
@@ -323,23 +324,12 @@ func (p *unmarshalerPacker) Pack(value interface{}) (reflect.Value, error) {
323324
}
324325

325326
v := reflect.New(p.ValueType)
326-
if err := v.Interface().(Unmarshaler).UnmarshalGraphQL(value); err != nil {
327+
if err := v.Interface().(decode.Unmarshaler).UnmarshalGraphQL(value); err != nil {
327328
return reflect.Value{}, err
328329
}
329330
return v.Elem(), nil
330331
}
331332

332-
type Unmarshaler interface {
333-
// ImplementsGraphQLType maps the implementing custom Go type
334-
// to the GraphQL scalar type in the schema.
335-
ImplementsGraphQLType(name string) bool
336-
// UnmarshalGraphQL is the custom unmarshaler for the implementing type
337-
//
338-
// This function will be called whenever you use the
339-
// custom GraphQL scalar type as an input
340-
UnmarshalGraphQL(input interface{}) error
341-
}
342-
343333
func unmarshalInput(typ reflect.Type, input interface{}) (interface{}, error) {
344334
if reflect.TypeOf(input) == typ {
345335
return input, nil
@@ -391,7 +381,7 @@ func stripUnderscore(s string) string {
391381

392382
// NullUnmarshaller is an unmarshaller that can handle a nil input
393383
type NullUnmarshaller interface {
394-
Unmarshaler
384+
decode.Unmarshaler
395385
Nullable()
396386
}
397387

internal/exec/resolvable/resolvable.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"reflect"
77
"strings"
88

9+
"github.com/graph-gophers/graphql-go/decode"
910
"github.com/graph-gophers/graphql-go/internal/common"
1011
"github.com/graph-gophers/graphql-go/internal/exec/packer"
1112
"github.com/graph-gophers/graphql-go/internal/schema"
@@ -207,7 +208,7 @@ func makeScalarExec(t *schema.Scalar, resolverType reflect.Type) (Resolvable, er
207208
implementsType = t.Name == "String"
208209
case *bool:
209210
implementsType = t.Name == "Boolean"
210-
case packer.Unmarshaler:
211+
case decode.Unmarshaler:
211212
implementsType = r.ImplementsGraphQLType(t.Name)
212213
}
213214
if !implementsType {

time_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
. "github.com/graph-gophers/graphql-go"
10+
"github.com/graph-gophers/graphql-go/decode"
1011
)
1112

1213
func TestTime_ImplementsUnmarshaler(t *testing.T) {
@@ -16,8 +17,8 @@ func TestTime_ImplementsUnmarshaler(t *testing.T) {
1617
}
1718
}()
1819

19-
// assert *Time implements Unmarshaler interface
20-
var _ Unmarshaler = (*Time)(nil)
20+
// assert *Time implements decode.Unmarshaler interface
21+
var _ decode.Unmarshaler = (*Time)(nil)
2122
}
2223

2324
func TestTime_ImplementsGraphQLType(t *testing.T) {

0 commit comments

Comments
 (0)