Skip to content

Commit 57c4ac1

Browse files
committed
Some progress
1 parent 8584404 commit 57c4ac1

File tree

6 files changed

+44
-15
lines changed

6 files changed

+44
-15
lines changed

strawberry/http/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass
4-
from typing import TYPE_CHECKING, Any, Optional
4+
from typing import TYPE_CHECKING, Any, Optional, Union
55
from typing_extensions import Literal, NotRequired, TypedDict
66

7+
from strawberry.types import InitialIncrementalExecutionResult
8+
79
if TYPE_CHECKING:
810
from strawberry.types import ExecutionResult
911

@@ -14,14 +16,20 @@ class GraphQLHTTPResponse(TypedDict, total=False):
1416
extensions: Optional[dict[str, object]]
1517

1618

17-
def process_result(result: ExecutionResult) -> GraphQLHTTPResponse:
19+
def process_result(
20+
result: Union[ExecutionResult, InitialIncrementalExecutionResult],
21+
) -> GraphQLHTTPResponse:
1822
data: GraphQLHTTPResponse = {"data": result.data}
1923

2024
if result.errors:
2125
data["errors"] = [err.formatted for err in result.errors]
2226
if result.extensions:
2327
data["extensions"] = result.extensions
2428

29+
if isinstance(result, InitialIncrementalExecutionResult):
30+
data["hasNext"] = result.has_next
31+
data["pending"] = result.pending
32+
2533
return data
2634

2735

@@ -39,6 +47,7 @@ class IncrementalGraphQLHTTPResponse(TypedDict):
3947
incremental: list[GraphQLHTTPResponse]
4048
hasNext: bool
4149
extensions: NotRequired[dict[str, Any]]
50+
completed: list[GraphQLHTTPResponse]
4251

4352

4453
__all__ = [

strawberry/http/async_base_view.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -578,14 +578,20 @@ async def process_result(
578578
result: Union[ExecutionResult, InitialIncrementalExecutionResult],
579579
) -> GraphQLHTTPResponse:
580580
if isinstance(result, InitialIncrementalExecutionResult):
581-
return {
582-
"data": result.data,
583-
"pending": [
584-
pending_result.formatted for pending_result in result.pending
585-
],
586-
"hasNext": result.has_next,
587-
"extensions": result.extensions,
588-
}
581+
# TODO: fix this mess
582+
from strawberry.types import (
583+
InitialIncrementalExecutionResult as InitialIncrementalExecutionResultType,
584+
)
585+
586+
# TODO: do this where we create ExecutionResult
587+
# or maybe remove our wrappers and just GraphQL core's types
588+
result = InitialIncrementalExecutionResultType(
589+
data=result.data,
590+
pending=[pending_result.formatted for pending_result in result.pending],
591+
has_next=result.has_next,
592+
extensions=result.extensions,
593+
errors=result.errors,
594+
)
589595

590596
result = await self.schema._handle_execution_result(
591597
context=self.schema.execution_context,

strawberry/schema/schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def _create_execution_context(
300300
provided_operation_name=operation_name,
301301
)
302302

303-
# TODO: is this the right place to do this?
303+
# TODO: is this the right place to do this?
304304
async def _handle_execution_result(
305305
self,
306306
context: ExecutionContext,
@@ -319,6 +319,7 @@ async def _handle_execution_result(
319319
if isinstance(result, GraphQLExecutionResult):
320320
result = ExecutionResult(data=result.data, errors=result.errors)
321321

322+
# TODO: not correct when handling incremental results
322323
result.extensions = await extensions_runner.get_extensions_results(context)
323324

324325
context.result = result # type: ignore # mypy failed to deduce correct type.

strawberry/types/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
from .base import get_object_definition, has_object_definition
2-
from .execution import ExecutionContext, ExecutionResult, SubscriptionExecutionResult
2+
from .execution import (
3+
ExecutionContext,
4+
ExecutionResult,
5+
InitialIncrementalExecutionResult,
6+
SubscriptionExecutionResult,
7+
)
38
from .info import Info
49

510
__all__ = [
611
"ExecutionContext",
712
"ExecutionResult",
813
"Info",
9-
"Info",
14+
"InitialIncrementalExecutionResult",
1015
"SubscriptionExecutionResult",
1116
"get_object_definition",
1217
"has_object_definition",

strawberry/types/execution.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ class ExecutionResult:
9292
extensions: Optional[dict[str, Any]] = None
9393

9494

95+
@dataclasses.dataclass
96+
class InitialIncrementalExecutionResult:
97+
data: Optional[dict[str, Any]]
98+
errors: Optional[list[GraphQLError]]
99+
pending: list[Any]
100+
has_next: bool
101+
extensions: Optional[dict[str, Any]] = None
102+
103+
95104
@dataclasses.dataclass
96105
class PreExecutionError(ExecutionResult):
97106
"""Differentiate between a normal execution result and an immediate error.

tests/http/incremental/test_defer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ async def test_basic_defer(method: Literal["get", "post"], http_client: HttpClie
3030
"data": {"hero": {"id": "1"}},
3131
"hasNext": True,
3232
"pending": [{"path": ["hero"], "id": "0"}],
33-
# TODO: why is this None?
34-
"extensions": None,
33+
"extensions": {"example": "example"},
3534
}
3635

3736
subsequent = await stream.__anext__()

0 commit comments

Comments
 (0)