How to use the tartiflette.coercers.common.CoercionResult function in tartiflette

To help you get started, we’ve selected a few tartiflette examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tartiflette / tartiflette / tartiflette / coercers / literals / input_object_coercer.py View on Github external
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)
github tartiflette / tartiflette / tartiflette / coercers / literals / scalar_coercer.py View on Github external
:param variables: the variables provided in the GraphQL request
    :param path: the path traveled until this coercer
    :type parent_node: Union[VariableDefinitionNode, InputValueDefinitionNode]
    :type node: Union[ValueNode, VariableNode]
    :type ctx: Optional[Any]
    :type scalar_type: GraphQLScalarType
    :type variables: Optional[Dict[str, Any]]
    :type path: Optional[Path]
    :return: the computed value
    :rtype: CoercionResult
    """
    # pylint: disable=unused-argument
    try:
        value = scalar_type.parse_literal(node)
        if not is_invalid_value(value):
            return CoercionResult(value=value)
    except Exception:  # pylint: disable=broad-except
        pass
    return CoercionResult(value=UNDEFINED_VALUE)
github tartiflette / tartiflette / tartiflette / coercers / inputs / scalar_coercer.py View on Github external
)
                ]
            )
    except Exception as e:  # pylint: disable=broad-except
        return CoercionResult(
            errors=[
                coercion_error(
                    f"Expected type < {scalar_type.name} >",
                    node,
                    path,
                    sub_message=str(e),
                    original_error=e,
                )
            ]
        )
    return CoercionResult(value=coerced_value)
github tartiflette / tartiflette / tartiflette / coercers / inputs / null_coercer.py View on Github external
async def wrapper(
        parent_node: Union[
            "VariableDefinitionNode", "InputValueDefinitionNode"
        ],
        node: "Node",
        value: Any,
        ctx: Optional[Any],
        **kwargs,
    ) -> "CoercionResult":
        if value is None:
            return CoercionResult(value=None)

        return await coercer(parent_node, node, value, ctx, **kwargs)
github tartiflette / tartiflette / tartiflette / coercers / inputs / enum_coercer.py View on Github external
:param value: the raw value to compute
    :param ctx: context passed to the query execution
    :param enum_type: the GraphQLEnumType instance of the enum
    :param path: the path traveled until this coercer
    :type parent_node: Union[VariableDefinitionNode, InputValueDefinitionNode]
    :type node: Node
    :type value: Any
    :type ctx: Optional[Any]
    :type enum_type: GraphQLEnumType
    :type path: Optional[Path]
    :return: the coercion result
    :rtype: CoercionResult
    """
    try:
        enum_value = enum_type.get_value(value)
        return CoercionResult(
            value=await enum_value.input_coercer(parent_node, value, ctx)
        )
    except Exception:  # pylint: disable=broad-except
        return CoercionResult(
            errors=[
                coercion_error(
                    f"Expected type < {enum_type.name} >",
                    node,
                    path,
                    did_you_mean(
                        get_close_matches(
                            str(value),
                            [enum.value for enum in enum_type.values],
                            n=5,
                        )
github tartiflette / tartiflette / tartiflette / coercers / inputs / input_object_coercer.py View on Github external
if input_field_name not in input_fields:
            errors.append(
                coercion_error(
                    f"Field < {input_field_name} > is not defined by type "
                    f"< {input_object_type.name} >",
                    node,
                    path,
                    did_you_mean(
                        get_close_matches(
                            input_field_name, input_fields.keys(), n=5
                        )
                    ),
                )
            )

    return CoercionResult(value=coerced_values, errors=errors)
github tartiflette / tartiflette / tartiflette / coercers / variables.py View on Github external
]
        )

    if has_value:
        coerced_value, coerce_errors = await input_coercer(
            variable_definition_node, value, ctx
        )
        if coerce_errors:
            for coerce_error in coerce_errors:
                if isinstance(coerce_error, CoercionError):
                    coerce_error.message = (
                        f"Variable < ${var_name} > got invalid value "
                        f"< {value} >; {coerce_error.message}"
                    )
            return CoercionResult(errors=coerce_errors)
        return CoercionResult(value=coerced_value)

    return UNDEFINED_VALUE
github tartiflette / tartiflette / tartiflette / coercers / inputs / directives_coercer.py View on Github external
if not directives:
        return coercion_result

    value, errors = coercion_result
    if errors:
        return coercion_result

    try:
        return CoercionResult(
            value=await directives(
                parent_node, value, ctx, context_coercer=ctx
            )
        )
    except Exception as raw_exception:  # pylint: disable=broad-except
        return CoercionResult(
            errors=[
                graphql_error_from_nodes(
                    str(raw_exception),
                    node,
                    original_error=(
                        raw_exception
                        if not is_coercible_exception(raw_exception)
                        else None
                    ),
                )
                for raw_exception in (
                    raw_exception.exceptions
                    if isinstance(raw_exception, MultipleException)
                    else [raw_exception]
                )
github tartiflette / tartiflette / tartiflette / coercers / inputs / input_object_coercer.py View on Github external
:type parent_node: Union[VariableDefinitionNode, InputValueDefinitionNode]
    :type node: Node
    :type value: Any
    :type ctx: Optional[Any]
    :type input_field: GraphQLInputField
    :type path: Path
    :return: the coercion result
    :rtype: Union[CoercionResult, UNDEFINED_VALUE]
    """
    if is_invalid_value(value):
        if input_field.default_value is not None:
            return await input_field.literal_coercer(
                parent_node, input_field.default_value, ctx
            )
        if input_field.graphql_type.is_non_null_type:
            return CoercionResult(
                errors=[
                    coercion_error(
                        f"Field < {path} > of required type "
                        f"< {input_field.gql_type} > was not provided",
                        node,
                    )
                ]
            )
        return UNDEFINED_VALUE

    return await input_field.input_coercer(
        parent_node, node, value, ctx, path=path
    )
github tartiflette / tartiflette / tartiflette / coercers / literals / null_and_variable_coercer.py View on Github external
:rtype: CoercionResult
        """
        if not node:
            return CoercionResult(value=UNDEFINED_VALUE)

        if isinstance(node, NullValueNode):
            return CoercionResult(value=None)

        if isinstance(node, VariableNode):
            if not variables:
                return CoercionResult(value=UNDEFINED_VALUE)

            value = variables.get(node.name.value, UNDEFINED_VALUE)
            if is_invalid_value(value) or (value is None and is_non_null_type):
                return CoercionResult(value=UNDEFINED_VALUE)
            return CoercionResult(value=value)

        return await coercer(
            parent_node, node, ctx, variables=variables, **kwargs
        )