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

Add support for optional connections #3707

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Add support for optional connections #3707

wants to merge 3 commits into from

Conversation

patrick91
Copy link
Member

@patrick91 patrick91 commented Nov 20, 2024

Closes #3703

sourcery-ai bot and others added 3 commits November 20, 2024 12:10
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
Copy link
Contributor

sourcery-ai bot commented Nov 20, 2024

Reviewer's Guide by Sourcery

This PR adds support for nullable (optional) Connection types in Strawberry's relay field decorator. The implementation modifies the type handling in the relay fields to properly handle Optional Connection types and includes test cases to verify the functionality.

Class diagram for updated relay field decorator

classDiagram
    class StrawberryField {
        +apply(field: StrawberryField) void
        +resolve() void
        +connection_type: Type[Connection[Node]]
    }
    class Connection {
        <<interface>>
    }
    class Node {
        <<interface>>
    }
    class StrawberryOptional {
        +of_type: Type
    }
    class ConnectionGraphQLType {
        <<alias>>
    }
    StrawberryField --> Connection
    StrawberryField --> Node
    StrawberryField --> StrawberryOptional
    StrawberryField --> ConnectionGraphQLType
    note for ConnectionGraphQLType "Alias for handling optional Connection types"
Loading

Class diagram for new test cases

classDiagram
    class User {
        +id: NodeID
        +name: str
        +resolve_nodes(info, node_ids, required) List[User]
    }
    class UserConnection {
        +resolve_connection(nodes: Iterable[User], info, after, before, first, last) Optional[Self]
    }
    class TestPermission {
        +message: str
        +has_permission(source, info, **kwargs) bool
    }
    UserConnection --> User
    TestPermission --> UserConnection
    note for User "Represents a user node in the relay connection"
    note for UserConnection "Handles the connection logic for User nodes"
    note for TestPermission "Custom permission class for testing"
Loading

File-Level Changes

Change Details Files
Added support for nullable Connection types in the relay field decorator
  • Modified type checking to handle StrawberryOptional types
  • Updated connection type casting to use the inner type for optional connections
  • Changed ConnectionGraphQLType from Type[Connection[NodeType]] to Any to support optional types
  • Added handling for union types in resolver type checking
strawberry/relay/fields.py
Fixed a typo in the optional type creation documentation
  • Corrected 'di' to 'do' in comment about Union type behavior
strawberry/annotation.py
Added comprehensive tests for nullable connections
  • Added test for nullable connection with Optional type
  • Added test for nullable connection with pipe syntax (Python 3.10+)
  • Added test for nullable connection with permissions
tests/relay/test_connection.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

codspeed-hq bot commented Nov 20, 2024

CodSpeed Performance Report

Merging #3707 will not alter performance

Comparing fix-3703 (1b82b34) with main (069fe2c)

Summary

✅ 15 untouched benchmarks

Copy link

codecov bot commented Nov 21, 2024

Codecov Report

Attention: Patch coverage is 96.96970% with 2 lines in your changes missing coverage. Please review.

Project coverage is 63.13%. Comparing base (069fe2c) to head (1b82b34).

❗ There is a different number of reports uploaded between BASE (069fe2c) and HEAD (1b82b34). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (069fe2c) HEAD (1b82b34)
2 1
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #3707       +/-   ##
===========================================
- Coverage   96.70%   63.13%   -33.58%     
===========================================
  Files         500      496        -4     
  Lines       33438    32339     -1099     
  Branches     5590     1663     -3927     
===========================================
- Hits        32336    20417    -11919     
- Misses        882    11589    +10707     
- Partials      220      333      +113     
---- 🚨 Try these New Features:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for nullable Connection
1 participant