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

field: None = strawberry.field() results in UnresolvedFieldTypeError: Could not resolve the type of ... error #3493

Open
huonw opened this issue May 9, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@huonw
Copy link
Contributor

huonw commented May 9, 2024

Describe the Bug

Using None as an annotation directly (not None | Something, just None) on a data class-style field with strawberry.field() doesn't work. Various other similar incantations do work.

import strawberry

@strawberry.type
class Query:
    working1: "None" = strawberry.field() # deferred
    working2: None # no strawberry.field
    @strawberry.field # method
    def working3(self) -> None: return None 

    # BUG:
    broken: None = strawberry.field()

schema = strawberry.Schema(Query)
Traceback (most recent call last):
  File "/lib/python311.zip/_pyodide/_base.py", line 573, in eval_code_async
    await CodeRunner(
  File "/lib/python311.zip/_pyodide/_base.py", line 395, in run_async
    await coroutine
  File "<exec>", line 21, in <module>
  File "/lib/python3.11/site-packages/strawberry/schema/schema.py", line 162, in __init__
    raise error.__cause__ from None
  File "/lib/python3.11/site-packages/graphql/type/definition.py", line 808, in fields
    fields = resolve_thunk(self._fields)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/graphql/type/definition.py", line 300, in resolve_thunk
    return thunk() if callable(thunk) else thunk
           ^^^^^^^
  File "/lib/python3.11/site-packages/strawberry/schema/schema_converter.py", line 525, in <lambda>
    fields=lambda: self.get_graphql_fields(object_type),
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/strawberry/schema/schema_converter.py", line 382, in get_graphql_fields
    return _get_thunk_mapping(
           ^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/strawberry/schema/schema_converter.py", line 132, in _get_thunk_mapping
    raise UnresolvedFieldTypeError(type_definition, field)
strawberry.exceptions.unresolved_field_type.UnresolvedFieldTypeError: Could not resolve the type of 'broken'. Check that the class is accessible from the global module scope.

If I comment out broken, then strawberry accepts this code, and the resulting schema is what I expect ✅

type Query {
  working1: Void
  working2: Void
  working3: Void
}

"""Represents NULL values"""
scalar Void

(The fact that "None" works means that it also works to run with from __future__ import annotations to have all annotations be deferred strings.)

System Information

Additional Context

This annotation is weird but is meaningful/semi-useful (for historical reasons, we have an input type that has no real fields, so we have _unused: None = strawberry.field(default=None) instead.

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
@huonw huonw added the bug Something isn't working label May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant