graphql.execution2.result.ResultNodesUtil Maven / Gradle / Ivy
package graphql.execution2.result;
import graphql.Assert;
import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import graphql.GraphQLError;
import graphql.Internal;
import graphql.execution.NonNullableFieldWasNullError;
import graphql.execution.NonNullableFieldWasNullException;
import graphql.execution2.FetchedValueAnalysis;
import graphql.execution2.result.ObjectExecutionResultNode.UnresolvedObjectResultNode;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import static graphql.execution2.result.ExecutionResultNodePosition.index;
import static graphql.execution2.result.ExecutionResultNodePosition.key;
@Internal
public class ResultNodesUtil {
public static ExecutionResult toExecutionResult(ExecutionResultNode root) {
ExecutionResultData executionResultData = toDataImpl(root);
return ExecutionResultImpl.newExecutionResult()
.data(executionResultData.data)
.errors(executionResultData.errors)
.build();
}
private static class ExecutionResultData {
Object data;
List errors = new ArrayList<>();
public ExecutionResultData(Object data) {
this.data = data;
}
public ExecutionResultData(Object data, List errors) {
this.data = data;
this.errors = errors;
}
}
private static ExecutionResultData data(Object data) {
return new ExecutionResultData(data);
}
private static ExecutionResultData data(Object data, ExecutionResultNode executionResultNode) {
return new ExecutionResultData(data, executionResultNode.getFetchedValueAnalysis().getErrors());
}
private static ExecutionResultData data(Object data, List errors) {
return new ExecutionResultData(data, errors);
}
private static ExecutionResultData data(Object data, NonNullableFieldWasNullException exception) {
return new ExecutionResultData(data, Arrays.asList(new NonNullableFieldWasNullError(exception)));
}
private static ExecutionResultData toDataImpl(ExecutionResultNode root) {
if (root instanceof LeafExecutionResultNode) {
return root.getFetchedValueAnalysis().isNullValue() ? data(null) : data(((LeafExecutionResultNode) root).getValue(), root);
}
if (root instanceof ListExecutionResultNode) {
Optional childNonNullableException = ((ListExecutionResultNode) root).getChildNonNullableException();
if (childNonNullableException.isPresent()) {
return data(null, childNonNullableException.get());
}
List list = root.getChildren().stream().map(ResultNodesUtil::toDataImpl).collect(Collectors.toList());
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy