| 1 |
import re |
| 2 |
from math import isfinite |
| 3 |
from typing import Any, Mapping, Optional, cast |
| 4 |
|
| 5 |
from ..language import ( |
| 6 |
BooleanValueNode, |
| 7 |
EnumValueNode, |
| 8 |
FloatValueNode, |
| 9 |
IntValueNode, |
| 10 |
ListValueNode, |
| 11 |
NameNode, |
| 12 |
NullValueNode, |
| 13 |
ObjectFieldNode, |
| 14 |
ObjectValueNode, |
| 15 |
StringValueNode, |
| 16 |
ValueNode, |
| 17 |
) |
| 18 |
from ..pyutils import inspect, is_iterable, Undefined |
| 19 |
from ..type import ( |
| 20 |
GraphQLID, |
| 21 |
GraphQLInputType, |
| 22 |
GraphQLInputObjectType, |
| 23 |
GraphQLList, |
| 24 |
GraphQLNonNull, |
| 25 |
is_enum_type, |
| 26 |
is_input_object_type, |
| 27 |
is_leaf_type, |
| 28 |
is_list_type, |
| 29 |
is_non_null_type, |
| 30 |
) |
| 31 |
|
| 32 |
__all__ = ["ast_from_value"] |
| 33 |
|
| 34 |
_re_integer_string = re.compile("^-?(?:0|[1-9][0-9]*)$") |
| 35 |
|
| 36 |
|
| 37 |
def ast_from_value(value: Any, type_: GraphQLInputType) -> Optional[ValueNode]: |
| 38 |
"""Produce a GraphQL Value AST given a Python object. |
| 39 |
|
| 40 |
This function will match Python/JSON values to GraphQL AST schema format by using |
| 41 |
the suggested GraphQLInputType. |
| 42 |
|
| 43 |
A GraphQL type must be provided, which will be used to interpret different Python |
| 44 |
values. |
| 45 |
|
| 46 |
================ ======================= |
| 47 |
JSON Value GraphQL Value |
| 48 |
================ ======================= |
| 49 |
Object Input Object |
| 50 |
Array List |
| 51 |
Boolean Boolean |
| 52 |
String String / Enum Value |
| 53 |
Number Int / Float |
| 54 |
Mixed Enum Value |
| 55 |
null NullValue |
| 56 |
================ ======================= |
| 57 |
|
| 58 |
""" |
| 59 |
if is_non_null_type(type_): |
| 60 |
type_ = cast(GraphQLNonNull, type_) |
| 61 |
ast_value = ast_from_value(value, type_.of_type) |
| 62 |
if isinstance(ast_value, NullValueNode): |
| 63 |
return None |
| 64 |
return ast_value |
| 65 |
|
| 66 |
# only explicit None, not Undefined or NaN |
| 67 |
if value is None: |
| 68 |
return NullValueNode() |
| 69 |
|
| 70 |
# undefined |
| 71 |
if value is Undefined: |
| 72 |
return None |
| 73 |
|
| 74 |
# Convert Python list to GraphQL list. If the GraphQLType is a list, but the value |
| 75 |
# is not a list, convert the value using the list's item type. |
| 76 |
if is_list_type(type_): |
| 77 |
type_ = cast(GraphQLList, type_) |
| 78 |
item_type = type_.of_type |
| 79 |
if is_iterable(value): |
| 80 |
maybe_value_nodes = (ast_from_value(item, item_type) for item in value) |
| 81 |
value_nodes = tuple(node for node in maybe_value_nodes if node) |
| 82 |
return ListValueNode(values=value_nodes) |
| 83 |
return ast_from_value(value, item_type) |
| 84 |
|
| 85 |
# Populate the fields of the input object by creating ASTs from each value in the |
| 86 |
# Python dict according to the fields in the input type. |
| 87 |
if is_input_object_type(type_): |
| 88 |
if value is None or not isinstance(value, Mapping): |
| 89 |
return None |
| 90 |
type_ = cast(GraphQLInputObjectType, type_) |
| 91 |
field_items = ( |
| 92 |
(field_name, ast_from_value(value[field_name], field.type)) |
| 93 |
for field_name, field in type_.fields.items() |
| 94 |
if field_name in value |
| 95 |
) |
| 96 |
field_nodes = tuple( |
| 97 |
ObjectFieldNode(name=NameNode(value=field_name), value=field_value) |
| 98 |
for field_name, field_value in field_items |
| 99 |
if field_value |
| 100 |
) |
| 101 |
return ObjectValueNode(fields=field_nodes) |
| 102 |
|
| 103 |
if is_leaf_type(type_): |
| 104 |
# Since value is an internally represented value, it must be serialized to an |
| 105 |
# externally represented value before converting into an AST. |
| 106 |
serialized = type_.serialize(value) # type: ignore |
| 107 |
if serialized is None or serialized is Undefined: |
| 108 |
return None |
| 109 |
|
| 110 |
# Others serialize based on their corresponding Python scalar types. |
| 111 |
if isinstance(serialized, bool): |
| 112 |
return BooleanValueNode(value=serialized) |
| 113 |
|
| 114 |
# Python ints and floats correspond nicely to Int and Float values. |
| 115 |
if isinstance(serialized, int): |
| 116 |
return IntValueNode(value=str(serialized)) |
| 117 |
if isinstance(serialized, float) and isfinite(serialized): |
| 118 |
value = str(serialized) |
| 119 |
if value.endswith(".0"): |
| 120 |
value = value[:-2] |
| 121 |
return FloatValueNode(value=value) |
| 122 |
|
| 123 |
if isinstance(serialized, str): |
| 124 |
# Enum types use Enum literals. |
| 125 |
if is_enum_type(type_): |
| 126 |
return EnumValueNode(value=serialized) |
| 127 |
|
| 128 |
# ID types can use Int literals. |
| 129 |
if type_ is GraphQLID and _re_integer_string.match(serialized): |
| 130 |
return IntValueNode(value=serialized) |
| 131 |
|
| 132 |
return StringValueNode(value=serialized) |
| 133 |
|
| 134 |
raise TypeError(f"Cannot convert value to AST: {inspect(serialized)}.") |
| 135 |
|
| 136 |
# Not reachable. All possible input types have been considered. |
| 137 |
raise TypeError(f"Unexpected input type: {inspect(type_)}.") |