diff --git a/cmd/ggraphqlc/extractor.go b/cmd/ggraphqlc/extractor.go index 41ba78aafbf..691d5def4bd 100644 --- a/cmd/ggraphqlc/extractor.go +++ b/cmd/ggraphqlc/extractor.go @@ -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 { @@ -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)) diff --git a/cmd/ggraphqlc/types.go b/cmd/ggraphqlc/types.go index 65abbff3395..f87d34308e2 100644 --- a/cmd/ggraphqlc/types.go +++ b/cmd/ggraphqlc/types.go @@ -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) } } diff --git a/example/starwars/gen/generated.go b/example/starwars/gen/generated.go index 9c616107616..3f01a7dfcd2 100644 --- a/example/starwars/gen/generated.go +++ b/example/starwars/gen/generated.go @@ -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() @@ -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() @@ -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)) @@ -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() @@ -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) @@ -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() @@ -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) @@ -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() @@ -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() @@ -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)) }