-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
values.py
244 lines (209 loc) · 8.51 KB
/
values.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""Helpers for handling values"""
from __future__ import annotations
from typing import Any, Callable, Collection, Dict, List, Union
from ..error import GraphQLError
from ..language import (
DirectiveNode,
EnumValueDefinitionNode,
ExecutableDefinitionNode,
FieldDefinitionNode,
FieldNode,
InputValueDefinitionNode,
NullValueNode,
SchemaDefinitionNode,
SelectionNode,
TypeDefinitionNode,
TypeExtensionNode,
VariableDefinitionNode,
VariableNode,
print_ast,
)
from ..pyutils import Undefined, inspect, print_path_list
from ..type import (
GraphQLDirective,
GraphQLField,
GraphQLSchema,
is_input_type,
is_non_null_type,
)
from ..utilities.coerce_input_value import coerce_input_value
from ..utilities.type_from_ast import type_from_ast
from ..utilities.value_from_ast import value_from_ast
try:
from typing import TypeAlias
except ImportError: # Python < 3.10
from typing_extensions import TypeAlias
__all__ = ["get_argument_values", "get_directive_values", "get_variable_values"]
CoercedVariableValues: TypeAlias = Union[List[GraphQLError], Dict[str, Any]]
def get_variable_values(
schema: GraphQLSchema,
var_def_nodes: Collection[VariableDefinitionNode],
inputs: dict[str, Any],
max_errors: int | None = None,
) -> CoercedVariableValues:
"""Get coerced variable values based on provided definitions.
Prepares a dict of variable values of the correct type based on the provided
variable definitions and arbitrary input. If the input cannot be parsed to match
the variable definitions, a GraphQLError will be raised.
"""
errors: list[GraphQLError] = []
def on_error(error: GraphQLError) -> None:
if max_errors is not None and len(errors) >= max_errors:
msg = (
"Too many errors processing variables,"
" error limit reached. Execution aborted."
)
raise GraphQLError(msg)
errors.append(error)
try:
coerced = coerce_variable_values(schema, var_def_nodes, inputs, on_error)
if not errors:
return coerced
except GraphQLError as e:
errors.append(e)
return errors
def coerce_variable_values(
schema: GraphQLSchema,
var_def_nodes: Collection[VariableDefinitionNode],
inputs: dict[str, Any],
on_error: Callable[[GraphQLError], None],
) -> dict[str, Any]:
coerced_values: dict[str, Any] = {}
for var_def_node in var_def_nodes:
var_name = var_def_node.variable.name.value
var_type = type_from_ast(schema, var_def_node.type)
if not is_input_type(var_type):
# Must use input types for variables. This should be caught during
# validation, however is checked again here for safety.
var_type_str = print_ast(var_def_node.type)
on_error(
GraphQLError(
f"Variable '${var_name}' expected value of type '{var_type_str}'"
" which cannot be used as an input type.",
var_def_node.type,
)
)
continue
if var_name not in inputs:
if var_def_node.default_value:
coerced_values[var_name] = value_from_ast(
var_def_node.default_value, var_type
)
elif is_non_null_type(var_type): # pragma: no cover else
var_type_str = inspect(var_type)
on_error(
GraphQLError(
f"Variable '${var_name}' of required type '{var_type_str}'"
" was not provided.",
var_def_node,
)
)
continue
value = inputs[var_name]
if value is None and is_non_null_type(var_type):
var_type_str = inspect(var_type)
on_error(
GraphQLError(
f"Variable '${var_name}' of non-null type '{var_type_str}'"
" must not be null.",
var_def_node,
)
)
continue
def on_input_value_error(
path: list[str | int], invalid_value: Any, error: GraphQLError
) -> None:
invalid_str = inspect(invalid_value)
prefix = f"Variable '${var_name}' got invalid value {invalid_str}" # noqa: B023
if path:
prefix += f" at '{var_name}{print_path_list(path)}'" # noqa: B023
on_error(
GraphQLError(
prefix + "; " + error.message,
var_def_node, # noqa: B023
original_error=error,
)
)
coerced_values[var_name] = coerce_input_value(
value, var_type, on_input_value_error
)
return coerced_values
def get_argument_values(
type_def: GraphQLField | GraphQLDirective,
node: FieldNode | DirectiveNode,
variable_values: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""Get coerced argument values based on provided definitions and nodes.
Prepares a dict of argument values given a list of argument definitions and list
of argument AST nodes.
"""
coerced_values: dict[str, Any] = {}
arg_node_map = {arg.name.value: arg for arg in node.arguments or []}
for name, arg_def in type_def.args.items():
arg_type = arg_def.type
argument_node = arg_node_map.get(name)
if argument_node is None:
if arg_def.default_value is not Undefined:
coerced_values[arg_def.out_name or name] = arg_def.default_value
elif is_non_null_type(arg_type): # pragma: no cover else
msg = (
f"Argument '{name}' of required type '{arg_type}'"
" was not provided."
)
raise GraphQLError(msg, node)
continue # pragma: no cover
value_node = argument_node.value
is_null = isinstance(argument_node.value, NullValueNode)
if isinstance(value_node, VariableNode):
variable_name = value_node.name.value
if variable_values is None or variable_name not in variable_values:
if arg_def.default_value is not Undefined:
coerced_values[arg_def.out_name or name] = arg_def.default_value
elif is_non_null_type(arg_type): # pragma: no cover else
msg = (
f"Argument '{name}' of required type '{arg_type}'"
f" was provided the variable '${variable_name}'"
" which was not provided a runtime value."
)
raise GraphQLError(msg, value_node)
continue # pragma: no cover
is_null = variable_values[variable_name] is None
if is_null and is_non_null_type(arg_type):
msg = f"Argument '{name}' of non-null type '{arg_type}' must not be null."
raise GraphQLError(msg, value_node)
coerced_value = value_from_ast(value_node, arg_type, variable_values)
if coerced_value is Undefined:
# Note: `values_of_correct_type` validation should catch this before
# execution. This is a runtime check to ensure execution does not
# continue with an invalid argument value.
msg = f"Argument '{name}' has invalid value {print_ast(value_node)}."
raise GraphQLError(msg, value_node)
coerced_values[arg_def.out_name or name] = coerced_value
return coerced_values
NodeWithDirective: TypeAlias = Union[
EnumValueDefinitionNode,
ExecutableDefinitionNode,
FieldDefinitionNode,
InputValueDefinitionNode,
SelectionNode,
SchemaDefinitionNode,
TypeDefinitionNode,
TypeExtensionNode,
]
def get_directive_values(
directive_def: GraphQLDirective,
node: NodeWithDirective,
variable_values: dict[str, Any] | None = None,
) -> dict[str, Any] | None:
"""Get coerced argument values based on provided nodes.
Prepares a dict of argument values given a directive definition and an AST node
which may contain directives. Optionally also accepts a dict of variable values.
If the directive does not exist on the node, returns None.
"""
directives = node.directives
if directives:
directive_name = directive_def.name
for directive in directives:
if directive.name.value == directive_name:
return get_argument_values(directive_def, directive, variable_values)
return None