# GraphQL Java Integration

## Learn how to capture exceptions and about the performance of queries executed with GraphQL Java (Version <= 21).

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](/platforms/java/guides/spring/integrations/graphql22/).

## [Install](#install)

To install use:

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

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

## [Set Up](#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](/platforms/java/guides/spring/configuration/filtering/)).

```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();
```

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](/platforms/java/guides/spring/configuration/filtering/)).

## [Capture Tracing Information](#capture-tracing-information)

To be able to capture transactions, you have to first [set up tracing](/platforms/java/guides/spring/tracing/).

### [Modify or Drop Spans](#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.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();
```

## [Use with Netflix DGS](#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.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());
  }
}
```
