-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add Python integration docs for Strawberry #8085
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0f4b090
add to table
sentrivana 3c72a35
fixes
sentrivana 1226907
wording
sentrivana 21b7c80
style(lint): Auto commit lint changes
getsantry[bot] a3a61cc
Update src/platforms/python/common/integrations/index.mdx
lizokm d99e1be
style(lint): Auto commit lint changes
getsantry[bot] 4f8c3c7
Update src/platforms/python/common/integrations/strawberry/index.mdx
lizokm a147ac1
Update src/platforms/python/common/integrations/strawberry/index.mdx
sentrivana f28210d
Update src/platforms/python/common/integrations/strawberry/index.mdx
sentrivana 91a60ca
Update src/platforms/python/common/integrations/strawberry/index.mdx
sentrivana f1765b7
Update src/platforms/python/common/integrations/strawberry/index.mdx
sentrivana 7cecd4a
Update src/platforms/python/common/integrations/strawberry/index.mdx
sentrivana 8e8092d
Update src/platforms/python/common/integrations/strawberry/index.mdx
sentrivana cd42036
Update src/platforms/python/common/integrations/strawberry/index.mdx
sentrivana 911a6ec
Update src/platforms/python/common/integrations/strawberry/index.mdx
sentrivana 8312897
update
sentrivana c03d01f
merge integrations/index
sentrivana 6be14ae
style(lint): Auto commit lint changes
getsantry[bot] 40845be
merge
sentrivana 7eb8072
typo
sentrivana 4a61527
Update src/platforms/python/common/integrations/strawberry/index.mdx
sentrivana 9d61090
apply feedback
sentrivana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
src/platforms/python/common/integrations/strawberry/index.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
--- | ||
title: Strawberry | ||
description: "Learn how to import the Strawberry GraphQL integration and how it captures GraphQL errors and spans." | ||
--- | ||
|
||
The Strawberry integration captures errors and operations from the | ||
[Strawberry GraphQL library](https://strawberry.rocks/), which can then be viewed | ||
in [Sentry](https://sentry.io). | ||
|
||
## Install | ||
|
||
To get started, install `sentry-sdk` from PyPI. | ||
|
||
```bash | ||
pip install --upgrade sentry-sdk | ||
``` | ||
|
||
## Configure | ||
|
||
Add `StrawberryIntegration()` to your `integrations` list: | ||
|
||
<SignInNote /> | ||
|
||
```python | ||
import sentry_sdk | ||
from sentry_sdk.integrations.strawberry import StrawberryIntegration | ||
|
||
sentry_sdk.init( | ||
dsn="___PUBLIC_DSN___", | ||
# Set traces_sample_rate to 1.0 to capture 100% | ||
# of transactions for performance monitoring. | ||
traces_sample_rate=1.0, | ||
integrations=[ | ||
# Set async_execution to True if you have at least one async resolver | ||
StrawberryIntegration(async_execution=True), | ||
], | ||
) | ||
``` | ||
|
||
## Verify | ||
|
||
Make sure you have all the prerequisites installed: | ||
|
||
```bash | ||
pip install 'strawberry-graphql[debug-server]' | ||
pip install fastapi | ||
``` | ||
|
||
Create a `schema.py` file with the below content: | ||
|
||
```python | ||
import strawberry | ||
|
||
sentry_sdk.init(...) # same as above | ||
|
||
async def resolve_hello(root) -> str: | ||
1 / 0 | ||
return "Hello world!" | ||
|
||
@strawberry.type | ||
class Query: | ||
hello: str = strawberry.field(resolver=resolve_hello) | ||
|
||
schema = strawberry.Schema(Query) | ||
``` | ||
|
||
To start the web server, run: | ||
|
||
```bash | ||
strawberry server schema | ||
``` | ||
|
||
Navigate to [http://127.0.0.1:8000/graphql](http://127.0.0.1:8000/graphql) in your | ||
browser. You should see the GraphiQL graphical interface. | ||
|
||
Type `query HelloQuery { hello }` into the query input field then press the | ||
"Execute query" button. Your web server will be queried, which should result in an | ||
exception due to the `ZeroDivisionError` we've snuck into the `resolve_hello` | ||
resolver. | ||
|
||
This will create a `GraphQLError` in the Issues section as well as a transaction | ||
in the Performance section of [sentry.io](https://sentry.io). It will take a couple of moments for the data to appear in [sentry.io](https://sentry.io). | ||
|
||
## Options | ||
|
||
There are several options you will get to choose from: | ||
|
||
### Synchronous vs. Asynchronous Execution | ||
|
||
Strawberry supports both synchronous and asynchronous execution of GraphQL | ||
queries. If you have at least one async resolver, you should initialize | ||
`StrawberryIntegration` with `async_execution=True`, otherwise set it to `False`. | ||
|
||
The SDK will make a best-effort guess if `async_execution` is not provided, | ||
based on installed web frameworks. | ||
|
||
```python | ||
sentry_sdk.init( | ||
# (...) other options | ||
integrations=[ | ||
StrawberryIntegration(async_execution=True), # or False | ||
], | ||
) | ||
``` | ||
|
||
### Capturing Request and Response Bodies | ||
|
||
The Strawberry integration can capture request and response bodies | ||
for each GraphQL error that happens. Since these may contain sensitive data, | ||
this is the default behavior. To enable capturing request and response bodies, the | ||
SDK needs to be initialized with the | ||
[send_default_pii](https://docs.sentry.io/platforms/python/configuration/options/#send-default-pii) option set to `True`. | ||
|
||
```python | ||
sentry_sdk.init( | ||
# same options as above | ||
send_default_pii=True, | ||
) | ||
``` | ||
|
||
<Alert level="warning" title="Note"> | ||
|
||
Since `send_default_pii` is a global SDK option, setting it to `True` will affect all | ||
integrations, not just Strawberry. Please make sure to | ||
[remove sensitive data](/platforms/python/data-management/sensitive-data/) | ||
from events before enabling this option. | ||
|
||
</Alert> | ||
|
||
## Notes | ||
|
||
Strawberry comes with a (now deprecated) built-in | ||
[Sentry tracing extension](https://strawberry.rocks/docs/extensions/sentry-tracing) | ||
that this integration is built on. To prevent duplicate traces, the Sentry SDK integration will deactivate the built-in Strawberry extension if you happen to be using both. | ||
|
||
## Supported Versions | ||
|
||
- strawberry-graphql: 0.209.5+ | ||
- Python: 3.8+ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.