Skip to content

Commit

Permalink
Modify to prevent unreachable code from occurring (#2846)
Browse files Browse the repository at this point in the history
* fix: 型の数でソートする処理を追加

* 戻し

* fix: case文の最初にスーパークラスが来ないようにする

* testdata追加

* fix: Added sorting by number of types.
* fix: Prevent superclass from appearing at the beginning of case statement
  • Loading branch information
taako-502 committed Dec 7, 2023
1 parent 68744ad commit e080a96
Show file tree
Hide file tree
Showing 8 changed files with 536 additions and 1 deletion.
9 changes: 8 additions & 1 deletion codegen/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codegen
import (
"fmt"
"go/types"
"sort"

"github.com/vektah/gqlparser/v2/ast"

Expand Down Expand Up @@ -40,7 +41,13 @@ func (b *builder) buildInterface(typ *ast.Definition) (*Interface, error) {
return nil, fmt.Errorf("%s is not an interface", i.Type)
}

for _, implementor := range b.Schema.GetPossibleTypes(typ) {
// Sort so that more specific types are evaluated first.
implementors := b.Schema.GetPossibleTypes(typ)
sort.Slice(implementors, func(i, j int) bool {
return len(implementors[i].Interfaces) > len(implementors[j].Interfaces)
})

for _, implementor := range implementors {
obj, err := b.Binder.DefaultUserObject(implementor.Name)
if err != nil {
return nil, fmt.Errorf("%s has no backing go type", implementor.Name)
Expand Down
206 changes: 206 additions & 0 deletions codegen/testserver/followschema/interfaces.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions codegen/testserver/followschema/interfaces.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ type ConcreteNodeInterface implements Node {
id: ID!
child: Node!
}

interface Mammalian implements Animal {
species: String!
size: Size!
}

# Types with multiple interfaces are evaluated first in the case statement
type Horse implements Mammalian & Animal {
species: String!
size: Size!
horseBreed: String!
}
19 changes: 19 additions & 0 deletions codegen/testserver/followschema/models-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions codegen/testserver/followschema/root_.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e080a96

Please sign in to comment.