strawberry-graphql / strawberry-django

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

Cannot use relative import when importing root schema to use as a fixture for Pytest #328

Closed fireteam99 closed 1 month ago

fireteam99 commented 11 months ago

Describe the Bug

Attempting to import the root schema into a conftest using a relative path will cause a cryptic strawberry.exceptions.duplicated_type_name.DuplicatedTypeName error. Using a absolute path fixes the problem.

Example

# conftest.py

from .schema import schema as strawberry_schema                       # <-- this breaks!
from api_graphql.schema import schema as strawberry_schema   # <-- this works!

@pytest.fixture
def schema():
    return strawberry_schema

Stack trace

ImportError while loading conftest '/workspace/backend_service/medallion/api_graphql/conftest.py'.
/usr/local/lib/python3.9/site-packages/ddtrace/internal/module.py:220: in _exec_module
    self.loader.exec_module(module)
backend_service/medallion/api_graphql/conftest.py:12: in <module>
    from .schema import schema as strawberry_schema
/usr/local/lib/python3.9/site-packages/ddtrace/internal/module.py:220: in _exec_module
    self.loader.exec_module(module)
backend_service/medallion/api_graphql/schema.py:101: in <module>
    schema = strawberry.Schema(
/usr/local/lib/python3.9/site-packages/strawberry/schema/schema.py:160: in __init__
    raise error.__cause__ from None
/usr/local/lib/python3.9/site-packages/graphql/type/definition.py:808: in fields
    fields = resolve_thunk(self._fields)
/usr/local/lib/python3.9/site-packages/graphql/type/definition.py:300: in resolve_thunk
    return thunk() if callable(thunk) else thunk
/usr/local/lib/python3.9/site-packages/strawberry/schema/schema_converter.py:516: in <lambda>
    fields=lambda: self.get_graphql_fields(object_type),
/usr/local/lib/python3.9/site-packages/strawberry/schema/schema_converter.py:373: in get_graphql_fields
    return _get_thunk_mapping(
/usr/local/lib/python3.9/site-packages/strawberry/schema/schema_converter.py:133: in _get_thunk_mapping
    thunk_mapping[name_converter(field)] = field_converter(
/usr/local/lib/python3.9/site-packages/strawberry/schema/schema_converter.py:314: in from_field
    self.from_maybe_optional(
/usr/local/lib/python3.9/site-packages/strawberry/schema/schema_converter.py:769: in from_maybe_optional
    return GraphQLNonNull(self.from_type(type_))
/usr/local/lib/python3.9/site-packages/strawberry/schema/schema_converter.py:787: in from_type
    return self.from_object(type_.__strawberry_definition__)
/usr/local/lib/python3.9/site-packages/strawberry/schema/schema_converter.py:517: in from_object
    interfaces=list(map(self.from_interface, object_type.interfaces)),
/usr/local/lib/python3.9/site-packages/strawberry/schema/schema_converter.py:426: in from_interface
    self.validate_same_type_definition(interface_name, interface, cached_type)
/usr/local/lib/python3.9/site-packages/strawberry/schema/schema_converter.py:949: in validate_same_type_definition
    raise DuplicatedTypeName(first_origin, second_origin, name)
E   strawberry.exceptions.duplicated_type_name.DuplicatedTypeName: Type UuidBasedTimeStampedInterface is defined multiple times in the schema

System Information

Additional Context

This bug was not present in previous versions. Specifically, it was working as expected in: strawberry-graphql 0.133.5, strawberry-graphql-django 0.7

Upvote & Fund

Fund with Polar

bellini666 commented 11 months ago

Woow, this is really interesting... Wondering what might be causing that.

Btw, does that only happen for strawberry django schemas, or for strawberry in general?

fireteam99 commented 11 months ago

Good point, I think this might be a strawberry issue rather than a strawberry django issue

bellini666 commented 1 month ago

We have solved a lot of "duplicated type name" issues in strawberry, so probably there's a high possibility of this have being solved.

I'm going to close this, specially as this is not a strawberry-django issue but a strawberry one. In case this has not been fixed, please comment here and/or open an issue on strawberry itself so that we can try to find the root cause of the issue and fix it