---
title: "GraphQL Java 22 Integration"
description: "Learn how to capture exceptions and about the performance of queries executed with GraphQL Java (Version 22)."
url: https://docs.sentry.io/platforms/java/integrations/graphql22/
---

# GraphQL Java 22 Integration | Sentry for Java

Sentry's [GraphQL Java](https://github.com/graphql-java/graphql-java/) integration is provided through:

* `SentryGenericDataFetcherExceptionHandler`, which checks for exceptions thrown during data fetcher executions and then passes them to `SentryInstrumentation`.
* `SentryInstrumentation`, which creates spans around each data fetcher execution, captures exceptions, and adds breadcrumbs.

Our GraphQL integration can be configured automatically if you're using `spring-graphql` with either the `sentry-spring-boot-starter` or the `sentry-spring-boot-jakarta-starter` integration.

If you're using version 21 or older of `graphql-java`, please refer to [GraphQL docs](https://docs.sentry.io/platforms/java/integrations/graphql.md).

## [Install](https://docs.sentry.io/platforms/java/integrations/graphql22.md#install)

To install use:

```groovy
plugins {
  id "io.sentry.jvm.gradle" version "6.3.0"
}
```

For other dependency managers, check out the [central Maven repository](https://search.maven.org/artifact/io.sentry/sentry-graphql).

## [Set Up](https://docs.sentry.io/platforms/java/integrations/graphql22.md#set-up)

When building a `GraphQL` instance:

* set `defaultDataFetcherExceptionHandler` to an instance of `SentryGenericDataFetcherExceptionHandler` and pass the delegate that handles the exception to the constructor.
* set `instrumentation` to an instance of `SentryInstrumentation.`

You may want to filter some of the errors by using `beforeSend` or an `EventProcessor` (read more about [Filters](https://docs.sentry.io/platforms/java/configuration/filtering.md)).

```java
import graphql.GraphQL;
import graphql.execution.SimpleDataFetcherExceptionHandler;
import io.sentry.graphql.SentryGenericDataFetcherExceptionHandler;
import io.sentry.graphql22.SentryInstrumentation;

SimpleDataFetcherExceptionHandler defaultExceptionHandler = new SimpleDataFetcherExceptionHandler();
SentryGenericDataFetcherExceptionHandler sentryExceptionHandler = new SentryGenericDataFetcherExceptionHandler(defaultExceptionHandler);

GraphQL graphQL = GraphQL.newGraphQL(...)
    // ...
    .defaultDataFetcherExceptionHandler(sentryExceptionHandler)
    .instrumentation(new SentryInstrumentation(
      // If you're not using our Spring integration, please provide NoOpSubscriptionHandler.getInstance() instead.
      new SentrySpringSubscriptionHandler(),
      // Set this to false when using Spring WebMVC
      true
    ))
    .build();
```

The `SentryDataFetcherExceptionHandler` has been deprecated. Please upgrade to `SentryGenericDataFetcherExceptionHandler` and make sure `SentryInstrumentation` is configured to have more exceptions captured, more detailed exceptions, breadcrumbs, and better hub propagation. You may want to filter the errors by using `beforeSend` or an `EventProcessor` (read more about [Filters](https://docs.sentry.io/platforms/java/configuration/filtering.md)).

## [Capture Tracing Information](https://docs.sentry.io/platforms/java/integrations/graphql22.md#capture-tracing-information)

To be able to capture transactions, you have to first [set up tracing](https://docs.sentry.io/platforms/java/tracing.md).

### [Modify or Drop Spans](https://docs.sentry.io/platforms/java/integrations/graphql22.md#modify-or-drop-spans)

Spans created around requests can be modified by returning a modified Span, or dropped by returning `null`, using `SentryInstrumentation.BeforeSpanCallback` passed to `SentryInstrumentation`:

```java
import io.sentry.graphql22.SentryInstrumentation;

import graphql.GraphQL;

GraphQL graphQL = GraphQL.newGraphQL()
    // ...
    .instrumentation(new SentryInstrumentation((span, environment, result) -> {
      if ("/shows".equals(environment.getExecutionStepInfo().getPath().segmentToString())) {
        span.setTag("tag-name", "tag-value");
      }
      return span;
    }, new SentrySpringSubscriptionHandler(), true))
    .build();
```

## [Use with Netflix DGS](https://docs.sentry.io/platforms/java/integrations/graphql22.md#use-with-netflix-dgs)

[Netflix DGS](https://netflix.github.io/dgs) automatically detects and configures `Instrumentation` and `DataFetcherExceptionHandler` beans. To use the Sentry GraphQL integration, create `SentryGenericDataFetcherExceptionHandler` and `SentryInstrumentation` beans:

```java
import com.netflix.graphql.dgs.exceptions.DefaultDataFetcherExceptionHandler;
import io.sentry.graphql.SentryGenericDataFetcherExceptionHandler;
import io.sentry.graphql22.SentryInstrumentation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
class SentryConfiguration {

  @Bean
  SentryInstrumentation sentryInstrumentation() {
    return new SentryInstrumentation(new SentryDgsSubscriptionHandler(), true);
  }

  @Bean
  SentryGenericDataFetcherExceptionHandler sentryDataFetcherExceptionHandler() {
    // delegate to default Netflix DGS exception handler
    return new SentryGenericDataFetcherExceptionHandler(new DefaultDataFetcherExceptionHandler());
  }
}
```
