| 1 |
from typing import Any, Callable, Collection, Dict, List, Optional, Union, cast |
| 2 |
|
| 3 |
from ..error import GraphQLError |
| 4 |
from ..language import ( |
| 5 |
DirectiveDefinitionNode, |
| 6 |
DirectiveExtensionNode, |
| 7 |
DirectiveNode, |
| 8 |
EnumValueDefinitionNode, |
| 9 |
ExecutableDefinitionNode, |
| 10 |
FieldDefinitionNode, |
| 11 |
FieldNode, |
| 12 |
InputValueDefinitionNode, |
| 13 |
NullValueNode, |
| 14 |
SchemaDefinitionNode, |
| 15 |
SelectionNode, |
| 16 |
TypeDefinitionNode, |
| 17 |
TypeExtensionNode, |
| 18 |
VariableDefinitionNode, |
| 19 |
VariableNode, |
| 20 |
print_ast, |
| 21 |
) |
| 22 |
from ..pyutils import Undefined, inspect, print_path_list |
| 23 |
from ..type import ( |
| 24 |
GraphQLDirective, |
| 25 |
GraphQLField, |
| 26 |
GraphQLInputType, |
| 27 |
GraphQLSchema, |
| 28 |
is_input_object_type, |
| 29 |
is_input_type, |
| 30 |
is_non_null_type, |
| 31 |
) |
| 32 |
from ..utilities.coerce_input_value import coerce_input_value |
| 33 |
from ..utilities.type_from_ast import type_from_ast |
| 34 |
from ..utilities.value_from_ast import value_from_ast |
| 35 |
|
| 36 |
__all__ = ["get_argument_values", "get_directive_values", "get_variable_values"] |
| 37 |
|
| 38 |
|
| 39 |
CoercedVariableValues = Union[List[GraphQLError], Dict[str, Any]] |
| 40 |
|
| 41 |
|
| 42 |
def get_variable_values( |
| 43 |
schema: GraphQLSchema, |
| 44 |
var_def_nodes: Collection[VariableDefinitionNode], |
| 45 |
inputs: Dict[str, Any], |
| 46 |
max_errors: Optional[int] = None, |
| 47 |
) -> CoercedVariableValues: |
| 48 |
"""Get coerced variable values based on provided definitions. |
| 49 |
|
| 50 |
Prepares a dict of variable values of the correct type based on the provided |
| 51 |
variable definitions and arbitrary input. If the input cannot be parsed to match |
| 52 |
the variable definitions, a GraphQLError will be raised. |
| 53 |
""" |
| 54 |
errors: List[GraphQLError] = [] |
| 55 |
|
| 56 |
def on_error(error: GraphQLError) -> None: |
| 57 |
if max_errors is not None and len(errors) >= max_errors: |
| 58 |
raise GraphQLError( |
| 59 |
"Too many errors processing variables," |
| 60 |
" error limit reached. Execution aborted." |
| 61 |
) |
| 62 |
errors.append(error) |
| 63 |
|
| 64 |
try: |
| 65 |
coerced = coerce_variable_values(schema, var_def_nodes, inputs, on_error) |
| 66 |
if not errors: |
| 67 |
return coerced |
| 68 |
except GraphQLError as e: |
| 69 |
errors.append(e) |
| 70 |
|
| 71 |
return errors |
| 72 |
|
| 73 |
|
| 74 |
def coerce_variable_values( |
| 75 |
schema: GraphQLSchema, |
| 76 |
var_def_nodes: Collection[VariableDefinitionNode], |
| 77 |
inputs: Dict[str, Any], |
| 78 |
on_error: Callable[[GraphQLError], None], |
| 79 |
) -> Dict[str, Any]: |
| 80 |
coerced_values: Dict[str, Any] = {} |
| 81 |
for var_def_node in var_def_nodes: |
| 82 |
var_name = var_def_node.variable.name.value |
| 83 |
var_type = type_from_ast(schema, var_def_node.type) |
| 84 |
if not is_input_type(var_type): |
| 85 |
# Must use input types for variables. This should be caught during |
| 86 |
# validation, however is checked again here for safety. |
| 87 |
var_type_str = print_ast(var_def_node.type) |
| 88 |
on_error( |
| 89 |
GraphQLError( |
| 90 |
f"Variable '${var_name}' expected value of type '{var_type_str}'" |
| 91 |
" which cannot be used as an input type.", |
| 92 |
var_def_node.type, |
| 93 |
) |
| 94 |
) |
| 95 |
continue |
| 96 |
|
| 97 |
var_type = cast(GraphQLInputType, var_type) |
| 98 |
if var_name not in inputs: |
| 99 |
if var_def_node.default_value: |
| 100 |
coerced_values[var_name] = value_from_ast( |
| 101 |
var_def_node.default_value, var_type |
| 102 |
) |
| 103 |
elif is_non_null_type(var_type): # pragma: no cover else |
| 104 |
var_type_str = inspect(var_type) |
| 105 |
on_error( |
| 106 |
GraphQLError( |
| 107 |
f"Variable '${var_name}' of required type '{var_type_str}'" |
| 108 |
" was not provided.", |
| 109 |
var_def_node, |
| 110 |
) |
| 111 |
) |
| 112 |
continue |
| 113 |
|
| 114 |
value = inputs[var_name] |
| 115 |
if value is None and is_non_null_type(var_type): |
| 116 |
var_type_str = inspect(var_type) |
| 117 |
on_error( |
| 118 |
GraphQLError( |
| 119 |
f"Variable '${var_name}' of non-null type '{var_type_str}'" |
| 120 |
" must not be null.", |
| 121 |
var_def_node, |
| 122 |
) |
| 123 |
) |
| 124 |
continue |
| 125 |
|
| 126 |
def on_input_value_error( |
| 127 |
path: List[Union[str, int]], |
| 128 |
invalid_value: Any, |
| 129 |
error: GraphQLError, |
| 130 |
var_name: str = var_name, |
| 131 |
var_def_node: VariableDefinitionNode = var_def_node, |
| 132 |
) -> None: |
| 133 |
invalid_str = inspect(invalid_value) |
| 134 |
prefix = f"Variable '${var_name}' got invalid value {invalid_str}" |
| 135 |
if path: |
| 136 |
prefix += f" at '{var_name}{print_path_list(path)}'" |
| 137 |
on_error( |
| 138 |
GraphQLError( |
| 139 |
prefix + "; " + error.message, |
| 140 |
var_def_node, |
| 141 |
original_error=error, |
| 142 |
) |
| 143 |
) |
| 144 |
|
| 145 |
coerced_values[var_name] = coerce_input_value( |
| 146 |
value, var_type, on_input_value_error |
| 147 |
) |
| 148 |
|
| 149 |
return coerced_values |
| 150 |
|
| 151 |
|
| 152 |
def get_argument_values( |
| 153 |
type_def: Union[GraphQLField, GraphQLDirective], |
| 154 |
node: Union[FieldNode, DirectiveNode], |
| 155 |
variable_values: Optional[Dict[str, Any]] = None, |
| 156 |
) -> Dict[str, Any]: |
| 157 |
"""Get coerced argument values based on provided definitions and nodes. |
| 158 |
|
| 159 |
Prepares a dict of argument values given a list of argument definitions and list |
| 160 |
of argument AST nodes. |
| 161 |
""" |
| 162 |
coerced_values: Dict[str, Any] = {} |
| 163 |
arg_node_map = {arg.name.value: arg for arg in node.arguments or []} |
| 164 |
|
| 165 |
for name, arg_def in type_def.args.items(): |
| 166 |
arg_type = arg_def.type |
| 167 |
argument_node = arg_node_map.get(name) |
| 168 |
|
| 169 |
if argument_node is None: |
| 170 |
value = arg_def.default_value |
| 171 |
if value is not Undefined: |
| 172 |
if is_input_object_type(arg_def.type): |
| 173 |
# coerce input value so that out_names are used |
| 174 |
value = coerce_input_value(value, arg_def.type) |
| 175 |
|
| 176 |
coerced_values[arg_def.out_name or name] = value |
| 177 |
elif is_non_null_type(arg_type): # pragma: no cover else |
| 178 |
raise GraphQLError( |
| 179 |
f"Argument '{name}' of required type '{arg_type}'" |
| 180 |
" was not provided.", |
| 181 |
node, |
| 182 |
) |
| 183 |
continue # pragma: no cover |
| 184 |
|
| 185 |
value_node = argument_node.value |
| 186 |
is_null = isinstance(argument_node.value, NullValueNode) |
| 187 |
|
| 188 |
if isinstance(value_node, VariableNode): |
| 189 |
variable_name = value_node.name.value |
| 190 |
if variable_values is None or variable_name not in variable_values: |
| 191 |
value = arg_def.default_value |
| 192 |
if value is not Undefined: |
| 193 |
if is_input_object_type(arg_def.type): |
| 194 |
# coerce input value so that out_names are used |
| 195 |
value = coerce_input_value(value, arg_def.type) |
| 196 |
coerced_values[arg_def.out_name or name] = value |
| 197 |
elif is_non_null_type(arg_type): # pragma: no cover else |
| 198 |
raise GraphQLError( |
| 199 |
f"Argument '{name}' of required type '{arg_type}'" |
| 200 |
f" was provided the variable '${variable_name}'" |
| 201 |
" which was not provided a runtime value.", |
| 202 |
value_node, |
| 203 |
) |
| 204 |
continue # pragma: no cover |
| 205 |
variable_value = variable_values[variable_name] |
| 206 |
is_null = variable_value is None or variable_value is Undefined |
| 207 |
|
| 208 |
if is_null and is_non_null_type(arg_type): |
| 209 |
raise GraphQLError( |
| 210 |
f"Argument '{name}' of non-null type '{arg_type}' must not be null.", |
| 211 |
value_node, |
| 212 |
) |
| 213 |
|
| 214 |
coerced_value = value_from_ast(value_node, arg_type, variable_values) |
| 215 |
if coerced_value is Undefined: |
| 216 |
# Note: `values_of_correct_type` validation should catch this before |
| 217 |
# execution. This is a runtime check to ensure execution does not |
| 218 |
# continue with an invalid argument value. |
| 219 |
raise GraphQLError( |
| 220 |
f"Argument '{name}' has invalid value {print_ast(value_node)}.", |
| 221 |
value_node, |
| 222 |
) |
| 223 |
coerced_values[arg_def.out_name or name] = coerced_value |
| 224 |
|
| 225 |
return coerced_values |
| 226 |
|
| 227 |
|
| 228 |
NodeWithDirective = Union[ |
| 229 |
DirectiveDefinitionNode, |
| 230 |
DirectiveExtensionNode, |
| 231 |
EnumValueDefinitionNode, |
| 232 |
ExecutableDefinitionNode, |
| 233 |
FieldDefinitionNode, |
| 234 |
InputValueDefinitionNode, |
| 235 |
SelectionNode, |
| 236 |
SchemaDefinitionNode, |
| 237 |
TypeDefinitionNode, |
| 238 |
TypeExtensionNode, |
| 239 |
] |
| 240 |
|
| 241 |
|
| 242 |
def get_directive_values( |
| 243 |
directive_def: GraphQLDirective, |
| 244 |
node: NodeWithDirective, |
| 245 |
variable_values: Optional[Dict[str, Any]] = None, |
| 246 |
) -> Optional[Dict[str, Any]]: |
| 247 |
"""Get coerced argument values based on provided nodes. |
| 248 |
|
| 249 |
Prepares a dict of argument values given a directive definition and an AST node |
| 250 |
which may contain directives. Optionally also accepts a dict of variable values. |
| 251 |
|
| 252 |
If the directive does not exist on the node, returns None. |
| 253 |
""" |
| 254 |
directives = node.directives |
| 255 |
if directives: |
| 256 |
directive_name = directive_def.name |
| 257 |
for directive in directives: |
| 258 |
if directive.name.value == directive_name: |
| 259 |
return get_argument_values(directive_def, directive, variable_values) |
| 260 |
return None |