Repository: strawberry-graphql/strawberry

Issue:
Compatibility with pydantic 1.4
I'm trying to use strawberry in a project that has pydantic pinned at 1.4. I chatted with @patrick91 on discord about this, and he thought it would be reasonable to achieve compatibility with this version.

Pydantic appears to only be used in the [strawberry.experimental](https://github.com/strawberry-graphql/strawberry/blob/main/strawberry/experimental/__init__.py) module, which only gets loaded if pydantic is present. One way to solve this for me in particular would be to lazily load strawberry.experimental/pydantic, such that when an older version of pydantic is present, one can still import other packages in strawberry.

Thank you!


Discussion:

Resolving patch:
diff --git a/strawberry/experimental/pydantic/fields.py b/strawberry/experimental/pydantic/fields.py
--- a/strawberry/experimental/pydantic/fields.py
+++ b/strawberry/experimental/pydantic/fields.py
@@ -7,52 +7,59 @@
 from .exceptions import UnsupportedTypeError
 
 
+ATTR_TO_TYPE_MAP = {
+    "NoneStr": Optional[str],
+    "NoneBytes": Optional[bytes],
+    "StrBytes": None,
+    "NoneStrBytes": None,
+    "StrictStr": str,
+    "ConstrainedBytes": bytes,
+    "conbytes": bytes,
+    "ConstrainedList": None,
+    "conlist": None,
+    "ConstrainedSet": None,
+    "conset": None,
+    "ConstrainedStr": str,
+    "constr": str,
+    "EmailStr": str,
+    "PyObject": None,
+    "ConstrainedInt": int,
+    "conint": int,
+    "PositiveInt": int,
+    "NegativeInt": int,
+    "ConstrainedFloat": float,
+    "confloat": float,
+    "PositiveFloat": float,
+    "NegativeFloat": float,
+    "ConstrainedDecimal": Decimal,
+    "condecimal": Decimal,
+    "UUID1": UUID,
+    "UUID3": UUID,
+    "UUID4": UUID,
+    "UUID5": UUID,
+    "FilePath": None,
+    "DirectoryPath": None,
+    "Json": None,
+    "JsonWrapper": None,
+    "SecretStr": str,
+    "SecretBytes": bytes,
+    "StrictBool": bool,
+    "StrictInt": int,
+    "StrictFloat": float,
+    "PaymentCardNumber": None,
+    "ByteSize": None,
+    "AnyUrl": str,
+    "AnyHttpUrl": str,
+    "HttpUrl": str,
+    "PostgresDsn": str,
+    "RedisDsn": str,
+}
+
+
 FIELDS_MAP = {
-    pydantic.NoneStr: Optional[str],
-    pydantic.NoneBytes: Optional[bytes],
-    pydantic.StrBytes: None,
-    pydantic.NoneStrBytes: None,
-    pydantic.StrictStr: str,
-    pydantic.ConstrainedBytes: bytes,
-    pydantic.conbytes: bytes,
-    pydantic.ConstrainedList: None,
-    pydantic.conlist: None,
-    pydantic.ConstrainedSet: None,
-    pydantic.conset: None,
-    pydantic.ConstrainedStr: str,
-    pydantic.constr: str,
-    pydantic.EmailStr: str,
-    pydantic.PyObject: None,
-    pydantic.ConstrainedInt: int,
-    pydantic.conint: int,
-    pydantic.PositiveInt: int,
-    pydantic.NegativeInt: int,
-    pydantic.ConstrainedFloat: float,
-    pydantic.confloat: float,
-    pydantic.PositiveFloat: float,
-    pydantic.NegativeFloat: float,
-    pydantic.ConstrainedDecimal: Decimal,
-    pydantic.condecimal: Decimal,
-    pydantic.UUID1: UUID,
-    pydantic.UUID3: UUID,
-    pydantic.UUID4: UUID,
-    pydantic.UUID5: UUID,
-    pydantic.FilePath: None,
-    pydantic.DirectoryPath: None,
-    pydantic.Json: None,
-    pydantic.JsonWrapper: None,
-    pydantic.SecretStr: str,
-    pydantic.SecretBytes: bytes,
-    pydantic.StrictBool: bool,
-    pydantic.StrictInt: int,
-    pydantic.StrictFloat: float,
-    pydantic.PaymentCardNumber: None,
-    pydantic.ByteSize: None,
-    pydantic.AnyUrl: str,
-    pydantic.AnyHttpUrl: str,
-    pydantic.HttpUrl: str,
-    pydantic.PostgresDsn: str,
-    pydantic.RedisDsn: str,
+    getattr(pydantic, field_name): type
+    for field_name, type in ATTR_TO_TYPE_MAP.items()
+    if hasattr(pydantic, field_name)
 }
 
 


Test patch: