Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse_literal(ast: "Node") -> Union[str, "UNDEFINED_VALUE"]:
return (
ast.value.capitalize()
if isinstance(ast, StringValueNode)
else UNDEFINED_VALUE
)
:type input_field: GraphQLInputField
:type parent_node: Union[VariableDefinitionNode, InputValueDefinitionNode]
:type value_node: Union[ValueNode, VariableNode, UNDEFINED_VALUE]
:type ctx: Optional[Any]
:type variables: Optional[Dict[str, Any]]
:type path: Optional[Path]
:return: the computed value
:rtype: Union[CoercionResult, UNDEFINED_VALUE, SKIP_FIELD]
"""
if is_invalid_value(value_node) or is_missing_variable(
value_node.value, variables
):
if input_field.default_value is not None:
input_field_node = input_field.default_value
elif input_field.graphql_type.is_non_null_type:
return UNDEFINED_VALUE
else:
return SKIP_FIELD
else:
input_field_node = value_node.value
return await input_field.literal_coercer(
parent_node, input_field_node, ctx, variables=variables, path=path
)
def parse_literal(self, ast: "Node") -> Union[int, "UNDEFINED_VALUE"]:
"""
Coerce the input value from an AST node.
:param ast: AST node to coerce
:type ast: Node
:return: the coerced value
:rtype: Union[int, UNDEFINED_VALUE]
"""
# pylint: disable=no-self-use
if not isinstance(ast, IntValueNode):
return UNDEFINED_VALUE
try:
value = int(ast.value)
if _MIN_INT <= value <= _MAX_INT:
return value
except Exception: # pylint: disable=broad-except
pass
return UNDEFINED_VALUE
:param inner_coercer: the pre-computed coercer to use for the item
:param variables: the variables provided in the GraphQL request
:param path: the path traveled until this coercer
:type parent_node: Union[VariableDefinitionNode, InputValueDefinitionNode]
:type item_node: Union[ValueNode, VariableNode]
:type ctx: Optional[Any]
:type is_non_null_item_type: bool
:type inner_coercer: Callable
:type variables: Optional[Dict[str, Any]]
:type path: Optional[Path]
:return: the computed value
:rtype: Union[CoercionResult, UNDEFINED_VALUE]
"""
if is_missing_variable(item_node, variables):
if is_non_null_item_type:
return UNDEFINED_VALUE
return CoercionResult(value=None)
return await inner_coercer(
parent_node, item_node, ctx, variables=variables, path=path
)
"""
# pylint: disable=too-many-locals,too-many-branches,too-complex
name = argument_definition.name
arg_type = argument_definition.graphql_type
if argument_node and isinstance(argument_node.value, VariableNode):
variable_name = argument_node.value.name.value
has_value = variable_values and variable_name in variable_values
is_null = has_value and variable_values[variable_name] is None
else:
has_value = argument_node is not None
is_null = argument_node and isinstance(
argument_node.value, NullValueNode
)
coercion_result = UNDEFINED_VALUE
value_node = None
if not has_value and argument_definition.default_value is not None:
value_node = argument_definition.default_value
elif (not has_value or is_null) and arg_type.is_non_null_type:
if is_null:
return CoercionResult(
errors=[
graphql_error_from_nodes(
f"Argument < {name} > of non-null type < {arg_type} > "
"must not be null.",
nodes=argument_node.value,
)
]
)
if argument_node and isinstance(argument_node.value, VariableNode):
return CoercionResult(
def parse_literal(self, ast: "Node") -> Union[datetime, "UNDEFINED_VALUE"]:
"""
Coerce the input value from an AST node.
:param ast: AST node to coerce
:type ast: Node
:return: the coerced value
:rtype: Union[datetime, UNDEFINED_VALUE]
"""
# pylint: disable=no-self-use
if not isinstance(ast, StringValueNode):
return UNDEFINED_VALUE
try:
return datetime.strptime(ast.value, "%Y-%m-%dT%H:%M:%S")
except Exception: # pylint: disable=broad-except
pass
return UNDEFINED_VALUE
def parse_literal(self, ast: "Node") -> Union[bool, "UNDEFINED_VALUE"]:
"""
Coerce the input value from an AST node.
:param ast: AST node to coerce
:type ast: Node
:return: the coerced value
:rtype: Union[bool, UNDEFINED_VALUE]
"""
# pylint: disable=no-self-use
return (
ast.value if isinstance(ast, BooleanValueNode) else UNDEFINED_VALUE
)
def parse_literal(self, ast: "Node") -> Union[str, "UNDEFINED_VALUE"]:
"""
Coerce the input value from an AST node.
:param ast: AST node to coerce
:type ast: Node
:return: the coerced value
:rtype: Union[str, UNDEFINED_VALUE]
"""
# pylint: disable=no-self-use
return (
ast.value if isinstance(ast, StringValueNode) else UNDEFINED_VALUE
)
for input_field_name, input_field in input_fields.items()
]
)
errors = []
coerced_values = {}
for input_field_name, input_field_result in zip(input_fields, results):
if input_field_result is SKIP_FIELD:
continue
if is_invalid_value(input_field_result):
return CoercionResult(value=UNDEFINED_VALUE)
input_field_value, input_field_errors = input_field_result
if is_invalid_value(input_field_value):
return CoercionResult(value=UNDEFINED_VALUE)
if input_field_errors:
errors.extend(input_field_errors)
elif not errors:
coerced_values[input_field_name] = input_field_value
return CoercionResult(value=coerced_values, errors=errors)
item_node,
ctx,
is_non_null_item_type,
inner_coercer,
variables,
path=Path(path, index),
)
for index, item_node in enumerate(node.values)
]
)
errors = []
coerced_values = []
for coerced_result in results:
if is_invalid_value(coerced_result):
return CoercionResult(value=UNDEFINED_VALUE)
coerced_value, coerced_errors = coerced_result
if is_invalid_value(coerced_value):
return CoercionResult(value=UNDEFINED_VALUE)
if coerced_errors:
errors.extend(coerced_errors)
elif not errors:
coerced_values.append(coerced_value)
return CoercionResult(value=coerced_values, errors=errors)
coerced_item_value, coerced_item_errors = await inner_coercer(
parent_node, node, ctx, variables=variables, path=path
)
if is_invalid_value(coerced_item_value):
return CoercionResult(value=UNDEFINED_VALUE)