Servlet Listener

The GraphQL Servlet library allows you to add a Servlet Listener for listening to the GraphQL request. It provides hooks into the servlet request execution (success, error, and finally):

public class MyServlet extends GraphQLHttpServlet {

  @Override
  protected GraphQLConfiguration getConfiguration() {
    return GraphQLConfiguration.with(createSchema())
      .with(queryInvoker)
      .with(Arrays.asList(listener))
      .build();
  }
}

Instrumentation

The Servlet Listener listens to the servlet request, but not to the GraphQL query execution. If you want to listen to that you should use the Instrumentation provided by GraphQL Java. You can extend SimpleInstrumentation to quickly create a custom implementation and use it when creating the GraphQLConfiguration.

See the GraphQL Java documentation for details on creating custom instrumentations.

public class MyServlet extends GraphQLHttpServlet {

  @Override
  protected GraphQLConfiguration getConfiguration() {
    GraphQLQueryInvoker queryInvoker = GraphQLQueryInvoker.newBuilder()
      .withExecutionStrategyProvider(executionStrategyProvider)
      .with(Arrays.asList(instrumentation))
      .build();
    return GraphQLConfiguration.with(createSchema())
      .with(queryInvoker)
      .build();
  }
}