Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/java/graphql/analysis/QueryTraversal.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLUnmodifiedType;
import graphql.schema.SchemaUtil;
import graphql.util.TraversalControl;
import graphql.util.TraverserContext;

Expand All @@ -35,6 +34,7 @@
import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertShouldNeverHappen;
import static graphql.language.NodeTraverser.LeaveOrEnter.LEAVE;
import static graphql.schema.GraphQLTypeUtil.unwrapAll;

/**
* Helps to traverse (or reduce) a Document (or parts of it) and tracks at the same time the corresponding Schema types.
Expand All @@ -55,7 +55,6 @@ public class QueryTraversal {

private final ConditionalNodes conditionalNodes = new ConditionalNodes();
private final ValuesResolver valuesResolver = new ValuesResolver();
private final SchemaUtil schemaUtil = new SchemaUtil();
private final ChildrenOfSelectionProvider childrenOfSelectionProvider;
private final GraphQLObjectType rootParentType;

Expand Down Expand Up @@ -268,7 +267,7 @@ public TraversalControl visitField(Field field, TraverserContext<Node> context)

preOrderCallback.visitField(environment);

GraphQLUnmodifiedType unmodifiedType = schemaUtil.getUnmodifiedType(fieldDefinition.getType());
GraphQLUnmodifiedType unmodifiedType = unwrapAll(fieldDefinition.getType());
QueryTraversalContext fieldEnv = (unmodifiedType instanceof GraphQLCompositeType)
? new QueryTraversalContext((GraphQLCompositeType) unmodifiedType, environment, field)
: new QueryTraversalContext(null, environment, field);// Terminal (scalar) node, EMPTY FRAME
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import static graphql.execution.FieldValueInfo.CompleteValueType.OBJECT;
import static graphql.execution.FieldValueInfo.CompleteValueType.SCALAR;
import static graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment;
import static graphql.schema.GraphQLTypeUtil.isList;
import static java.util.concurrent.CompletableFuture.completedFuture;

/**
Expand Down Expand Up @@ -166,7 +167,7 @@ protected ExecutionStrategy(DataFetcherExceptionHandler dataFetcherExceptionHand
* @throws NonNullableFieldWasNullException in the future if a non null field resolves to a null value
*/
protected CompletableFuture<ExecutionResult> resolveField(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
return resolveFieldWithInfo(executionContext,parameters).thenCompose(FieldValueInfo::getFieldValue);
return resolveFieldWithInfo(executionContext, parameters).thenCompose(FieldValueInfo::getFieldValue);
}

/**
Expand Down Expand Up @@ -391,7 +392,7 @@ protected FieldValueInfo completeValue(ExecutionContext executionContext, Execut
if (result == null) {
fieldValue = completeValueForNull(parameters);
return FieldValueInfo.newFieldValueInfo(NULL).fieldValue(fieldValue).build();
} else if (fieldType instanceof GraphQLList) {
} else if (isList(fieldType)) {
return completeValueForList(executionContext, parameters, result);
} else if (fieldType instanceof GraphQLScalarType) {
fieldValue = completeValueForScalar(executionContext, parameters, (GraphQLScalarType) fieldType, result);
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/graphql/execution/ExecutionTypeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
import java.util.Stack;

import static graphql.Assert.assertNotNull;
import static graphql.schema.GraphQLNonNull.nonNull;
import static graphql.schema.GraphQLTypeUtil.isList;
import static graphql.schema.GraphQLTypeUtil.isNonNull;
import static graphql.schema.GraphQLTypeUtil.isNotWrapped;
import static graphql.schema.GraphQLTypeUtil.unwrapAll;
import static graphql.schema.GraphQLTypeUtil.unwrapOne;

/**
* As the graphql query executes, it forms a hierarchy from parent fields (and their type) to their child fields (and their type)
Expand Down Expand Up @@ -88,7 +94,7 @@ public boolean isNonNullType() {
* @return true if the type is a list
*/
public boolean isListType() {
return type instanceof GraphQLList;
return isList(type);
}

/**
Expand Down Expand Up @@ -134,13 +140,10 @@ public static Stack<GraphQLType> unwrapType(GraphQLType type) {
Stack<GraphQLType> decoration = new Stack<>();
while (true) {
decoration.push(type);
if (type instanceof GraphQLNonNull) {
type = ((GraphQLNonNull) type).getWrappedType();
} else if (type instanceof GraphQLList) {
type = ((GraphQLList) type).getWrappedType();
} else {
if (isNotWrapped(type)) {
break;
}
type = unwrapOne(type);
}
return decoration;
}
Expand All @@ -154,7 +157,7 @@ public static Stack<GraphQLType> unwrapType(GraphQLType type) {
* @return the underlying raw type with {@link GraphQLNonNull} and {@link GraphQLList} type wrappers removed
*/
public static GraphQLType unwrapBaseType(GraphQLType type) {
return unwrapType(type).pop();
return unwrapAll(type);
}


Expand All @@ -165,7 +168,7 @@ public String toAst() {
// type info unwraps non nulls - we need it back here
GraphQLType type = this.getType();
if (isNonNullType()) {
type = GraphQLNonNull.nonNull(type);
type = nonNull(type);
}
return GraphQLTypeUtil.getUnwrappedTypeName(type);

Expand All @@ -184,8 +187,8 @@ public String toString() {

private static GraphQLType unwrapNonNull(GraphQLType type) {
// its possible to have non nulls wrapping non nulls of things but it must end at some point
while (type instanceof GraphQLNonNull) {
type = ((GraphQLNonNull) type).getWrappedType();
while (isNonNull(type)) {
type = unwrapOne(type);
}
return type;
}
Expand Down Expand Up @@ -231,7 +234,7 @@ public Builder path(ExecutionPath executionPath) {


public ExecutionTypeInfo build() {
if (type instanceof GraphQLNonNull) {
if (isNonNull(type)) {
return new ExecutionTypeInfo(unwrapNonNull(type), fieldDefinition, executionPath, parentType, true);
}
return new ExecutionTypeInfo(type, fieldDefinition, executionPath, parentType, false);
Expand Down
46 changes: 22 additions & 24 deletions src/main/java/graphql/execution/ValuesResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;
Expand All @@ -31,6 +30,9 @@
import java.util.stream.Collectors;

import static graphql.Assert.assertShouldNeverHappen;
import static graphql.schema.GraphQLTypeUtil.isList;
import static graphql.schema.GraphQLTypeUtil.isNonNull;
import static graphql.schema.GraphQLTypeUtil.unwrapOne;
import static graphql.schema.visibility.DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY;

@Internal
Expand Down Expand Up @@ -78,7 +80,7 @@ public Map<String, Object> coerceArgumentValues(GraphQLSchema schema, List<Varia
// 3.e.i
Object coercedValue = coerceValueAst(fieldVisibility, variableType, variableDefinition.getDefaultValue(), null);
coercedValues.put(variableName, coercedValue);
} else if (isNonNullType(variableType)) {
} else if (isNonNull(variableType)) {
// 3.e.ii
throw new NonNullableValueCoercedAsNullException(variableDefinition, variableType);
}
Expand All @@ -103,10 +105,6 @@ private Object getVariableValue(GraphqlFieldVisibility fieldVisibility, Variable
return coerceValue(fieldVisibility, variableDefinition, variableDefinition.getName(), variableType, value);
}

private boolean isNonNullType(GraphQLType variableType) {
return variableType instanceof GraphQLNonNull;
}

public Map<String, Object> getArgumentValues(List<GraphQLArgument> argumentTypes, List<Argument> arguments, Map<String, Object> variables) {
return getArgumentValues(DEFAULT_FIELD_VISIBILITY, argumentTypes, arguments, variables);
}
Expand Down Expand Up @@ -149,9 +147,9 @@ private Map<String, Argument> argumentMap(List<Argument> arguments) {
@SuppressWarnings("unchecked")
private Object coerceValue(GraphqlFieldVisibility fieldVisibility, VariableDefinition variableDefinition, String inputName, GraphQLType graphQLType, Object value) {
try {
if (graphQLType instanceof GraphQLNonNull) {
if (isNonNull(graphQLType)) {
Object returnValue =
coerceValue(fieldVisibility, variableDefinition, inputName, ((GraphQLNonNull) graphQLType).getWrappedType(), value);
coerceValue(fieldVisibility, variableDefinition, inputName, unwrapOne(graphQLType), value);
if (returnValue == null) {
throw new NonNullableValueCoercedAsNullException(variableDefinition, graphQLType);
}
Expand All @@ -173,22 +171,22 @@ private Object coerceValue(GraphqlFieldVisibility fieldVisibility, VariableDefin
return coerceValueForInputObjectType(fieldVisibility, variableDefinition, (GraphQLInputObjectType) graphQLType, (Map<String, Object>) value);
} else {
throw new CoercingParseValueException(
"Expected type 'Map' but was '" + value.getClass().getSimpleName() +
"'. Variables for input objects must be an instance of type 'Map'."
"Expected type 'Map' but was '" + value.getClass().getSimpleName() +
"'. Variables for input objects must be an instance of type 'Map'."
);
}
} else {
return assertShouldNeverHappen("unhandled type %s", graphQLType);
}
} catch (CoercingParseValueException e) {
if (e.getLocations() != null) {
throw e;
throw e;
}

throw new CoercingParseValueException(
"Variable '" + inputName + "' has an invalid value. " + e.getMessage(),
e.getCause(),
variableDefinition.getSourceLocation()
"Variable '" + inputName + "' has an invalid value. " + e.getMessage(),
e.getCause(),
variableDefinition.getSourceLocation()
);
}
}
Expand All @@ -205,19 +203,19 @@ private Object coerceValueForInputObjectType(GraphqlFieldVisibility fieldVisibil

for (GraphQLInputObjectField inputField : fields) {
if (input.containsKey(inputField.getName()) || alwaysHasValue(inputField)) {
Object value = coerceValue(fieldVisibility, variableDefinition,
inputField.getName(),
inputField.getType(),
input.get(inputField.getName()));
result.put(inputField.getName(), value == null ? inputField.getDefaultValue() : value);
Object value = coerceValue(fieldVisibility, variableDefinition,
inputField.getName(),
inputField.getType(),
input.get(inputField.getName()));
result.put(inputField.getName(), value == null ? inputField.getDefaultValue() : value);
}
}
return result;
}

private boolean alwaysHasValue(GraphQLInputObjectField inputField) {
return inputField.getDefaultValue() != null
|| inputField.getType() instanceof GraphQLNonNull;
|| isNonNull(inputField.getType());
}

private Object coerceValueForScalar(GraphQLScalarType graphQLScalarType, Object value) {
Expand Down Expand Up @@ -250,16 +248,16 @@ private Object coerceValueAst(GraphqlFieldVisibility fieldVisibility, GraphQLTyp
if (type instanceof GraphQLScalarType) {
return parseLiteral(inputValue, ((GraphQLScalarType) type).getCoercing());
}
if (type instanceof GraphQLNonNull) {
return coerceValueAst(fieldVisibility, ((GraphQLNonNull) type).getWrappedType(), inputValue, variables);
if (isNonNull(type)) {
return coerceValueAst(fieldVisibility, unwrapOne(type), inputValue, variables);
}
if (type instanceof GraphQLInputObjectType) {
return coerceValueAstForInputObject(fieldVisibility, (GraphQLInputObjectType) type, (ObjectValue) inputValue, variables);
}
if (type instanceof GraphQLEnumType) {
return parseLiteral(inputValue, ((GraphQLEnumType) type).getCoercing());
}
if (type instanceof GraphQLList) {
if (isList(type)) {
return coerceValueAstForList(fieldVisibility, (GraphQLList) type, inputValue, variables);
}
return null;
Expand Down Expand Up @@ -328,7 +326,7 @@ private Object coerceValueAstForInputObject(GraphqlFieldVisibility fieldVisibili
}

private void assertNonNullInputField(GraphQLInputObjectField inputTypeField) {
if (inputTypeField.getType() instanceof GraphQLNonNull) {
if (isNonNull(inputTypeField.getType())) {
throw new NonNullableValueCoercedAsNullException(inputTypeField);
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/graphql/introspection/Introspection.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import graphql.schema.GraphQLInputType;
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLModifiedType;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLTypeUtil;
import graphql.schema.GraphQLUnionType;
import graphql.schema.visibility.GraphqlFieldVisibility;

Expand Down Expand Up @@ -200,7 +202,7 @@ private static String print(Object value, GraphQLInputType type) {
public static final DataFetcher possibleTypesFetcher = environment -> {
Object type = environment.getSource();
if (type instanceof GraphQLInterfaceType) {
return environment.getGraphQLSchema().getImplementations((GraphQLInterfaceType)type);
return environment.getGraphQLSchema().getImplementations((GraphQLInterfaceType) type);
}
if (type instanceof GraphQLUnionType) {
return ((GraphQLUnionType) type).getTypes();
Expand Down Expand Up @@ -236,11 +238,8 @@ private static String print(Object value, GraphQLInputType type) {

public static final DataFetcher OfTypeFetcher = environment -> {
Object type = environment.getSource();
if (type instanceof GraphQLList) {
return ((GraphQLList) type).getWrappedType();
}
if (type instanceof GraphQLNonNull) {
return ((GraphQLNonNull) type).getWrappedType();
if (type instanceof GraphQLModifiedType) {
return GraphQLTypeUtil.unwrapOne((GraphQLModifiedType) type);
}
return null;
};
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/graphql/language/AstValueHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.util.List;
import java.util.Map;

import static graphql.schema.GraphQLTypeUtil.isList;
import static graphql.schema.GraphQLTypeUtil.isNonNull;

public class AstValueHelper {

/**
Expand Down Expand Up @@ -56,13 +59,13 @@ public static Value astFromValue(Object value, GraphQLType type) {
return null;
}

if (type instanceof GraphQLNonNull) {
if (isNonNull(type)) {
return handleNonNull(value, (GraphQLNonNull) type);
}

// Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
// the value is not an array, convert the value using the list's item type.
if (type instanceof GraphQLList) {
if (isList(type)) {
return handleList(value, (GraphQLList) type);
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/graphql/relay/Relay.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLOutputType;
import graphql.schema.TypeResolver;
Expand All @@ -26,7 +24,7 @@
import static graphql.schema.GraphQLInputObjectField.newInputObjectField;
import static graphql.schema.GraphQLInputObjectType.newInputObject;
import static graphql.schema.GraphQLInterfaceType.newInterface;
import static graphql.schema.GraphQLList.*;
import static graphql.schema.GraphQLList.list;
import static graphql.schema.GraphQLNonNull.nonNull;
import static graphql.schema.GraphQLObjectType.newObject;

Expand Down
Loading