Skip to content

Commit a0bf6f9

Browse files
committed
Fix validators to be aware of type-condition-less inline frags
1 parent b6a326b commit a0bf6f9

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

src/validation/__tests__/FragmentsOnCompositeTypes.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ describe('Validate: Fragments on composite types', () => {
5050
`);
5151
});
5252

53+
it('inline fragment without type is valid', () => {
54+
expectPassesRule(FragmentsOnCompositeTypes, `
55+
fragment validFragment on Pet {
56+
... {
57+
name
58+
}
59+
}
60+
`);
61+
});
62+
5363
it('union is valid fragment type', () => {
5464
expectPassesRule(FragmentsOnCompositeTypes, `
5565
fragment validFragment on CatOrDog {

src/validation/__tests__/OverlappingFieldsCanBeMerged.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,17 @@ describe('Validate: Overlapping fields can be merged', () => {
455455
`);
456456
});
457457

458+
it('allows inline typeless fragments', () => {
459+
expectPassesRuleWithSchema(schema, OverlappingFieldsCanBeMerged, `
460+
{
461+
a
462+
... {
463+
a
464+
}
465+
}
466+
`);
467+
});
468+
458469
it('compares deep types including list', () => {
459470
expectFailsRuleWithSchema(schema, OverlappingFieldsCanBeMerged, `
460471
{

src/validation/rules/FragmentsOnCompositeTypes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function FragmentsOnCompositeTypes(context: ValidationContext): any {
3939
return {
4040
InlineFragment(node) {
4141
var type = context.getType();
42-
if (type && !isCompositeType(type)) {
42+
if (node.typeCondition && type && !isCompositeType(type)) {
4343
return new GraphQLError(
4444
inlineFragmentOnNonCompositeErrorMessage(print(node.typeCondition)),
4545
[ node.typeCondition ]

src/validation/rules/OverlappingFieldsCanBeMerged.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,13 @@ function collectFieldASTsAndDefs(
261261
_astAndDefs[responseName].push([ selection, fieldDef ]);
262262
break;
263263
case INLINE_FRAGMENT:
264+
var typeCondition = selection.typeCondition;
265+
var inlineFragmentType = typeCondition ?
266+
typeFromAST(context.getSchema(), selection.typeCondition) :
267+
parentType;
264268
_astAndDefs = collectFieldASTsAndDefs(
265269
context,
266-
typeFromAST(context.getSchema(), selection.typeCondition),
270+
inlineFragmentType,
267271
selection.selectionSet,
268272
_visitedFragmentNames,
269273
_astAndDefs

0 commit comments

Comments
 (0)