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

Refactor subscription implementation #1002

Conversation

DoctorJohn
Copy link
Member

Description

This PR extends and supersedes #897. The main focus of this PR was to refactor the ASGI subscription implementation and increase its test coverage by porting the AIOHTTP implementations tests. Now both subscription implementations are very similar which makes them easier to maintain and potentially abstract in the future. As a side effect the checks added in #897 have been tested.

Types of Changes

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

Issues Fixed or Closed by This PR

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

@botberry
Copy link
Member

botberry commented Jun 7, 2021

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release fixes the ASGI subscription implementation by handling disconnecting clients properly.

Additionally, the ASGI implementation has been internally refactored to match the AIOHTTP implementation.

@DoctorJohn
Copy link
Member Author

DoctorJohn commented Jun 7, 2021

Uhm, I'll take a look at the failing test tomorrow.

@codecov
Copy link

codecov bot commented Jun 7, 2021

Codecov Report

Merging #1002 (0ce4de5) into main (10af5aa) will increase coverage by 0.22%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #1002      +/-   ##
==========================================
+ Coverage   97.53%   97.75%   +0.22%     
==========================================
  Files          77       78       +1     
  Lines        2755     2805      +50     
  Branches      385      385              
==========================================
+ Hits         2687     2742      +55     
+ Misses         42       41       -1     
+ Partials       26       22       -4     

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.

I've added a couple of comments, will do another review later :)

By the way, @wuyuanyi135 you also worked on this, so if you want a sticker feel free to request one here: https://forms.gle/dmnfQUPoY5gZbVT67

RELEASE.md Outdated Show resolved Hide resolved
strawberry/aiohttp/views.py Outdated Show resolved Hide resolved
strawberry/aiohttp/views.py Outdated Show resolved Hide resolved
strawberry/aiohttp/views.py Show resolved Hide resolved
strawberry/aiohttp/views.py Show resolved Hide resolved
) -> None:
operation_id = message["id"]
payload: dict = message["payload"]
payload: StartPayload = message["payload"] # type: ignore
Copy link
Member Author

Choose a reason for hiding this comment

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

@patrick91 mypy didn't know which type from the payload Union to expect here so I added payload: StartPayload. Then mypy complained again:

error: Incompatible types in assignment (expression has type "Union[Dict[str, Any], StartPayload, DataPayload, ErrorPayload]", variable has type "StartPayload")

To deal with this I added # type: ignore. Do you know an alternative solution?

Copy link
Member

Choose a reason for hiding this comment

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

yes! we can use type guards in future, but for now I'd use cast(StartPayload, message["payload"]) :)

Copy link
Member

Choose a reason for hiding this comment

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

we can update to use cast or merge now and update to use typeguards in future, I'll let you decide :D

Copy link
Member Author

@DoctorJohn DoctorJohn Jun 9, 2021

Choose a reason for hiding this comment

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

I just tried using TypeGuards (interesting concept), unfortunately our mypy version doesn't play nice with them: error: Variable "typing_extensions.TypeGuard" is not valid as a type. Temporarely updating mypy solved this issue, but mypy then complaints about various other things (like library stubs not being installed for flask, dateutil and click). I'll probably use TypeGuards and somehow satisfy mypy (probably using type ignore). When we update mypy the type ignore can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Forget the last part, I went with cast for now. Using TypeGuards became a little tricky since a functions input is validated to have a certain type but I need the whole message to confirm its payload has a certain type. I'll look at typeguards in more depth in a separate PR.

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.

2021-06-09 11 07 12

@patrick91 patrick91 merged commit 894bce9 into strawberry-graphql:main Jun 9, 2021
@DoctorJohn DoctorJohn deleted the refactor-asgi-subscription-support branch November 20, 2024 16:03
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.

subscription AsyncGenerator raises error during client disconnected finalization process
4 participants