Skip to content

Commit ba1a314

Browse files
committed
Assert subscription field is not introspection.
1 parent cd273ad commit ba1a314

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/validation/rules/SingleFieldSubscriptionsRule.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import type { OperationDefinitionNode } from '../../language/ast';
66
import type { ASTValidationContext } from '../ValidationContext';
77

88
/**
9-
* Subscriptions must only include one field.
9+
* Subscriptions must only include a non-introspection field.
1010
*
11-
* A GraphQL subscription is valid only if it contains a single root field.
11+
* A GraphQL subscription is valid only if it contains a single root field and
12+
* that root field is not an introspection field.
1213
*/
1314
export function SingleFieldSubscriptionsRule(
1415
context: ASTValidationContext,
@@ -25,6 +26,20 @@ export function SingleFieldSubscriptionsRule(
2526
node.selectionSet.selections.slice(1),
2627
),
2728
);
29+
} else {
30+
const selection = node.selectionSet.selections[0];
31+
const fieldName = selection[0].name.value;
32+
// fieldName represents an introspection field if it starts with `__`
33+
if (fieldName[0] === '_' && fieldName[1] === '_') {
34+
context.reportError(
35+
new GraphQLError(
36+
node.name
37+
? `Subscription "${node.name.value}" must not select an introspection top level field.`
38+
: 'Anonymous Subscription must not select an introspection top level field.',
39+
node.selectionSet.selections,
40+
),
41+
);
42+
}
2843
}
2944
}
3045
},

0 commit comments

Comments
 (0)