示例#1
0
async def list_coercer(
    result: Any,
    info: "ResolveInfo",
    execution_context: "ExecutionContext",
    field_nodes: List["FieldNode"],
    path: "Path",
    item_type: "GraphQLOutputType",
    inner_coercer: Callable,
) -> List[Any]:
    """
    Computes the value of a list.
    :param result: resolved value
    :param info: information related to the execution and the resolved field
    :param execution_context: instance of the query execution context
    :param field_nodes: AST nodes related to the resolved field
    :param path: the path traveled until this resolver
    :param item_type: GraphQLType of list items
    :param inner_coercer: the pre-computed coercer to use on the result
    :type result: Any
    :type info: ResolveInfo
    :type execution_context: ExecutionContext
    :type field_nodes: List[FieldNode]
    :type path: Path
    :type item_type: GraphQLOutputType
    :type inner_coercer: Callable
    :return: the computed value
    :rtype: List[Any]
    """
    # pylint: disable=too-many-locals
    if not isinstance(result, list):
        raise TypeError("Expected Iterable, but did not find one for field "
                        f"{info.parent_type.name}.{info.field_name}.")

    results = []
    for index, item in enumerate(result):
        try:
            value = await complete_value_catching_error(
                item,
                info,
                execution_context,
                field_nodes,
                Path(path, index),
                item_type,
                inner_coercer,
            )
        except Exception as e:  # pylint: disable=broad-except
            value = e
        results.append(value)

    exceptions = extract_exceptions_from_results(results)
    if exceptions:
        raise exceptions

    return results
示例#2
0
async def execute_fields(
    execution_context: "ExecutionContext",
    parent_type: "GraphQLObjectType",
    source_value: Any,
    path: Optional["Path"],
    fields: Dict[str, List["FieldNode"]],
    is_introspection_context: bool = False,
) -> Dict[str, Any]:
    """
    Implements the "Evaluating selection sets" section of the spec for "read"
    mode.
    :param execution_context: instance of the query execution context
    :param parent_type: GraphQLObjectType of the field's parent
    :param source_value: default root value or field parent value
    :param path: the path traveled until this resolver
    :param fields: dictionary of collected fields
    :param is_introspection_context: determines whether or not the resolved
    field is in a context of an introspection query
    :type execution_context: ExecutionContext
    :type parent_type: GraphQLObjectType
    :type source_value: Any
    :type path: Optional[Path]
    :type fields: Dict[str, List[FieldNode]]
    :type is_introspection_context: bool
    :return: the computed fields value
    :rtype: Dict[str, Any]
    """
    results = await asyncio.gather(
        *[
            resolve_field(
                execution_context,
                parent_type,
                source_value,
                field_nodes,
                Path(path, entry_key),
                is_introspection_context,
            ) for entry_key, field_nodes in fields.items()
        ],
        return_exceptions=True,
    )

    exceptions = extract_exceptions_from_results(results)
    if exceptions:
        raise exceptions

    return {
        entry_key: result
        for entry_key, result in zip(fields, results)
        if not is_invalid_value(result)
    }