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

WIP defer support #3753

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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

@defer 👀
2,719 changes: 1,343 additions & 1,376 deletions poetry.lock

Large diffs are not rendered by default.

92 changes: 49 additions & 43 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "strawberry-graphql"
packages = [ { include = "strawberry" } ]
packages = [{ include = "strawberry" }]
version = "0.258.0"
description = "A library for creating GraphQL APIs"
authors = ["Patrick Arminio <[email protected]>"]
Expand Down Expand Up @@ -35,55 +35,55 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry.dependencies]
python = "^3.9"
graphql-core = ">=3.2.0,<3.4.0"
graphql-core = ">=3.2.0"
typing-extensions = ">=4.5.0"
python-dateutil = "^2.7.0"
starlette = {version = ">=0.18.0", optional = true}
typer = {version = ">=0.7.0", optional = true}
pygments = {version = "^2.3", optional = true}
uvicorn = {version = ">=0.11.6", optional = true}
Django = {version = ">=3.2", optional = true}
asgiref = {version = "^3.2", optional = true}
flask = {version = ">=1.1", optional = true}
quart = {version = ">=0.19.3", optional = true}
opentelemetry-api = {version = "<2", optional = true}
opentelemetry-sdk = {version = "<2", optional = true}
chalice = {version = "^1.22", optional = true}
pydantic = {version = ">1.6.1", optional = true}
python-multipart = {version = ">=0.0.7", optional = true}
sanic = {version = ">=20.12.2", optional = true}
aiohttp = {version = "^3.7.4.post0", optional = true}
fastapi = {version = ">=0.65.2", optional = true}
litestar = {version = ">=2", optional = true}
channels = {version = ">=3.0.5", optional = true}
libcst = {version = ">=0.4.7", optional = true}
rich = {version = ">=12.0.0", optional = true}
pyinstrument = {version = ">=4.0.0", optional = true}
starlette = { version = ">=0.18.0", optional = true }
typer = { version = ">=0.7.0", optional = true }
pygments = { version = "^2.3", optional = true }
uvicorn = { version = ">=0.11.6", optional = true }
Django = { version = ">=3.2", optional = true }
asgiref = { version = "^3.2", optional = true }
flask = { version = ">=1.1", optional = true }
quart = { version = ">=0.19.3", optional = true }
opentelemetry-api = { version = "<2", optional = true }
opentelemetry-sdk = { version = "<2", optional = true }
chalice = { version = "^1.22", optional = true }
pydantic = { version = ">1.6.1", optional = true }
python-multipart = { version = ">=0.0.7", optional = true }
sanic = { version = ">=20.12.2", optional = true }
aiohttp = { version = "^3.7.4.post0", optional = true }
fastapi = { version = ">=0.65.2", optional = true }
litestar = { version = ">=2", optional = true }
channels = { version = ">=3.0.5", optional = true }
libcst = { version = ">=0.4.7", optional = true }
rich = { version = ">=12.0.0", optional = true }
pyinstrument = { version = ">=4.0.0", optional = true }

[tool.poetry.group.dev.dependencies]
asgiref = "^3.2"
email-validator = {version = ">=1.1.3,<3.0.0", optional = false}
email-validator = { version = ">=1.1.3,<3.0.0", optional = false }
freezegun = "^1.2.1"
libcst = {version = ">=1.0.0", optional = false}
libcst = { version = ">=1.0.0", optional = false }
MarkupSafe = "2.1.3"
nox = "^2024.4.15"
nox-poetry = "^1.0.3"
opentelemetry-api = "<2"
opentelemetry-sdk = "<2"
pygments = "^2.3"
pyinstrument = {version = ">=4.0.0", optional = false}
pyinstrument = { version = ">=4.0.0", optional = false }
pytest = "^7.2"
pytest-asyncio = ">=0.20.3"
pytest-codspeed = { version = ">=3.0.0", python = ">=3.9" }
pytest-cov = "^4.0.0"
pytest-emoji = "^0.2.0"
pytest-mock = "^3.10"
pytest-snapshot = "^0.9.0"
pytest-xdist = {extras = ["psutil"], version = "^3.1.0"}
pytest-xdist = { extras = ["psutil"], version = "^3.1.0" }
python-multipart = ">=0.0.7"
rich = {version = ">=12.5.1", optional = false}
rich = { version = ">=12.5.1", optional = false }
sanic-testing = ">=22.9,<24.0"
typer = {version = ">=0.7.0", optional = false}
typer = { version = ">=0.7.0", optional = false }
types-aiofiles = ">=22.1"
types-certifi = "^2021.10.8"
types-chardet = "^5.0.4"
Expand All @@ -96,42 +96,51 @@ types-protobuf = "^4.23.0.1"
poetry-plugin-export = "^1.6.0"
# another bug in poetry
urllib3 = "<2"
graphlib_backport = {version = "*", python = "<3.9", optional = false}
graphlib_backport = { version = "*", python = "<3.9", optional = false }
inline-snapshot = "^0.10.1"
types-deprecated = "^1.2.15.20241117"
types-six = "^1.17.0.20241205"
types-pyyaml = "^6.0.12.20240917"
mypy = "^1.13.0"
graphql-core = "3.3.0a7"

[tool.poetry.group.integrations]
optional = true

[tool.poetry.group.integrations.dependencies]
aiohttp = "^3.7.4.post0"
chalice = {version = "^1.22"}
chalice = { version = "^1.22" }
channels = ">=3.0.5,<5.0.0"
Django = ">=3.2"
fastapi = {version = ">=0.65.0", optional = false}
fastapi = { version = ">=0.65.0", optional = false }
flask = ">=1.1"
quart = ">=0.19.3"
pydantic = {version = ">=2.0", optional = false}
pydantic = { version = ">=2.0", optional = false }
pytest-aiohttp = "^1.0.3"
pytest-django = {version = "^4.5"}
pytest-django = { version = "^4.5" }
sanic = ">=20.12.2"
starlette = ">=0.13.6"
litestar = {version = ">=2", optional = false}
litestar = { version = ">=2", optional = false }
uvicorn = ">=0.11.6"
daphne = "^4.0.0"

[tool.poetry.extras]
aiohttp = ["aiohttp"]
asgi = ["starlette", "python-multipart"]
debug = ["rich", "libcst"]
debug-server = ["starlette", "uvicorn", "python-multipart", "typer", "pygments", "rich", "libcst"]
debug-server = [
"starlette",
"uvicorn",
"python-multipart",
"typer",
"pygments",
"rich",
"libcst",
]
django = ["Django", "pytest-django", "asgiref"]
channels = ["channels", "asgiref"]
flask = ["flask"]
quart=["quart"]
quart = ["quart"]
opentelemetry = ["opentelemetry-api", "opentelemetry-sdk"]
pydantic = ["pydantic"]
sanic = ["sanic"]
Expand Down Expand Up @@ -190,7 +199,7 @@ append-github-contributor = true

[tool.pyright]
# include = ["strawberry"]
exclude = ["**/__pycache__",]
exclude = ["**/__pycache__"]
reportMissingImports = true
reportMissingTypeStubs = false
pythonVersion = "3.9"
Expand Down Expand Up @@ -220,7 +229,7 @@ exclude = [
"dist",
"node_modules",
"venv",
"tests/*/snapshots"
"tests/*/snapshots",
]
src = ["strawberry", "tests"]

Expand Down Expand Up @@ -376,10 +385,7 @@ known-third-party = ["django", "graphql"]
extra-standard-library = ["typing_extensions"]

[tool.ruff.format]
exclude =[
'tests/codegen/snapshots/',
'tests/cli/snapshots/'
]
exclude = ['tests/codegen/snapshots/', 'tests/cli/snapshots/']

[tool.ruff.lint.pydocstyle]
convention = "google"
22 changes: 19 additions & 3 deletions strawberry/http/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Optional
from typing_extensions import Literal, TypedDict
from typing import TYPE_CHECKING, Any, Optional, Union
from typing_extensions import Literal, NotRequired, TypedDict

Check warning on line 5 in strawberry/http/__init__.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/__init__.py#L4-L5

Added lines #L4 - L5 were not covered by tests

from strawberry.types import InitialIncrementalExecutionResult

Check warning on line 7 in strawberry/http/__init__.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/__init__.py#L7

Added line #L7 was not covered by tests

if TYPE_CHECKING:
from strawberry.types import ExecutionResult
Expand All @@ -14,14 +16,20 @@
extensions: Optional[dict[str, object]]


def process_result(result: ExecutionResult) -> GraphQLHTTPResponse:
def process_result(

Check warning on line 19 in strawberry/http/__init__.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/__init__.py#L19

Added line #L19 was not covered by tests
result: Union[ExecutionResult, InitialIncrementalExecutionResult],
) -> GraphQLHTTPResponse:
data: GraphQLHTTPResponse = {"data": result.data}

if result.errors:
data["errors"] = [err.formatted for err in result.errors]
if result.extensions:
data["extensions"] = result.extensions

if isinstance(result, InitialIncrementalExecutionResult):
data["hasNext"] = result.has_next
data["pending"] = result.pending

Check warning on line 31 in strawberry/http/__init__.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/__init__.py#L30-L31

Added lines #L30 - L31 were not covered by tests

return data


Expand All @@ -35,8 +43,16 @@
protocol: Literal["http", "multipart-subscription"] = "http"


class IncrementalGraphQLHTTPResponse(TypedDict):
incremental: list[GraphQLHTTPResponse]
hasNext: bool
extensions: NotRequired[dict[str, Any]]
completed: list[GraphQLHTTPResponse]

Check warning on line 50 in strawberry/http/__init__.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/__init__.py#L46-L50

Added lines #L46 - L50 were not covered by tests


__all__ = [
"GraphQLHTTPResponse",
"GraphQLRequestData",
"IncrementalGraphQLHTTPResponse",
"process_result",
]
103 changes: 98 additions & 5 deletions strawberry/http/async_base_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from strawberry.http import (
GraphQLHTTPResponse,
GraphQLRequestData,
IncrementalGraphQLHTTPResponse,
process_result,
)
from strawberry.http.ides import GraphQL_IDE
Expand Down Expand Up @@ -50,6 +51,28 @@
WebSocketResponse,
)

try:
from graphql.execution.execute import ExperimentalIncrementalExecutionResults
from graphql.execution.incremental_publisher import (

Check warning on line 56 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L54-L56

Added lines #L54 - L56 were not covered by tests
IncrementalDeferResult,
IncrementalResult,
IncrementalStreamResult,
InitialIncrementalExecutionResult,
PendingResult,
SubsequentIncrementalExecutionResult,
)

except ImportError as e:
from types import NoneType

Check warning on line 66 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L65-L66

Added lines #L65 - L66 were not covered by tests

print(e)

Check warning on line 68 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L68

Added line #L68 was not covered by tests

InitialIncrementalExecutionResult = NoneType
IncrementalResult = NoneType
IncrementalStreamResult = NoneType
SubsequentIncrementalExecutionResult = NoneType
PendingResult = NoneType

Check warning on line 74 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L70-L74

Added lines #L70 - L74 were not covered by tests


class AsyncHTTPRequestAdapter(abc.ABC):
@property
Expand Down Expand Up @@ -337,6 +360,29 @@
except MissingQueryError as e:
raise HTTPException(400, "No GraphQL query found in the request") from e

if isinstance(result, ExperimentalIncrementalExecutionResults):

async def stream():
yield "---"
response = await self.process_result(request, result.initial_result)
yield self.encode_multipart_data(response, "-")

Check warning on line 368 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L365-L368

Added lines #L365 - L368 were not covered by tests

async for value in result.subsequent_results:
response = await self.process_subsequent_result(request, value)
yield self.encode_multipart_data(response, "-")

Check warning on line 372 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L371-L372

Added lines #L371 - L372 were not covered by tests

yield "--\r\n"

Check warning on line 374 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L374

Added line #L374 was not covered by tests

return await self.create_streaming_response(

Check warning on line 376 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L376

Added line #L376 was not covered by tests
request,
stream,
sub_response,
headers={
"Transfer-Encoding": "chunked",
"Content-Type": 'multipart/mixed; boundary="-"',
},
)

if isinstance(result, SubscriptionExecutionResult):
stream = self._get_stream(request, result)

Expand All @@ -360,12 +406,15 @@
)

def encode_multipart_data(self, data: Any, separator: str) -> str:
encoded_data = self.encode_json(data)

Check warning on line 409 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L409

Added line #L409 was not covered by tests

return "".join(
[
f"\r\n--{separator}\r\n",
"Content-Type: application/json\r\n\r\n",
self.encode_json(data),
"\n",
"\r\n",
"Content-Type: application/json; charset=utf-8\r\n",
"\r\n",
encoded_data,
f"\r\n--{separator}",
]
)

Expand Down Expand Up @@ -475,9 +524,53 @@
protocol=protocol,
)

async def process_subsequent_result(

Check warning on line 527 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L527

Added line #L527 was not covered by tests
self,
request: Request,
result: "SubsequentIncrementalExecutionResult",
) -> IncrementalGraphQLHTTPResponse:
data = {

Check warning on line 532 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L532

Added line #L532 was not covered by tests
"incremental": [
await self.process_result(request, value)
for value in result.incremental
],
"completed": [
completed_result.formatted for completed_result in result.completed
],
"hasNext": result.has_next,
"extensions": result.extensions,
}

return data

Check warning on line 544 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L544

Added line #L544 was not covered by tests

async def process_result(
self, request: Request, result: ExecutionResult
self,
request: Request,
result: Union[ExecutionResult, InitialIncrementalExecutionResult],
) -> GraphQLHTTPResponse:
if isinstance(result, InitialIncrementalExecutionResult):
# TODO: fix this mess
from strawberry.types import (

Check warning on line 553 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L553

Added line #L553 was not covered by tests
InitialIncrementalExecutionResult as InitialIncrementalExecutionResultType,
)

# TODO: do this where we create ExecutionResult
# or maybe remove our wrappers and just GraphQL core's types
result = InitialIncrementalExecutionResultType(

Check warning on line 559 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L559

Added line #L559 was not covered by tests
data=result.data,
pending=[pending_result.formatted for pending_result in result.pending],
has_next=result.has_next,
extensions=result.extensions,
errors=result.errors,
)

result = await self.schema._handle_execution_result(

Check warning on line 567 in strawberry/http/async_base_view.py

View check run for this annotation

Codecov / codecov/patch

strawberry/http/async_base_view.py#L567

Added line #L567 was not covered by tests
context=self.schema.execution_context,
result=result,
extensions_runner=self.schema.extensions_runner,
process_errors=self.schema.process_errors,
)

return process_result(result)

async def on_ws_connect(
Expand Down
Loading
Loading