Skip to content

Commit

Permalink
support for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Oct 13, 2016
1 parent 565e59f commit cff8b30
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
6 changes: 6 additions & 0 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func exec(s *Schema, t schema.Type, sel *query.SelectionSet, resolver reflect.Va
switch t := t.(type) {
case *schema.Scalar:
return resolver.Interface()
case *schema.Array:
a := make([]interface{}, resolver.Len())
for i := range a {
a[i] = exec(s, t.Elem, sel, resolver.Index(i))
}
return a
case *schema.TypeName:
return exec(s, s.Types[t.Name], sel, resolver)
case *schema.Object:
Expand Down
38 changes: 33 additions & 5 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ func (r *helloWorldResolver) Hello() string {
type starWarsResolver struct{}

func (r *starWarsResolver) Hero() *userResolver {
return &userResolver{id: "2001", name: "R2-D2"}
return &userResolver{
id: "2001",
name: "R2-D2",
friends: []*userResolver{
{name: "Luke Skywalker"},
{name: "Han Solo"},
{name: "Leia Organa"},
},
}
}

type userResolver struct {
id string
name string
id string
name string
friends []*userResolver
}

func (r *userResolver) ID() string {
Expand All @@ -31,6 +40,10 @@ func (r *userResolver) Name() string {
return r.name
}

func (r *userResolver) Friends() []*userResolver {
return r.friends
}

var tests = []struct {
name string
schema string
Expand Down Expand Up @@ -58,7 +71,7 @@ var tests = []struct {
`,
},
{
name: "User",
name: "StarWars",
schema: `
type Query {
hero: User
Expand All @@ -67,6 +80,7 @@ var tests = []struct {
type User {
id: String
name: String
friends: [User]
}
`,
resolver: &starWarsResolver{},
Expand All @@ -75,14 +89,28 @@ var tests = []struct {
hero {
id
name
friends {
name
}
}
}
`,
result: `
{
"hero": {
"id": "2001",
"name": "R2-D2"
"name": "R2-D2",
"friends": [
{
"name": "Luke Skywalker"
},
{
"name": "Han Solo"
},
{
"name": "Leia Organa"
}
]
}
}
`,
Expand Down
17 changes: 15 additions & 2 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type Type interface{}
type Scalar struct {
}

type Array struct {
Elem Type
}

type TypeName struct {
Name string
}
Expand Down Expand Up @@ -79,8 +83,10 @@ func parseTypeDecl(l *lexer.Lexer) (string, *Object) {
}

func parseType(l *lexer.Lexer) Type {
// TODO check args
// TODO check return type
if l.Peek() == '[' {
return parseArray(l)
}

name := l.ConsumeIdent()
if name == "String" {
return &Scalar{}
Expand All @@ -89,3 +95,10 @@ func parseType(l *lexer.Lexer) Type {
Name: name,
}
}

func parseArray(l *lexer.Lexer) *Array {
l.ConsumeToken('[')
elem := parseType(l)
l.ConsumeToken(']')
return &Array{Elem: elem}
}

0 comments on commit cff8b30

Please sign in to comment.