|
| 1 | +--- |
| 2 | +title: GraphQL Operation Tracking |
| 3 | +sidebar_order: 14 |
| 4 | +description: "Enable tracking of GraphQL operation names in HTTP breadcrumbs and failed request events" |
| 5 | +--- |
| 6 | + |
| 7 | +When enabled, the SDK extracts the GraphQL operation name from HTTP requests that have `Content-Type: application/json` and contain a JSON body with an `operationName` field. The operation name is then attached to: |
| 8 | + |
| 9 | +- HTTP breadcrumbs as `graphql_operation_name` (when network breadcrumbs are enabled) |
| 10 | +- Failed request events in the context as `graphql.operation_name` (when HTTP client error capture is enabled) |
| 11 | + |
| 12 | +This feature is disabled by default. To enable it: |
| 13 | + |
| 14 | +```swift {tabTitle:Swift} |
| 15 | +import Sentry |
| 16 | + |
| 17 | +SentrySDK.start { options in |
| 18 | + options.dsn = "___PUBLIC_DSN___" |
| 19 | + options.enableGraphQLOperationTracking = true |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +```objc {tabTitle:Objective-C} |
| 24 | +@import Sentry; |
| 25 | + |
| 26 | +[SentrySDK startWithConfigureOptions:^(SentryOptions *options) { |
| 27 | + options.dsn = @"___PUBLIC_DSN___"; |
| 28 | + options.enableGraphQLOperationTracking = YES; |
| 29 | +}]; |
| 30 | +``` |
| 31 | +
|
| 32 | +## How It Works |
| 33 | +
|
| 34 | +The SDK automatically detects GraphQL requests by checking: |
| 35 | +
|
| 36 | +1. The HTTP request has `Content-Type: application/json` header |
| 37 | +2. The request body contains valid JSON |
| 38 | +3. The JSON body includes an `operationName` field |
| 39 | +
|
| 40 | +When these conditions are met, the SDK extracts the `operationName` value and includes it in: |
| 41 | +
|
| 42 | +- **HTTP Breadcrumbs**: When network breadcrumbs are enabled (default), the GraphQL operation name is added as `graphql_operation_name` in the breadcrumb data. This helps you identify which GraphQL operations were executed leading up to an error. |
| 43 | +
|
| 44 | +- **Failed Request Events**: When HTTP client error capture is enabled (default since version 8.0.0), the GraphQL operation name is included in the event context as `graphql.operation_name`. This makes it easier to identify which GraphQL operation failed when debugging HTTP client errors. |
| 45 | +
|
| 46 | +## Example GraphQL Request |
| 47 | +
|
| 48 | +The SDK extracts the operation name from requests like this: |
| 49 | +
|
| 50 | +```json |
| 51 | +{ |
| 52 | + "operationName": "GetUserProfile", |
| 53 | + "variables": { |
| 54 | + "id": "1234" |
| 55 | + }, |
| 56 | + "query": "query GetUserProfile($id: ID!) { user(id: $id) { name email } }" |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +In this example, `GetUserProfile` would be extracted and attached to breadcrumbs and error events. |
| 61 | + |
| 62 | +## Related Features |
| 63 | + |
| 64 | +- <PlatformLink to="/configuration/http-client-errors/">HTTP Client Errors</PlatformLink> - Learn about capturing HTTP client errors |
| 65 | +- <PlatformLink to="/enriching-events/breadcrumbs/">Breadcrumbs</PlatformLink> - Learn about breadcrumb tracking |
0 commit comments