Skip to content

Commit

Permalink
Temporarily skip on_execute, see strawberry-graphql#3613
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick91 committed Sep 2, 2024
1 parent b65322e commit 87fd45d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 43 deletions.
19 changes: 9 additions & 10 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Release type: minor
Support for schema-extensions in subscriptions.

i.e:

```python
import asyncio
from typing import AsyncIterator
Expand Down Expand Up @@ -39,14 +40,6 @@ class MyExtension(SchemaExtension):
# The subscription has ended
print("Subscription ended")

async def on_execute(self):
# The subscription is trying to yield a new result
print(f"before yield {self.count}")
yield
# the subscription has yielded a new result
print(f"after yield {self.count}")
self.count += 1

# Other hooks are the same as in normal execution.
async def resolve(self, _next, root, info, *args, **kwargs):
res = _next(root, info, *args, **kwargs)
Expand All @@ -72,6 +65,7 @@ asyncio.run(main())
```

Should output this

```console
Subscription started
before yield 0
Expand All @@ -89,9 +83,11 @@ before yield 4
after yield 4
Subscription ended
```

### Breaking changes
This release also updates the signature of `Schema.subscribe`.
From:

This release also updates the signature of `Schema.subscribe`. From:

```py
async def subscribe(
self,
Expand All @@ -102,7 +98,9 @@ async def subscribe(
operation_name: Optional[str] = None,
) -> Union[AsyncIterator[GraphQLExecutionResult], GraphQLExecutionResult]:
```

To:

```py
async def subscribe(
self,
Expand All @@ -113,4 +111,5 @@ async def subscribe(
operation_name: Optional[str] = None,
) -> Union[AsyncGenerator[ExecutionResult, None], PreExecutionError]:
```

Due to moving away from graphql-core result types to our internal types.
28 changes: 14 additions & 14 deletions strawberry/schema/subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,20 @@ async def _subscribe(
while running:
# reset extensions results for each iteration
execution_context.extensions_results = {}
async with extensions_runner.executing():
try:
origin_result: Union[
ExecutionResult, OriginalExecutionResult
] = await aiterator.__anext__()

except StopAsyncIteration:
break
except Exception as exc:
# graphql-core doesn't handle exceptions raised in the async generator.
origin_result = ExecutionResult(
data=None, errors=[_coerce_error(exc)]
)
running = False

try:
origin_result: Union[
ExecutionResult, OriginalExecutionResult
] = await aiterator.__anext__()

except StopAsyncIteration:
break
except Exception as exc:
# graphql-core doesn't handle exceptions raised in the async generator.
origin_result = ExecutionResult(
data=None, errors=[_coerce_error(exc)]
)
running = False
# we could have yielded in the except block above.
# but this way we make sure `get_result` hook is called deterministically after
# `on_execute` hook is done.
Expand Down
24 changes: 5 additions & 19 deletions tests/schema/extensions/schema_extensions/test_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,19 @@ async def test_subscription_success_many_fields(
)
subscription_per_yield_hooks_exp = []
for _ in range(5): # number of yields in the subscription
subscription_per_yield_hooks_exp.extend(
["on_execute Entered", "resolve", "on_execute Exited", "get_results"]
)
subscription_per_yield_hooks_exp.extend(["resolve", "get_results"])

async_extension.expected = [
"on_operation Entered",
"on_parse Entered",
"on_parse Exited",
"on_validate Entered",
"on_validate Exited",
# first one would not yield anything if there are no errors.
# so it doesn't call the "resolve" / "get_results" hooks
"on_execute Entered",
"on_execute Exited",
*subscription_per_yield_hooks_exp,
# last one doesn't call the "resolve" / "get_results" hooks because
# the subscription is done
"on_execute Entered",
"on_execute Exited",
"on_operation Exited",
]
async for res in assert_agen(
Expand Down Expand Up @@ -125,12 +120,8 @@ async def count(self) -> AsyncGenerator[int, None]:
"on_validate Exited",
"on_execute Entered",
"on_execute Exited",
"on_execute Entered",
"resolve",
"on_execute Exited",
"get_results",
"on_execute Entered",
"on_execute Exited",
"get_results",
"on_operation Exited",
]
Expand All @@ -143,22 +134,17 @@ async def test_extensions_results_are_cleared_between_subscription_yields(
class MyExtension(SchemaExtension):
execution_number = 0

def on_execute(self):
yield
self.execution_number += 1

def get_results(self):
self.execution_number += 1
return {str(self.execution_number): self.execution_number}

schema = strawberry.Schema(
query=default_query_types_and_query.query_type,
subscription=default_query_types_and_query.subscription_type,
extensions=[MyExtension],
)
# the first execution is done before the first yield
# and because `get_results` is called after `on_execute`
# we expect the first extension result to be 2
res_num = 2

res_num = 1

async for res in assert_agen(
await schema.subscribe(default_query_types_and_query.subscription)
Expand Down

0 comments on commit 87fd45d

Please sign in to comment.