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

fix: make payload:null handling in connection_init consistent between protocols #3744

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

Conversation

Object905
Copy link

@Object905 Object905 commented Dec 28, 2024

When client specifies payload: null in connection_init message graphql_transport_ws erored with Invalid connection init payload, while in graphql_ws this case was handled.

Noticed this with latest apollo-router as a client.

Summary by Sourcery

Bug Fixes:

  • Fixed an issue where graphql_transport_ws would error with "Invalid connection init payload" when the client specified payload: null in the connection_init message, while graphql_ws handled this case correctly.

Copy link
Contributor

sourcery-ai bot commented Dec 28, 2024

Reviewer's Guide by Sourcery

This pull request fixes an inconsistency in how payload: null is handled in connection_init messages between the graphql_transport_ws and graphql_ws protocols. Previously, graphql_transport_ws raised an error when payload was null, while graphql_ws handled this case gracefully. This change aligns the behavior of both protocols by ensuring that a null payload is treated as an empty dictionary {} in both graphql_transport_ws and graphql_ws.

Sequence diagram showing updated connection_init message handling

sequenceDiagram
    participant Client
    participant GraphQL_WS as GraphQL WS Protocol
    participant GraphQL_Transport_WS as GraphQL Transport WS Protocol

    Note over Client,GraphQL_Transport_WS: Both protocols now handle payload:null consistently

    Client->>GraphQL_WS: connection_init (payload:null)
    GraphQL_WS-->>Client: Convert null to {} and continue

    Client->>GraphQL_Transport_WS: connection_init (payload:null)
    GraphQL_Transport_WS-->>Client: Convert null to {} and continue
Loading

State diagram for connection_init payload handling

stateDiagram-v2
    [*] --> CheckPayload: Receive connection_init
    CheckPayload --> ConvertNull: payload is null
    CheckPayload --> ValidateType: payload exists
    ConvertNull --> ValidateType: convert to {}
    ValidateType --> Success: is dictionary
    ValidateType --> Error: not dictionary
    Success --> [*]: Continue connection
    Error --> [*]: Close connection
Loading

File-Level Changes

Change Details Files
Handle null payloads in connection_init message consistently.
  • Added a check for payload being None and setting it to an empty dictionary {} if so.
strawberry/subscriptions/protocols/graphql_ws/handlers.py
strawberry/subscriptions/protocols/graphql_transport_ws/handlers.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
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 @Object905 - 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: 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.

@botberry
Copy link
Member

Hi, thanks for contributing to Strawberry 🍓!

We noticed that this PR is missing a RELEASE.md file. We use that to automatically do releases here on GitHub and, most importantly, to PyPI!

So as soon as this PR is merged, a release will be made 🚀.

Here's an example of RELEASE.md:

Release type: patch

Description of the changes, ideally with some examples, if adding a new feature.

Release type can be one of patch, minor or major. We use semver, so make sure to pick the appropriate type. If in doubt feel free to ask :)

Here's the tweet text:

🆕 Release (next) is out! Thanks to Zverev Konstantin for the PR 👏

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

@@ -97,7 +97,9 @@ async def handle_message(

async def handle_connection_init(self, message: ConnectionInitMessage) -> None:
payload = message.get("payload")
if payload is not None and not isinstance(payload, dict):
if payload is None:
Copy link
Author

Choose a reason for hiding this comment

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

For graphql_ws before this PR connection_params could be null, if payload is null.
So this may be a slightly breaking change.

@@ -167,7 +167,8 @@ async def handle_connection_init(self, message: ConnectionInitMessage) -> None:
self.connection_init_timeout_task.cancel()

payload = message.get("payload", {})

if payload is None:
Copy link
Author

Choose a reason for hiding this comment

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

Not breaking for graphql_transport_ws, because it was required to be dict.

It's possible to make other way around and allow null in graphql_transport_ws, like in graphql_ws.
But since it's "recommended" protocol I decided to change graphql_ws and not the other way around.

@erikwrede erikwrede requested a review from DoctorJohn December 30, 2024 17:03
@DoctorJohn
Copy link
Member

Hi @Object905, I noticed this recently myself and planned to do two things:

  1. Update these checks
  2. No longer default to empty dictionaries at all, so that we can differentiate between
    • no payload
    • a null payload
    • an empty object as payload

Your PR essentially addresses (1.), that's great, but it might be worth it to do both things in the same PR so that we only have one breaking release. What do you think? You could also just do (1.) and make sure both handlers accept the payloads defined by their protocols (I recommend checking the types.py files next to the handlers).

Either way, we would need tests for these changes.

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.

3 participants