Skip to content

Commit

Permalink
pointer juggling
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 7, 2018
1 parent 9e99c14 commit 4468127
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
7 changes: 7 additions & 0 deletions cmd/ggraphqlc/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func (e *extractor) extract() {
sort.Slice(e.Objects, func(i, j int) bool {
return strings.Compare(e.Objects[i].Name, e.Objects[j].Name) == -1
})

sort.Slice(e.Interfaces, func(i, j int) bool {
return strings.Compare(e.Interfaces[i].Name, e.Interfaces[j].Name) == -1
})
}

func (e *extractor) introspect() error {
Expand Down Expand Up @@ -258,6 +262,9 @@ func (e *extractor) buildType(t common.Type) kind {
case *schema.Union:
t := e.getType(val.Name)
t.Modifiers = modifiers
if t.Modifiers[len(t.Modifiers)-1] == modPtr {
t.Modifiers = t.Modifiers[0 : len(t.Modifiers)-1]
}

for _, implementor := range val.PossibleTypes {
t.Implementors = append(t.Implementors, e.getType(implementor.Name))
Expand Down
6 changes: 5 additions & 1 deletion cmd/ggraphqlc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ func (t kind) doWriteJson(val string, remainingMods []string, isPtr bool) string
return fmt.Sprintf("ec.json.%s(%s)", ucFirst(t.Name), val)

default:
if !isPtr {
needPtr := len(t.Implementors) == 0
if needPtr && !isPtr {
val = "&" + val
}
if !needPtr && isPtr {
val = "*" + val
}
return fmt.Sprintf("ec._%s(field.Selections, %s)", lcFirst(t.GraphQLName), val)
}
}
Expand Down
30 changes: 15 additions & 15 deletions example/starwars/gen/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (ec *executionContext) _droid(sel []query.Selection, it *starwars.Droid) {
}
ec.json.BeginArray()
for _, val := range res {
ec._character(field.Selections, &val)
ec._character(field.Selections, val)
}
ec.json.EndArray()

Expand Down Expand Up @@ -239,7 +239,7 @@ func (ec *executionContext) _friendsConnection(sel []query.Selection, it *starwa
}
ec.json.BeginArray()
for _, val := range res {
ec._character(field.Selections, &val)
ec._character(field.Selections, val)
}
ec.json.EndArray()

Expand Down Expand Up @@ -269,7 +269,7 @@ func (ec *executionContext) _friendsEdge(sel []query.Selection, it *starwars.Fri
case "node":
ec.json.ObjectKey(field.Alias)
res := it.Node
ec._character(field.Selections, &res)
ec._character(field.Selections, res)

default:
panic("unknown field " + strconv.Quote(field.Name))
Expand Down Expand Up @@ -324,7 +324,7 @@ func (ec *executionContext) _human(sel []query.Selection, it *starwars.Human) {
}
ec.json.BeginArray()
for _, val := range res {
ec._character(field.Selections, &val)
ec._character(field.Selections, val)
}
ec.json.EndArray()

Expand Down Expand Up @@ -484,7 +484,7 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) {
ec.json.Null()
continue
}
ec._character(field.Selections, &res)
ec._character(field.Selections, res)

case "reviews":
ec.json.ObjectKey(field.Alias)
Expand Down Expand Up @@ -546,7 +546,7 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) {
}
ec.json.BeginArray()
for _, val := range res {
ec._searchResult(field.Selections, &val)
ec._searchResult(field.Selections, val)
}
ec.json.EndArray()

Expand All @@ -568,7 +568,7 @@ func (ec *executionContext) _query(sel []query.Selection, it *interface{}) {
ec.json.Null()
continue
}
ec._character(field.Selections, &res)
ec._character(field.Selections, res)

case "droid":
ec.json.ObjectKey(field.Alias)
Expand Down Expand Up @@ -1161,7 +1161,7 @@ func (ec *executionContext) ___Type(sel []query.Selection, it *introspection.Typ
ec.json.EndObject()
}

func (ec *executionContext) _searchResult(sel []query.Selection, it *starwars.SearchResult) {
func (ec *executionContext) _character(sel []query.Selection, it starwars.Character) {
switch it := it.(type) {
case nil:
ec.json.Null()
Expand All @@ -1178,18 +1178,12 @@ func (ec *executionContext) _searchResult(sel []query.Selection, it *starwars.Se
case *starwars.Droid:
ec._droid(sel, it)

case starwars.Starship:
ec._starship(sel, &it)

case *starwars.Starship:
ec._starship(sel, it)

default:
panic(fmt.Errorf("unexpected type %T", it))
}
}

func (ec *executionContext) _character(sel []query.Selection, it starwars.Character) {
func (ec *executionContext) _searchResult(sel []query.Selection, it starwars.SearchResult) {
switch it := it.(type) {
case nil:
ec.json.Null()
Expand All @@ -1206,6 +1200,12 @@ func (ec *executionContext) _character(sel []query.Selection, it starwars.Charac
case *starwars.Droid:
ec._droid(sel, it)

case starwars.Starship:
ec._starship(sel, &it)

case *starwars.Starship:
ec._starship(sel, it)

default:
panic(fmt.Errorf("unexpected type %T", it))
}
Expand Down

0 comments on commit 4468127

Please sign in to comment.