Skip to content

Commit

Permalink
Fix validators to be aware of type-condition-less inline frags
Browse files Browse the repository at this point in the history
Commit:
a0bf6f9bb1155a502649c8de743b4e35841b08b0 [a0bf6f9]
Parents:
b6a326bfd3
Author:
Lee Byron <[email protected]>
Date:
2 October 2015 at 11:14:39 AM SGT
  • Loading branch information
sogko committed Apr 11, 2016
1 parent b54810f commit 72e6332
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
10 changes: 7 additions & 3 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func FragmentsOnCompositeTypesRule(context *ValidationContext) *ValidationRuleIn
Kind: func(p visitor.VisitFuncParams) (string, interface{}) {
if node, ok := p.Node.(*ast.InlineFragment); ok {
ttype := context.Type()
if ttype != nil && !IsCompositeType(ttype) {
if node.TypeCondition != nil && ttype != nil && !IsCompositeType(ttype) {
return reportError(
context,
fmt.Sprintf(`Fragment cannot condition on non composite type "%v".`, ttype),
Expand Down Expand Up @@ -1073,10 +1073,14 @@ func collectFieldASTsAndDefs(context *ValidationContext, parentType Named, selec
FieldDef: fieldDef,
})
case *ast.InlineFragment:
parentType, _ := typeFromAST(*context.Schema(), selection.TypeCondition)
inlineFragmentType := parentType
if selection.TypeCondition != nil {
parentType, _ := typeFromAST(*context.Schema(), selection.TypeCondition)
inlineFragmentType = parentType
}
astAndDefs = collectFieldASTsAndDefs(
context,
parentType,
inlineFragmentType,
selection.SelectionSet,
visitedFragmentNames,
astAndDefs,
Expand Down
9 changes: 9 additions & 0 deletions rules_fragments_on_composite_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ func TestValidate_FragmentsOnCompositeTypes_ObjectIsValidInlineFragmentType(t *t
}
`)
}
func TestValidate_FragmentsOnCompositeTypes_InlineFragmentWithoutTypeIsValid(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.FragmentsOnCompositeTypesRule, `
fragment validFragment on Pet {
... {
name
}
}
`)
}
func TestValidate_FragmentsOnCompositeTypes_UnionIsValidFragmentType(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.FragmentsOnCompositeTypesRule, `
fragment validFragment on CatOrDog {
Expand Down
10 changes: 10 additions & 0 deletions rules_overlapping_fields_can_be_merged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,16 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_Same
}
`)
}
func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_AllowsInlineTypelessFragments(t *testing.T) {
testutil.ExpectPassesRuleWithSchema(t, &schema, graphql.OverlappingFieldsCanBeMergedRule, `
{
a
... {
a
}
}
`)
}
func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_ComparesDeepTypesIncludingList(t *testing.T) {
testutil.ExpectFailsRuleWithSchema(t, &schema, graphql.OverlappingFieldsCanBeMergedRule, `
{
Expand Down

0 comments on commit 72e6332

Please sign in to comment.