strawberry-graphql 0.279.0.dev1754138688__py3-none-any.whl → 0.279.0.dev1754159379__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,7 @@ Example:
10
10
  age: int
11
11
  """
12
12
 
13
+ from .error import Error
13
14
  from .object_type import input as input_decorator
14
15
  from .object_type import interface
15
16
  from .object_type import type as type_decorator
@@ -18,4 +19,4 @@ from .object_type import type as type_decorator
18
19
  input = input_decorator
19
20
  type = type_decorator
20
21
 
21
- __all__ = ["input", "interface", "type"]
22
+ __all__ = ["Error", "input", "interface", "type"]
@@ -0,0 +1,51 @@
1
+ """Generic error type for Pydantic validation errors in Strawberry GraphQL.
2
+
3
+ This module provides a generic Error type that can be used to represent
4
+ Pydantic validation errors in GraphQL responses.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import TYPE_CHECKING
10
+
11
+ from strawberry.types.object_type import type as strawberry_type
12
+
13
+ if TYPE_CHECKING:
14
+ from pydantic import ValidationError
15
+
16
+
17
+ @strawberry_type
18
+ class ErrorDetail:
19
+ """Represents a single validation error detail."""
20
+
21
+ type: str
22
+ loc: list[str]
23
+ msg: str
24
+
25
+
26
+ @strawberry_type
27
+ class Error:
28
+ """Generic error type for Pydantic validation errors."""
29
+
30
+ errors: list[ErrorDetail]
31
+
32
+ @staticmethod
33
+ def from_validation_error(exc: ValidationError) -> Error:
34
+ """Create an Error instance from a Pydantic ValidationError.
35
+
36
+ Args:
37
+ exc: The Pydantic ValidationError to convert
38
+
39
+ Returns:
40
+ An Error instance containing all validation errors
41
+ """
42
+ return Error(
43
+ errors=[
44
+ ErrorDetail(
45
+ type=error["type"],
46
+ loc=[str(loc) for loc in error["loc"]],
47
+ msg=error["msg"],
48
+ )
49
+ for error in exc.errors()
50
+ ]
51
+ )
@@ -1,5 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import contextlib
3
4
  import dataclasses
4
5
  import sys
5
6
  from functools import partial, reduce
@@ -731,14 +732,27 @@ class GraphQLCoreConverter:
731
732
  ) -> Any:
732
733
  # parse field arguments into Strawberry input types and convert
733
734
  # field names to Python equivalents
734
- field_args, field_kwargs = get_arguments(
735
- field=field,
736
- source=_source,
737
- info=info,
738
- kwargs=kwargs,
739
- config=self.config,
740
- scalar_registry=self.scalar_registry,
741
- )
735
+ try:
736
+ field_args, field_kwargs = get_arguments(
737
+ field=field,
738
+ source=_source,
739
+ info=info,
740
+ kwargs=kwargs,
741
+ config=self.config,
742
+ scalar_registry=self.scalar_registry,
743
+ )
744
+ except Exception as exc:
745
+ # Try to import Pydantic ValidationError
746
+ with contextlib.suppress(ImportError):
747
+ from pydantic import ValidationError
748
+
749
+ if isinstance(
750
+ exc, ValidationError
751
+ ) and self._should_convert_validation_error(field):
752
+ from strawberry.pydantic import Error
753
+
754
+ return Error.from_validation_error(exc)
755
+ raise
742
756
 
743
757
  resolver_requested_info = False
744
758
  if "info" in field_kwargs:
@@ -799,6 +813,22 @@ class GraphQLCoreConverter:
799
813
  _resolver._is_default = not field.base_resolver # type: ignore
800
814
  return _resolver
801
815
 
816
+ def _should_convert_validation_error(self, field: StrawberryField) -> bool:
817
+ """Check if field return type is a Union containing strawberry.pydantic.Error."""
818
+ from strawberry.types.union import StrawberryUnion
819
+
820
+ field_type = field.type
821
+ if isinstance(field_type, StrawberryUnion):
822
+ # Import Error dynamically to avoid circular imports
823
+ try:
824
+ from strawberry.pydantic import Error
825
+
826
+ return any(union_type is Error for union_type in field_type.types)
827
+ except ImportError:
828
+ # If strawberry.pydantic doesn't exist or Error isn't available
829
+ return False
830
+ return False
831
+
802
832
  def from_scalar(self, scalar: type) -> GraphQLScalarType:
803
833
  from strawberry.relay.types import GlobalID
804
834
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: strawberry-graphql
3
- Version: 0.279.0.dev1754138688
3
+ Version: 0.279.0.dev1754159379
4
4
  Summary: A library for creating GraphQL APIs
5
5
  License: MIT
6
6
  Keywords: graphql,api,rest,starlette,async
@@ -152,7 +152,8 @@ strawberry/printer/__init__.py,sha256=DmepjmgtkdF5RxK_7yC6qUyRWn56U-9qeZMbkztYB9
152
152
  strawberry/printer/ast_from_value.py,sha256=Tkme60qlykbN2m3dNPNMOe65X-wj6EmcDQwgQv7gUkc,4987
153
153
  strawberry/printer/printer.py,sha256=5E9w0wDsUv1hvkeXof12277NLMiCVy5MgJ6gSo_NJhQ,19177
154
154
  strawberry/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
155
- strawberry/pydantic/__init__.py,sha256=1Z6_WtYpLGZR_Ev0indyL40EXFlNS3bqtonNRm2O9q8,558
155
+ strawberry/pydantic/__init__.py,sha256=_QAGALzygcRyKE2iiK_rHt4s_d0g3ms0SUP64rEjrYI,592
156
+ strawberry/pydantic/error.py,sha256=EvS2R99CCoe5Oo5Dh3V9Xc6eGcSFpo-r4F5Wq8RjF7U,1271
156
157
  strawberry/pydantic/fields.py,sha256=8oBY7Z-sTj6Tt3l9sNl1Wqk2ERxh-Doe2onQYnsGCP4,7188
157
158
  strawberry/pydantic/object_type.py,sha256=sxFTE_VSpAcmOm7-EGL-tp0aobjoctOSjVQs34fRcD8,10785
158
159
  strawberry/quart/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -176,7 +177,7 @@ strawberry/schema/config.py,sha256=bkEMn0EkBRg2Tl6ZZH5hpOGBNiAw9QcOclt5dI_Yd1g,1
176
177
  strawberry/schema/exceptions.py,sha256=8gsMxxFDynMvRkUDuVL9Wwxk_zsmo6QoJ2l4NPxd64M,1137
177
178
  strawberry/schema/name_converter.py,sha256=JG5JKLr9wp8BMJIvG3_bVkwFdoLGbknNR1Bt75urXN0,6950
178
179
  strawberry/schema/schema.py,sha256=EZ6YLV5wqHrjxi3rVJ0HvbGeIlZrbSOZqEomKlu4-OY,39433
179
- strawberry/schema/schema_converter.py,sha256=ZGkZjLsqjZ-0y5NItsECHZbOOhjJioYRT6YROwmo4Gg,40125
180
+ strawberry/schema/schema_converter.py,sha256=6wgiLkQtA77AEBCH9TqKdGWmRsv4HScKx9YahnZlQQg,41441
180
181
  strawberry/schema/types/__init__.py,sha256=oHO3COWhL3L1KLYCJNY1XFf5xt2GGtHiMC-UaYbFfnA,68
181
182
  strawberry/schema/types/base_scalars.py,sha256=JRUq0WjEkR9dFewstZnqnZKp0uOEipo4UXNF5dzRf4M,1971
182
183
  strawberry/schema/types/concrete_type.py,sha256=axIyFZgdwNv-XYkiqX67464wuFX6Vp0jYATwnBZSUvM,750
@@ -239,8 +240,8 @@ strawberry/utils/logging.py,sha256=U1cseHGquN09YFhFmRkiphfASKCyK0HUZREImPgVb0c,7
239
240
  strawberry/utils/operation.py,sha256=ZgVOw3K2jQuLjNOYUHauF7itJD0QDNoPw9PBi0IYf6k,1234
240
241
  strawberry/utils/str_converters.py,sha256=-eH1Cl16IO_wrBlsGM-km4IY0IKsjhjnSNGRGOwQjVM,897
241
242
  strawberry/utils/typing.py,sha256=SDvX-Du-9HAV3-XXjqi7Q5f5qPDDFd_gASIITiwBQT4,14073
242
- strawberry_graphql-0.279.0.dev1754138688.dist-info/LICENSE,sha256=m-XnIVUKqlG_AWnfi9NReh9JfKhYOB-gJfKE45WM1W8,1072
243
- strawberry_graphql-0.279.0.dev1754138688.dist-info/METADATA,sha256=JMWoCdEfWN1cM7Y5cOj95RNtcdAWpRjbLTwuJCy08VA,7407
244
- strawberry_graphql-0.279.0.dev1754138688.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
245
- strawberry_graphql-0.279.0.dev1754138688.dist-info/entry_points.txt,sha256=Nk7-aT3_uEwCgyqtHESV9H6Mc31cK-VAvhnQNTzTb4k,49
246
- strawberry_graphql-0.279.0.dev1754138688.dist-info/RECORD,,
243
+ strawberry_graphql-0.279.0.dev1754159379.dist-info/LICENSE,sha256=m-XnIVUKqlG_AWnfi9NReh9JfKhYOB-gJfKE45WM1W8,1072
244
+ strawberry_graphql-0.279.0.dev1754159379.dist-info/METADATA,sha256=d3JxV0Ow32IbDnXy_XQBZmYFP9dfbrzcXbycU5arydU,7407
245
+ strawberry_graphql-0.279.0.dev1754159379.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
246
+ strawberry_graphql-0.279.0.dev1754159379.dist-info/entry_points.txt,sha256=Nk7-aT3_uEwCgyqtHESV9H6Mc31cK-VAvhnQNTzTb4k,49
247
+ strawberry_graphql-0.279.0.dev1754159379.dist-info/RECORD,,