| 1 |
from typing import Any |
| 2 |
|
| 3 |
from ...error import GraphQLError |
| 4 |
from ...language import SchemaDefinitionNode |
| 5 |
from . import SDLValidationRule, SDLValidationContext |
| 6 |
|
| 7 |
__all__ = ["LoneSchemaDefinitionRule"] |
| 8 |
|
| 9 |
|
| 10 |
class LoneSchemaDefinitionRule(SDLValidationRule): |
| 11 |
"""Lone Schema definition |
| 12 |
|
| 13 |
A GraphQL document is only valid if it contains only one schema definition. |
| 14 |
""" |
| 15 |
|
| 16 |
def __init__(self, context: SDLValidationContext): |
| 17 |
super().__init__(context) |
| 18 |
old_schema = context.schema |
| 19 |
self.already_defined = old_schema and ( |
| 20 |
old_schema.ast_node |
| 21 |
or old_schema.query_type |
| 22 |
or old_schema.mutation_type |
| 23 |
or old_schema.subscription_type |
| 24 |
) |
| 25 |
self.schema_definitions_count = 0 |
| 26 |
|
| 27 |
def enter_schema_definition(self, node: SchemaDefinitionNode, *_args: Any) -> None: |
| 28 |
if self.already_defined: |
| 29 |
self.report_error( |
| 30 |
GraphQLError( |
| 31 |
"Cannot define a new schema within a schema extension.", node |
| 32 |
) |
| 33 |
) |
| 34 |
else: |
| 35 |
if self.schema_definitions_count: |
| 36 |
self.report_error( |
| 37 |
GraphQLError("Must provide only one schema definition.", node) |
| 38 |
) |
| 39 |
self.schema_definitions_count += 1 |