I'm trying to define a GQL Pydantic model w/ a JSON scalar attribute:
JSONScalar = strawberry.scalar(
NewType("JSONScalar", Any),
serialize=lambda v: v,
parse_value=lambda v: json.loads(v),
description="The GenericScalar scalar type represents a generic GraphQL scalar value that could be: List or Object."
)
class ResourcePyd(PydanticModel):
type: str
# TODO: Figure out how to use `Json` type for this attribute
# spec: Optional[Json]
# spec: Optional[str]
spec: Optional[JSONScalar]
However I can't get Pydantic to comply:
[cloud-app] [Tue Nov 9 17:21:12 2021] Running: pytest --durations=3
[cloud-app] ImportError while loading conftest '/cloud-app/tests/conftest.py'.
[cloud-app] tests/conftest.py:24: in <module>
[cloud-app] import app.gql_mutations as gql_mutations
[cloud-app] app/gql_mutations.py:21: in <module>
[cloud-app] from app.gql_errors import BadRequest, NotAuthorized, InternalError
[cloud-app] app/gql_errors.py:15: in <module>
[cloud-app] from app.models import Error, Errors, ResponseCode
[cloud-app] app/models.py:128: in <module>
[cloud-app] class ResourcePyd(PydanticModel):
[cloud-app] pydantic/main.py:299: in pydantic.main.ModelMetaclass.__new__
[cloud-app] ???
[cloud-app] pydantic/fields.py:411: in pydantic.fields.ModelField.infer
[cloud-app] ???
[cloud-app] pydantic/fields.py:342: in pydantic.fields.ModelField.__init__
[cloud-app] ???
[cloud-app] pydantic/fields.py:456: in pydantic.fields.ModelField.prepare
[cloud-app] ???
[cloud-app] pydantic/fields.py:670: in pydantic.fields.ModelField.populate_validators
[cloud-app] ???
[cloud-app] pydantic/validators.py:710: in find_validators
[cloud-app] ???
[cloud-app] E RuntimeError: error checking inheritance of <strawberry.custom_scalar.ScalarWrapper object at 0x7ff0045537f0> (type: ScalarWrapper)
Additionally when I try to use pydantic.Json
or pydantic.JsonWrapper
I get an UnsupportedTypeError
thrown, which is obvious from reading the code:
Maybe it will be obvious to someone what I'm doing wrong here?
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too