Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "Interfaces Implementing Interfaces" #471

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4220,3 +4220,54 @@ func TestQueryVariablesValidation(t *testing.T) {
}},
}})
}

type interfaceImplementingInterfaceResolver struct{}
type interfaceImplementingInterfaceExample struct {
A string
B string
C bool
}

func (r *interfaceImplementingInterfaceResolver) Hey() *interfaceImplementingInterfaceExample {
return &interfaceImplementingInterfaceExample{
A: "testing",
B: "test",
C: true,
}
}

func TestInterfaceImplementingInterface(t *testing.T) {
gqltesting.RunTests(t, []*gqltesting.Test{{
Schema: graphql.MustParseSchema(`
interface A {
a: String!
}
interface B implements A {
a: String!
b: String!
}
interface C implements B & A {
a: String!
b: String!
c: Boolean!
}
type ABC implements C {
a: String!
b: String!
c: Boolean!
}
type Query {
hey: ABC
}`, &interfaceImplementingInterfaceResolver{}, graphql.UseFieldResolvers(), graphql.UseFieldResolvers()),
Query: `query {hey { a b c }}`,
ExpectedResult: `
{
"hey": {
"a": "testing",
"b": "test",
"c": true
}
}
`,
}})
}
37 changes: 37 additions & 0 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,33 @@ func Parse(s *types.Schema, schemaString string, useStringDescriptions bool) err
s.EntryPoints[key] = t
}

// Interface types need validation: https://spec.graphql.org/draft/#sec-Interfaces.Interfaces-Implementing-Interfaces
for _, typeDef := range s.Types {
switch t := typeDef.(type) {
case *types.InterfaceTypeDefinition:
for i, implements := range t.Interfaces {
typ, ok := s.Types[implements.Name]
if !ok {
return errors.Errorf("interface %q not found", implements)
}
inteface, ok := typ.(*types.InterfaceTypeDefinition)
if !ok {
return errors.Errorf("type %q is not an interface", inteface)
}

for _, f := range inteface.Fields.Names() {
if t.Fields.Get(f) == nil {
return errors.Errorf("interface %q expects field %q but %q does not provide it", inteface.Name, f, t.Name)
}
}

t.Interfaces[i] = inteface
}
default:
continue
}
}

for _, obj := range s.Objects {
obj.Interfaces = make([]*types.InterfaceTypeDefinition, len(obj.InterfaceNames))
if err := resolveDirectives(s, obj.Directives, "OBJECT"); err != nil {
Expand Down Expand Up @@ -406,6 +433,16 @@ func parseObjectDef(l *common.Lexer) *types.ObjectTypeDefinition {
func parseInterfaceDef(l *common.Lexer) *types.InterfaceTypeDefinition {
i := &types.InterfaceTypeDefinition{Loc: l.Location(), Name: l.ConsumeIdent()}

if l.Peek() == scanner.Ident {
l.ConsumeKeyword("implements")
i.Interfaces = append(i.Interfaces, &types.InterfaceTypeDefinition{Name: l.ConsumeIdent()})

for l.Peek() == '&' {
l.ConsumeToken('&')
i.Interfaces = append(i.Interfaces, &types.InterfaceTypeDefinition{Name: l.ConsumeIdent()})
}
}

i.Directives = common.ParseDirectives(l)

l.ConsumeToken('{')
Expand Down
134 changes: 134 additions & 0 deletions internal/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,3 +873,137 @@ Second line of the description.
})
}
}

func TestInterfaceImplementsInterface(t *testing.T) {
for _, tt := range []struct {
name string
sdl string
useStringDescriptions bool
pavelnikolov marked this conversation as resolved.
Show resolved Hide resolved
validateError func(err error) error
pavelnikolov marked this conversation as resolved.
Show resolved Hide resolved
validateSchema func(s *types.Schema) error
}{
{
name: "Parses interface implementing other interface",
sdl: `
interface Foo {
field: String!
}
interface Bar implements Foo {
field: String!
}
`,
validateSchema: func(s *types.Schema) error {
const implementedInterfaceName = "Bar"
typ, ok := s.Types[implementedInterfaceName].(*types.InterfaceTypeDefinition)
if !ok {
return fmt.Errorf("interface %q not found", implementedInterfaceName)
}
if len(typ.Fields) != 1 {
return fmt.Errorf("invalid number of fields: want %d, have %d", 1, len(typ.Fields))
}
const fieldName = "field"

if typ.Fields[0].Name != fieldName {
return fmt.Errorf("field %q not found", fieldName)
}

if len(typ.Interfaces) != 1 {
return fmt.Errorf("invalid number of implementing interfaces found on %q: want %d, have %d", implementedInterfaceName, 1, len(typ.Interfaces))
}

const implementingInterfaceName = "Foo"
if typ.Interfaces[0].Name != implementingInterfaceName {
return fmt.Errorf("interface %q not found", implementingInterfaceName)
}

return nil
},
},
{
name: "Parses interface transitively implementing an interface that implements an interface",
sdl: `
interface Foo {
field: String!
}
interface Bar implements Foo {
field: String!
}
interface Baz implements Bar & Foo {
field: String!
}
`,
validateSchema: func(s *types.Schema) error {
const implementedInterfaceName = "Baz"
typ, ok := s.Types[implementedInterfaceName].(*types.InterfaceTypeDefinition)
if !ok {
return fmt.Errorf("interface %q not found", implementedInterfaceName)
}
if len(typ.Fields) != 1 {
return fmt.Errorf("invalid number of fields: want %d, have %d", 1, len(typ.Fields))
}
const fieldName = "field"

if typ.Fields[0].Name != fieldName {
return fmt.Errorf("field %q not found", fieldName)
}

if len(typ.Interfaces) != 2 {
return fmt.Errorf("invalid number of implementing interfaces found on %q: want %d, have %d", implementedInterfaceName, 2, len(typ.Interfaces))
}

const firstImplementingInterfaceName = "Bar"
if typ.Interfaces[0].Name != firstImplementingInterfaceName {
return fmt.Errorf("first interface %q not found", firstImplementingInterfaceName)
}

const secondImplementingInterfaceName = "Foo"
if typ.Interfaces[1].Name != secondImplementingInterfaceName {
return fmt.Errorf("second interface %q not found", secondImplementingInterfaceName)
}

return nil
},
},
{
name: "Transitively implemented interfaces must also be defined on an implementing type or interface",
sdl: `
interface A {
message: String!
}
interface B implements A {
message: String!
name: String!
}
interface C implements B {
message: String!
name: String!
hug: Boolean!
}
`,
validateError: func(err error) error {
msg := `graphql: interface "C" must explicitly implement transitive interface "A"`
if err == nil || err.Error() != msg {
return fmt.Errorf("expected error %q, but got %q", msg, err)
}
return nil
},
},
} {
t.Run(tt.name, func(t *testing.T) {
s, err := schema.ParseSchema(tt.sdl, tt.useStringDescriptions)
if err != nil {
if tt.validateError == nil {
t.Fatal(err)
}
if err := tt.validateError(err); err != nil {
t.Fatal(err)
}
}
if tt.validateSchema != nil {
if err := tt.validateSchema(s); err != nil {
t.Fatal(err)
}
}
})
}
}
4 changes: 3 additions & 1 deletion types/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package types

import "github.com/graph-gophers/graphql-go/errors"

// InterfaceTypeDefinition represents a list of named fields and their arguments.
// InterfaceTypeDefinition recusrively defines list of named fields with their arguments via the
// implementation chain of interfaces.
//
// GraphQL objects can then implement these interfaces which requires that the object type will
// define all fields defined by those interfaces.
Expand All @@ -15,6 +16,7 @@ type InterfaceTypeDefinition struct {
Desc string
Directives DirectiveList
Loc errors.Location
Interfaces []*InterfaceTypeDefinition
}

func (*InterfaceTypeDefinition) Kind() string { return "INTERFACE" }
Expand Down