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

Integrate websockets into the async base view #3638

Conversation

DoctorJohn
Copy link
Member

@DoctorJohn DoctorJohn commented Sep 22, 2024

Description

This PR introduces adapter classes for WebSockets, similar to the adapter classes for HTTP requests. This allows us to share all common WebSocket logic between all integrations.

As a result, WebSockets now behave much more consistently between different integrations, which allows us to share all WebSocket tests between them.

I put much energy into keeping this a non-breaking change and keeping it as reviewable as possible. As a result, the Channels integration still looks as funny as before, i.e., its HTTP and WS handlers are still split. This deserves another look in a separate PR.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Summary by Sourcery

Integrate WebSocket adapter classes into the async base view to unify WebSocket logic across integrations, ensuring consistent behavior and simplifying maintenance. Remove redundant WebSocket handler implementations and update tests to reflect the new architecture.

New Features:

  • Introduce adapter classes for WebSockets to unify and share common WebSocket logic across different integrations, enhancing consistency and maintainability.

Enhancements:

  • Refactor WebSocket handling to use a shared base class, improving consistency and reducing duplication across HTTP integrations.

Tests:

  • Add new tests to verify the behavior of WebSocket subprotocol handling, including scenarios for unsupported subprotocols and client preferences.

Chores:

  • Remove obsolete WebSocket handler files and tests that are no longer needed due to the new unified WebSocket handling approach.

Copy link
Contributor

sourcery-ai bot commented Sep 22, 2024

Reviewer's Guide by Sourcery

This pull request integrates WebSocket functionality into the async base view, resulting in more consistent WebSocket behavior across different integrations. The changes involve refactoring the WebSocket handling logic, removing redundant code, and updating various integrations to use the new unified approach.

File-Level Changes

Change Details Files
Integrated WebSocket functionality into AsyncBaseHTTPView
  • Added WebSocket-related methods to AsyncBaseHTTPView
  • Introduced AsyncWebSocketAdapter class for handling WebSocket connections
  • Updated existing integrations to use the new WebSocket handling approach
  • Removed integration-specific WebSocket handler classes
strawberry/http/async_base_view.py
strawberry/channels/handlers/ws_handler.py
strawberry/litestar/controller.py
strawberry/aiohttp/views.py
strawberry/asgi/__init__.py
strawberry/fastapi/router.py
Refactored WebSocket protocol handling
  • Implemented unified approach for selecting WebSocket subprotocols
  • Updated WebSocket connection initialization process
  • Removed redundant protocol-specific handler classes
strawberry/subscriptions/protocols/graphql_ws/handlers.py
strawberry/subscriptions/protocols/graphql_transport_ws/handlers.py
tests/websockets/test_websockets.py
Updated test suite for WebSocket functionality
  • Added new tests for WebSocket subprotocol selection and rejection
  • Updated existing tests to work with the new WebSocket handling approach
  • Removed integration-specific WebSocket test files
tests/websockets/test_websockets.py
tests/http/clients/base.py
tests/http/clients/channels.py
tests/http/clients/asgi.py
tests/http/clients/aiohttp.py
tests/http/clients/litestar.py
tests/http/clients/fastapi.py
Cleaned up and reorganized code structure
  • Removed redundant WebSocket handler files
  • Updated import statements across the project
  • Simplified class inheritance in various integrations
strawberry/channels/__init__.py
strawberry/http/exceptions.py
strawberry/http/typevars.py
RELEASE.md

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

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 @DoctorJohn - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding more comprehensive error handling for WebSocket connections, especially for edge cases that might occur during runtime.
  • It would be beneficial to include performance benchmarks to ensure that the centralization of WebSocket logic doesn't introduce any significant overhead.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟡 Documentation: 1 issue found

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 to tell me if it was helpful.

strawberry/aiohttp/views.py Show resolved Hide resolved
RELEASE.md Show resolved Hide resolved
strawberry/channels/handlers/ws_handler.py Show resolved Hide resolved
@botberry
Copy link
Member

botberry commented Sep 22, 2024

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


Starting with this release, WebSocket logic now lives in the base class shared between all HTTP integrations.
This makes the behaviour of WebSockets much more consistent between integrations and easier to maintain.

Here's the tweet text:

🚀 Starting with Strawberry (next), WebSocket logic now lives in the base
class shared across all HTTP integrations. More consistent behavior and easier
maintenance for WebSockets across integrations. 🎉

Thanks to @NucleonJohn for the PR 👏

https://strawberry.rocks/release/(next)

@botberry
Copy link
Member

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


Starting with this release, WebSocket logic now lives in the base class shared between all HTTP integrations.
This makes the behaviour of WebSockets much more consistent between integrations and easier to maintain.

Here's the tweet text:

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

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

@DoctorJohn DoctorJohn force-pushed the integrate-websockets-into-async-base-view branch from 2d01e81 to 1430fe5 Compare September 22, 2024 16:55
Copy link

codecov bot commented Sep 22, 2024

Codecov Report

Attention: Patch coverage is 98.86105% with 5 lines in your changes missing coverage. Please review.

Project coverage is 96.82%. Comparing base (1b33547) to head (75789d6).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3638      +/-   ##
==========================================
+ Coverage   96.76%   96.82%   +0.05%     
==========================================
  Files         522      503      -19     
  Lines       33824    33409     -415     
  Branches     5635     5583      -52     
==========================================
- Hits        32731    32348     -383     
+ Misses        863      830      -33     
- Partials      230      231       +1     

Copy link

codspeed-hq bot commented Sep 22, 2024

CodSpeed Performance Report

Merging #3638 will not alter performance

Comparing DoctorJohn:integrate-websockets-into-async-base-view (75789d6) with main (1b33547)

Summary

✅ 15 untouched benchmarks

@DoctorJohn DoctorJohn force-pushed the integrate-websockets-into-async-base-view branch from d46fbbb to abac0e4 Compare September 22, 2024 17:36
@patrick91
Copy link
Member

This PR makes me so happy <3

@erikwrede
Copy link
Member

erikwrede commented Sep 27, 2024

@patrick91 wait until we get the incremental delivery going 🫢

[different pr of course]

Copy link
Member

@patrick91 patrick91 left a comment

Choose a reason for hiding this comment

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

Looks solid!

@patrick91 patrick91 merged commit 40a0504 into strawberry-graphql:main Oct 5, 2024
117 checks passed
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