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

Let graphql-ws use type inference whenever possible #3704

Conversation

DoctorJohn
Copy link
Member

@DoctorJohn DoctorJohn commented Nov 19, 2024

Description

This is a followup PR for #3689 briging the changes we discussed in #3701 also to the legacy protocol (i.e., using type inference whenever possible). I also made assertions in two related tests more precise.

Types of Changes

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

Copy link
Contributor

sourcery-ai bot commented Nov 19, 2024

Reviewer's Guide by Sourcery

This PR enhances type inference in the legacy GraphQL WebSocket protocol by simplifying message handling and removing explicit type casting. The changes focus on consolidating WebSocket message sending through dedicated methods and improving type safety.

Sequence diagram for WebSocket message handling

sequenceDiagram
    participant Client
    participant WebSocket
    participant Handler

    Client->>WebSocket: send_legacy_message({"type": "connection_init"})
    WebSocket->>Handler: handle_connection_init
    Handler->>WebSocket: send_message({"type": "connection_ack"})

    Client->>WebSocket: send_legacy_message({"type": "start", "id": "demo", "payload": {"query": "subscription { echo(message: 'Hi') }"}})
    WebSocket->>Handler: handle_async_results
    Handler->>WebSocket: send_data_message(result, "demo")

    Client->>WebSocket: send_legacy_message({"type": "stop", "id": "demo"})
    WebSocket->>Handler: handle_stop
    Handler->>WebSocket: send_message({"type": "complete", "id": "demo"})
Loading

Updated class diagram for WebSocket message handling

classDiagram
    class BaseGraphQLWSHandler {
        +handle_connection_init(message: ConnectionInitMessage)
        +handle_keep_alive()
        +handle_async_results(operation_id: str)
        +cleanup_operation(operation_id: str)
        +send_data_message(execution_result: ExecutionResult, operation_id: str)
        +send_message(message: OperationMessage)
    }

    class WebSocketClient {
        +send_legacy_message(message: OperationMessage)
    }

    BaseGraphQLWSHandler <|-- WebSocketClient
    note for BaseGraphQLWSHandler "Consolidated message sending methods for type safety"
Loading

File-Level Changes

Change Details Files
Simplified WebSocket message handling by introducing a dedicated send_legacy_message method
  • Added new send_legacy_message method to handle legacy protocol messages
  • Replaced direct send_json calls with send_legacy_message for better type safety
  • Removed explicit type casting in message handling
tests/http/clients/base.py
tests/websockets/test_graphql_ws.py
tests/websockets/test_graphql_transport_ws.py
Refactored message sending in the GraphQL WebSocket handler
  • Introduced send_message method to centralize message sending logic
  • Renamed send_data to send_data_message for clarity
  • Simplified error and completion message handling
strawberry/subscriptions/protocols/graphql_ws/handlers.py
Enhanced type definitions for WebSocket messages
  • Updated OperationMessage type to include ConnectionKeepAliveMessage
  • Improved type safety in message handling functions
strawberry/subscriptions/protocols/graphql_ws/types.py

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 @DoctorJohn - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Testing: 3 issues found
  • 🟢 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.

tests/websockets/test_graphql_ws.py Show resolved Hide resolved
tests/websockets/test_graphql_ws.py Show resolved Hide resolved
tests/websockets/test_graphql_ws.py Show resolved Hide resolved
@botberry
Copy link
Member

botberry commented Nov 19, 2024

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release refactors part of the legacy graphql-ws protocol implementation, making it easier to read, maintain, and extend.

Here's the tweet text:

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

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

Copy link

codecov bot commented Nov 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.71%. Comparing base (4551c04) to head (fb9a3f6).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3704   +/-   ##
=======================================
  Coverage   96.71%   96.71%           
=======================================
  Files         500      500           
  Lines       33436    33438    +2     
  Branches     5590     5590           
=======================================
+ Hits        32336    32340    +4     
+ Misses        883      881    -2     
  Partials      217      217           
---- 🚨 Try these New Features:

Copy link

codspeed-hq bot commented Nov 19, 2024

CodSpeed Performance Report

Merging #3704 will not alter performance

Comparing DoctorJohn:make-more-use-of-type-inference-with-graphql-ws-types (fb9a3f6) with main (4551c04)

Summary

✅ 15 untouched benchmarks

@DoctorJohn DoctorJohn merged commit 60b026e into strawberry-graphql:main Nov 19, 2024
111 of 113 checks passed
@DoctorJohn DoctorJohn deleted the make-more-use-of-type-inference-with-graphql-ws-types branch November 20, 2024 15: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.

3 participants