Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use dict or JSON types in Pydantic models as attributes #1409

Open
mecampbellsoup opened this issue Nov 9, 2021 · 1 comment
Open

Comments

@mecampbellsoup
Copy link
Contributor

mecampbellsoup commented Nov 9, 2021

Description

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:

https://github.com/strawberry-graphql/strawberry/blob/main/strawberry/experimental/pydantic/fields.py#L42-L43

Maybe it will be obvious to someone what I'm doing wrong here?

Refs

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@lovetoburnswhen
Copy link

Here's what I needed to do to get JSONScalar working with inline values

JSONScalar = strawberry.scalar(
    NewType("JSONScalar", Any),
    serialize=lambda v: v,
    parse_value=lambda v: json.loads(v),
    parse_literal=graphql.utilities.value_from_ast_untyped,
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants