asgi-cors-strawberry
ASGI middleware to apply CORS header especially for Strawberry GraphQL
installation
pip install asgi-cors-strawberry
when to use
According to mdn MDN - CORS, cross-origin requests are preflighted using OPTIONS method. The response from preflight has Access-Control-Allow-Origin, Access-Control-Allow-Headers, etc.
Since Strawberry GraphQL Consumer is designed to handle only GET and POST methods, if you use general ASGI CORS middleware, you will get a 405 code as a response to preflight.
- Our middleware responds with Okay status in case of OPTIONS method
- Our middleware checks Access-Control-Allow-Origin from hosts, wildcards setting, All hosts are allowed by default!
- Our middleware adds Content-Type in Access-Control-Allow-Headers, because gql will be passed to the request
how to use
# in django asgi.py
from asgi_cors_strawberry import CorsMiddleware
...
application = ProtocolTypeRouter(
{
"http": URLRouter(
[
re_path(r"^graphql", GraphQLHTTPConsumer.as_asgi(schema=schema)),
re_path(r"^", get_asgi_application()),
]
),
"websocket": URLRouter([
re_path(r"^graphql", GraphQLWSConsumer.as_asgi(schema=schema))
])
}
)
application = CorsMiddleware(application)
The example above is an ASGI application using Strawberry-GraphQL[Channels]
CorsMiddleware parameters
- CorsMiddleware(application, allow_all=True, hosts=[ ], host_wildcards=[ ], headers=["content-type", "authorization"])
allow_all
True if all hosts are allowed
hosts
if allow_all is False, hosts is a list of allowed hosts
host_wildcards
you can use wildcards in hosts, like ["*.example.com"]
headers
you can add headers to Access-Control-Allow-Headers
default header is "content-type" because gql will be passed to the request
TODO
- testcase
Metadata
Release files for asgi-cors-strawberry 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| asgi-cors-strawberry-0.2.0.tar.gz | 7.0 kB | Without provenance | Details for asgi-cors-strawberry-0.2.0.tar.gz |
Release files / asgi-cors-strawberry-0.2.0.tar.gz
| Download URL | asgi-cors-strawberry-0.2.0.tar.gz |
|---|---|
| Size | 7.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
39e41b025b5c67061938d1eff537b9266f1d82ffd0bc36aa07393b253aa25e2e
|
|
BLAKE2b-256 checksum How to use checksums |
148707c6d61302913243385b66331c20dc81eeaa8f3e79914f3343ea8ff11888
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.9
|
Security for asgi-cors-strawberry 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.