Skip to content

Commit

Permalink
Bring the view extensions docs on-par (#3711)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorJohn authored Nov 28, 2024
1 parent 1400a7e commit 2e4e3cc
Show file tree
Hide file tree
Showing 10 changed files with 391 additions and 76 deletions.
36 changes: 22 additions & 14 deletions docs/integrations/aiohttp.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ The `GraphQLView` accepts the following options at the moment:

## Extending the view

The base `GraphQLView` class can be extended by overriding the following
The base `GraphQLView` class can be extended by overriding any of the following
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`
- `async def get_context(self, request: Request, response: Union[Response, WebSocketResponse]) -> Context`
- `async def get_root_value(self, request: Request) -> Optional[RootValue]`
- `async def process_result(self, request: Request, result: ExecutionResult) -> GraphQLHTTPResponse`
- `def decode_json(self, data: Union[str, bytes]) -> object`
- `def encode_json(self, data: object) -> str`
- `async def render_graphql_ide(self, request: aiohttp.web.Request) -> aiohttp.web.Response`
- `async def render_graphql_ide(self, request: Request) -> Response`

### get_context

Expand All @@ -61,13 +62,16 @@ a dictionary with the request.

```python
import strawberry
from aiohttp import web
from typing import Union
from strawberry.types import Info
from strawberry.aiohttp.views import GraphQLView
from aiohttp.web import Request, Response, WebSocketResponse


class MyGraphQLView(GraphQLView):
async def get_context(self, request: web.Request, response: web.StreamResponse):
async def get_context(
self, request: Request, response: Union[Response, WebSocketResponse]
):
return {"request": request, "response": response, "example": 1}


Expand All @@ -94,12 +98,12 @@ Here's an example:

```python
import strawberry
from aiohttp import web
from aiohttp.web import Request
from strawberry.aiohttp.views import GraphQLView


class MyGraphQLView(GraphQLView):
async def get_root_value(self, request: web.Request):
async def get_root_value(self, request: Request):
return Query(name="Patrick")


Expand All @@ -121,15 +125,15 @@ It needs to return an object of `GraphQLHTTPResponse` and accepts the request
and execution result.

```python
from aiohttp import web
from aiohttp.web import Request
from strawberry.aiohttp.views import GraphQLView
from strawberry.http import GraphQLHTTPResponse
from strawberry.types import ExecutionResult


class MyGraphQLView(GraphQLView):
async def process_result(
self, request: web.Request, result: ExecutionResult
self, request: Request, result: ExecutionResult
) -> GraphQLHTTPResponse:
data: GraphQLHTTPResponse = {"data": result.data}

Expand Down Expand Up @@ -170,6 +174,10 @@ responses. By default we use `json.dumps` but you can override this method to
use a different encoder.

```python
import json
from strawberry.aiohttp.views import GraphQLView


class MyGraphQLView(GraphQLView):
def encode_json(self, data: object) -> str:
return json.dumps(data, indent=2)
Expand All @@ -181,13 +189,13 @@ In case you need more control over the rendering of the GraphQL IDE than the
`graphql_ide` option provides, you can override the `render_graphql_ide` method.

```python
from aiohttp import web
from aiohttp.web import Request, Response
from strawberry.aiohttp.views import GraphQLView


class MyGraphQLView(GraphQLView):
async def render_graphql_ide(self, request: web.Request) -> web.Response:
async def render_graphql_ide(self, request: Request) -> Response:
custom_html = """<html><body><h1>Custom GraphQL IDE</h1></body></html>"""

return web.Response(text=custom_html, content_type="text/html")
return Response(text=custom_html, content_type="text/html")
```
37 changes: 31 additions & 6 deletions docs/integrations/asgi.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ The `GraphQL` app accepts the following options at the moment:

## Extending the view

We allow to extend the base `GraphQL` app, by overriding the following methods:
The base `GraphQL` class can be extended by overriding any of 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`
- `async def get_context(self, request: Union[Request, WebSocket], response: Union[Response, WebSocket]) -> Context`
- `async def get_root_value(self, request: Union[Request, WebSocket]) -> Optional[RootValue]`
- `async def process_result(self, request: Request, result: ExecutionResult) -> GraphQLHTTPResponse`
- `def decode_json(self, data: Union[str, bytes]) -> object`
- `def encode_json(self, data: object) -> str`
- `async def render_graphql_ide(self, request: Request) -> Response`
Expand All @@ -60,10 +61,17 @@ resolver. You can return anything here, by default we return a dictionary with
the request and the response.

```python
import strawberry
from typing import Union
from strawberry.asgi import GraphQL
from starlette.requests import Request
from starlette.responses import Response


class MyGraphQL(GraphQL):
async def get_context(
self, request: Union[Request, WebSocket], response: Optional[Response] = None
) -> Any:
):
return {"example": 1}


Expand All @@ -90,6 +98,9 @@ This is possible by updating the response object contained inside the context of
the `Info` object.

```python
import strawberry


@strawberry.type
class Mutation:
@strawberry.mutation
Expand All @@ -105,6 +116,7 @@ Similarly, [background tasks](https://www.starlette.io/background/) can be set
on the response via the context:

```python
import strawberry
from starlette.background import BackgroundTask


Expand All @@ -126,8 +138,15 @@ probably not used a lot but it might be useful in certain situations.
Here's an example:

```python
import strawberry
from typing import Union
from strawberry.asgi import GraphQL
from starlette.requests import Request
from starlette.websockets import WebSocket


class MyGraphQL(GraphQL):
async def get_root_value(self, request: Request) -> Any:
async def get_root_value(self, request: Union[Request, WebSocket]):
return Query(name="Patrick")


Expand All @@ -149,8 +168,10 @@ It needs to return an object of `GraphQLHTTPResponse` and accepts the request
and the execution results.

```python
from strawberry.asgi import GraphQL
from strawberry.http import GraphQLHTTPResponse
from strawberry.types import ExecutionResult
from starlette.requests import Request


class MyGraphQL(GraphQL):
Expand Down Expand Up @@ -195,6 +216,10 @@ responses. By default we use `json.dumps` but you can override this method to
use a different encoder.

```python
import json
from strawberry.asgi import GraphQL


class MyGraphQLView(GraphQL):
def encode_json(self, data: object) -> str:
return json.dumps(data, indent=2)
Expand Down
33 changes: 25 additions & 8 deletions docs/integrations/chalice.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ The `GraphQLView` accepts two options at the moment:

## Extending the view

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: object) -> str`
The base `GraphQLView` class can be extended by overriding any of the following
methods:

- `def get_context(self, request: Request, response: TemporalResponse) -> Context`
- `def get_root_value(self, request: Request) -> Optional[RootValue]`
- `def process_result(self, request: Request, result: ExecutionResult) -> GraphQLHTTPResponse`
- `def decode_json(self, data: Union[str, bytes]) -> object`
- `def encode_json(self, data: object) -> str`
- `def render_graphql_ide(self, request: Request) -> Response`

### get_context
Expand All @@ -86,8 +88,14 @@ the request. By default; the `Response` object from `flask` is injected via the
parameters.

```python
import strawberry
from strawberry.chalice.views import GraphQLView
from strawberry.http.temporal import TemporalResponse
from chalice.app import Request


class MyGraphQLView(GraphQLView):
def get_context(self, response: Response) -> Any:
def get_context(self, request: Request, response: TemporalResponse):
return {"example": 1}


Expand All @@ -112,8 +120,12 @@ probably not used a lot but it might be useful in certain situations.
Here's an example:

```python
import strawberry
from strawberry.chalice.views import GraphQLView


class MyGraphQLView(GraphQLView):
def get_root_value(self) -> Any:
def get_root_value(self):
return Query(name="Patrick")


Expand All @@ -137,6 +149,7 @@ result.
```python
from strawberry.http import GraphQLHTTPResponse
from strawberry.types import ExecutionResult
from strawberry.chalice.views import GraphQLView


class MyGraphQLView(GraphQLView):
Expand Down Expand Up @@ -179,6 +192,10 @@ responses. By default we use `json.dumps` but you can override this method to
use a different encoder.

```python
import json
from strawberry.chalice.views import GraphQLView


class MyGraphQLView(GraphQLView):
def encode_json(self, data: object) -> str:
return json.dumps(data, indent=2)
Expand Down
16 changes: 11 additions & 5 deletions docs/integrations/channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,14 @@ GraphQLWebsocketCommunicator(

### Extending the consumer

We allow to extend `GraphQLHTTPConsumer`, by overriding the following methods:
The base `GraphQLHTTPConsumer` class can be extended by overriding any of the
following methods:

- `async def get_context(self, request: ChannelsRequest, response: TemporalResponse) -> Context`
- `async def get_root_value(self, request: ChannelsRequest) -> Optional[RootValue]`
- `async def process_result(self, request: Request, result: ExecutionResult) -> GraphQLHTTPResponse`.
- `async def process_result(self, request: Request, result: ExecutionResult) -> GraphQLHTTPResponse`
- `def decode_json(self, data: Union[str, bytes]) -> object`
- `def encode_json(self, data: object) -> str`
- `async def render_graphql_ide(self, request: ChannelsRequest) -> ChannelsResponse`

#### Context
Expand Down Expand Up @@ -582,10 +585,13 @@ class MyGraphQLHTTPConsumer(GraphQLHTTPConsumer):

### Extending the consumer

We allow to extend `GraphQLWSConsumer`, by overriding the following methods:
The base `GraphQLWSConsumer` class can be extended by overriding any of the
following methods:

- `async def get_context(self, request: ChannelsConsumer, connection_params: Any) -> Context`
- `async def get_root_value(self, request: ChannelsConsumer) -> Optional[RootValue]`
- `async def get_context(self, request: GraphQLWSConsumer, response: GraphQLWSConsumer) -> Context`
- `async def get_root_value(self, request: GraphQLWSConsumer) -> Optional[RootValue]`
- `def decode_json(self, data: Union[str, bytes]) -> object`
- `def encode_json(self, data: object) -> str`

### Context

Expand Down
Loading

0 comments on commit 2e4e3cc

Please sign in to comment.