Skip to content

Commit 9d9dab7

Browse files
committed
Use explicit type for error extensions
Replicates graphql/graphql-js@06d9cb4
1 parent 13aef8a commit 9d9dab7

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/graphql/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
# Create, format, and print GraphQL errors.
4747
from .error import (
4848
GraphQLError,
49+
GraphQLErrorExtensions,
4950
GraphQLFormattedError,
5051
GraphQLSyntaxError,
5152
located_error,
@@ -694,6 +695,7 @@
694695
"NoDeprecatedCustomRule",
695696
"NoSchemaIntrospectionCustomRule",
696697
"GraphQLError",
698+
"GraphQLErrorExtensions",
697699
"GraphQLFormattedError",
698700
"GraphQLSyntaxError",
699701
"located_error",

src/graphql/error/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
errors.
55
"""
66

7-
from .graphql_error import GraphQLError, GraphQLFormattedError
7+
from .graphql_error import GraphQLError, GraphQLErrorExtensions, GraphQLFormattedError
88

99
from .syntax_error import GraphQLSyntaxError
1010

1111
from .located_error import located_error
1212

1313
__all__ = [
1414
"GraphQLError",
15+
"GraphQLErrorExtensions",
1516
"GraphQLFormattedError",
1617
"GraphQLSyntaxError",
1718
"located_error",

src/graphql/error/graphql_error.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@
1414
) # noqa: F401
1515
from ..language.source import Source # noqa: F401
1616

17-
__all__ = ["GraphQLError", "GraphQLFormattedError"]
17+
__all__ = ["GraphQLError", "GraphQLErrorExtensions", "GraphQLFormattedError"]
18+
19+
20+
# Custom extensions
21+
GraphQLErrorExtensions = Dict[str, Any]
22+
# Use a unique identifier name for your extension, for example the name of
23+
# your library or project. Do not use a shortened identifier as this increases
24+
# the risk of conflicts. We recommend you add at most one extension key,
25+
# a dictionary which can contain all the values you need.
1826

1927

2028
class GraphQLFormattedError(TypedDict, total=False):
@@ -33,7 +41,7 @@ class GraphQLFormattedError(TypedDict, total=False):
3341
path: List[Union[str, int]]
3442
# Reserved for implementors to extend the protocol however they see fit,
3543
# and hence there are no additional restrictions on its contents.
36-
extensions: Dict[str, Any]
44+
extensions: GraphQLErrorExtensions
3745

3846

3947
class GraphQLError(Exception):
@@ -91,7 +99,7 @@ class GraphQLError(Exception):
9199
original_error: Optional[Exception]
92100
"""The original error thrown from a field resolver during execution"""
93101

94-
extensions: Optional[Dict[str, Any]]
102+
extensions: Optional[GraphQLErrorExtensions]
95103
"""Extension fields to add to the formatted error"""
96104

97105
__slots__ = (
@@ -115,7 +123,7 @@ def __init__(
115123
positions: Optional[Collection[int]] = None,
116124
path: Optional[Collection[Union[str, int]]] = None,
117125
original_error: Optional[Exception] = None,
118-
extensions: Optional[Dict[str, Any]] = None,
126+
extensions: Optional[GraphQLErrorExtensions] = None,
119127
) -> None:
120128
super().__init__(message)
121129
self.message = message

0 commit comments

Comments
 (0)