Skip to content

Commit 03ae4c2

Browse files
authored
Consolidates the isNonNull and isList for consistency and readability (graphql-java#1095)
* Consolidates the isNonNull and isList for consistency and readability * PR fix ups plus moved isLeaf and isInput to the GraphqlTypeUtils
1 parent a683b9e commit 03ae4c2

29 files changed

Lines changed: 286 additions & 205 deletions

src/main/java/graphql/analysis/QueryTraversal.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import graphql.schema.GraphQLObjectType;
2323
import graphql.schema.GraphQLSchema;
2424
import graphql.schema.GraphQLUnmodifiedType;
25-
import graphql.schema.SchemaUtil;
2625
import graphql.util.TraversalControl;
2726
import graphql.util.TraverserContext;
2827

@@ -35,6 +34,7 @@
3534
import static graphql.Assert.assertNotNull;
3635
import static graphql.Assert.assertShouldNeverHappen;
3736
import static graphql.language.NodeTraverser.LeaveOrEnter.LEAVE;
37+
import static graphql.schema.GraphQLTypeUtil.unwrapAll;
3838

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

5656
private final ConditionalNodes conditionalNodes = new ConditionalNodes();
5757
private final ValuesResolver valuesResolver = new ValuesResolver();
58-
private final SchemaUtil schemaUtil = new SchemaUtil();
5958
private final ChildrenOfSelectionProvider childrenOfSelectionProvider;
6059
private final GraphQLObjectType rootParentType;
6160

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

269268
preOrderCallback.visitField(environment);
270269

271-
GraphQLUnmodifiedType unmodifiedType = schemaUtil.getUnmodifiedType(fieldDefinition.getType());
270+
GraphQLUnmodifiedType unmodifiedType = unwrapAll(fieldDefinition.getType());
272271
QueryTraversalContext fieldEnv = (unmodifiedType instanceof GraphQLCompositeType)
273272
? new QueryTraversalContext((GraphQLCompositeType) unmodifiedType, environment, field)
274273
: new QueryTraversalContext(null, environment, field);// Terminal (scalar) node, EMPTY FRAME

src/main/java/graphql/execution/ExecutionStrategy.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import static graphql.execution.FieldValueInfo.CompleteValueType.OBJECT;
5555
import static graphql.execution.FieldValueInfo.CompleteValueType.SCALAR;
5656
import static graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment;
57+
import static graphql.schema.GraphQLTypeUtil.isList;
5758
import static java.util.concurrent.CompletableFuture.completedFuture;
5859

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

172173
/**
@@ -391,7 +392,7 @@ protected FieldValueInfo completeValue(ExecutionContext executionContext, Execut
391392
if (result == null) {
392393
fieldValue = completeValueForNull(parameters);
393394
return FieldValueInfo.newFieldValueInfo(NULL).fieldValue(fieldValue).build();
394-
} else if (fieldType instanceof GraphQLList) {
395+
} else if (isList(fieldType)) {
395396
return completeValueForList(executionContext, parameters, result);
396397
} else if (fieldType instanceof GraphQLScalarType) {
397398
fieldValue = completeValueForScalar(executionContext, parameters, (GraphQLScalarType) fieldType, result);

src/main/java/graphql/execution/ExecutionTypeInfo.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
import java.util.Stack;
1212

1313
import static graphql.Assert.assertNotNull;
14+
import static graphql.schema.GraphQLNonNull.nonNull;
15+
import static graphql.schema.GraphQLTypeUtil.isList;
16+
import static graphql.schema.GraphQLTypeUtil.isNonNull;
17+
import static graphql.schema.GraphQLTypeUtil.isNotWrapped;
18+
import static graphql.schema.GraphQLTypeUtil.unwrapAll;
19+
import static graphql.schema.GraphQLTypeUtil.unwrapOne;
1420

1521
/**
1622
* As the graphql query executes, it forms a hierarchy from parent fields (and their type) to their child fields (and their type)
@@ -88,7 +94,7 @@ public boolean isNonNullType() {
8894
* @return true if the type is a list
8995
*/
9096
public boolean isListType() {
91-
return type instanceof GraphQLList;
97+
return isList(type);
9298
}
9399

94100
/**
@@ -134,13 +140,10 @@ public static Stack<GraphQLType> unwrapType(GraphQLType type) {
134140
Stack<GraphQLType> decoration = new Stack<>();
135141
while (true) {
136142
decoration.push(type);
137-
if (type instanceof GraphQLNonNull) {
138-
type = ((GraphQLNonNull) type).getWrappedType();
139-
} else if (type instanceof GraphQLList) {
140-
type = ((GraphQLList) type).getWrappedType();
141-
} else {
143+
if (isNotWrapped(type)) {
142144
break;
143145
}
146+
type = unwrapOne(type);
144147
}
145148
return decoration;
146149
}
@@ -154,7 +157,7 @@ public static Stack<GraphQLType> unwrapType(GraphQLType type) {
154157
* @return the underlying raw type with {@link GraphQLNonNull} and {@link GraphQLList} type wrappers removed
155158
*/
156159
public static GraphQLType unwrapBaseType(GraphQLType type) {
157-
return unwrapType(type).pop();
160+
return unwrapAll(type);
158161
}
159162

160163

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

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

185188
private static GraphQLType unwrapNonNull(GraphQLType type) {
186189
// its possible to have non nulls wrapping non nulls of things but it must end at some point
187-
while (type instanceof GraphQLNonNull) {
188-
type = ((GraphQLNonNull) type).getWrappedType();
190+
while (isNonNull(type)) {
191+
type = unwrapOne(type);
189192
}
190193
return type;
191194
}
@@ -231,7 +234,7 @@ public Builder path(ExecutionPath executionPath) {
231234

232235

233236
public ExecutionTypeInfo build() {
234-
if (type instanceof GraphQLNonNull) {
237+
if (isNonNull(type)) {
235238
return new ExecutionTypeInfo(unwrapNonNull(type), fieldDefinition, executionPath, parentType, true);
236239
}
237240
return new ExecutionTypeInfo(type, fieldDefinition, executionPath, parentType, false);

src/main/java/graphql/execution/ValuesResolver.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import graphql.schema.GraphQLInputObjectField;
1818
import graphql.schema.GraphQLInputObjectType;
1919
import graphql.schema.GraphQLList;
20-
import graphql.schema.GraphQLNonNull;
2120
import graphql.schema.GraphQLScalarType;
2221
import graphql.schema.GraphQLSchema;
2322
import graphql.schema.GraphQLType;
@@ -31,6 +30,9 @@
3130
import java.util.stream.Collectors;
3231

3332
import static graphql.Assert.assertShouldNeverHappen;
33+
import static graphql.schema.GraphQLTypeUtil.isList;
34+
import static graphql.schema.GraphQLTypeUtil.isNonNull;
35+
import static graphql.schema.GraphQLTypeUtil.unwrapOne;
3436
import static graphql.schema.visibility.DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY;
3537

3638
@Internal
@@ -78,7 +80,7 @@ public Map<String, Object> coerceArgumentValues(GraphQLSchema schema, List<Varia
7880
// 3.e.i
7981
Object coercedValue = coerceValueAst(fieldVisibility, variableType, variableDefinition.getDefaultValue(), null);
8082
coercedValues.put(variableName, coercedValue);
81-
} else if (isNonNullType(variableType)) {
83+
} else if (isNonNull(variableType)) {
8284
// 3.e.ii
8385
throw new NonNullableValueCoercedAsNullException(variableDefinition, variableType);
8486
}
@@ -103,10 +105,6 @@ private Object getVariableValue(GraphqlFieldVisibility fieldVisibility, Variable
103105
return coerceValue(fieldVisibility, variableDefinition, variableDefinition.getName(), variableType, value);
104106
}
105107

106-
private boolean isNonNullType(GraphQLType variableType) {
107-
return variableType instanceof GraphQLNonNull;
108-
}
109-
110108
public Map<String, Object> getArgumentValues(List<GraphQLArgument> argumentTypes, List<Argument> arguments, Map<String, Object> variables) {
111109
return getArgumentValues(DEFAULT_FIELD_VISIBILITY, argumentTypes, arguments, variables);
112110
}
@@ -149,9 +147,9 @@ private Map<String, Argument> argumentMap(List<Argument> arguments) {
149147
@SuppressWarnings("unchecked")
150148
private Object coerceValue(GraphqlFieldVisibility fieldVisibility, VariableDefinition variableDefinition, String inputName, GraphQLType graphQLType, Object value) {
151149
try {
152-
if (graphQLType instanceof GraphQLNonNull) {
150+
if (isNonNull(graphQLType)) {
153151
Object returnValue =
154-
coerceValue(fieldVisibility, variableDefinition, inputName, ((GraphQLNonNull) graphQLType).getWrappedType(), value);
152+
coerceValue(fieldVisibility, variableDefinition, inputName, unwrapOne(graphQLType), value);
155153
if (returnValue == null) {
156154
throw new NonNullableValueCoercedAsNullException(variableDefinition, graphQLType);
157155
}
@@ -173,22 +171,22 @@ private Object coerceValue(GraphqlFieldVisibility fieldVisibility, VariableDefin
173171
return coerceValueForInputObjectType(fieldVisibility, variableDefinition, (GraphQLInputObjectType) graphQLType, (Map<String, Object>) value);
174172
} else {
175173
throw new CoercingParseValueException(
176-
"Expected type 'Map' but was '" + value.getClass().getSimpleName() +
177-
"'. Variables for input objects must be an instance of type 'Map'."
174+
"Expected type 'Map' but was '" + value.getClass().getSimpleName() +
175+
"'. Variables for input objects must be an instance of type 'Map'."
178176
);
179177
}
180178
} else {
181179
return assertShouldNeverHappen("unhandled type %s", graphQLType);
182180
}
183181
} catch (CoercingParseValueException e) {
184182
if (e.getLocations() != null) {
185-
throw e;
183+
throw e;
186184
}
187185

188186
throw new CoercingParseValueException(
189-
"Variable '" + inputName + "' has an invalid value. " + e.getMessage(),
190-
e.getCause(),
191-
variableDefinition.getSourceLocation()
187+
"Variable '" + inputName + "' has an invalid value. " + e.getMessage(),
188+
e.getCause(),
189+
variableDefinition.getSourceLocation()
192190
);
193191
}
194192
}
@@ -205,19 +203,19 @@ private Object coerceValueForInputObjectType(GraphqlFieldVisibility fieldVisibil
205203

206204
for (GraphQLInputObjectField inputField : fields) {
207205
if (input.containsKey(inputField.getName()) || alwaysHasValue(inputField)) {
208-
Object value = coerceValue(fieldVisibility, variableDefinition,
209-
inputField.getName(),
210-
inputField.getType(),
211-
input.get(inputField.getName()));
212-
result.put(inputField.getName(), value == null ? inputField.getDefaultValue() : value);
206+
Object value = coerceValue(fieldVisibility, variableDefinition,
207+
inputField.getName(),
208+
inputField.getType(),
209+
input.get(inputField.getName()));
210+
result.put(inputField.getName(), value == null ? inputField.getDefaultValue() : value);
213211
}
214212
}
215213
return result;
216214
}
217215

218216
private boolean alwaysHasValue(GraphQLInputObjectField inputField) {
219217
return inputField.getDefaultValue() != null
220-
|| inputField.getType() instanceof GraphQLNonNull;
218+
|| isNonNull(inputField.getType());
221219
}
222220

223221
private Object coerceValueForScalar(GraphQLScalarType graphQLScalarType, Object value) {
@@ -250,16 +248,16 @@ private Object coerceValueAst(GraphqlFieldVisibility fieldVisibility, GraphQLTyp
250248
if (type instanceof GraphQLScalarType) {
251249
return parseLiteral(inputValue, ((GraphQLScalarType) type).getCoercing());
252250
}
253-
if (type instanceof GraphQLNonNull) {
254-
return coerceValueAst(fieldVisibility, ((GraphQLNonNull) type).getWrappedType(), inputValue, variables);
251+
if (isNonNull(type)) {
252+
return coerceValueAst(fieldVisibility, unwrapOne(type), inputValue, variables);
255253
}
256254
if (type instanceof GraphQLInputObjectType) {
257255
return coerceValueAstForInputObject(fieldVisibility, (GraphQLInputObjectType) type, (ObjectValue) inputValue, variables);
258256
}
259257
if (type instanceof GraphQLEnumType) {
260258
return parseLiteral(inputValue, ((GraphQLEnumType) type).getCoercing());
261259
}
262-
if (type instanceof GraphQLList) {
260+
if (isList(type)) {
263261
return coerceValueAstForList(fieldVisibility, (GraphQLList) type, inputValue, variables);
264262
}
265263
return null;
@@ -328,7 +326,7 @@ private Object coerceValueAstForInputObject(GraphqlFieldVisibility fieldVisibili
328326
}
329327

330328
private void assertNonNullInputField(GraphQLInputObjectField inputTypeField) {
331-
if (inputTypeField.getType() instanceof GraphQLNonNull) {
329+
if (isNonNull(inputTypeField.getType())) {
332330
throw new NonNullableValueCoercedAsNullException(inputTypeField);
333331
}
334332
}

src/main/java/graphql/introspection/Introspection.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
import graphql.schema.GraphQLInputType;
1919
import graphql.schema.GraphQLInterfaceType;
2020
import graphql.schema.GraphQLList;
21+
import graphql.schema.GraphQLModifiedType;
2122
import graphql.schema.GraphQLNonNull;
2223
import graphql.schema.GraphQLObjectType;
2324
import graphql.schema.GraphQLScalarType;
2425
import graphql.schema.GraphQLSchema;
26+
import graphql.schema.GraphQLTypeUtil;
2527
import graphql.schema.GraphQLUnionType;
2628
import graphql.schema.visibility.GraphqlFieldVisibility;
2729

@@ -200,7 +202,7 @@ private static String print(Object value, GraphQLInputType type) {
200202
public static final DataFetcher possibleTypesFetcher = environment -> {
201203
Object type = environment.getSource();
202204
if (type instanceof GraphQLInterfaceType) {
203-
return environment.getGraphQLSchema().getImplementations((GraphQLInterfaceType)type);
205+
return environment.getGraphQLSchema().getImplementations((GraphQLInterfaceType) type);
204206
}
205207
if (type instanceof GraphQLUnionType) {
206208
return ((GraphQLUnionType) type).getTypes();
@@ -236,11 +238,8 @@ private static String print(Object value, GraphQLInputType type) {
236238

237239
public static final DataFetcher OfTypeFetcher = environment -> {
238240
Object type = environment.getSource();
239-
if (type instanceof GraphQLList) {
240-
return ((GraphQLList) type).getWrappedType();
241-
}
242-
if (type instanceof GraphQLNonNull) {
243-
return ((GraphQLNonNull) type).getWrappedType();
241+
if (type instanceof GraphQLModifiedType) {
242+
return GraphQLTypeUtil.unwrapOne((GraphQLModifiedType) type);
244243
}
245244
return null;
246245
};

src/main/java/graphql/language/AstValueHelper.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import java.util.List;
2828
import java.util.Map;
2929

30+
import static graphql.schema.GraphQLTypeUtil.isList;
31+
import static graphql.schema.GraphQLTypeUtil.isNonNull;
32+
3033
public class AstValueHelper {
3134

3235
/**
@@ -56,13 +59,13 @@ public static Value astFromValue(Object value, GraphQLType type) {
5659
return null;
5760
}
5861

59-
if (type instanceof GraphQLNonNull) {
62+
if (isNonNull(type)) {
6063
return handleNonNull(value, (GraphQLNonNull) type);
6164
}
6265

6366
// Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
6467
// the value is not an array, convert the value using the list's item type.
65-
if (type instanceof GraphQLList) {
68+
if (isList(type)) {
6669
return handleList(value, (GraphQLList) type);
6770
}
6871

src/main/java/graphql/relay/Relay.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import graphql.schema.GraphQLInputObjectField;
88
import graphql.schema.GraphQLInputObjectType;
99
import graphql.schema.GraphQLInterfaceType;
10-
import graphql.schema.GraphQLList;
11-
import graphql.schema.GraphQLNonNull;
1210
import graphql.schema.GraphQLObjectType;
1311
import graphql.schema.GraphQLOutputType;
1412
import graphql.schema.TypeResolver;
@@ -26,7 +24,7 @@
2624
import static graphql.schema.GraphQLInputObjectField.newInputObjectField;
2725
import static graphql.schema.GraphQLInputObjectType.newInputObject;
2826
import static graphql.schema.GraphQLInterfaceType.newInterface;
29-
import static graphql.schema.GraphQLList.*;
27+
import static graphql.schema.GraphQLList.list;
3028
import static graphql.schema.GraphQLNonNull.nonNull;
3129
import static graphql.schema.GraphQLObjectType.newObject;
3230

0 commit comments

Comments
 (0)