GraphQL Java Integration

Sentry's 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.

To install use:

Copied
plugins {
  id "io.sentry.jvm.gradle" version "4.4.1"
}

For other dependency managers, check out the central Maven repository.

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).

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

To be able to capture transactions, you have to first set up performance monitoring.

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

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

Netflix DGS automatically detects and configures Instrumentation and DataFetcherExceptionHandler beans. To use the Sentry GraphQL integration, create SentryGenericDataFetcherExceptionHandler and SentryInstrumentation beans:

Copied
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());
  }
}
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").