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

feat: Allow connection to be used without relay for generic cursor pagination #3669

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bellini666
Copy link
Member

@bellini666 bellini666 commented Oct 12, 2024

There's no need to force Connection to be relay specific, as it doesn't enforce the objects being paginated to implement the Node interface or even to use GlobalIDs.

This is moving the Connection/ListConnection/etc to strawberry.pagination`, allowing it to be used without relay for general-purpose cursor pagination.

This is something that we have been discussing for a while.

obs. I have not changed the implementation of anything other than removing the bound from NodeType, which should allow Connection[FooType] when FooType doesn't implement Node. The changes seem massive but they're mostly moving code from one place to the other =P

obs2. I also kept the older names as aliases in the relay package, but added a deprecation warning in case someone imports from there. We can remove those in the future.

@bellini666 bellini666 self-assigned this Oct 12, 2024
Copy link
Contributor

sourcery-ai bot commented Oct 12, 2024

Reviewer's Guide by Sourcery

This pull request moves the Connection implementation from the relay package to a new pagination package, allowing for more general-purpose cursor pagination. It introduces new imports from strawberry.pagination, deprecates some imports from strawberry.relay, and updates documentation and tests accordingly. The changes aim to make the Connection feature more accessible and flexible for use outside of the Relay specification context.

Class diagram for the new pagination package

classDiagram
    class Connection {
        +PageInfo page_info
        +List~Edge~ edges
        +resolve_node(node, info, kwargs) NodeType
        +resolve_connection(nodes, info, before, after, first, last, kwargs) AwaitableOrValue~Self~
    }
    class ListConnection {
        +PageInfo page_info
        +List~Edge~ edges
        +resolve_connection(nodes, info, before, after, first, last, kwargs) AwaitableOrValue~Self~
    }
    class PageInfo {
        +bool has_next_page
        +bool has_previous_page
        +Optional~str~ start_cursor
        +Optional~str~ end_cursor
    }
    class Edge {
        +str cursor
        +NodeType node
        +resolve_edge(node, cursor) Self
    }
    Connection <|-- ListConnection
    Connection o-- PageInfo
    Connection o-- Edge
    Edge --> NodeType
Loading

Class diagram for the updated relay package

classDiagram
    class GlobalIDValueError {
    }
    class NodeIDAnnotationError {
    }
    class NodeExtension {
    }
    class Node {
    }
    class GlobalID {
    }
    class NodeID {
    }
    GlobalIDValueError <|-- NodeIDAnnotationError
    Node <|-- NodeExtension
    Node <|-- GlobalID
    Node <|-- NodeID
Loading

File-Level Changes

Change Details Files
Move Connection implementation to a new pagination package
  • Create new files for pagination types, fields, utils, and exceptions
  • Update imports to use new pagination package
  • Deprecate Connection-related imports from relay package
  • Update documentation to reflect new pagination package usage
strawberry/pagination/types.py
strawberry/pagination/fields.py
strawberry/pagination/utils.py
strawberry/pagination/exceptions.py
strawberry/pagination/__init__.py
strawberry/relay/types.py
strawberry/relay/fields.py
strawberry/relay/utils.py
strawberry/relay/exceptions.py
strawberry/relay/__init__.py
Update documentation for Connection usage
  • Modify guides to use new pagination imports
  • Update examples in documentation to reflect new usage
  • Add explanation for general-purpose cursor pagination
docs/guides/pagination/connections.md
docs/guides/relay.md
docs/guides/pagination/overview.md
Refactor and update tests
  • Move Connection-related tests to pagination package
  • Update existing tests to use new pagination imports
  • Add new tests for pagination functionality
tests/pagination/test_types.py
tests/pagination/test_exceptions.py
tests/relay/test_types.py
tests/relay/test_exceptions.py
Add release notes
  • Create RELEASE.md file with details about the changes
  • Specify release type as minor
RELEASE.md

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
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @bellini666 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

}


def __getattr__(name: str) -> Any:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Consider performance impact of getattr

While using getattr for deprecation warnings is a clean approach, consider its potential impact on import times and overall performance. Consider profiling the import process to ensure this doesn't introduce significant overhead, especially for large projects.

def __getattr__(name: str) -> Any:
    if name in _DEPRECATIONS:
        return _lazy_load(name)
    raise AttributeError(f"module '{__name__}' has no attribute '{name}'")

def _lazy_load(name: str) -> Any:
    warnings.warn(_DEPRECATIONS[name], DeprecationWarning, stacklevel=2)
    return importlib.import_module(f".{name}", __package__)

@botberry
Copy link
Member

botberry commented Oct 12, 2024

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release moves the Connection implementation outside the relay package,
allowing it to be used for general-purpose cursor pagination.

The following now can be imported from strawberry.pagination:

  • Connection - base generic class for implementing connections
  • ListConnection - a limit-offset implementation of the connection
  • connection - field decorator for creating connections

Those can still be used together with the relay package, but importing from it
is now deprecated.

You can read more about connections in the
Strawberry Connection Docs.

Here's the tweet text:

🆕 Release (next) is out! Thanks to @_bellini666 for the PR 👏

Get it here 👉 https://strawberry.rocks/release/(next)

@@ -0,0 +1,21 @@
from .fields import ConnectionExtension, connection
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pagination and everything inside it is composed of Connection code moved from the relay package. No code here is new 😊

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmh, I wonder about this, Connection is a relay specific term, no? I wonder if moving it outside will bring confusion 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think relay coined that term, but today it is a general way of doing pagination in GraphQL. An example is this: https://graphql.org/learn/pagination

I have absolutely nothing against keeping it in relay, and calling it "relay style pagination" (as we were discussing in private =P), I'm only afraid of still making people confused, thinking this can only be used with relay.dev

Let me ping @strawberry-graphql/core here to get everyone's opinions :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think relay coined that term, but today it is a general way of doing pagination in GraphQL. An example is this: https://graphql.org/learn/pagination

On the last section of this page it says:

To ensure a consistent implementation of this pattern, the Relay project has a formal specification you can follow for building GraphQL APIs which use a cursor based connection pattern.

We should, therefore, be very clear about what kind of connection/pagination types we want to provide.

Do we want to (1) provide a single Connection type that does not strictly follow any specification but can be used with and without Relay nodes? Or (2) do we want to provide two separate Connection types: One that strictly follows the "GraphQL Cursor Connections Specification" by Relay (i.e., the one where nodes need to be bound to Node) and another one that can be used with any type.

If we want a connection type that strictly follows the spec, we could still share the implementation internally but must ensure the nodes are still bound to Node.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DoctorJohn my idea in this PR was for (1), but if we go for (2), which is what @patrick91 was suggesting, we are going to limit it from being used with non Node objects, which is not something that the connection actually requires (but I got your point)

What do you think about scheduling a discussion on this on discord? @patrick91 is out right now but we can do that once he gets back :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for what is worth, I'm ok with dropping the requirement on Node, but still keep it as relay pagination 😊

Copy link

codspeed-hq bot commented Oct 12, 2024

CodSpeed Performance Report

Merging #3669 will not alter performance

Comparing generic_connection (e68edba) with main (56172dc)

Summary

✅ 15 untouched benchmarks

Copy link

codecov bot commented Oct 12, 2024

Codecov Report

Attention: Patch coverage is 96.36364% with 16 lines in your changes missing coverage. Please review.

Project coverage is 96.98%. Comparing base (d504428) to head (e68edba).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3669      +/-   ##
==========================================
+ Coverage   96.67%   96.98%   +0.31%     
==========================================
  Files         503      511       +8     
  Lines       33457    33554      +97     
  Branches     5602     5625      +23     
==========================================
+ Hits        32344    32544     +200     
+ Misses        880      800      -80     
+ Partials      233      210      -23     

@bellini666 bellini666 marked this pull request as draft October 13, 2024 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants