strawberry-graphql / strawberry-sqlalchemy

A SQLAlchemy Integration for strawberry-graphql
MIT License
91 stars 26 forks source link

Could not resolve the type of 'node'. Check that the class is accessible from the global module scope. #18

Closed EricChambersPowerDigital closed 4 months ago

EricChambersPowerDigital commented 1 year ago

Could not resolve the type of 'node'. Check that the class is accessible from the global module scope

Description

After adding a relationship to two SQLAlchemy models, the following error from strawberry is produced when the models are being mapped by this package.

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.11/site-packages/uvicorn/_subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "/usr/local/lib/python3.11/site-packages/uvicorn/server.py", line 59, in run
    return asyncio.run(self.serve(sockets=sockets))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete
  File "/usr/local/lib/python3.11/site-packages/uvicorn/server.py", line 66, in serve
    config.load()
  File "/usr/local/lib/python3.11/site-packages/uvicorn/config.py", line 471, in load
    self.loaded_app = import_from_string(self.app)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/usr/app/run_server.py", line 8, in <module>
    from src.graphql.schema import schema
  File "/usr/app/src/graphql/schema.py", line 44, in <module>
    schema = strawberry.federation.Schema(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/strawberry/federation/schema.py", line 73, in __init__
    super().__init__(
  File "/usr/local/lib/python3.11/site-packages/strawberry/schema/schema.py", line 158, in __init__
    raise error.__cause__ from None
  File "/usr/local/lib/python3.11/site-packages/graphql/type/definition.py", line 808, in fields
    fields = resolve_thunk(self._fields)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/graphql/type/definition.py", line 300, in resolve_thunk
    return thunk() if callable(thunk) else thunk
           ^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/strawberry/schema/schema_converter.py", line 437, in <lambda>
    fields=lambda: self.get_graphql_fields(object_type),
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/strawberry/schema/schema_converter.py", line 326, in get_graphql_fields
    return self._get_thunk_mapping(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/strawberry/schema/schema_converter.py", line 316, in _get_thunk_mapping
    raise UnresolvedFieldTypeError(type_definition, field)
strawberry.exceptions.unresolved_field_type.UnresolvedFieldTypeError: Could not resolve the type of 'node'. Check that the class is accessible from the global module scope.

Steps to Reproduce

  1. Add relationships to SQLAlchemy models.

    
    class Books(Base):
    '''Book model.
    '''
    __tablename__ = 'books'
    
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    title = Column(String(256), nullable=False)
    
    reviews = relationship('Reviews', back_populates='book')

class Reviews(Base): '''Review model. ''' tablename = 'reviews'

id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
body = Column(String(256), nullable=False)
book_id = Column(UUID(as_uuid=True), ForeignKey('books.id'), nullable=False)

book = relationship('Books', back_populates='reviews')
2. Create GraphQL type for Book.

strawberry_model = StrawberrySQLAlchemyMapper()

@strawberry_model.type(model=Books) class Book: '''Book type. ''' exclude = ['deleted_at']

3. Finalize the mapper class 

strawberry_model.finalize()



## Environment

- Python: 3.11.3 
- sqlalchemy: 2.0.15
- strawberry-sqlalchemy-mapper: 0.1.1

<!-- POLAR PLEDGE BADGE START -->
## Upvote & Fund

- We're using [Polar.sh](https://polar.sh/strawberry-graphql) 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.

<a href="https://polar.sh/strawberry-graphql/strawberry-sqlalchemy/issues/18">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/strawberry-graphql/strawberry-sqlalchemy/issues/18/pledge.svg?darkmode=1">
  <img alt="Fund with Polar" src="https://polar.sh/api/github/strawberry-graphql/strawberry-sqlalchemy/issues/18/pledge.svg">
</picture>
</a>
<!-- POLAR PLEDGE BADGE END -->
TimDumol commented 1 year ago

From the trace, you seem to be using a federated schema. The library currently assumes you use a non-federated schema, so you'll encounter issues because it uses the standard strawberry.type decorator rather than the strawberry.federation.type decoratorm c.f. the linked docs

Notice that the Book type is using the strawberry.federation.type decorator, as opposed to the normal strawberry.type, this new decorator extends the base one and allows us to define federation-specific attributes on the type.

I'll consider this as wont-fix and will add a note on the README about lack of federated schema support, although you're free to contribute a PR if you can make a fix.

EricChambersPowerDigital commented 1 year ago

@TimDumol Thank you for the detailed and quick response!

Also would like to note that you can use strawberry.federation.type as long as the model does not define a relationship.

Ckk3 commented 4 months ago

Hi, @TimDumol and @EricChambersPowerDigital, I Think this Issue already has been resolved in #9 .