From 31b3f10927a0e7c1e52e7e140cc5900cfb2fbbb3 Mon Sep 17 00:00:00 2001 From: James Kominick Date: Sun, 16 Aug 2020 23:58:07 -0400 Subject: [PATCH] accept a method to override graphql error formatting --- starlette/graphql.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/starlette/graphql.py b/starlette/graphql.py index 020480e4ef..2cde7a38e4 100644 --- a/starlette/graphql.py +++ b/starlette/graphql.py @@ -26,9 +26,11 @@ def __init__( executor: typing.Any = None, executor_class: type = None, graphiql: bool = True, + format_error: typing.Optional[typing.Callable[Exception, dict]] = None, ) -> None: self.schema = schema self.graphiql = graphiql + self._format_error_override = format_error if executor is None: # New style in 0.10.0. Use 'executor_class'. # See issue https://github.com/encode/starlette/issues/242 @@ -116,7 +118,8 @@ async def handle_graphql(self, request: Request) -> Response: ) def format_error(self, error: Exception) -> dict: - return format_graphql_error(error) + fmt = self._format_error_override or format_graphql_error + return fmt(error) async def execute( # type: ignore self, query, variables=None, context=None, operation_name=None