| 1 |
from typing import Any |
| 2 |
|
| 3 |
from ....error import GraphQLError |
| 4 |
from ....language import FieldNode |
| 5 |
from ....type import get_named_type, is_introspection_type |
| 6 |
from .. import ValidationRule |
| 7 |
|
| 8 |
__all__ = ["NoSchemaIntrospectionCustomRule"] |
| 9 |
|
| 10 |
|
| 11 |
class NoSchemaIntrospectionCustomRule(ValidationRule): |
| 12 |
"""Prohibit introspection queries |
| 13 |
|
| 14 |
A GraphQL document is only valid if all fields selected are not fields that |
| 15 |
return an introspection type. |
| 16 |
|
| 17 |
Note: This rule is optional and is not part of the Validation section of the |
| 18 |
GraphQL Specification. This rule effectively disables introspection, which |
| 19 |
does not reflect best practices and should only be done if absolutely necessary. |
| 20 |
""" |
| 21 |
|
| 22 |
def enter_field(self, node: FieldNode, *_args: Any) -> None: |
| 23 |
type_ = get_named_type(self.context.get_type()) |
| 24 |
if type_ and is_introspection_type(type_): |
| 25 |
self.report_error( |
| 26 |
GraphQLError( |
| 27 |
"GraphQL introspection has been disabled, but the requested query" |
| 28 |
f" contained the field '{node.name.value}'.", |
| 29 |
node, |
| 30 |
) |
| 31 |
) |