Skip to content

Commit

Permalink
added support for input object literals
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Nov 21, 2016
1 parent 663e466 commit 4130d54
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
16 changes: 12 additions & 4 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1258,8 +1258,12 @@ func (r *inputResolver) Boolean(args *struct{ Value bool }) bool {
return args.Value
}

func (r *inputResolver) List(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 {
l[i] = entry.V
}
return l
}

func TestInput(t *testing.T) {
Expand All @@ -1273,7 +1277,11 @@ func TestInput(t *testing.T) {
float(value: Float!): Float!
string(value: String!): String!
boolean(value: Boolean!): Boolean!
list(value: [Int!]!): [Int!]!
list(value: [Input!]!): [Int!]!
}
input Input {
v: Int!
}
`, &inputResolver{})
graphql.RunTests(t, []*graphql.Test{
Expand All @@ -1287,7 +1295,7 @@ func TestInput(t *testing.T) {
float2: float(value: 42.5)
string(value: "foo")
boolean(value: true)
list(value: [41, 42, 43])
list(value: [{v: 41}, {v: 42}, {v: 43}])
}
`,
ExpectedResult: `
Expand Down
26 changes: 18 additions & 8 deletions internal/common/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ func ParseValue(l *lexer.Lexer, constOnly bool) interface{} {
}
l.ConsumeToken('$')
return Variable(l.ConsumeIdent())
case '[':
l.ConsumeToken('[')
var list []interface{}
for l.Peek() != ']' {
list = append(list, ParseValue(l, constOnly))
}
l.ConsumeToken(']')
return list
case scanner.Int:
return l.ConsumeInt()
case scanner.Float:
Expand All @@ -65,6 +57,24 @@ func ParseValue(l *lexer.Lexer, constOnly bool) interface{} {
default:
return ident
}
case '[':
l.ConsumeToken('[')
var list []interface{}
for l.Peek() != ']' {
list = append(list, ParseValue(l, constOnly))
}
l.ConsumeToken(']')
return list
case '{':
l.ConsumeToken('{')
obj := make(map[string]interface{})
for l.Peek() != '}' {
name := l.ConsumeIdent()
l.ConsumeToken(':')
obj[name] = ParseValue(l, constOnly)
}
l.ConsumeToken('}')
return obj
default:
l.SyntaxError("invalid value")
panic("unreachable")
Expand Down

0 comments on commit 4130d54

Please sign in to comment.