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

Support schema extensions for subsctriptions - Alternative #2825

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: minor

Subscriptions now support Schema Extensions.
17 changes: 15 additions & 2 deletions docs/guides/custom-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ resolvers.
If you need to wrap only certain field resolvers with additional logic, please
check out [field extensions](field-extensions.md).

Note that `resolve` can also be implemented asynchronously.

```python
from strawberry.types import Info
from strawberry.extensions import SchemaExtension
Expand All @@ -48,6 +46,21 @@ class MyExtension(SchemaExtension):
return _next(root, info, *args, **kwargs)
```

Note that `resolve` can also be implemented asynchronously, in which
case the result from `_next` must be optionally awaited:

```python
from inspect import isawaitable
from strawberry.types import Info
from strawberry.extensions import SchemaExtension


class MyExtension(SchemaExtension):
async def resolve(self, _next, root, info: Info, *args, **kwargs):
result = _next(root, info, *args, **kwargs)
return await result if isawaitable(result) else result
```

### Get results

`get_results` allows to return a dictionary of data or alternatively an
Expand Down
14 changes: 12 additions & 2 deletions strawberry/schema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

from abc import abstractmethod
from functools import lru_cache
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Type, Union
from typing import (
TYPE_CHECKING,
Any,
AsyncGenerator,
Dict,
Iterable,
List,
Optional,
Type,
Union,
)
from typing_extensions import Protocol

from strawberry.utils.logging import StrawberryLogger
Expand Down Expand Up @@ -62,7 +72,7 @@ async def subscribe(
context_value: Optional[Any] = None,
root_value: Optional[Any] = None,
operation_name: Optional[str] = None,
) -> Any:
) -> Union[ExecutionResult, AsyncGenerator[ExecutionResult, None]]:
raise NotImplementedError

@abstractmethod
Expand Down
Loading
Loading