Пример #1
0
    async def __call__(
        self,
        execution_ctx: ExecutionContext,
        request_ctx: Optional[Dict[str, Any]],
        parent_result: Optional[Any] = None,
        parent_marshalled: Optional[Any] = None,
    ) -> None:
        raw, coerced = await self.field_executor(
            parent_result,
            self.arguments,
            request_ctx,
            Info(
                query_field=self,
                schema_field=self.field_executor.schema_field,
                schema=self.schema,
                path=self.path,
                location=self.location,
                execution_ctx=execution_ctx,
            ),
        )

        if parent_marshalled is not None:
            parent_marshalled[self.alias] = coerced
        else:
            self.marshalled = coerced

        if isinstance(raw, Exception):
            try:
                if callable(raw.coerce_value):
                    gql_error = raw
            except (TypeError, AttributeError):
                gql_error = GraphQLError(str(raw), self.path, [self.location])

            gql_error.coerce_value = partial(
                gql_error.coerce_value,
                path=self.path,
                locations=[self.location],
            )

            if ((self.cant_be_null or self.contains_not_null) and self.parent
                    and self.cant_be_null):
                self.parent.bubble_error()
            execution_ctx.add_error(gql_error)
        elif self.children and raw is not None:
            await self._execute_children(execution_ctx,
                                         request_ctx,
                                         result=raw,
                                         coerced=coerced)
Пример #2
0
    async def create_source_event_stream(
        self,
        execution_ctx: ExecutionContext,
        request_ctx: Optional[Dict[str, Any]],
        parent_result: Optional[Any] = None,
    ):
        if not self.subscribe:
            raise GraphQLError(
                "Can't execute a subscription query on a field which doesn't "
                "provide a source event stream with < @Subscription >.")

        info = Info(
            query_field=self,
            schema_field=self.field_executor.schema_field,
            schema=self.schema,
            path=self.path,
            location=self.location,
            execution_ctx=execution_ctx,
        )

        return self.subscribe(
            parent_result,
            await coerce_arguments(
                self.field_executor.schema_field.arguments,
                self.arguments,
                request_ctx,
                info,
            ),
            request_ctx,
            info,
        )
Пример #3
0
def _add_errors_to_execution_context(
    execution_context: ExecutionContext,
    raw_exception: Union[Exception, MultipleException],
    path: Union[str, List[str]],
    location: "Location",
) -> None:
    exceptions = (
        raw_exception.exceptions
        if isinstance(raw_exception, MultipleException)
        else [raw_exception]
    )

    for exception in exceptions:
        gql_error = (
            exception
            if is_coercible_exception(exception)
            else GraphQLError(
                str(exception), path, [location], original_error=exception
            )
        )

        gql_error.coerce_value = partial(
            gql_error.coerce_value, path=path, locations=[location]
        )

        execution_context.add_error(gql_error)
Пример #4
0
def _add_errors_to_execution_context(
    execution_context: ExecutionContext,
    raw_exception: Union[Exception, MultipleException],
    path: Union[str, List[str]],
    location: "Location",
) -> None:
    exceptions = (
        raw_exception.exceptions
        if isinstance(raw_exception, MultipleException)
        else [raw_exception]
    )

    for exception in exceptions:
        try:
            if callable(exception.coerce_value):
                gql_error = exception
        except (TypeError, AttributeError):
            gql_error = GraphQLError(str(exception), path, [location])

        gql_error.coerce_value = partial(
            gql_error.coerce_value, path=path, locations=[location]
        )

        execution_context.add_error(gql_error)
Пример #5
0
    def _parse_query_to_operations(self, query, variables):
        try:
            operations, errors = self._parser.parse_and_tartify(
                self._schema, query, variables=variables
            )
        except GraphQLError as e:
            errors = [e]
        except Exception:  # pylint: disable=broad-except
            errors = [GraphQLError("Server encountered an error.")]

        if errors:
            return (
                None,
                {
                    "data": None,
                    "errors": [self._error_coercer(err) for err in errors],
                },
            )
        return operations, None
Пример #6
0
    await nf(exectx, reqctx, parent_marshalled=prm)

    assert bool(exectx.errors)

    assert exectx.errors[0] is raw
    assert exectx.errors[0].coerce_value() == {
        "msg": "error",
        "type": "bad_request",
    }


@pytest.mark.parametrize(
    "raw_exception,expected_messages,expected_original_errors",
    [
        (GraphQLError("AGraphQLError"), ["AGraphQLError"], [type(None)]),
        (TypeError("ATypeError"), ["ATypeError"], [TypeError]),
        (
            MultipleException(
                exceptions=[
                    GraphQLError("AGraphQLError"),
                    TypeError("ATypeError"),
                ]
            ),
            ["AGraphQLError", "ATypeError"],
            [type(None), TypeError],
        ),
    ],
)
def test_add_errors_to_execution_context(
    raw_exception, expected_messages, expected_original_errors
Пример #7
0
 async def my_execute(_, exec_ctx, *__, **___):
     exec_ctx.add_error(GraphQLError("My error"))
Пример #8
0
def to_graphql_error(exception):
    if isinstance(exception, GraphQLError):
        return exception
    return GraphQLError(str(exception))
Пример #9
0
def test_graphqlerror_coerce_value(message, init_kwargs, coerce_value_kwargs,
                                   expected):
    graphql_error = GraphQLError(message, **init_kwargs)
    assert graphql_error.coerce_value(**coerce_value_kwargs) == expected
Пример #10
0
def to_graphql_error(exception, message=None):
    if is_coercible_exception(exception):
        return exception
    return GraphQLError(message or str(exception), original_error=exception)