diff --git a/docs/integrations/aiohttp.md b/docs/integrations/aiohttp.md
index ceed995299..64655d5fbf 100644
--- a/docs/integrations/aiohttp.md
+++ b/docs/integrations/aiohttp.md
@@ -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
@@ -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}
@@ -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")
@@ -121,7 +125,7 @@ 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
@@ -129,7 +133,7 @@ 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}
@@ -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)
@@ -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 = """
Custom GraphQL IDE
"""
- return web.Response(text=custom_html, content_type="text/html")
+ return Response(text=custom_html, content_type="text/html")
```
diff --git a/docs/integrations/asgi.md b/docs/integrations/asgi.md
index 4687ef05a6..3cad706772 100644
--- a/docs/integrations/asgi.md
+++ b/docs/integrations/asgi.md
@@ -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`
@@ -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}
@@ -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
@@ -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
@@ -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")
@@ -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):
@@ -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)
diff --git a/docs/integrations/chalice.md b/docs/integrations/chalice.md
index f2e9ef8edf..84fa6d8843 100644
--- a/docs/integrations/chalice.md
+++ b/docs/integrations/chalice.md
@@ -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
@@ -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}
@@ -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")
@@ -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):
@@ -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)
diff --git a/docs/integrations/channels.md b/docs/integrations/channels.md
index af495a1ed9..86c3041d10 100644
--- a/docs/integrations/channels.md
+++ b/docs/integrations/channels.md
@@ -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
@@ -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
diff --git a/docs/integrations/django.md b/docs/integrations/django.md
index 408772d86b..bf4d9bf312 100644
--- a/docs/integrations/django.md
+++ b/docs/integrations/django.md
@@ -62,9 +62,11 @@ encoding process.
We allow to extend the base `GraphQLView`, by overriding the following methods:
-- `def get_context(self, request: HttpRequest, response: HttpResponse) -> Any`
-- `def get_root_value(self, request: HttpRequest) -> Any`
-- `def process_result(self, request: HttpRequest, result: ExecutionResult) -> GraphQLHTTPResponse`
+- `def get_context(self, request: HttpRequest, response: HttpResponse) -> Context`
+- `def get_root_value(self, request: HttpRequest) -> 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: HttpRequest) -> HttpResponse`
### get_context
@@ -74,6 +76,9 @@ resolver. You can return anything here, by default we return a
`StrawberryDjangoContext` object.
```python
+import strawberry
+
+
@strawberry.type
class Query:
@strawberry.field
@@ -84,8 +89,13 @@ class Query:
or in case of a custom context:
```python
+import strawberry
+from strawberry.django.views import GraphQLView
+from django.http import HttpRequest, HttpResponse
+
+
class MyGraphQLView(GraphQLView):
- def get_context(self, request: HttpRequest, response: HttpResponse) -> Any:
+ def get_context(self, request: HttpRequest, response: HttpResponse):
return {"example": 1}
@@ -110,8 +120,13 @@ probably not used a lot but it might be useful in certain situations.
Here's an example:
```python
+import strawberry
+from strawberry.django.views import GraphQLView
+from django.http import HttpRequest
+
+
class MyGraphQLView(GraphQLView):
- def get_root_value(self, request: HttpRequest) -> Any:
+ def get_root_value(self, request: HttpRequest):
return Query(name="Patrick")
@@ -135,6 +150,8 @@ and the execution results.
```python
from strawberry.http import GraphQLHTTPResponse
from strawberry.types import ExecutionResult
+from strawberry.django.views import GraphQLView
+from django.http import HttpRequest
class MyGraphQLView(GraphQLView):
@@ -152,6 +169,43 @@ class MyGraphQLView(GraphQLView):
In this case we are doing the default processing of the result, but it can be
tweaked based on your needs.
+### decode_json
+
+`decode_json` allows to customize the decoding of HTTP and WebSocket JSON
+requests. By default we use `json.loads` but you can override this method to use
+a different decoder.
+
+```python
+from strawberry.django.views import GraphQLView
+from typing import Union
+import orjson
+
+
+class MyGraphQLView(GraphQLView):
+ def decode_json(self, data: Union[str, bytes]) -> object:
+ return orjson.loads(data)
+```
+
+Make sure your code raises `json.JSONDecodeError` or a subclass of it if the
+JSON cannot be decoded. The library shown in the example above, `orjson`, does
+this by default.
+
+### encode_json
+
+`encode_json` allows to customize the encoding of HTTP and WebSocket JSON
+responses. By default we use `json.dumps` but you can override this method to
+use a different encoder.
+
+```python
+import json
+from strawberry.django.views import GraphQLView
+
+
+class MyGraphQLView(GraphQLView):
+ def encode_json(self, data: object) -> str:
+ return json.dumps(data, indent=2)
+```
+
### render_graphql_ide
In case you need more control over the rendering of the GraphQL IDE than the
@@ -159,7 +213,7 @@ In case you need more control over the rendering of the GraphQL IDE than the
```python
from strawberry.django.views import GraphQLView
-from django.http import HttpResponse
+from django.http import HttpResponse, HttpRequest
from django.template.loader import render_to_string
@@ -202,12 +256,13 @@ The `AsyncGraphQLView` accepts the following arguments:
## Extending the view
-We allow to extend the base `AsyncGraphQLView`, by overriding the following
-methods:
+The base `AsyncGraphQLView` class can be extended by overriding any of the
+following 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`
+- `async def get_context(self, request: HttpRequest, response: HttpResponse) -> Context`
+- `async def get_root_value(self, request: HttpRequest) -> 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: HttpRequest) -> HttpResponse`
@@ -218,8 +273,13 @@ resolver. You can return anything here, by default we return a dictionary with
the request.
```python
+import strawberry
+from strawberry.django.views import AsyncGraphQLView
+from django.http import HttpRequest, HttpResponse
+
+
class MyGraphQLView(AsyncGraphQLView):
- async def get_context(self, request: HttpRequest, response: HttpResponse) -> Any:
+ async def get_context(self, request: HttpRequest, response: HttpResponse):
return {"example": 1}
@@ -244,8 +304,13 @@ probably not used a lot but it might be useful in certain situations.
Here's an example:
```python
+import strawberry
+from strawberry.django.views import AsyncGraphQLView
+from django.http import HttpRequest
+
+
class MyGraphQLView(AsyncGraphQLView):
- async def get_root_value(self, request: HttpRequest) -> Any:
+ async def get_root_value(self, request: HttpRequest):
return Query(name="Patrick")
@@ -269,6 +334,8 @@ and the execution results.
```python
from strawberry.http import GraphQLHTTPResponse
from strawberry.types import ExecutionResult
+from strawberry.django.views import AsyncGraphQLView
+from django.http import HttpRequest
class MyGraphQLView(AsyncGraphQLView):
@@ -314,6 +381,10 @@ responses. By default we use `json.dumps` but you can override this method to
use a different encoder.
```python
+import json
+from strawberry.django.views import AsyncGraphQLView
+
+
class MyGraphQLView(AsyncGraphQLView):
def encode_json(self, data: object) -> str:
return json.dumps(data, indent=2)
diff --git a/docs/integrations/fastapi.md b/docs/integrations/fastapi.md
index dd95440683..1095dfca9f 100644
--- a/docs/integrations/fastapi.md
+++ b/docs/integrations/fastapi.md
@@ -259,6 +259,16 @@ app.include_router(graphql_app, prefix="/graphql")
Here we are returning a Query where the name is "Patrick", so when we request
the field name we'll return "Patrick".
+## Extending the router
+
+The base `GraphQLRouter` class can be extended by overriding any of the
+following methods:
+
+- `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) -> HTMLResponse`
+
### process_result
The `process_result` option allows you to customize and/or process results
@@ -269,7 +279,7 @@ It needs to return a `GraphQLHTTPResponse` object and accepts the request and
execution results.
```python
-from fastapi import Request
+from starlette.requests import Request
from strawberry.fastapi import GraphQLRouter
from strawberry.http import GraphQLHTTPResponse
from strawberry.types import ExecutionResult
@@ -318,6 +328,10 @@ responses. By default we use `json.dumps` but you can override this method to
use a different encoder.
```python
+from strawberry.fastapi import GraphQLRouter
+import json
+
+
class MyGraphQLRouter(GraphQLRouter):
def encode_json(self, data: object) -> bytes:
return json.dumps(data, indent=2)
@@ -331,6 +345,7 @@ In case you need more control over the rendering of the GraphQL IDE than the
```python
from strawberry.fastapi import GraphQLRouter
from starlette.responses import HTMLResponse, Response
+from starlette.requests import Request
class MyGraphQLRouter(GraphQLRouter):
diff --git a/docs/integrations/flask.md b/docs/integrations/flask.md
index bacd6b4011..6d489fedaa 100644
--- a/docs/integrations/flask.md
+++ b/docs/integrations/flask.md
@@ -49,19 +49,21 @@ The `GraphQLView` accepts the following options at the moment:
## Extending the view
-We allow to extend the base `GraphQLView`, by overriding the following methods:
-
-- `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: object) -> str`
+The base `GraphQLView` class can be extended by overriding any of the following
+methods:
+
+- `def get_context(self, request: Request, response: Response) -> 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`
-Note that the `AsyncGraphQLView` can also be extended by overriding the same
-methods above, but `get_context`, `get_root_value` and `process_result` are
-async functions.
+Note that the `AsyncGraphQLView` can also be extended in the same way, but the
+`get_context`, `get_root_value`, `process_result`, and `render_graphql_ide`
+methods are asynchronous.
@@ -73,8 +75,13 @@ the request. By default; the `Response` object from `flask` is injected via the
parameters.
```python
+import strawberry
+from strawberry.flask.views import GraphQLView
+from flask import Request, Response
+
+
class MyGraphQLView(GraphQLView):
- def get_context(self, request: Request, response: Response) -> Any:
+ def get_context(self, request: Request, response: Response):
return {"example": 1}
@@ -99,8 +106,13 @@ probably not used a lot but it might be useful in certain situations.
Here's an example:
```python
+import strawberry
+from strawberry.flask.views import GraphQLView
+from flask import Request
+
+
class MyGraphQLView(GraphQLView):
- def get_root_value(self, request: Request) -> Any:
+ def get_root_value(self, request: Request):
return Query(name="Patrick")
@@ -122,6 +134,7 @@ It needs to return an object of `GraphQLHTTPResponse` and accepts the execution
result.
```python
+from strawberry.flask.views import GraphQLView
from strawberry.http import GraphQLHTTPResponse
from strawberry.types import ExecutionResult
@@ -166,6 +179,10 @@ responses. By default we use `json.dumps` but you can override this method to
use a different encoder.
```python
+import json
+from strawberry.flask.views import GraphQLView
+
+
class MyGraphQLView(GraphQLView):
def encode_json(self, data: object) -> str:
return json.dumps(data, indent=2)
diff --git a/docs/integrations/litestar.md b/docs/integrations/litestar.md
index 86ee0bc179..002fb5cbee 100644
--- a/docs/integrations/litestar.md
+++ b/docs/integrations/litestar.md
@@ -320,10 +320,129 @@ app = Litestar(route_handlers=[GraphQLController])
## Extending the controller
-The `make_graphql_controller` function returns a `GraphQLController` class that
-can be extended by overriding the following methods:
+The base `GraphQLController` class returned by `make_graphql_controller` can be
+extended by overriding any of the following methods:
-1. `async def render_graphql_ide(self, request: Request) -> Response`
+- `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`
+
+### process_result
+
+The `process_result` option allows you to customize and/or process results
+before they are sent to the clients. This can be useful for logging errors or
+hiding them (for example to hide internal exceptions).
+
+It needs to return a `GraphQLHTTPResponse` object and accepts the request and
+execution results.
+
+```python
+import strawberry
+from strawberry.http import GraphQLHTTPResponse
+from strawberry.types import ExecutionResult
+from strawberry.litestar import make_graphql_controller
+from litestar import Request
+
+
+@strawberry.type
+class Query:
+ @strawberry.field
+ def hello(self) -> str:
+ return "world"
+
+
+schema = strawberry.Schema(Query)
+
+GraphQLController = make_graphql_controller(
+ schema,
+ path="/graphql",
+)
+
+
+class MyGraphQLController(GraphQLController):
+ async def process_result(
+ self, request: Request, result: ExecutionResult
+ ) -> GraphQLHTTPResponse:
+ data: GraphQLHTTPResponse = {"data": result.data}
+
+ if result.errors:
+ data["errors"] = [err.formatted for err in result.errors]
+
+ return data
+```
+
+In this case we are doing the default processing of the result, but it can be
+tweaked based on your needs.
+
+### decode_json
+
+`decode_json` allows to customize the decoding of HTTP and WebSocket JSON
+requests. By default we use `json.loads` but you can override this method to use
+a different decoder.
+
+```python
+import strawberry
+import orjson
+from strawberry.litestar import make_graphql_controller
+from typing import Union
+
+
+@strawberry.type
+class Query:
+ @strawberry.field
+ def hello(self) -> str:
+ return "world"
+
+
+schema = strawberry.Schema(Query)
+
+GraphQLController = make_graphql_controller(
+ schema,
+ path="/graphql",
+)
+
+
+class MyGraphQLController(GraphQLController):
+ def decode_json(self, data: Union[str, bytes]) -> object:
+ return orjson.loads(data)
+```
+
+Make sure your code raises `json.JSONDecodeError` or a subclass of it if the
+JSON cannot be decoded. The library shown in the example above, `orjson`, does
+this by default.
+
+### encode_json
+
+`encode_json` allows to customize the encoding of HTTP and WebSocket JSON
+responses. By default we use `json.dumps` but you can override this method to
+use a different encoder.
+
+```python
+import json
+import strawberry
+from strawberry.litestar import make_graphql_controller
+
+
+@strawberry.type
+class Query:
+ @strawberry.field
+ def hello(self) -> str:
+ return "world"
+
+
+schema = strawberry.Schema(Query)
+
+GraphQLController = make_graphql_controller(
+ schema,
+ path="/graphql",
+)
+
+
+class MyGraphQLController(GraphQLController):
+ def encode_json(self, data: object) -> bytes:
+ return json.dumps(data, indent=2)
+```
### render_graphql_ide
diff --git a/docs/integrations/quart.md b/docs/integrations/quart.md
index feecf7b4f5..b0e56983db 100644
--- a/docs/integrations/quart.md
+++ b/docs/integrations/quart.md
@@ -41,12 +41,14 @@ The `GraphQLView` accepts the following options at the moment:
## Extending the view
-We allow to extend the base `GraphQLView`, by overriding the following methods:
-
-- `async def get_context(self, request: Request, response: Response) -> Any`
-- `async def get_root_value(self, request: Request) -> Any`
-- `async def process_result(self, result: ExecutionResult) -> GraphQLHTTPResponse`
-- `def encode_json(self, response_data: object) -> str`
+The base `GraphQLView` class can be extended by overriding any of the following
+methods:
+
+- `async def get_context(self, request: Request, response: Response) -> 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: Request) -> Response`
### get_context
@@ -57,8 +59,13 @@ the request. By default; the `Response` object from Quart is injected via the
parameters.
```python
+import strawberry
+from strawberry.quart.views import GraphQLView
+from quart import Request, Response
+
+
class MyGraphQLView(GraphQLView):
- async def get_context(self, request: Request, response: Response) -> Any:
+ async def get_context(self, request: Request, response: Response):
return {"example": 1}
@@ -83,8 +90,13 @@ probably not used a lot but it might be useful in certain situations.
Here's an example:
```python
+import strawberry
+from strawberry.quart.views import GraphQLView
+from quart import Request
+
+
class MyGraphQLView(GraphQLView):
- async def get_root_value(self, request: Request) -> Any:
+ async def get_root_value(self, request: Request):
return Query(name="Patrick")
@@ -106,12 +118,16 @@ It needs to return an object of `GraphQLHTTPResponse` and accepts the execution
result.
```python
+from strawberry.quart.views import GraphQLView
from strawberry.http import GraphQLHTTPResponse
from strawberry.types import ExecutionResult
+from quart import Request
class MyGraphQLView(GraphQLView):
- async def process_result(self, result: ExecutionResult) -> GraphQLHTTPResponse:
+ async def process_result(
+ self, request: Request, result: ExecutionResult
+ ) -> GraphQLHTTPResponse:
data: GraphQLHTTPResponse = {"data": result.data}
if result.errors:
@@ -150,6 +166,10 @@ responses. By default we use `json.dumps` but you can override this method to
use a different encoder.
```python
+import json
+from strawberry.quart.views import GraphQLView
+
+
class MyGraphQLView(GraphQLView):
def encode_json(self, data: object) -> str:
return json.dumps(data, indent=2)
diff --git a/docs/integrations/sanic.md b/docs/integrations/sanic.md
index f10ef60c02..631dc9e23f 100644
--- a/docs/integrations/sanic.md
+++ b/docs/integrations/sanic.md
@@ -37,12 +37,14 @@ 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: Request, response: Response) -> Any`
-- `async get_root_value(self, request: Request) -> Any`
-- `async process_result(self, result: ExecutionResult) -> GraphQLHTTPResponse`
+- `async def get_context(self, request: Request, response: TemporalResponse) -> 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: Request) -> HTTPResponse`
### get_context
@@ -52,8 +54,14 @@ for your resolvers. You can return anything here; by default GraphQLView returns
a dictionary with the request.
```python
+import strawberry
+from strawberry.sanic.views import GraphQLView
+from strawberry.http.temporal_response import TemporalResponse
+from sanic.request import Request
+
+
class MyGraphQLView(GraphQLView):
- async def get_context(self, request: Request, response: Response) -> Any:
+ async def get_context(self, request: Request, response: TemporalResponse):
return {"example": 1}
@@ -79,8 +87,13 @@ certain situations.
Here's an example:
```python
+import strawberry
+from strawberry.sanic.views import GraphQLView
+from sanic.request import Request
+
+
class MyGraphQLView(GraphQLView):
- async def get_root_value(self, request: Request) -> Any:
+ async def get_root_value(self, request: Request):
return Query(name="Patrick")
@@ -148,6 +161,10 @@ responses. By default we use `json.dumps` but you can override this method to
use a different encoder.
```python
+import json
+from strawberry.sanic.views import GraphQLView
+
+
class MyGraphQLView(GraphQLView):
def encode_json(self, data: object) -> str:
return json.dumps(data, indent=2)