| 1 |
from typing import Any, Callable, Dict, List, Optional, Union, cast |
| 2 |
|
| 3 |
|
| 4 |
from ..error import GraphQLError |
| 5 |
from ..pyutils import ( |
| 6 |
Path, |
| 7 |
did_you_mean, |
| 8 |
inspect, |
| 9 |
is_iterable, |
| 10 |
print_path_list, |
| 11 |
suggestion_list, |
| 12 |
Undefined, |
| 13 |
) |
| 14 |
from ..type import ( |
| 15 |
GraphQLInputObjectType, |
| 16 |
GraphQLInputType, |
| 17 |
GraphQLList, |
| 18 |
GraphQLScalarType, |
| 19 |
is_leaf_type, |
| 20 |
is_input_object_type, |
| 21 |
is_list_type, |
| 22 |
is_non_null_type, |
| 23 |
GraphQLNonNull, |
| 24 |
) |
| 25 |
|
| 26 |
__all__ = ["coerce_input_value"] |
| 27 |
|
| 28 |
|
| 29 |
OnErrorCB = Callable[[List[Union[str, int]], Any, GraphQLError], None] |
| 30 |
|
| 31 |
|
| 32 |
def default_on_error( |
| 33 |
path: List[Union[str, int]], invalid_value: Any, error: GraphQLError |
| 34 |
) -> None: |
| 35 |
error_prefix = "Invalid value " + inspect(invalid_value) |
| 36 |
if path: |
| 37 |
error_prefix += f" at 'value{print_path_list(path)}'" |
| 38 |
error.message = error_prefix + ": " + error.message |
| 39 |
raise error |
| 40 |
|
| 41 |
|
| 42 |
def coerce_input_value( |
| 43 |
input_value: Any, |
| 44 |
type_: GraphQLInputType, |
| 45 |
on_error: OnErrorCB = default_on_error, |
| 46 |
path: Optional[Path] = None, |
| 47 |
) -> Any: |
| 48 |
"""Coerce a Python value given a GraphQL Input Type.""" |
| 49 |
if is_non_null_type(type_): |
| 50 |
if input_value is not None and input_value is not Undefined: |
| 51 |
type_ = cast(GraphQLNonNull, type_) |
| 52 |
return coerce_input_value(input_value, type_.of_type, on_error, path) |
| 53 |
on_error( |
| 54 |
path.as_list() if path else [], |
| 55 |
input_value, |
| 56 |
GraphQLError( |
| 57 |
f"Expected non-nullable type '{inspect(type_)}' not to be None." |
| 58 |
), |
| 59 |
) |
| 60 |
return Undefined |
| 61 |
|
| 62 |
if input_value is None or input_value is Undefined: |
| 63 |
# Explicitly return the value null. |
| 64 |
return None |
| 65 |
|
| 66 |
if is_list_type(type_): |
| 67 |
type_ = cast(GraphQLList, type_) |
| 68 |
item_type = type_.of_type |
| 69 |
if is_iterable(input_value): |
| 70 |
coerced_list: List[Any] = [] |
| 71 |
append_item = coerced_list.append |
| 72 |
for index, item_value in enumerate(input_value): |
| 73 |
append_item( |
| 74 |
coerce_input_value( |
| 75 |
item_value, item_type, on_error, Path(path, index, None) |
| 76 |
) |
| 77 |
) |
| 78 |
return coerced_list |
| 79 |
# Lists accept a non-list value as a list of one. |
| 80 |
return [coerce_input_value(input_value, item_type, on_error, path)] |
| 81 |
|
| 82 |
if is_input_object_type(type_): |
| 83 |
type_ = cast(GraphQLInputObjectType, type_) |
| 84 |
if not isinstance(input_value, dict): |
| 85 |
on_error( |
| 86 |
path.as_list() if path else [], |
| 87 |
input_value, |
| 88 |
GraphQLError(f"Expected type '{type_.name}' to be a mapping."), |
| 89 |
) |
| 90 |
return Undefined |
| 91 |
|
| 92 |
coerced_dict: Dict[str, Any] = {} |
| 93 |
fields = type_.fields |
| 94 |
|
| 95 |
for field_name, field in fields.items(): |
| 96 |
field_value = input_value.get(field_name, Undefined) |
| 97 |
|
| 98 |
if field_value is Undefined: |
| 99 |
if field.default_value is not Undefined: |
| 100 |
# Use out name as name if it exists (extension of GraphQL.js). |
| 101 |
coerced_dict[field.out_name or field_name] = field.default_value |
| 102 |
elif is_non_null_type(field.type): # pragma: no cover else |
| 103 |
type_str = inspect(field.type) |
| 104 |
on_error( |
| 105 |
path.as_list() if path else [], |
| 106 |
input_value, |
| 107 |
GraphQLError( |
| 108 |
f"Field '{field_name}' of required type '{type_str}'" |
| 109 |
" was not provided." |
| 110 |
), |
| 111 |
) |
| 112 |
continue |
| 113 |
|
| 114 |
coerced_dict[field.out_name or field_name] = coerce_input_value( |
| 115 |
field_value, field.type, on_error, Path(path, field_name, type_.name) |
| 116 |
) |
| 117 |
|
| 118 |
# Ensure every provided field is defined. |
| 119 |
for field_name in input_value: |
| 120 |
if field_name not in fields: |
| 121 |
suggestions = suggestion_list(field_name, fields) |
| 122 |
on_error( |
| 123 |
path.as_list() if path else [], |
| 124 |
input_value, |
| 125 |
GraphQLError( |
| 126 |
f"Field '{field_name}' is not defined by type '{type_.name}'." |
| 127 |
+ did_you_mean(suggestions) |
| 128 |
), |
| 129 |
) |
| 130 |
|
| 131 |
if type_.is_one_of: |
| 132 |
keys = list(coerced_dict) |
| 133 |
if len(keys) != 1: |
| 134 |
on_error( |
| 135 |
path.as_list() if path else [], |
| 136 |
input_value, |
| 137 |
GraphQLError( |
| 138 |
"Exactly one key must be specified" |
| 139 |
f" for OneOf type '{type_.name}'.", |
| 140 |
), |
| 141 |
) |
| 142 |
else: |
| 143 |
key = keys[0] |
| 144 |
value = coerced_dict[key] |
| 145 |
if value is None: |
| 146 |
on_error( |
| 147 |
(path.as_list() if path else []) + [key], |
| 148 |
value, |
| 149 |
GraphQLError( |
| 150 |
f"Field '{key}' must be non-null.", |
| 151 |
), |
| 152 |
) |
| 153 |
|
| 154 |
return type_.out_type(coerced_dict) |
| 155 |
|
| 156 |
if is_leaf_type(type_): |
| 157 |
# Scalars determine if a value is valid via `parse_value()`, which can throw to |
| 158 |
# indicate failure. If it throws, maintain a reference to the original error. |
| 159 |
type_ = cast(GraphQLScalarType, type_) |
| 160 |
try: |
| 161 |
parse_result = type_.parse_value(input_value) |
| 162 |
except GraphQLError as error: |
| 163 |
on_error(path.as_list() if path else [], input_value, error) |
| 164 |
return Undefined |
| 165 |
except Exception as error: |
| 166 |
on_error( |
| 167 |
path.as_list() if path else [], |
| 168 |
input_value, |
| 169 |
GraphQLError( |
| 170 |
f"Expected type '{type_.name}'. {error}", original_error=error |
| 171 |
), |
| 172 |
) |
| 173 |
return Undefined |
| 174 |
if parse_result is Undefined: |
| 175 |
on_error( |
| 176 |
path.as_list() if path else [], |
| 177 |
input_value, |
| 178 |
GraphQLError(f"Expected type '{type_.name}'."), |
| 179 |
) |
| 180 |
return parse_result |
| 181 |
|
| 182 |
# Not reachable. All possible input types have been considered. |
| 183 |
raise TypeError(f"Unexpected input type: {inspect(type_)}.") |