|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -from typing import TYPE_CHECKING, Any, cast |
| 5 | +from typing import TYPE_CHECKING, Any |
6 | 6 |
|
7 | 7 | from ...error import GraphQLError |
| 8 | +from ...language import ( |
| 9 | + FieldNode, |
| 10 | + FragmentDefinitionNode, |
| 11 | + FragmentSpreadNode, |
| 12 | + InlineFragmentNode, |
| 13 | + OperationType, |
| 14 | +) |
8 | 15 | from ...type import GraphQLDeferDirective, GraphQLStreamDirective |
9 | | -from . import ASTValidationRule, ValidationContext |
| 16 | +from . import ValidationRule |
10 | 17 |
|
11 | 18 | if TYPE_CHECKING: |
12 | | - from ...language import DirectiveNode, Node |
| 19 | + from ...language import ( |
| 20 | + DirectiveNode, |
| 21 | + OperationDefinitionNode, |
| 22 | + SelectionSetNode, |
| 23 | + ) |
| 24 | + from ...type import GraphQLObjectType |
13 | 25 |
|
14 | 26 | __all__ = ["DeferStreamDirectiveOnRootField"] |
15 | 27 |
|
16 | 28 |
|
17 | | -class DeferStreamDirectiveOnRootField(ASTValidationRule): |
| 29 | +def get_directive( |
| 30 | + node: FieldNode | FragmentSpreadNode | InlineFragmentNode, name: str |
| 31 | +) -> DirectiveNode | None: |
| 32 | + for directive in node.directives or (): |
| 33 | + if directive.name.value == name: |
| 34 | + return directive |
| 35 | + return None |
| 36 | + |
| 37 | + |
| 38 | +class DeferStreamDirectiveOnRootField(ValidationRule): |
18 | 39 | """Defer and stream directives are used on valid root field |
19 | 40 |
|
20 | 41 | A GraphQL document is only valid if defer directives are not used on root |
21 | 42 | mutation or subscription types. |
22 | 43 | """ |
23 | 44 |
|
24 | | - def enter_directive( |
25 | | - self, |
26 | | - node: DirectiveNode, |
27 | | - _key: Any, |
28 | | - _parent: Any, |
29 | | - _path: Any, |
30 | | - _ancestors: list[Node], |
| 45 | + def enter_operation_definition( |
| 46 | + self, node: OperationDefinitionNode, *_args: Any |
31 | 47 | ) -> None: |
32 | | - context = cast("ValidationContext", self.context) |
33 | | - parent_type = context.get_parent_type() |
34 | | - if not parent_type: |
| 48 | + operation = node.operation |
| 49 | + if operation not in (OperationType.SUBSCRIPTION, OperationType.MUTATION): |
35 | 50 | return |
36 | | - schema = context.schema |
37 | | - mutation_type = schema.mutation_type |
38 | | - subscription_type = schema.subscription_type |
| 51 | + schema = self.context.schema |
| 52 | + root_type = schema.get_root_type(operation) |
| 53 | + if root_type: |
| 54 | + document = self.context.document |
| 55 | + fragments = { |
| 56 | + definition.name.value: definition |
| 57 | + for definition in document.definitions |
| 58 | + if isinstance(definition, FragmentDefinitionNode) |
| 59 | + } |
| 60 | + self.forbid_defer_stream( |
| 61 | + operation, root_type, fragments, node.selection_set, set() |
| 62 | + ) |
39 | 63 |
|
40 | | - if node.name.value == GraphQLDeferDirective.name: |
41 | | - if mutation_type and parent_type is mutation_type: |
42 | | - self.report_error( |
43 | | - GraphQLError( |
44 | | - "Defer directive cannot be used on root" |
45 | | - f" mutation type '{parent_type.name}'.", |
46 | | - node, |
47 | | - ) |
48 | | - ) |
49 | | - if subscription_type and parent_type is subscription_type: |
50 | | - self.report_error( |
51 | | - GraphQLError( |
52 | | - "Defer directive cannot be used on root" |
53 | | - f" subscription type '{parent_type.name}'.", |
54 | | - node, |
| 64 | + def forbid_defer_stream( |
| 65 | + self, |
| 66 | + operation_type: OperationType, |
| 67 | + root_type: GraphQLObjectType, |
| 68 | + fragments: dict[str, FragmentDefinitionNode], |
| 69 | + selection_set: SelectionSetNode, |
| 70 | + visited_fragments: set[str], |
| 71 | + ) -> None: |
| 72 | + for selection in selection_set.selections: |
| 73 | + if isinstance(selection, FieldNode): |
| 74 | + stream = get_directive(selection, GraphQLStreamDirective.name) |
| 75 | + if stream: |
| 76 | + self.report_error( |
| 77 | + GraphQLError( |
| 78 | + "Stream directive cannot be used on root" |
| 79 | + f" {operation_type.value} type '{root_type.name}'.", |
| 80 | + stream, |
| 81 | + ) |
55 | 82 | ) |
56 | | - ) |
57 | | - if node.name.value == GraphQLStreamDirective.name: |
58 | | - if mutation_type and parent_type is mutation_type: |
59 | | - self.report_error( |
60 | | - GraphQLError( |
61 | | - "Stream directive cannot be used on root" |
62 | | - f" mutation type '{parent_type.name}'.", |
63 | | - node, |
| 83 | + elif isinstance(selection, FragmentSpreadNode): |
| 84 | + fragment_name = selection.name.value |
| 85 | + if fragment_name in visited_fragments: |
| 86 | + continue |
| 87 | + fragment = fragments.get(fragment_name) |
| 88 | + if fragment: |
| 89 | + defer = get_directive(selection, GraphQLDeferDirective.name) |
| 90 | + if defer: |
| 91 | + self.report_error( |
| 92 | + GraphQLError( |
| 93 | + "Defer directive cannot be used on root" |
| 94 | + f" {operation_type.value} type '{root_type.name}'.", |
| 95 | + defer, |
| 96 | + ) |
| 97 | + ) |
| 98 | + self.forbid_defer_stream( |
| 99 | + operation_type, |
| 100 | + root_type, |
| 101 | + fragments, |
| 102 | + fragment.selection_set, |
| 103 | + visited_fragments, |
64 | 104 | ) |
65 | | - ) |
66 | | - if subscription_type and parent_type is subscription_type: |
67 | | - self.report_error( |
68 | | - GraphQLError( |
69 | | - "Stream directive cannot be used on root" |
70 | | - f" subscription type '{parent_type.name}'.", |
71 | | - node, |
| 105 | + visited_fragments.add(fragment_name) |
| 106 | + elif isinstance(selection, InlineFragmentNode): |
| 107 | + defer = get_directive(selection, GraphQLDeferDirective.name) |
| 108 | + if defer: |
| 109 | + self.report_error( |
| 110 | + GraphQLError( |
| 111 | + "Defer directive cannot be used on root" |
| 112 | + f" {operation_type.value} type '{root_type.name}'.", |
| 113 | + defer, |
| 114 | + ) |
72 | 115 | ) |
| 116 | + self.forbid_defer_stream( |
| 117 | + operation_type, |
| 118 | + root_type, |
| 119 | + fragments, |
| 120 | + selection.selection_set, |
| 121 | + visited_fragments, |
73 | 122 | ) |
0 commit comments