Skip to content

Commit

Permalink
Assert subscription field is not introspection.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Nov 25, 2020
1 parent cd273ad commit ba1a314
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/validation/rules/SingleFieldSubscriptionsRule.js
Expand Up @@ -6,9 +6,10 @@ import type { OperationDefinitionNode } from '../../language/ast';
import type { ASTValidationContext } from '../ValidationContext';

/**
* Subscriptions must only include one field.
* Subscriptions must only include a non-introspection field.
*
* A GraphQL subscription is valid only if it contains a single root field.
* A GraphQL subscription is valid only if it contains a single root field and
* that root field is not an introspection field.
*/
export function SingleFieldSubscriptionsRule(
context: ASTValidationContext,
Expand All @@ -25,6 +26,20 @@ export function SingleFieldSubscriptionsRule(
node.selectionSet.selections.slice(1),
),
);
} else {
const selection = node.selectionSet.selections[0];
const fieldName = selection[0].name.value;
// fieldName represents an introspection field if it starts with `__`
if (fieldName[0] === '_' && fieldName[1] === '_') {
context.reportError(
new GraphQLError(
node.name
? `Subscription "${node.name.value}" must not select an introspection top level field.`
: 'Anonymous Subscription must not select an introspection top level field.',
node.selectionSet.selections,
),
);
}
}
}
},
Expand Down

0 comments on commit ba1a314

Please sign in to comment.