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

Add batching support #2698

Draft
wants to merge 20 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
6 changes: 4 additions & 2 deletions docs/integrations/aiohttp.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ methods:
- `async get_context(self, request: aiohttp.web.Request, response: aiohttp.web.StreamResponse) -> object`
- `async get_root_value(self, request: aiohttp.web.Request) -> object`
- `async process_result(self, request: aiohttp.web.Request, result: ExecutionResult) -> GraphQLHTTPResponse`
- `def encode_json(self, data: GraphQLHTTPResponse) -> str`
- `def encode_json(self, data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]) -> str`

## get_context

Expand Down Expand Up @@ -143,6 +143,8 @@ we use `json.dumps` but you can override this method to use a different encoder.

```python
class MyGraphQLView(GraphQLView):
def encode_json(self, data: GraphQLHTTPResponse) -> str:
def encode_json(
self, data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]
) -> str:
return json.dumps(data, indent=2)
```
6 changes: 4 additions & 2 deletions docs/integrations/asgi.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ We allow to extend the base `GraphQL` app, by overriding the following methods:
- `async get_context(self, request: Union[Request, WebSocket], response: Optional[Response] = None) -> Any`
- `async get_root_value(self, request: Request) -> Any`
- `async process_result(self, request: Request, result: ExecutionResult) -> GraphQLHTTPResponse`
- `def encode_json(self, response_data: GraphQLHTTPResponse) -> str`
- `def encode_json(self, response_data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]) -> str`

## get_context

Expand Down Expand Up @@ -169,6 +169,8 @@ we use `json.dumps` but you can override this method to use a different encoder.

```python
class MyGraphQLView(GraphQL):
def encode_json(self, data: GraphQLHTTPResponse) -> str:
def encode_json(
self, data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]
) -> str:
return json.dumps(data, indent=2)
```
12 changes: 7 additions & 5 deletions docs/integrations/chalice.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ The `GraphQLView` accepts two options at the moment:

We allow to extend the base `GraphQLView`, by overriding the following methods:

- `get_context(self, request: Request, response: TemporalResponse) -> Any`
- `get_root_value(self, request: Request) -> Any`
- `process_result(self, request: Request, result: ExecutionResult) -> GraphQLHTTPResponse`
- `encode_json(self, response_data: GraphQLHTTPResponse) -> str`
- `def get_context(self, request: Request, response: TemporalResponse) -> Any`
- `def get_root_value(self, request: Request) -> Any`
- `def process_result(self, request: Request, result: ExecutionResult) -> GraphQLHTTPResponse`
- `def encode_json(self, response_data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]) -> str`

## get_context

Expand Down Expand Up @@ -156,6 +156,8 @@ we use `json.dumps` but you can override this method to use a different encoder.

```python
class MyGraphQLView(GraphQLView):
def encode_json(self, data: GraphQLHTTPResponse) -> str:
def encode_json(
self, data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]
) -> str:
return json.dumps(data, indent=2)
```
4 changes: 3 additions & 1 deletion docs/integrations/creating-an-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ class MyView(
...

def create_response(
self, response_data: GraphQLHTTPResponse, sub_response: SubResponse
self,
response_data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]],
sub_response: SubResponse,
) -> Response:
...
```
Expand Down
6 changes: 4 additions & 2 deletions docs/integrations/django.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ methods:
- `async get_context(self, request: HttpRequest) -> Any`
- `async get_root_value(self, request: HttpRequest) -> Any`
- `async process_result(self, request: HttpRequest, result: ExecutionResult) -> GraphQLHTTPResponse`
- `def encode_json(self, data: GraphQLHTTPResponse) -> str`
- `def encode_json(self, data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]) -> str`

## get_context

Expand Down Expand Up @@ -268,7 +268,9 @@ we use `json.dumps` but you can override this method to use a different encoder.

```python
class MyGraphQLView(AsyncGraphQLView):
def encode_json(self, data: GraphQLHTTPResponse) -> str:
def encode_json(
self, data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]
) -> str:
return json.dumps(data, indent=2)
```

Expand Down
4 changes: 3 additions & 1 deletion docs/integrations/fastapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ we use `json.dumps` but you can override this method to use a different encoder.

```python
class MyGraphQLRouter(GraphQLRouter):
def encode_json(self, data: GraphQLHTTPResponse) -> str:
def encode_json(
self, data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]
) -> str:
return json.dumps(data, indent=2)
```
12 changes: 7 additions & 5 deletions docs/integrations/flask.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ The `GraphQLView` accepts two options at the moment:

We allow to extend the base `GraphQLView`, by overriding the following methods:

- `get_context(self, request: Request, response: Response) -> Any`
- `get_root_value(self, request: Request) -> Any`
- `process_result(self, result: ExecutionResult) -> GraphQLHTTPResponse`
- `encode_json(self, response_data: GraphQLHTTPResponse) -> str`
- `def get_context(self, request: Request, response: Response) -> Any`
- `def get_root_value(self, request: Request) -> Any`
- `def process_result(self, result: ExecutionResult) -> GraphQLHTTPResponse`
- `def encode_json(self, response_data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]) -> str`

<Note>

Expand Down Expand Up @@ -140,6 +140,8 @@ we use `json.dumps` but you can override this method to use a different encoder.

```python
class MyGraphQLView(GraphQLView):
def encode_json(self, data: GraphQLHTTPResponse) -> str:
def encode_json(
self, data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]
) -> str:
return json.dumps(data, indent=2)
```
6 changes: 4 additions & 2 deletions docs/integrations/sanic.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The `GraphQLView` accepts two options at the moment:
interface.
- `allow_queries_via_get`: optional, defaults to `True`, whether to enable
queries via `GET` requests
- `def encode_json(self, data: GraphQLHTTPResponse) -> str`
- `def encode_json(self, data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]) -> str`

## Extending the view

Expand Down Expand Up @@ -123,6 +123,8 @@ we use `json.dumps` but you can override this method to use a different encoder.

```python
class MyGraphQLView(GraphQLView):
def encode_json(self, data: GraphQLHTTPResponse) -> str:
def encode_json(
self, data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]]
) -> str:
return json.dumps(data, indent=2)
```
6 changes: 5 additions & 1 deletion strawberry/aiohttp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
Any,
Dict,
Iterable,
List,
Mapping,
Optional,
Union,
cast,
)

Expand Down Expand Up @@ -163,7 +165,9 @@ async def get_context(
return {"request": request, "response": response} # type: ignore

def create_response(
self, response_data: GraphQLHTTPResponse, sub_response: web.Response
self,
response_data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]],
sub_response: web.Response,
) -> web.Response:
sub_response.text = self.encode_json(response_data)
sub_response.content_type = "application/json"
Expand Down
5 changes: 4 additions & 1 deletion strawberry/asgi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import (
TYPE_CHECKING,
Any,
List,
Mapping,
Optional,
Sequence,
Expand Down Expand Up @@ -189,7 +190,9 @@ def render_graphiql(self, request: Union[Request, WebSocket]) -> Response:
return HTMLResponse(html)

def create_response(
self, response_data: GraphQLHTTPResponse, sub_response: Response
self,
response_data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]],
sub_response: Response,
) -> Response:
response = Response(
self.encode_json(response_data),
Expand Down
6 changes: 4 additions & 2 deletions strawberry/chalice/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Union, cast
from typing import TYPE_CHECKING, Any, Dict, List, Mapping, Optional, Union, cast

from chalice.app import Request, Response
from strawberry.http.exceptions import HTTPException
Expand Down Expand Up @@ -101,7 +101,9 @@ def get_context(self, request: Request, response: TemporalResponse) -> Context:
return {"request": request, "response": response} # type: ignore

def create_response(
self, response_data: GraphQLHTTPResponse, sub_response: TemporalResponse
self,
response_data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]],
sub_response: TemporalResponse,
) -> Response:
status_code = 200

Expand Down
6 changes: 4 additions & 2 deletions strawberry/channels/handlers/http_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import dataclasses
import json
from io import BytesIO
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Union
from typing import TYPE_CHECKING, Any, Dict, List, Mapping, Optional, Union
from urllib.parse import parse_qs

from django.conf import settings
Expand Down Expand Up @@ -162,7 +162,9 @@ def render_graphiql(self, request: ChannelsRequest) -> ChannelsResponse:
return ChannelsResponse(content=html.encode(), content_type="text/html")

def create_response(
self, response_data: GraphQLHTTPResponse, sub_response: TemporalResponse
self,
response_data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]],
sub_response: TemporalResponse,
) -> ChannelsResponse:
return ChannelsResponse(
content=json.dumps(response_data).encode(),
Expand Down
2 changes: 1 addition & 1 deletion strawberry/django/apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.apps import AppConfig # pragma: no cover


class StrawberryConfig(AppConfig): # pragma: no cover
class StrawberryAppConfig(AppConfig): # pragma: no cover
name = "strawberry"
5 changes: 4 additions & 1 deletion strawberry/django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
TYPE_CHECKING,
Any,
Callable,
List,
Mapping,
Optional,
Union,
Expand Down Expand Up @@ -156,7 +157,9 @@ def render_graphiql(self, request: HttpRequest) -> HttpResponse:
return response

def create_response(
self, response_data: GraphQLHTTPResponse, sub_response: HttpResponse
self,
response_data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]],
sub_response: HttpResponse,
) -> HttpResponse:
data = self.encode_json(response_data) # type: ignore

Expand Down
5 changes: 4 additions & 1 deletion strawberry/fastapi/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Any,
Awaitable,
Callable,
List,
Mapping,
Optional,
Sequence,
Expand Down Expand Up @@ -300,7 +301,9 @@ async def get_sub_response(self, request: Request) -> Response:
return self.temporal_response

def create_response(
self, response_data: GraphQLHTTPResponse, sub_response: Response
self,
response_data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]],
sub_response: Response,
) -> Response:
response = Response(
self.encode_json(response_data),
Expand Down
4 changes: 3 additions & 1 deletion strawberry/flask/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def render_graphiql(self, request: Request) -> Response:
return render_template_string(template) # type: ignore

def create_response(
self, response_data: GraphQLHTTPResponse, sub_response: Response
self,
response_data: Union[GraphQLHTTPResponse, List[GraphQLHTTPResponse]],
sub_response: Response,
) -> Response:
sub_response.set_data(self.encode_json(response_data)) # type: ignore

Expand Down
Loading
Loading