Skip to content

Commit

Permalink
move packer.Unmarshaler interface to decode.Unmarshaler, so the metho…
Browse files Browse the repository at this point in the history
…ds are actually visible
  • Loading branch information
sGy1980de committed Apr 20, 2021
1 parent 04aa634 commit 60ab3c8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
13 changes: 13 additions & 0 deletions decode/decode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package decode

// Unmarshaler defines the api of Go types mapped to custom GraphQL scalar types
type Unmarshaler interface {
// ImplementsGraphQLType maps the implementing custom Go type
// to the GraphQL scalar type in the schema.
ImplementsGraphQLType(name string) bool
// UnmarshalGraphQL is the custom unmarshaler for the implementing type
//
// This function will be called whenever you use the
// custom GraphQL scalar type as an input
UnmarshalGraphQL(input interface{}) error
}
4 changes: 0 additions & 4 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/graph-gophers/graphql-go/errors"
"github.com/graph-gophers/graphql-go/internal/common"
"github.com/graph-gophers/graphql-go/internal/exec"
"github.com/graph-gophers/graphql-go/internal/exec/packer"
"github.com/graph-gophers/graphql-go/internal/exec/resolvable"
"github.com/graph-gophers/graphql-go/internal/exec/selected"
"github.com/graph-gophers/graphql-go/internal/query"
Expand All @@ -21,9 +20,6 @@ import (
"github.com/graph-gophers/graphql-go/trace"
)

// Unmarshaler defines the public api of Go types mapped to custom GraphQL scalar types
type Unmarshaler = packer.Unmarshaler

// ParseSchema parses a GraphQL schema and attaches the given root resolver. It returns an error if
// the Go type signature of the resolvers does not match the schema. If nil is passed as the
// resolver, then the schema can not be executed, but it may be inspected (e.g. with ToJSON).
Expand Down
18 changes: 4 additions & 14 deletions internal/exec/packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"strings"

"github.com/graph-gophers/graphql-go/decode"
"github.com/graph-gophers/graphql-go/errors"
"github.com/graph-gophers/graphql-go/internal/common"
"github.com/graph-gophers/graphql-go/internal/schema"
Expand Down Expand Up @@ -115,7 +116,7 @@ func (b *Builder) makePacker(schemaType common.Type, reflectType reflect.Type) (
}

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

v := reflect.New(p.ValueType)
if err := v.Interface().(Unmarshaler).UnmarshalGraphQL(value); err != nil {
if err := v.Interface().(decode.Unmarshaler).UnmarshalGraphQL(value); err != nil {
return reflect.Value{}, err
}
return v.Elem(), nil
}

type Unmarshaler interface {
// ImplementsGraphQLType maps the implementing custom Go type
// to the GraphQL scalar type in the schema.
ImplementsGraphQLType(name string) bool
// UnmarshalGraphQL is the custom unmarshaler for the implementing type
//
// This function will be called whenever you use the
// custom GraphQL scalar type as an input
UnmarshalGraphQL(input interface{}) error
}

func unmarshalInput(typ reflect.Type, input interface{}) (interface{}, error) {
if reflect.TypeOf(input) == typ {
return input, nil
Expand Down Expand Up @@ -391,7 +381,7 @@ func stripUnderscore(s string) string {

// NullUnmarshaller is an unmarshaller that can handle a nil input
type NullUnmarshaller interface {
Unmarshaler
decode.Unmarshaler
Nullable()
}

Expand Down
3 changes: 2 additions & 1 deletion internal/exec/resolvable/resolvable.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"strings"

"github.com/graph-gophers/graphql-go/decode"
"github.com/graph-gophers/graphql-go/internal/common"
"github.com/graph-gophers/graphql-go/internal/exec/packer"
"github.com/graph-gophers/graphql-go/internal/schema"
Expand Down Expand Up @@ -207,7 +208,7 @@ func makeScalarExec(t *schema.Scalar, resolverType reflect.Type) (Resolvable, er
implementsType = t.Name == "String"
case *bool:
implementsType = t.Name == "Boolean"
case packer.Unmarshaler:
case decode.Unmarshaler:
implementsType = r.ImplementsGraphQLType(t.Name)
}
if !implementsType {
Expand Down
5 changes: 3 additions & 2 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

. "github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/decode"
)

func TestTime_ImplementsUnmarshaler(t *testing.T) {
Expand All @@ -16,8 +17,8 @@ func TestTime_ImplementsUnmarshaler(t *testing.T) {
}
}()

// assert *Time implements Unmarshaler interface
var _ Unmarshaler = (*Time)(nil)
// assert *Time implements decode.Unmarshaler interface
var _ decode.Unmarshaler = (*Time)(nil)
}

func TestTime_ImplementsGraphQLType(t *testing.T) {
Expand Down

0 comments on commit 60ab3c8

Please sign in to comment.