-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
lone_anonymous_operation.py
41 lines (30 loc) · 1.29 KB
/
lone_anonymous_operation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Lone anonymous operation rule"""
from __future__ import annotations
from typing import Any
from ...error import GraphQLError
from ...language import DocumentNode, OperationDefinitionNode
from . import ASTValidationContext, ASTValidationRule
__all__ = ["LoneAnonymousOperationRule"]
class LoneAnonymousOperationRule(ASTValidationRule):
"""Lone anonymous operation
A GraphQL document is only valid if when it contains an anonymous operation
(the query short-hand) that it contains only that one operation definition.
See https://spec.graphql.org/draft/#sec-Lone-Anonymous-Operation
"""
def __init__(self, context: ASTValidationContext) -> None:
super().__init__(context)
self.operation_count = 0
def enter_document(self, node: DocumentNode, *_args: Any) -> None:
self.operation_count = sum(
isinstance(definition, OperationDefinitionNode)
for definition in node.definitions
)
def enter_operation_definition(
self, node: OperationDefinitionNode, *_args: Any
) -> None:
if not node.name and self.operation_count > 1:
self.report_error(
GraphQLError(
"This anonymous operation must be the only defined operation.", node
)
)