-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
directives.py
181 lines (157 loc) · 5.97 KB
/
directives.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from typing import Any, Dict, Sequence, cast
from ..language import ast, DirectiveLocation
from ..pyutils import inspect
from .definition import GraphQLArgument, GraphQLInputType, GraphQLNonNull, is_input_type
from .scalars import GraphQLBoolean, GraphQLString
__all__ = [
"is_directive",
"assert_directive",
"is_specified_directive",
"specified_directives",
"GraphQLDirective",
"GraphQLIncludeDirective",
"GraphQLSkipDirective",
"GraphQLDeprecatedDirective",
"DirectiveLocation",
"DEFAULT_DEPRECATION_REASON",
]
class GraphQLDirective:
"""GraphQL Directive
Directives are used by the GraphQL runtime as a way of modifying execution behavior.
Type system creators will usually not create these directly.
"""
def __init__(
self,
name: str,
locations: Sequence[DirectiveLocation],
args: Dict[str, GraphQLArgument] = None,
description: str = None,
ast_node: ast.DirectiveDefinitionNode = None,
) -> None:
if not name:
raise TypeError("Directive must be named.")
elif not isinstance(name, str):
raise TypeError("The directive name must be a string.")
if not isinstance(locations, (list, tuple)):
raise TypeError(f"{name} locations must be a list/tuple.")
if not all(isinstance(value, DirectiveLocation) for value in locations):
try:
locations = [
value
if isinstance(value, DirectiveLocation)
else DirectiveLocation[value]
for value in locations
]
except (KeyError, TypeError):
raise TypeError(f"{name} locations must be DirectiveLocation objects.")
if args is None:
args = {}
elif not isinstance(args, dict) or not all(
isinstance(key, str) for key in args
):
raise TypeError(f"{name} args must be a dict with argument names as keys.")
elif not all(
isinstance(value, GraphQLArgument) or is_input_type(value)
for value in args.values()
):
raise TypeError(
f"{name} args must be GraphQLArgument or input type objects."
)
else:
args = {
name: cast(GraphQLArgument, value)
if isinstance(value, GraphQLArgument)
else GraphQLArgument(cast(GraphQLInputType, value))
for name, value in args.items()
}
if description is not None and not isinstance(description, str):
raise TypeError(f"{name} description must be a string.")
if ast_node and not isinstance(ast_node, ast.DirectiveDefinitionNode):
raise TypeError(f"{name} AST node must be a DirectiveDefinitionNode.")
self.name = name
self.locations = locations
self.args = args
self.description = description
self.ast_node = ast_node
def __str__(self):
return f"@{self.name}"
def __repr__(self):
return f"<{self.__class__.__name__}({self})>"
def to_kwargs(self) -> Dict[str, Any]:
return dict(
name=self.name,
locations=self.locations,
args=self.args,
description=self.description,
ast_node=self.ast_node,
)
def is_directive(directive: Any) -> bool:
"""Test if the given value is a GraphQL directive."""
return isinstance(directive, GraphQLDirective)
def assert_directive(directive: Any) -> GraphQLDirective:
if not is_directive(directive):
raise TypeError(f"Expected {inspect(directive)} to be a GraphQL directive.")
return cast(GraphQLDirective, directive)
# Used to conditionally include fields or fragments.
GraphQLIncludeDirective = GraphQLDirective(
name="include",
locations=[
DirectiveLocation.FIELD,
DirectiveLocation.FRAGMENT_SPREAD,
DirectiveLocation.INLINE_FRAGMENT,
],
args={
"if": GraphQLArgument(
GraphQLNonNull(GraphQLBoolean), description="Included when true."
)
},
description="Directs the executor to include this field or fragment"
" only when the `if` argument is true.",
)
# Used to conditionally skip (exclude) fields or fragments:
GraphQLSkipDirective = GraphQLDirective(
name="skip",
locations=[
DirectiveLocation.FIELD,
DirectiveLocation.FRAGMENT_SPREAD,
DirectiveLocation.INLINE_FRAGMENT,
],
args={
"if": GraphQLArgument(
GraphQLNonNull(GraphQLBoolean), description="Skipped when true."
)
},
description="Directs the executor to skip this field or fragment"
" when the `if` argument is true.",
)
# Constant string used for default reason for a deprecation:
DEFAULT_DEPRECATION_REASON = "No longer supported"
# Used to declare element of a GraphQL schema as deprecated:
GraphQLDeprecatedDirective = GraphQLDirective(
name="deprecated",
locations=[DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.ENUM_VALUE],
args={
"reason": GraphQLArgument(
GraphQLString,
description="Explains why this element was deprecated,"
" usually also including a suggestion for how to access"
" supported similar data."
" Formatted using the Markdown syntax, as specified by"
" [CommonMark](https://commonmark.org/).",
default_value=DEFAULT_DEPRECATION_REASON,
)
},
description="Marks an element of a GraphQL schema as no longer supported.",
)
# The full list of specified directives.
specified_directives = (
GraphQLIncludeDirective,
GraphQLSkipDirective,
GraphQLDeprecatedDirective,
)
def is_specified_directive(directive: Any) -> bool:
"""Check whether the given directive is one of the specified directives."""
return is_directive(directive) and any(
specified_directive.name == directive.name
for specified_directive in specified_directives
)