Skip to content

Commit f35d333

Browse files
authored
Document Apollo3 GraphQL client errors (#7135)
1 parent c28741d commit f35d333

File tree

1 file changed

+88
-1
lines changed
  • src/platforms/android/configuration/integrations

1 file changed

+88
-1
lines changed

src/platforms/android/configuration/integrations/apollo3.mdx

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ Add `Interceptors` to `ApolloClient.Builder` using `ApolloBuilderExtensions`:
3232
import com.apollographql.apollo3.ApolloClient;
3333
import io.sentry.apollo3.SentryApolloBuilderExtensionsKt;
3434

35-
ApolloClient apollo = SentryApolloBuilderExtensionsKt.sentryTracing(new ApolloClient.Builder())
35+
ApolloClient apollo = SentryApolloBuilderExtensionsKt
36+
.sentryTracing(new ApolloClient.Builder())
3637
.serverUrl("https://your-api-host/")
3738
.build();
3839
```
@@ -118,3 +119,89 @@ val apollo = ApolloClient.builder()
118119
}
119120
.build()
120121
```
122+
123+
## GraphQL Client Errors
124+
125+
Once enabled, this feature will automatically capture GraphQL client errors such as bad operations and response codes, and report them to Sentry as error events. Each error event will contain the `request` and `response` data, including the `url`, `status_code`, and `data` (stringified `query`).
126+
127+
Sentry will group GraphQL client errors by the `operationName`, `operationType`, and `statusCode`, so that you can easily see the number of errors that happened for each.
128+
129+
This is an opt-in feature and can be enabled by setting the `captureFailedRequests` option to `true`:
130+
131+
```kotlin
132+
import com.apollographql.apollo3.ApolloClient
133+
import io.sentry.apollo3.sentryTracing
134+
135+
val apollo = ApolloClient.builder()
136+
.serverUrl("https://your-api-host/")
137+
.sentryTracing(captureFailedRequests = true)
138+
.build()
139+
```
140+
141+
By default, only GraphQL client errors with a response that includes the [errors](https://spec.graphql.org/October2021/#sec-Errors) array will be captured as error events.
142+
143+
GraphQL client errors from every target (`.*` regular expression) are automatically captured, but you can change this behavior by setting the `failedRequestTargets` option with either a regular expression or a plain `String`. A plain string must contain at least one of the items from the list. Plain strings don't have to be full matches, meaning the URL of a request is matched when it contains a string provided through the option.
144+
145+
```kotlin
146+
import com.apollographql.apollo3.ApolloClient
147+
import io.sentry.apollo3.sentryTracing
148+
149+
val apollo = ApolloClient.builder()
150+
.serverUrl("https://your-api-host/")
151+
.sentryTracing(captureFailedRequests = true, failedRequestTargets = listOf("myapi.com"))
152+
.build()
153+
```
154+
155+
By default, error events won't contain `Headers` or `Cookies`, but you can change this behavior by setting the `sendDefaultPii` option to `true`:
156+
157+
```xml {filename:AndroidManifest.xml}
158+
<application>
159+
<meta-data android:name="io.sentry.send-default-pii" android:value="true" />
160+
</application>
161+
```
162+
163+
Error events will contain the raw bodies of GraphQL requests and responses, which may include sensitive data. To avoid this, parameterize your queries using the [variables](https://spec.graphql.org/October2021/#sec-Language.Variables) field. [Relay](/product/relay) will then run [PII Data Scrubbing](/product/relay/#pii-data-scrubbing), automatically transforming values into `[Filtered]`.
164+
165+
Alternatively, you can customize the event and scrub the data yourself.
166+
167+
### Customize or Drop the Error Event
168+
169+
To customize or drop the error event, you'll need to do a [manual initialization](/platforms/android/configuration/manual-init/) of the Sentry Android SDK. The captured error event can then be customized or dropped with a `BeforeSendCallback`:
170+
171+
```kotlin
172+
import io.sentry.android.core.SentryAndroid
173+
import io.sentry.SentryOptions.BeforeSendCallback
174+
import com.apollographql.apollo3.api.http.HttpRequest
175+
import com.apollographql.apollo3.api.http.HttpResponse
176+
import io.sentry.TypeCheckHint.APOLLO_REQUEST
177+
import io.sentry.TypeCheckHint.APOLLO_RESPONSE
178+
179+
SentryAndroid.init(this) { options ->
180+
// Add a callback that will be used before the event is sent to Sentry.
181+
// With this callback, you can modify the event or, when returning null, also discard the event.
182+
options.beforeSend = BeforeSendCallback { event, hint ->
183+
val request = hint.getAs(APOLLO_REQUEST, HttpRequest::class.java)
184+
val response = hint.getAs(APOLLO_RESPONSE, HttpResponse::class.java)
185+
186+
// customize or drop the event
187+
event
188+
}
189+
}
190+
```
191+
192+
### Automatically and Manually Captured GraphQL Client Errors
193+
194+
When `captureFailedRequests` is enabled, some GraphQL client libraries will throw unchecked exceptions, such as the `ApolloException` and its implementations. This means the error event will be captured by both the GraphQL client library and the Sentry Android SDK. To avoid this, we recommend identifying these errors and using the `Sentry.captureException` method instead of capturing them manually:
195+
196+
```kotlin
197+
import io.sentry.Sentry
198+
import com.apollographql.apollo3.exception.ApolloException
199+
200+
try {
201+
// If this API call returns the `errors` array, it will be captured as an error event by the `SentryApollo3HttpInterceptor`.
202+
return apolloClient.query(LaunchDetailsQuery(launchId)).execute()
203+
} catch (e: ApolloException) {
204+
// Do not manually capture this exception to avoid duplicated error events.
205+
// Sentry.captureException(e)
206+
}
207+
```

0 commit comments

Comments
 (0)