Skip to content

Commit

Permalink
Mention scope and receive attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Jan 26, 2024
1 parent ad2b677 commit ec65fb3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/asgi.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ $ uvicorn myasgi:application
See the [reference](asgi-reference.md#constructor).


## The `request` instance

The ASGI application creates its own `request` object, an instance of the `Request` class from the [Starlette](https://github.com/encode/starlette/blob/0.36.1/starlette/requests.py#L199). It's `scope` and `receive` attributes are populated from the received request.

When writing the [ASGI middleware](https://asgi.readthedocs.io/en/latest/specs/main.html#middleware), remember to rely on the `request.scope` dict for storing additional data on the request object, instead of mutating the request object directly (like it's done in Django). For example:

```python
# This is wrong
request.app_data

# This is correct
request.scope["app_data"]
```


## Customizing JSON responses

Ariadne's ASGI application uses [Starlette's `JSONResponse`](https://github.com/encode/starlette/blob/0.36.1/starlette/responses.py#L169) for its JSON responses.
Expand Down
15 changes: 15 additions & 0 deletions website/versioned_docs/version-0.18/asgi.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ $ uvicorn myasgi:application
See the [reference](asgi-reference.md#constructor).


## The `request` instance

The ASGI application creates its own `request` object, an instance of the `Request` class from the [Starlette](https://github.com/encode/starlette/blob/0.36.1/starlette/requests.py#L199). It's `scope` and `receive` attributes are populated from the received request.

When writing the [ASGI middleware](https://asgi.readthedocs.io/en/latest/specs/main.html#middleware), remember to rely on the `request.scope` dict for storing additional data on the request object, instead of mutating the request object directly (like it's done in Django). For example:

```python
# This is wrong
request.app_data

# This is correct
request.scope["app_data"]
```


## Customizing JSON responses

Ariadne's ASGI application uses [Starlette's `JSONResponse`](https://github.com/encode/starlette/blob/0.36.1/starlette/responses.py#L169) for its JSON responses.
Expand Down

0 comments on commit ec65fb3

Please sign in to comment.