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

# GraphQL Java Integration | Sentry for Servlet

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 22 of `graphql-java`, please refer to [GraphQL 22 docs](https://docs.sentry.io/platforms/java/guides/servlet/integrations/graphql22.md).

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

To install use:

**Gradle Plugin**

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

**Gradle**

```groovy
implementation 'io.sentry:sentry-graphql:8.42.0'
```

**\[Maven Plugin] pom.xml**

```xml
<plugin>
  <groupId>io.sentry</groupId>
  <artifactId>sentry-maven-plugin</artifactId>
  <version>0.11.0</version>
  <!-- Required to allow auto-install of Sentry SDK and Integrations -->
  <extensions>true</extensions>
</plugin>
```

**Maven**

```xml
<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-graphql</artifactId>
    <version>8.42.0</version>
</dependency>
```

**SBT**

```scala
libraryDependencies += "io.sentry" % "sentry-graphql" % "8.42.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/guides/servlet/integrations/graphql.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/guides/servlet/configuration/filtering.md)).

**java**

```java
import graphql.GraphQL;
import graphql.execution.SimpleDataFetcherExceptionHandler;
import io.sentry.graphql.SentryGenericDataFetcherExceptionHandler;
import io.sentry.graphql.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();
```

**kotlin**

```kotlin
import graphql.GraphQL
import graphql.execution.SimpleDataFetcherExceptionHandler
import io.sentry.graphql.SentryGenericDataFetcherExceptionHandler
import io.sentry.graphql.SentryInstrumentation

val defaultExceptionHandler = SimpleDataFetcherExceptionHandler()
val sentryExceptionHandler = SentryGenericDataFetcherExceptionHandler(defaultExceptionHandler)

val graphql = GraphQL.newGraphQL()
  .defaultDataFetcherExceptionHandler(sentryExceptionHandler)
	.instrumentation(SentryInstrumentation(
    // If you're not using our Spring integration, please provide NoOpSubscriptionHandler.getInstance() instead.
    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/guides/servlet/configuration/filtering.md)).

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

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

### [Modify or Drop Spans](https://docs.sentry.io/platforms/java/guides/servlet/integrations/graphql.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**

```java
import io.sentry.graphql.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();
```

**kotlin**

```kotlin
import io.sentry.graphql.SentryInstrumentation

import graphql.GraphQL

val graphql = GraphQL.newGraphQL()
  // ...
  .instrumentation(SentryInstrumentation({ span: ISpan, env: DataFetchingEnvironment, result: Any? ->
    if ("/shows" == env.executionStepInfo.path.segmentToString()) {
      span.setTag("tag-name", "tag-value")
    }
    span
  }, SentrySpringSubscriptionHandler(), true))
  .build()
```

## [Use with Netflix DGS](https://docs.sentry.io/platforms/java/guides/servlet/integrations/graphql.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**

```java
import com.netflix.graphql.dgs.exceptions.DefaultDataFetcherExceptionHandler;
import io.sentry.graphql.SentryGenericDataFetcherExceptionHandler;
import io.sentry.graphql.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());
  }
}
```

**kotlin**

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

@Configuration
class SentryConfiguration {

  @Bean
  fun sentryInstrumentation() = SentryInstrumentation(SentryDgsSubscriptionHandler(), true)

  @Bean
  fun sentryDataFetcherExceptionHandler() = SentryGenericDataFetcherExceptionHandler(DefaultDataFetcherExceptionHandler())
}
```
