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

Support for nullable Connection #3703

Open
2 of 3 tasks
marmor157 opened this issue Nov 18, 2024 · 3 comments · May be fixed by #3707
Open
2 of 3 tasks

Support for nullable Connection #3703

marmor157 opened this issue Nov 18, 2024 · 3 comments · May be fixed by #3707

Comments

@marmor157
Copy link

marmor157 commented Nov 18, 2024

The field using strawberry.connection should be able to be nullable.

Feature Request Type

  • Core functionality
  • Alteration (enhancement/optimization) of existing feature(s)
  • New behavior

Description

This is needed in case if connection field would return an error e.g. from PermissionExtension and we don't want to error out the whole query.
Right now when the field has a declared type of strawberry.relay.ListConnection[...] | None the following error is thrown

strawberry.relay.exceptions.RelayWrongAnnotationError: Wrong annotation used on field "...". It should be annotated with a "Connection" subclass.

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
@patrick91
Copy link
Member

@sourcery-ai plan

Copy link
Contributor

sourcery-ai bot commented Nov 20, 2024

Objectives

  • Modify the connection field decorator in strawberry.relay.fields to support nullable Connection types
  • Update type checking logic to allow Optional[Connection[T]] and Connection[T] | None annotations
  • Add validation to ensure the inner type of Optional/Union is still a valid Connection type
  • Add tests to verify nullable connection fields work correctly with permission extensions
  • Add tests to verify nullable connection fields work with both Optional[Connection[T]] and Connection[T] | None syntax
  • Update documentation to reflect that connection fields can now be nullable

Setup commands

  • poetry install - ❌

File changes

strawberry/relay/fields.py

  • Modify ConnectionExtension.apply to handle Optional[Connection[T]] and Union[Connection[T], None] types
  • Update type checking logic to extract inner type from Optional and Union types
  • Add support for nullable connection fields by checking if the field type is wrapped in Optional or Union[T, None]

tests/relay/test_connection.py

  • Create new test file for connection field tests
  • Add test case for nullable connection field with Optional[Connection[T]]
  • Add test case for nullable connection field with Union[Connection[T], None]
  • Add test case for nullable connection field with permission extension that fails
  • Add test case to verify non-nullable connection fields still work

Verify commands

  • pytest tests/relay/test_connection.py -v - ❌

This plan was automatically generated.
Please review the plan carefully and make any necessary adjustments.

@patrick91
Copy link
Member

@sourcery-ai develop

sourcery-ai bot added a commit that referenced this issue Nov 20, 2024
Enable nullable Connection types in the connection field decorator by updating type checking logic and adding validation for inner types. Update documentation and add tests to ensure compatibility with permission extensions and different nullable syntax.

New Features:
- Support nullable Connection types in the connection field decorator in strawberry.relay.fields.

Enhancements:
- Update type checking logic to handle Optional[Connection[T]] and Connection[T] | None annotations.

Documentation:
- Update documentation to reflect that connection fields can now be nullable.

Tests:
- Add tests to verify nullable connection fields work correctly with permission extensions and both Optional[Connection[T]] and Connection[T] | None syntax.

Resolves #3703
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants