strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations šŸ“
https://strawberry.rocks
MIT License
3.87k stars 511 forks source link

Sanic - subscriptions not supported #2927

Open ndortega opened 1 year ago

ndortega commented 1 year ago

The example for subscriptions on the website is not working. Trying to trigger subscriptions from either postman or the built-in graphiql web app results in the following error: Cannot return null for non-nullable field Subscription.count.

In fact, I'm unable to get any subscriptions working in my web app. I've created a fresh new project and pulled down a fresh copy of strawberry-graphql and sanic.

System Information

Below is the sample application I'm encountering this issue.

import asyncio
import strawberry
from typing import AsyncGenerator
from sanic import Sanic
from strawberry.sanic.views import GraphQLView

@strawberry.type
class Query:
    @strawberry.field
    def hello(self) -> str:
        return "world"

@strawberry.type
class Subscription:
    @strawberry.subscription
    async def count(self, target: int = 100) -> AsyncGenerator[int, None]:
        for i in range(target):
            yield i
            await asyncio.sleep(0.5)

schema = strawberry.Schema(query=Query, subscription=Subscription)

app = Sanic(__name__)
app.add_route(GraphQLView.as_view(schema=schema, graphiql=True), "/graphql")

# Launch Application
if __name__ == '__main__':

    # dev mode
    app.run(dev=True)

requirement.txt

aiofiles==23.1.0
graphql-core==3.2.3
html5tagger==1.3.0
httptools==0.5.0
multidict==6.0.4
python-dateutil==2.8.2
sanic==23.3.0
sanic-routing==22.8.0
six==1.16.0
strawberry-graphql==0.193.1
tracerite==1.1.0
typing_extensions==4.7.1
websockets==11.0.3

Submitting the following query:

subscription {
    count(target: 10)
}

Results in this error:

Cannot return null for non-nullable field Subscription.count.

image

Upvote & Fund

Fund with Polar

patrick91 commented 1 year ago

@ndortega I think this is because we don't support websockets via sanic just yet

ndortega commented 1 year ago

@patrick91

Ah, well that would make sense. In that case, would you recommend closing this and opening a new one or repurposing this current issue to cover support for sanic.

patrick91 commented 1 year ago

@ndortega I've updated the title! šŸ˜Š