Skip to content

Commit

Permalink
Merge branch '1.3.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
bclozel committed Feb 5, 2025
2 parents 16aa33a + af74edb commit 0948e61
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,11 @@
import java.util.function.BiFunction;

import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.GraphQLContext;
import graphql.GraphQLError;
import graphql.GraphQLException;
import graphql.execution.ExecutionIdProvider;
import graphql.execution.instrumentation.dataloader.EmptyDataLoaderRegistryInstance;
import io.micrometer.context.ContextSnapshotFactory;
Expand Down Expand Up @@ -102,6 +105,12 @@ public final Mono<ExecutionGraphQlResponse> execute(ExecutionGraphQlRequest requ
ExecutionInput executionInputToUse = registerDataLoaders(executionInput);

return Mono.fromFuture(this.graphQlSource.graphQl().executeAsync(executionInputToUse))
.onErrorResume(GraphQLException.class, (exception) -> {
if (exception instanceof GraphQLError graphQLError) {
return Mono.just(ExecutionResult.newExecutionResult().addError(graphQLError).build());
}
return Mono.error(exception);
})
.map((result) -> new DefaultExecutionGraphQlResponse(executionInputToUse, result));
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@

import java.util.Map;

import graphql.ErrorType;
import org.dataloader.DataLoaderRegistry;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
Expand All @@ -28,14 +29,14 @@
import org.springframework.graphql.ExecutionGraphQlResponse;
import org.springframework.graphql.GraphQlSetup;
import org.springframework.graphql.TestExecutionRequest;
import org.springframework.graphql.support.DefaultExecutionGraphQlRequest;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Unit tests for {@link DefaultExecutionGraphQlService}.
*
* @author Rossen Stoyanchev
* @since 1.2.4
*/
public class DefaultExecutionGraphQlServiceTests {

Expand Down Expand Up @@ -64,4 +65,18 @@ void customDataLoaderRegistry() {
assertThat(dataLoaderRegistry.getDataLoaders()).hasSize(1);
}

@Test
void shouldHandleGraphQlErrors() {
GraphQlSource graphQlSource = GraphQlSetup.schemaContent("type Query { greeting: String }")
.queryFetcher("greeting", (env) -> "hi")
.toGraphQlSource();
DefaultExecutionGraphQlService graphQlService = new DefaultExecutionGraphQlService(graphQlSource);

ExecutionGraphQlRequest request = new DefaultExecutionGraphQlRequest("{ greeting }", "unknown",
null, null, "uniqueId", null);
ExecutionGraphQlResponse response = graphQlService.execute(request).block();
assertThat(response.getExecutionResult().getErrors()).singleElement()
.hasFieldOrPropertyWithValue("errorType", ErrorType.ValidationError);
}

}

0 comments on commit 0948e61

Please sign in to comment.