Skip to content

Commit

Permalink
improved support for null
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Nov 21, 2016
1 parent 4130d54 commit b94f2af
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
9 changes: 9 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,10 @@ func (r *inputResolver) Boolean(args *struct{ Value bool }) bool {
return args.Value
}

func (r *inputResolver) Nullable(args *struct{ Value *int32 }) *int32 {
return args.Value
}

func (r *inputResolver) List(args *struct{ Value []*struct{ V int32 } }) []int32 {
l := make([]int32, len(args.Value))
for i, entry := range args.Value {
Expand All @@ -1277,6 +1281,7 @@ func TestInput(t *testing.T) {
float(value: Float!): Float!
string(value: String!): String!
boolean(value: Boolean!): Boolean!
nullable(value: Int): Int
list(value: [Input!]!): [Int!]!
}
Expand All @@ -1295,6 +1300,8 @@ func TestInput(t *testing.T) {
float2: float(value: 42.5)
string(value: "foo")
boolean(value: true)
nullable1: nullable(value: 42)
nullable2: nullable(value: null)
list(value: [{v: 41}, {v: 42}, {v: 43}])
}
`,
Expand All @@ -1306,6 +1313,8 @@ func TestInput(t *testing.T) {
"float2": 42.5,
"string": "foo",
"boolean": true,
"nullable1": 42,
"nullable2": null,
"list": [41, 42, 43]
}
`,
Expand Down
2 changes: 2 additions & 0 deletions internal/common/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func ParseValue(l *lexer.Lexer, constOnly bool) interface{} {
return true
case "false":
return false
case "null":
return nil
default:
return ident
}
Expand Down
5 changes: 4 additions & 1 deletion internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,10 @@ func coerceValue(r *request, typ common.Type, value interface{}) (interface{}, e
return r.vars[string(v)], nil
}

t, _ := unwrapNonNull(typ)
t, nonNull := unwrapNonNull(typ)
if !nonNull && value == nil {
return nil, nil
}
switch t := t.(type) {
case *scalar:
v, err := t.coerceInput(value)
Expand Down
19 changes: 12 additions & 7 deletions internal/exec/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func makePacker(s *schema.Schema, schemaType common.Type, hasDefault bool, refle
return nil, fmt.Errorf("wrong type, expected %s", want)
}
return &valuePacker{
nonNull: nonNull,
valueType: reflectType,
nonNull: nonNull,
}, nil
case *schema.Enum:
want := reflect.TypeOf("")
Expand All @@ -39,7 +40,8 @@ func makePacker(s *schema.Schema, schemaType common.Type, hasDefault bool, refle
return nil, fmt.Errorf("wrong type, expected %s", want)
}
return &valuePacker{
nonNull: nonNull,
valueType: reflectType,
nonNull: nonNull,
}, nil
case *schema.InputObject:
e, err := makeStructPacker(s, &t.InputMap, reflectType)
Expand Down Expand Up @@ -150,15 +152,18 @@ func (e *listPacker) pack(value interface{}) reflect.Value {
}

type valuePacker struct {
nonNull bool
valueType reflect.Type
nonNull bool
}

func (p *valuePacker) pack(value interface{}) reflect.Value {
v := reflect.ValueOf(value)
if !p.nonNull {
ptr := reflect.New(v.Type())
ptr.Elem().Set(v)
if value == nil {
return reflect.Zero(p.valueType)
}
ptr := reflect.New(p.valueType.Elem())
ptr.Elem().Set(reflect.ValueOf(value))
return ptr
}
return v
return reflect.ValueOf(value)
}

0 comments on commit b94f2af

Please sign in to comment.