strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
MIT License
391 stars 115 forks source link

feat: MPTT relations compatibility #552

Closed Eraldo closed 2 weeks ago

Eraldo commented 2 weeks ago

Description

Trying to get an author and then the total count of the children of that author breaks when there are no children present but requested in the query. Error:

"message": "An unknown error occurred."

The PR includes a test test_nexted_children.py, which illustrates the use case and resulting error.

FYI: Disabling the DjangoOptimizerExtension makes the test pass without any issue.

Types of Changes

Reproduction

I crated a PR with a issue reproduction: https://github.com/strawberry-graphql/strawberry-django/pull/551

Context

Error stack trace

FAILED    [100%]ERROR 

GraphQL request:10:25
 9 |                     children {
10 |                         totalCount
   |                         ^
11 |                         edges {
Traceback (most recent call last):
  File "/Users/eraldo/Library/Caches/pypoetry/virtualenvs/strawberry-graphql-django-4L3LqJ2E-py3.12/lib/python3.12/site-packages/graphql/execution/execute.py", line 521, in execute_field
    result = resolve_fn(source, info, **args)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eraldo/Inbox/temp/sandbox/strawberry-django/strawberry_django/optimizer.py", line 1075, in resolve
    ret = _next(root, info, *args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eraldo/Library/Caches/pypoetry/virtualenvs/strawberry-graphql-django-4L3LqJ2E-py3.12/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 740, in _resolver
    return _get_result_with_extensions(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eraldo/Library/Caches/pypoetry/virtualenvs/strawberry-graphql-django-4L3LqJ2E-py3.12/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 727, in extension_resolver
    return reduce(
           ^^^^^^^
  File "/Users/eraldo/Library/Caches/pypoetry/virtualenvs/strawberry-graphql-django-4L3LqJ2E-py3.12/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 722, in wrapped_get_result
    return _get_result(
           ^^^^^^^^^^^^
  File "/Users/eraldo/Library/Caches/pypoetry/virtualenvs/strawberry-graphql-django-4L3LqJ2E-py3.12/lib/python3.12/site-packages/strawberry/schema/schema_converter.py", line 678, in _get_result
    return field.get_result(
           ^^^^^^^^^^^^^^^^^
  File "/Users/eraldo/Library/Caches/pypoetry/virtualenvs/strawberry-graphql-django-4L3LqJ2E-py3.12/lib/python3.12/site-packages/strawberry/field.py", line 227, in get_result
    return self.base_resolver(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eraldo/Library/Caches/pypoetry/virtualenvs/strawberry-graphql-django-4L3LqJ2E-py3.12/lib/python3.12/site-packages/strawberry/types/fields/resolver.py", line 203, in __call__
    return self.wrapped_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eraldo/Inbox/temp/sandbox/strawberry-django/strawberry_django/resolvers.py", line 116, in inner_wrapper
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/Users/eraldo/Inbox/temp/sandbox/strawberry-django/strawberry_django/resolvers.py", line 83, in sync_resolver
    retval = resolver(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eraldo/Inbox/temp/sandbox/strawberry-django/strawberry_django/relay.py", line 55, in total_count
    assert self.nodes is not None
AssertionError

tests/relay/lazy/test_nested_children.py:20 (test_nested_children_total_count)
def test_nested_children_total_count():
        parent = RelayAuthor.objects.create(name="Parent")
        RelayAuthor.objects.create(name="Child", parent=parent)
        query = """
        query {
            authors(last: 1) {
                totalCount
                edges {
                    node {
                        id
                        name
                        children {
                            totalCount
                            edges {
                                node {
                                    id
                                    name
                                }
                            }
                        }
                    }
                }
            }
        }
        """

        result = schema.execute_sync(query)
>       assert not result.errors
E       AssertionError: assert not [GraphQLError('', locations=[SourceLocation(line=10, column=25)], path=['authors', 'edges', 0, 'node', 'children', 'totalCount'])]
E        +  where [GraphQLError('', locations=[SourceLocation(line=10, column=25)], path=['authors', 'edges', 0, 'node', 'children', 'totalCount'])] = ExecutionResult(data={'authors': {'totalCount': 2, 'edges': [{'node': {'id': 'QXV0aG9yVHlwZToy', 'name': 'Child', 'children': {'totalCount': None, 'edges': []}}}]}}, errors=[GraphQLError('', locations=[SourceLocation(line=10, column=25)], path=['authors', 'edges', 0, 'node', 'children', 'totalCount'])], extensions={}).errors

lazy/test_nested_children.py:48: AssertionError

Upvote & Fund

Fund with Polar