Skip to content

Commit

Permalink
Merge pull request #302 from fadi-alkatut/master
Browse files Browse the repository at this point in the history
Validate types are including all fields from implemented interface(s)
  • Loading branch information
pavelnikolov authored Jan 8, 2019
2 parents 0079757 + 27cde2c commit d5b7dc6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ func (s *Schema) Parse(schemaString string, useStringDescriptions bool) error {
if !ok {
return errors.Errorf("type %q is not an interface", intfName)
}
for _, f := range intf.Fields.Names() {
if obj.Fields.Get(f) == nil {
return errors.Errorf("interface %q expects field %q but %q does not provide it", intfName, f, obj.Name)
}
}
obj.Interfaces[i] = intf
intf.PossibleTypes = append(intf.PossibleTypes, obj)
}
Expand Down
23 changes: 23 additions & 0 deletions internal/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package schema_test
import (
"testing"

"github.com/graph-gophers/graphql-go/errors"
"github.com/graph-gophers/graphql-go/internal/schema"
)

Expand Down Expand Up @@ -89,3 +90,25 @@ func TestParse(t *testing.T) {
})
}
}

func TestInvalidInterfaceImpl(t *testing.T) {
var tests = []parseTestCase{{
description: "Parses type Welcome that implements interface Greeting without providing required fields",
sdl: "interface Greeting { message: String! } type Welcome implements Greeting {}",
err: errors.Errorf(`interface "Greeting" expects field "message" but "Welcome" does not provide it`),
}}

setup := func(t *testing.T) *schema.Schema {
t.Helper()
return schema.New()
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
schema := setup(t)
err := schema.Parse(test.sdl, false)
if err == nil || err.Error() != test.err.Error() {
t.Fatal(err)
}
})
}
}

0 comments on commit d5b7dc6

Please sign in to comment.