graphql.execution2.FetchedValueAnalyzer Maven / Gradle / Ivy
package graphql.execution2;
import graphql.Internal;
import graphql.SerializationError;
import graphql.TypeMismatchError;
import graphql.UnresolvedTypeError;
import graphql.execution.ExecutionContext;
import graphql.execution.ExecutionStepInfo;
import graphql.execution.ExecutionStepInfoFactory;
import graphql.execution.FieldCollector;
import graphql.execution.FieldCollectorParameters;
import graphql.execution.NonNullableFieldWasNullException;
import graphql.execution.ResolveType;
import graphql.execution.UnresolvedTypeException;
import graphql.language.Field;
import graphql.schema.CoercingSerializeException;
import graphql.schema.GraphQLEnumType;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLType;
import graphql.util.FpKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static graphql.execution.FieldCollectorParameters.newParameters;
import static graphql.execution2.FetchedValueAnalysis.FetchedValueType.ENUM;
import static graphql.execution2.FetchedValueAnalysis.FetchedValueType.LIST;
import static graphql.execution2.FetchedValueAnalysis.FetchedValueType.OBJECT;
import static graphql.execution2.FetchedValueAnalysis.FetchedValueType.SCALAR;
import static graphql.execution2.FetchedValueAnalysis.newFetchedValueAnalysis;
import static graphql.schema.GraphQLTypeUtil.isList;
import static java.util.Collections.singletonList;
@Internal
public class FetchedValueAnalyzer {
private final ExecutionContext executionContext;
ResolveType resolveType;
FieldCollector fieldCollector = new FieldCollector();
ExecutionStepInfoFactory executionInfoFactory;
public FetchedValueAnalyzer(ExecutionContext executionContext) {
this.executionContext = executionContext;
this.resolveType = new ResolveType();
this.executionInfoFactory = new ExecutionStepInfoFactory();
}
private static final Logger log = LoggerFactory.getLogger(FetchedValueAnalyzer.class);
/*
* scalar: the value, null and/or error
* enum: same as scalar
* list: list of X: X can be list again, list of scalars or enum or objects
*/
public FetchedValueAnalysis analyzeFetchedValue(Object toAnalyze, String name, List field, ExecutionStepInfo executionInfo) throws NonNullableFieldWasNullException {
GraphQLType fieldType = executionInfo.getUnwrappedNonNullType();
FetchedValueAnalysis result = null;
if (isList(fieldType)) {
result = analyzeList(toAnalyze, name, executionInfo);
} else if (fieldType instanceof GraphQLScalarType) {
result = analyzeScalarValue(toAnalyze, name, (GraphQLScalarType) fieldType, executionInfo);
} else if (fieldType instanceof GraphQLEnumType) {
result = analyzeEnumValue(toAnalyze, name, (GraphQLEnumType) fieldType, executionInfo);
}
if (result != null) {
result.setExecutionStepInfo(executionInfo);
return result;
}
// when we are here, we have a complex type: Interface, Union or Object
// and we must go deeper
//
GraphQLObjectType resolvedObjectType;
try {
if (toAnalyze == null) {
return newFetchedValueAnalysis(OBJECT)
.name(name)
.executionStepInfo(executionInfo)
.nullValue()
.build();
}
resolvedObjectType = resolveType.resolveType(executionContext, field.get(0), toAnalyze, executionInfo.getArguments(), fieldType);
return analyzeObject(toAnalyze, name, resolvedObjectType, executionInfo);
} catch (UnresolvedTypeException ex) {
return handleUnresolvedTypeProblem(name, executionInfo, ex);
}
}
private FetchedValueAnalysis handleUnresolvedTypeProblem(String name, ExecutionStepInfo executionInfo, UnresolvedTypeException e) {
UnresolvedTypeError error = new UnresolvedTypeError(executionInfo.getPath(), executionInfo, e);
return newFetchedValueAnalysis(OBJECT)
.name(name)
.nullValue()
.error(error)
.build();
}
private FetchedValueAnalysis analyzeList(Object toAnalyze, String name, ExecutionStepInfo executionInfo) {
if (toAnalyze == null) {
return newFetchedValueAnalysis(LIST)
.name(name)
.nullValue()
.build();
}
if (toAnalyze.getClass().isArray() || toAnalyze instanceof Iterable) {
Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy