Polishing Javadoc and in security sample

This commit is contained in:
Rossen Stoyanchev
2021-07-05 14:31:58 +01:00
parent cc61c4baad
commit 16656d623f
8 changed files with 36 additions and 35 deletions

View File

@@ -2,8 +2,6 @@ package io.spring.sample.graphql;
import java.math.BigDecimal;
import reactor.core.publisher.Mono;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

View File

@@ -1,8 +1,12 @@
package io.spring.sample.graphql;
import java.util.Collections;
import java.util.List;
import graphql.GraphQLError;
import graphql.GraphqlErrorBuilder;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.graphql.execution.ErrorType;
import org.springframework.graphql.execution.SyncDataFetcherExceptionResolver;
import org.springframework.security.access.AccessDeniedException;
@@ -15,9 +19,6 @@ import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.util.Arrays;
import java.util.List;
@Component
public class SecurityDataFetcherExceptionResolver implements SyncDataFetcherExceptionResolver {
@@ -45,7 +46,7 @@ public class SecurityDataFetcherExceptionResolver implements SyncDataFetcherExce
}
private List<GraphQLError> unauthorized(DataFetchingEnvironment environment) {
return Arrays.asList(
return Collections.singletonList(
GraphqlErrorBuilder.newError(environment)
.errorType(ErrorType.UNAUTHORIZED)
.message("Unauthorized")
@@ -53,7 +54,7 @@ public class SecurityDataFetcherExceptionResolver implements SyncDataFetcherExce
}
private List<GraphQLError> forbidden(DataFetchingEnvironment environment) {
return Arrays.asList(
return Collections.singletonList(
GraphqlErrorBuilder.newError(environment)
.errorType(ErrorType.FORBIDDEN)
.message("Forbidden")

View File

@@ -15,7 +15,6 @@
*/
package io.spring.sample.graphql.greeting;
import java.util.Collections;
import java.util.Map;
import org.springframework.graphql.execution.ThreadLocalAccessor;
@@ -30,29 +29,23 @@ import org.springframework.web.context.request.RequestContextHolder;
@Component
public class RequestAttributesAccessor implements ThreadLocalAccessor {
private static final String ATTRIBUTES_KEY = RequestAttributesAccessor.class.getName() + ".requestAttributes";
private static final String KEY = RequestAttributesAccessor.class.getName();
@Override
public void extractValues(Map<String, Object> container) {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
if (attributes != null) {
container.put(ATTRIBUTES_KEY, attributes);
}
container.put(KEY, RequestContextHolder.getRequestAttributes());
}
@Override
public void restoreValues(Map<String, Object> values) {
RequestAttributes attributes = (RequestAttributes) values.get(ATTRIBUTES_KEY);
if (attributes != null) {
RequestContextHolder.setRequestAttributes(attributes);
if (values.containsKey(KEY)) {
RequestContextHolder.setRequestAttributes((RequestAttributes) values.get(KEY));
}
}
@Override
public void resetValues(Map<String, Object> values) {
if (values.get(ATTRIBUTES_KEY) != null) {
RequestContextHolder.resetRequestAttributes();
}
RequestContextHolder.resetRequestAttributes();
}
}

View File

@@ -31,8 +31,9 @@ import org.springframework.util.CollectionUtils;
/**
* Common representation for GraphQL request input. This can be converted to
* {@link ExecutionInput} via {@link #toExecutionInput()} and the {@code ExecutionInput}
* further customized via {@link #configureExecutionInput(BiFunction)}.
* {@link ExecutionInput} via {@link #toExecutionInput()} and the
* {@code ExecutionInput} further customized via
* {@link #configureExecutionInput(BiFunction)}.
*
* @author Rossen Stoyanchev
* @since 1.0.0

View File

@@ -93,6 +93,8 @@ import org.springframework.util.MultiValueMap;
* @see ReactiveQuerydslPredicateExecutor
* @see Predicate
* @see QuerydslBinderCustomizer
* @see <a href="https://docs.spring.io/spring-data/commons/docs/current/reference/html/#core.extensions.querydsl">
* Spring Data Querydsl extension</a>
*/
public abstract class QuerydslDataFetcher<T> {

View File

@@ -15,7 +15,8 @@
*/
/**
* Support for integrating Spring Data data fetchers.
* Spring Data support for GraphQL, including built-in Querydsl
* {@link graphql.schema.DataFetcher} implementations.
*/
@NonNullApi
@NonNullFields

View File

@@ -23,9 +23,12 @@ import graphql.schema.DataFetchingEnvironment;
import reactor.core.publisher.Mono;
/**
* Contract to resolve exceptions raised by {@link graphql.schema.DataFetcher}'s to
* {@code GraphQLError}'s to add to the GraphQL response. Implementations are typically
* declared as beans in Spring configuration and invoked in order until one emits a List.
* Contract for resolving exceptions from {@link graphql.schema.DataFetcher}'s
* to {@code GraphQLError}'s to be added to the GraphQL response, possibly also
* using Spring's {@link graphql.ErrorType} for the error category.
*
* <p>Implementations are typically declared as beans in Spring configuration
* and invoked in order until one emits a List.
*
* @author Rossen Stoyanchev
* @since 1.0.0
@@ -35,17 +38,19 @@ public interface DataFetcherExceptionResolver {
/**
* Resolve the given exception and return the error(s) to add to the response.
* <p>
* Implementations can use
* {@link graphql.GraphqlErrorBuilder#newError(DataFetchingEnvironment)} to create an
* error with the coordinates of the target field, and use {@link ErrorType} to
* specify a category for the error.
*
* <p>Implementations can use
* {@link graphql.GraphqlErrorBuilder#newError(DataFetchingEnvironment)} to
* create an error with the coordinates of the target field, and use
* {@link ErrorType} to specify a category for the error.
*
* @param exception the exception to resolve
* @param environment the environment for the invoked {@code DataFetcher}
* @return a {@code Mono} with errors to add to the GraphQL response; if the
* {@code Mono} completes with an empty List, the exception is resolved without any
* errors added to the response; if the {@code Mono} completes empty, without emitting
* a List, the exception remains unresolved and gives other resolvers a chance.
* @return a {@code Mono} with errors to add to the GraphQL response;
* if the {@code Mono} completes with an empty List, the exception is resolved
* without any errors added to the response; if the {@code Mono} completes
* empty, without emitting a List, the exception remains unresolved and gives
* other resolvers a chance.
*/
Mono<List<GraphQLError>> resolveException(Throwable exception, DataFetchingEnvironment environment);

View File

@@ -46,7 +46,7 @@ import org.springframework.util.Assert;
*/
class DefaultGraphQlSourceBuilder implements GraphQlSource.Builder {
private List<Resource> schemaResources = new ArrayList<>();
private final List<Resource> schemaResources = new ArrayList<>();
private RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring().build();