Skip to content

Pydantic v2 Incompatability with strawberry-graphql #612

Closed
@XChikuX

Description

@XChikuX

redis-om on version 0.3.0 with pydantic v2 raises this following error.

│ /root/test/.env/lib/python3.10/site-packages/strawberry/schema/schema_converter.py:777 in        │
│ from_type                                                                                        │
│                                                                                                  │
│   774 │   │   elif isinstance(type_, StrawberryObjectDefinition):                                │
│   775 │   │   │   return self.from_object(type_)                                                 │
│   776 │   │   elif isinstance(type_, StrawberryUnion):                                           │
│ ❱ 777 │   │   │   return self.from_union(type_)                                                  │
│   778 │   │   elif isinstance(type_, LazyType):                                                  │
│   779 │   │   │   return self.from_type(type_.resolve_type())                                    │
│   780 │   │   elif compat.is_scalar(                                                             │
│                                                                                                  │
│ ╭────────────────────────────────────────── locals ──────────────────────────────────────────╮   │
│ │  self = <strawberry.schema.schema_converter.GraphQLCoreConverter object at 0x7f0c5dabcd30> │   │
│ │ type_ = <strawberry.union.StrawberryUnion object at 0x7f0c5dadf190>                        │   │
│ ╰────────────────────────────────────────────────────────────────────────────────────────────╯   │
│                                                                                                  │
│ /root/test/.env/lib/python3.10/site-packages/strawberry/schema/schema_converter.py:795 in        │
│ from_union                                                                                       │
│                                                                                                  │
│   792 │   │   │   # TypeVars, Annotations, LazyTypes, etc it can't perfectly detect issues at    │
│   793 │   │   │   # that stage                                                                   │
│   794 │   │   │   if not StrawberryUnion.is_valid_union_type(type_):                             │
│ ❱ 795 │   │   │   │   raise InvalidUnionTypeError(union_name, type_, union_definition=union)     │
│   796 │   │                                                                                      │
│   797 │   │   # Don't reevaluate known types                                                     │
│   798 │   │   if union_name in self.type_map:                                                    │
│                                                                                                  │
│ ╭─────────────────────────────────────────── locals ───────────────────────────────────────────╮ │
│ │       self = <strawberry.schema.schema_converter.GraphQLCoreConverter object at              │ │
│ │              0x7f0c5dabcd30>                                                                 │ │
│ │      type_ = <class 'str'>                                                                   │ │
│ │      union = <strawberry.union.StrawberryUnion object at 0x7f0c5dadf190>                     │ │
│ │ union_name = "<class 'str'><class 'redis_om.model.model.ExpressionProxy'>"                   │ │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────╯ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
InvalidUnionTypeError: Type `str` cannot be used in a GraphQL Union

I see this issue with HashModel JsonModel and EmbeddedJsonModel being used.

If I have an object with redis-om but its not part of the graphql schema then I only get the below warning

Running strawberry on http://0.0.0.0:8000/graphql 🍓
/root/test/.env/lib/python3.10/site-packages/pydantic/_internal/_config.py:334: UserWarning: Valid config keys have changed in V2:
* 'orm_mode' has been renamed to 'from_attributes'
  warnings.warn(message, UserWarning)

Using regular BaseModel from pydantic seems to work fine.

So I've triaged the issue to changes in redis-om

Has there been some kind of Union with str attached to JsonModel and EmbeddedJsonModel? I'd love to be able to resolve this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions