Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the library to handle Graphql errors #78

Closed
robertionut95b opened this issue Oct 27, 2023 · 4 comments
Closed

Extend the library to handle Graphql errors #78

robertionut95b opened this issue Oct 27, 2023 · 4 comments

Comments

@robertionut95b
Copy link

robertionut95b commented Oct 27, 2023

Hello!

I think the library would grow even more if considered to include GraphQL error handling. There are a few implementations already for java-graphql and Spring framework (DGS, Kickstart, Spring GraphQL, etc.) but not many offer a simple, highly configurable error handling setup.

The main contract from which all error handlers would start is similar to the current implementation:

import graphql.GraphQLError;

public interface GraphqlExceptionHandler {

    boolean canHandle(Throwable throwable);

    GraphQLError handleError(Throwable throwable);
}

The only specific feature would be to implement a specialized bean or extend an ExceptionHandler interface required for the library to work.

Example for DGS:

@Component
public class CustomDataFetchingExceptionHandler implements DataFetcherExceptionHandler {

   @Override
   public CompletableFuture<DataFetcherExceptionHandlerResult> handleException(DataFetcherExceptionHandlerParameters handlerParameters) {
      if (handlerParameters.getException() instanceof MyException) {
         Map<String, Object> debugInfo = new HashMap<>();
         debugInfo.put("somefield", "somevalue");

         GraphQLError graphqlError = TypedGraphQLError.newInternalErrorBuilder()
                 .message("This custom thing went wrong!")
                 .debugInfo(debugInfo)
                 .path(handlerParameters.getPath()).build();

         DataFetcherExceptionHandlerResult result = DataFetcherExceptionHandlerResult.newResult()
                 .error(graphqlError)
                 .build();

         return CompletableFuture.completedFuture(result);
      } else {
         return DataFetcherExceptionHandler.super.handleException(handlerParameters);
      }
   }
}

What do you think about this?

@wimdeblauwe
Copy link
Owner

I have no experience with GraphQL at all, so this is not something I need. But if you are willing to develop and maintain the feature in such a way that people not using GraphQL are not getting extra dependencies they don't want, then I am willing to review the PR.

@robertionut95b
Copy link
Author

A baseline implementation for Spring would require at least spring-graphql.

If this is no issue I might try to invest some time in a PR.

@wimdeblauwe
Copy link
Owner

I assume we can add it as an optional dependency

@robertionut95b
Copy link
Author

Issued #80, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants