strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

pydantic TypeError: fields cannot be resolved

Open caspervk opened this issue 6 months ago • 0 comments

I am trying the following:

import strawberry
from pydantic.v1 import BaseModel


class Book(BaseModel):
    title: str


class Library(BaseModel):
    books: list[Book]


@strawberry.experimental.pydantic.type(model=Book, all_fields=True)
class BookType:
    pass


@strawberry.experimental.pydantic.type(model=Library, all_fields=True)
class LibraryType:
    pass


@strawberry.type
class Query:
    library: LibraryType


schema = strawberry.Schema(query=Query)
❯ python3.12 -m venv venv
❯ source venv/bin/activate.fish
venv ❯ pip install 'strawberry-graphql[debug-server]==0.237.2' pydantic==2.8.2
...
Successfully installed annotated-types-0.7.0 anyio-4.4.0 click-8.1.7 graphql-core-3.2.3 h11-0.14.0 idna-3.7 libcst-1.4.0 markdown-it-py-3.0.0 mdurl-0.1.2 pydantic-2.8.2 pydantic-core-2.20.1 pygments-2.18.0 python-dateutil-2.9.0.post0 python-multipart-0.0.9 pyyaml-6.0.1 rich-13.7.1 shellingham-1.5.4 six-1.16.0 sniffio-1.3.1 starlette-0.38.2 strawberry-graphql-0.237.2 typer-0.12.3 typing-extensions-4.12.2 uvicorn-0.30.3

venv ❯ _TYPER_STANDARD_TRACEBACK=1 strawberry server schema
Traceback (most recent call last):
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/graphql/type/definition.py", line 808, in fields
    fields = resolve_thunk(self._fields)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/graphql/type/definition.py", line 300, in resolve_thunk
    return thunk() if callable(thunk) else thunk
           ^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 635, in <lambda>
    fields=lambda: self.get_graphql_fields(object_type),
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 440, in get_graphql_fields
    return _get_thunk_mapping(
           ^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 138, in _get_thunk_mapping
    thunk_mapping[name_converter(field)] = field_converter(
                                           ^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 381, in from_field
    self.from_maybe_optional(
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 816, in from_maybe_optional
    return GraphQLNonNull(self.from_type(type_))
                          ^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 827, in from_type
    return self.from_list(type_)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 589, in from_list
    of_type = self.from_maybe_optional(type_.of_type)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 816, in from_maybe_optional
    return GraphQLNonNull(self.from_type(type_))
                          ^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 849, in from_type
    raise TypeError(f"Unexpected type '{type_}'")
TypeError: Unexpected type '<class 'schema.Book'>'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/tmp/tmp.uFBtUvCBNP/venv/bin/strawberry", line 8, in <module>
    sys.exit(run())
             ^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/cli/__init__.py", line 10, in run
    app()
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/typer/main.py", line 326, in __call__
    raise e
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/typer/main.py", line 309, in __call__
    return get_command(self)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/typer/core.py", line 723, in main
    return _main(
           ^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/typer/core.py", line 193, in _main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/typer/main.py", line 692, in wrapper
    return callback(**use_params)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/cli/commands/server.py", line 65, in server
    load_schema(schema, app_dir=app_dir)
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/cli/utils/__init__.py", line 14, in load_schema
    schema_symbol = import_module_symbol(schema, default_symbol_name="schema")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/utils/importer.py", line 15, in import_module_symbol
    module = importlib.import_module(module_name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/run/current-system/sw/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/tmp/tmp.uFBtUvCBNP/schema.py", line 28, in <module>
    schema = strawberry.Schema(query=Query)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/strawberry/schema/schema.py", line 180, in __init__
    self._schema = GraphQLSchema(
                   ^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/graphql/type/schema.py", line 224, in __init__
    collect_referenced_types(query)
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/graphql/type/schema.py", line 433, in collect_referenced_types
    collect_referenced_types(field.type)
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/graphql/type/schema.py", line 432, in collect_referenced_types
    for field in named_type.fields.values():
                 ^^^^^^^^^^^^^^^^^
  File "/run/current-system/sw/lib/python3.12/functools.py", line 995, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/tmp/tmp.uFBtUvCBNP/venv/lib/python3.12/site-packages/graphql/type/definition.py", line 811, in fields
    raise cls(f"{self.name} fields cannot be resolved. {error}") from error
TypeError: LibraryType fields cannot be resolved. Unexpected type '<class 'schema.Book'>'

If I change from pydantic.v1 import BaseModel to from pydantic import BaseModel the example works:

venv ❯ strawberry server schema
Running strawberry on http://0.0.0.0:8000/graphql 🍓

caspervk avatar Jul 31 '24 16:07 caspervk