Skip to content

Commit

Permalink
added unmarshalerPacker type
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Feb 5, 2017
1 parent f77f733 commit 943f80f
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions internal/exec/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func makeNonNullPacker(s *schema.Schema, schemaType common.Type, reflectType ref
if !u.ImplementsGraphQLType(schemaType.String()) {
return nil, fmt.Errorf("can not unmarshal %s into %s", schemaType, reflectType)
}
return &unmarshalerPacker{
valueType: reflectType,
}, nil
}

switch t := schemaType.(type) {
Expand Down Expand Up @@ -232,21 +235,33 @@ func (p *valuePacker) pack(r *request, value interface{}) (reflect.Value, error)
return reflect.Value{}, errors.Errorf("got null for non-null")
}

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

coerced, err := unmarshalInput(p.valueType, value)
if err != nil {
return reflect.Value{}, fmt.Errorf("could not unmarshal %#v (%T) into %s: %s", value, value, p.valueType, err)
}
return reflect.ValueOf(coerced), nil
}

type unmarshalerPacker struct {
valueType reflect.Type
}

func (p *unmarshalerPacker) pack(r *request, value interface{}) (reflect.Value, error) {
if v, ok := value.(common.Variable); ok {
value = r.vars[string(v)]
}

if value == nil {
return reflect.Value{}, errors.Errorf("got null for non-null")
}

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

type Unmarshaler interface {
ImplementsGraphQLType(name string) bool
UnmarshalGraphQL(input interface{}) error
Expand Down

0 comments on commit 943f80f

Please sign in to comment.