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

Add support for @stream and @defer #3402

Open
patrick91 opened this issue Mar 2, 2024 · 8 comments
Open

Add support for @stream and @defer #3402

patrick91 opened this issue Mar 2, 2024 · 8 comments

Comments

@patrick91
Copy link
Member

patrick91 commented Mar 2, 2024

Yesterday I was playing around with implementing support for @stream (and indirectly, for @defer)

Firstly, the work will be based on #3076, since it adds quite all the fundamentals we need for delivering incremental updates (the Multipart Subscription protocol is very similar to what we need for @stream and @defer). We'd probably need some refactoring, but the base is there :)

@stream, strawberry.Streamable, AsyncGenerator and lists

In GraphQL, @stream can only be used on fields that return a list of items, meanwhile in Python we need to use an AsyncGenerator to enable streaming, this is how a simple resolver would look like:

import strawberry
from typing import AsyncGenerator 

def count() -> AsyncGenerator[int]:
    yield 1
    yield 2

We are already using AsyncGenerator for subscription (and in subscription you don't need to use lists for values), so I thought of introducing a new type (which is just an alias to AsyncGenerator): strawberry.Streamable

The usage would look like this:

import strawberry

@strawberry.type
class Query:
    @strawberry.field
    def count() -> strawberry.Streamable[int]:
        yield 1
        yield 2

when using strawberry.Streamable[X], Strawberry knows to convert this to a list of X, so the GraphQL schema will be correct.

My only issue with this is that we are hiding the fact that count is a list in GraphQL, but I think it is worth the tradeoff, especially because we can't really do much else while keeping the typing working fine.

And having a dedicated type could allow us to raise proper errors when using @stream with lists that are not actually streamable (I don't think it would make sense in that case, no?)

What do you all think?

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@matclayton
Copy link

@patrick91 i wondered if you have a list of the requirements of what would be needed to add defer and stream support into strawberry. Or any idea how complex this is to implement.

It’s probably our next big performance win. So mulling over / investigating how hard it would be to make this happen, either through committing resources or funding for it.

@patrick91
Copy link
Member Author

@matclayton we have a few bits in places already, but we need support from GraphQL-core first, unless you're ok with using an alpha 😊

@matclayton
Copy link

I think we might be running it already, our coverage here is pretty good, so not too concerned about that! Ive been following the alpha closely for a while and kinda assuming it would have shipped already.

@patrick91
Copy link
Member Author

@matclayton I think they are waiting for GraphQL.js to ship a stable release of defer 😊

@matclayton
Copy link

Reasonable, we're on Relay 18.2.0 which has lots of nice defer based features :)

@patrick91
Copy link
Member Author

@matclayton I see, I think an on-going sponsorship would help us with getting this shipped 😊

I'm a bit worried about things changing in future version of GraphQL-core and GraphQL.js

@matclayton
Copy link

Lets grab a chat and see what that would entail, I'd love to find a way to make that work, just not sure how. do you have my email?

@patrick91
Copy link
Member Author

@matclayton yes, I'll send you a message :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants