Skip to content

Commit

Permalink
fix validation of interface fragments in the interface scope (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
devsergiy authored Apr 15, 2024
1 parent ecf0cae commit b6a7686
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
19 changes: 18 additions & 1 deletion v2/pkg/ast/ast_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"log"
"slices"

"github.com/wundergraph/graphql-go-tools/v2/pkg/internal/unsafebytes"
)
Expand Down Expand Up @@ -504,7 +505,8 @@ func (d *Document) NodeFragmentIsAllowedOnInterfaceTypeDefinition(fragmentNode,
case NodeKindObjectTypeDefinition:
return d.NodeImplementsInterfaceFields(fragmentNode, interfaceTypeNode)
case NodeKindInterfaceTypeDefinition:
return bytes.Equal(d.InterfaceTypeDefinitionNameBytes(fragmentNode.Ref), d.InterfaceTypeDefinitionNameBytes(interfaceTypeNode.Ref))
return bytes.Equal(d.InterfaceTypeDefinitionNameBytes(fragmentNode.Ref), d.InterfaceTypeDefinitionNameBytes(interfaceTypeNode.Ref)) ||
d.InterfaceNodeIntersectsInterfaceNode(fragmentNode, interfaceTypeNode)
case NodeKindUnionTypeDefinition:
return d.UnionNodeIntersectsInterfaceNode(fragmentNode, interfaceTypeNode)
}
Expand Down Expand Up @@ -554,3 +556,18 @@ func (d *Document) UnionNodeIntersectsInterfaceNode(unionNode, interfaceNode Nod
}
return false
}

func (d *Document) InterfaceNodeIntersectsInterfaceNode(interfaceNodeA, interfaceNodeB Node) bool {
typeNamesImplementingInterfaceA, _ := d.InterfaceTypeDefinitionImplementedByObjectWithNames(interfaceNodeA.Ref)
typeNamesImplementingInterfaceB, _ := d.InterfaceTypeDefinitionImplementedByObjectWithNames(interfaceNodeB.Ref)

hasIntersection := false
for _, typeNameB := range typeNamesImplementingInterfaceB {
if slices.Contains(typeNamesImplementingInterfaceA, typeNameB) {
hasIntersection = true
break
}
}

return hasIntersection
}
41 changes: 41 additions & 0 deletions v2/pkg/astvalidation/operation_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2799,6 +2799,47 @@ func TestExecutionValidation(t *testing.T) {
}`,
Fragments(), Invalid)
})
t.Run("interface into interface", func(t *testing.T) {
runWithDefinition(t,
`
scalar String
schema {
query: Query
}
interface Title {
title: String
}
interface Name {
name: String
}
type A implements Title & Name {
title: String
name: String
}
type B implements Name {
name: String
}
type Query {
titles: [Title]
}
`,
`
{
titles {
title
... on Name {
name
}
}
}`,
Fragments(), Valid)
})
})
})
})
Expand Down

0 comments on commit b6a7686

Please sign in to comment.