Skip to content

Commit 336b177

Browse files
authored
fix(strawberry): prepare for upstream extension removal (#3649)
As suggested by @szokeasaurusrex in strawberry-graphql/strawberry#3590, Strawberry is preparing to fully remove its deprecated SentryTracingExtension in favor of the integration provided by the Sentry SDK. This PR prepares the Sentry Strawberry integration for that removal by: - fixing that the integration would assume Strawberry is not installed if the extension cannot be imported - making sure tests with Strawberry versions before and after the removal still work I also checked that removing the extension does not otherwise affect the integration: The extension's sync and async variants are imported to replace them and to guess whether sync or async code is used. Both still works if the imports are defaulted to None.
1 parent 8d48961 commit 336b177

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

sentry_sdk/integrations/strawberry.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@
3131
from strawberry import Schema
3232
from strawberry.extensions import SchemaExtension # type: ignore
3333
from strawberry.extensions.tracing.utils import should_skip_tracing as strawberry_should_skip_tracing # type: ignore
34+
from strawberry.http import async_base_view, sync_base_view # type: ignore
35+
except ImportError:
36+
raise DidNotEnable("strawberry-graphql is not installed")
37+
38+
try:
3439
from strawberry.extensions.tracing import ( # type: ignore
3540
SentryTracingExtension as StrawberrySentryAsyncExtension,
3641
SentryTracingExtensionSync as StrawberrySentrySyncExtension,
3742
)
38-
from strawberry.http import async_base_view, sync_base_view # type: ignore
3943
except ImportError:
40-
raise DidNotEnable("strawberry-graphql is not installed")
44+
StrawberrySentryAsyncExtension = None
45+
StrawberrySentrySyncExtension = None
4146

4247
from typing import TYPE_CHECKING
4348

tests/integrations/strawberry/test_strawberry.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
from fastapi import FastAPI
1111
from fastapi.testclient import TestClient
1212
from flask import Flask
13-
from strawberry.extensions.tracing import (
14-
SentryTracingExtension,
15-
SentryTracingExtensionSync,
16-
)
1713
from strawberry.fastapi import GraphQLRouter
1814
from strawberry.flask.views import GraphQLView
1915

@@ -28,6 +24,15 @@
2824
)
2925
from tests.conftest import ApproxDict
3026

27+
try:
28+
from strawberry.extensions.tracing import (
29+
SentryTracingExtension,
30+
SentryTracingExtensionSync,
31+
)
32+
except ImportError:
33+
SentryTracingExtension = None
34+
SentryTracingExtensionSync = None
35+
3136
parameterize_strawberry_test = pytest.mark.parametrize(
3237
"client_factory,async_execution,framework_integrations",
3338
(
@@ -143,6 +148,10 @@ def test_infer_execution_type_from_installed_packages_sync(sentry_init):
143148
assert SentrySyncExtension in schema.extensions
144149

145150

151+
@pytest.mark.skipif(
152+
SentryTracingExtension is None,
153+
reason="SentryTracingExtension no longer available in this Strawberry version",
154+
)
146155
def test_replace_existing_sentry_async_extension(sentry_init):
147156
sentry_init(integrations=[StrawberryIntegration()])
148157

@@ -152,6 +161,10 @@ def test_replace_existing_sentry_async_extension(sentry_init):
152161
assert SentryAsyncExtension in schema.extensions
153162

154163

164+
@pytest.mark.skipif(
165+
SentryTracingExtensionSync is None,
166+
reason="SentryTracingExtensionSync no longer available in this Strawberry version",
167+
)
155168
def test_replace_existing_sentry_sync_extension(sentry_init):
156169
sentry_init(integrations=[StrawberryIntegration()])
157170

0 commit comments

Comments
 (0)