Skip to content

Commit

Permalink
schema.subscribe returns a Union[ExecutionResult, AsyncGen]
Browse files Browse the repository at this point in the history
Update graphql_ws handler

fix tests now that field verification is done by Strawberry

Return "extensions" as part of ExecutionResult if present.

Add some error/validation test cases

Add release.md

Fix tests, remove extra "extensions" payload we don't want to inspect

Add extensive tests for extension hook execution while running subscriptions

update docs for `resolve` extension hook
  • Loading branch information
kristjanvalur committed Jun 28, 2023
1 parent d6085a1 commit 7bbee00
Show file tree
Hide file tree
Showing 16 changed files with 634 additions and 193 deletions.
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

0 comments on commit 7bbee00

Please sign in to comment.