strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

TypeError: fields cannot be resolved. Unexpected type 'typing.Any'

Open hjilke opened this issue 3 years ago • 2 comments
trafficstars

Hey Everyone,

I am using FastAPI with strawberry GraphQL & I have a pydantic model that uses typing.Any which causes problems.

import strawberry
from pydantic import BaseModel
from typing import Optional, Any

class Model(BaseModel):
    name: str
    dataset: Optional[Any] = None

@strawberry.experimental.pydantic.input(model=Model, all_fields=True)
class ModeltStrawberry:
    pass

@strawberry.type
class Query:
    @strawberry.field
    def model_test(
        self,
        model: Optional[ModeltStrawberry] = None,
    ) -> str:
        pass

schema = strawberry.Schema(query=Query)

causes:

...
  File "python3.7/site-packages/strawberry/schema/schema_converter.py", line 586, in from_type
    raise TypeError(f"Unexpected type '{type_}'")
TypeError: Unexpected type 'typing.Any'

What am I doing wrong? How to use typing.Any with strawberry and FastAPI? I am using strawberry-graphql version 0.123.2.

Thanks!!!

hjilke avatar Aug 02 '22 13:08 hjilke

@hjilke hi there, in graphql, there isn't a notion of an "Any" type. all objects must have a type

thejaminator avatar Aug 02 '22 19:08 thejaminator

Thanks @thejaminator

hjilke avatar Aug 03 '22 05:08 hjilke

If it is defined as Any, please emit String as GraphQL schema and serialize it with str()?

For my case, I am not making a class for Strawberry, I am converting a Pydantic class, and I have to use Any when I use Python because of some library. Could you please reconsider this?

conao3 avatar Sep 27 '22 08:09 conao3

If it is defined as Any, please emit String as GraphQL schema and serialize it with str()?

For my case, I am not making a class for Strawberry, I am converting a Pydantic class, and I have to use Any when I use Python because of some library. Could you please reconsider this?

Hi, I'm cleaning up old issues. Please open a new issue with a code sample if you're still affected by this :)

DoctorJohn avatar Mar 13 '24 01:03 DoctorJohn