|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.graphql.data.federation; |
| 18 | + |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.LinkedHashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.concurrent.CompletionException; |
| 27 | + |
| 28 | +import com.apollographql.federation.graphqljava._Entity; |
| 29 | +import graphql.GraphQLError; |
| 30 | +import graphql.GraphqlErrorBuilder; |
| 31 | +import graphql.execution.DataFetcherResult; |
| 32 | +import graphql.execution.ExecutionStepInfo; |
| 33 | +import graphql.schema.DataFetcher; |
| 34 | +import graphql.schema.DataFetchingEnvironment; |
| 35 | +import graphql.schema.DelegatingDataFetchingEnvironment; |
| 36 | +import reactor.core.publisher.Mono; |
| 37 | + |
| 38 | +import org.springframework.graphql.data.method.annotation.support.HandlerDataFetcherExceptionResolver; |
| 39 | +import org.springframework.graphql.execution.ErrorType; |
| 40 | +import org.springframework.lang.Nullable; |
| 41 | + |
| 42 | +/** |
| 43 | + * DataFetcher that handles the "_entities" query by invoking |
| 44 | + * {@link EntityHandlerMethod}s. |
| 45 | + * |
| 46 | + * @author Rossen Stoyanchev |
| 47 | + * @since 1.3 |
| 48 | + * @see com.apollographql.federation.graphqljava.SchemaTransformer#fetchEntities(DataFetcher) |
| 49 | + */ |
| 50 | +final class EntitiesDataFetcher implements DataFetcher<Mono<DataFetcherResult<List<Object>>>> { |
| 51 | + |
| 52 | + private final Map<String, EntityHandlerMethod> handlerMethods; |
| 53 | + |
| 54 | + private final HandlerDataFetcherExceptionResolver exceptionResolver; |
| 55 | + |
| 56 | + |
| 57 | + public EntitiesDataFetcher( |
| 58 | + Map<String, EntityHandlerMethod> handlerMethods, HandlerDataFetcherExceptionResolver resolver) { |
| 59 | + |
| 60 | + this.handlerMethods = new LinkedHashMap<>(handlerMethods); |
| 61 | + this.exceptionResolver = resolver; |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + @Override |
| 66 | + public Mono<DataFetcherResult<List<Object>>> get(DataFetchingEnvironment environment) { |
| 67 | + List<Map<String, Object>> representations = environment.getArgument(_Entity.argumentName); |
| 68 | + |
| 69 | + List<Mono<Object>> monoList = new ArrayList<>(); |
| 70 | + for (int index = 0; index < representations.size(); index++) { |
| 71 | + Map<String, Object> map = representations.get(index); |
| 72 | + if (!(map.get("__typename") instanceof String typename)) { |
| 73 | + Exception ex = new RepresentationException(map, "Missing \"__typename\" argument"); |
| 74 | + monoList.add(resolveException(ex, environment, null, index)); |
| 75 | + continue; |
| 76 | + } |
| 77 | + EntityHandlerMethod handlerMethod = this.handlerMethods.get(typename); |
| 78 | + if (handlerMethod == null) { |
| 79 | + Exception ex = new RepresentationException(map, "No entity fetcher"); |
| 80 | + monoList.add(resolveException(ex, environment, null, index)); |
| 81 | + continue; |
| 82 | + } |
| 83 | + monoList.add(invokeResolver(environment, handlerMethod, map, index)); |
| 84 | + } |
| 85 | + return Mono.zip(monoList, Arrays::asList).map(EntitiesDataFetcher::toDataFetcherResult); |
| 86 | + } |
| 87 | + |
| 88 | + private Mono<Object> invokeResolver( |
| 89 | + DataFetchingEnvironment env, EntityHandlerMethod handlerMethod, Map<String, Object> map, int index) { |
| 90 | + |
| 91 | + return handlerMethod.getEntity(env, map, index) |
| 92 | + .switchIfEmpty(Mono.error(new RepresentationNotResolvedException(map, handlerMethod))) |
| 93 | + .onErrorResume(ex -> resolveException(ex, env, handlerMethod, index)); |
| 94 | + } |
| 95 | + |
| 96 | + private Mono<Object> resolveException( |
| 97 | + Throwable ex, DataFetchingEnvironment env, @Nullable EntityHandlerMethod handlerMethod, int index) { |
| 98 | + |
| 99 | + Throwable theEx = (ex instanceof CompletionException ? ex.getCause() : ex); |
| 100 | + DataFetchingEnvironment theEnv = new EntityDataFetchingEnvironment(env, index); |
| 101 | + Object handler = (handlerMethod != null ? handlerMethod.getBean() : null); |
| 102 | + |
| 103 | + return this.exceptionResolver.resolveException(theEx, theEnv, handler) |
| 104 | + .map(ErrorContainer::new) |
| 105 | + .switchIfEmpty(Mono.fromCallable(() -> createDefaultError(theEx, theEnv))) |
| 106 | + .cast(Object.class); |
| 107 | + } |
| 108 | + |
| 109 | + private ErrorContainer createDefaultError(Throwable ex, DataFetchingEnvironment env) { |
| 110 | + |
| 111 | + ErrorType errorType = (ex instanceof RepresentationException representationEx ? |
| 112 | + representationEx.getErrorType() : ErrorType.INTERNAL_ERROR); |
| 113 | + |
| 114 | + return new ErrorContainer(GraphqlErrorBuilder.newError(env) |
| 115 | + .errorType(errorType) |
| 116 | + .message(ex.getMessage()) |
| 117 | + .build()); |
| 118 | + } |
| 119 | + |
| 120 | + private static DataFetcherResult<List<Object>> toDataFetcherResult(List<Object> entities) { |
| 121 | + List<GraphQLError> errors = new ArrayList<>(); |
| 122 | + for (int i = 0; i < entities.size(); i++) { |
| 123 | + Object entity = entities.get(i); |
| 124 | + if (entity instanceof ErrorContainer errorContainer) { |
| 125 | + errors.addAll(errorContainer.errors()); |
| 126 | + entities.set(i, null); |
| 127 | + } |
| 128 | + } |
| 129 | + return DataFetcherResult.<List<Object>>newResult().data(entities).errors(errors).build(); |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + private static class EntityDataFetchingEnvironment extends DelegatingDataFetchingEnvironment { |
| 134 | + |
| 135 | + private final ExecutionStepInfo executionStepInfo; |
| 136 | + |
| 137 | + public EntityDataFetchingEnvironment(DataFetchingEnvironment env, int index) { |
| 138 | + super(env); |
| 139 | + this.executionStepInfo = ExecutionStepInfo.newExecutionStepInfo(env.getExecutionStepInfo()) |
| 140 | + .path(env.getExecutionStepInfo().getPath().segment(index)) |
| 141 | + .build(); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public ExecutionStepInfo getExecutionStepInfo() { |
| 146 | + return this.executionStepInfo; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + private record ErrorContainer(List<GraphQLError> errors) { |
| 152 | + |
| 153 | + ErrorContainer(GraphQLError error) { |
| 154 | + this(Collections.singletonList(error)); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments