Skip to content

Commit 4dc1f34

Browse files
committed
A smidge faster unwrap non-null - updated to only do 1 check
1 parent 51db09b commit 4dc1f34

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/main/java/graphql/schema/GraphQLNonNull.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public GraphQLNonNull(GraphQLType wrappedType) {
4646
}
4747

4848
private void assertNonNullWrapping(GraphQLType wrappedType) {
49-
assertTrue(!GraphQLTypeUtil.isNonNull(wrappedType),
50-
"A non null type cannot wrap an existing non null type '%s'", GraphQLTypeUtil.simplePrint(wrappedType));
49+
assertTrue(!GraphQLTypeUtil.isNonNull(wrappedType), () ->
50+
String.format("A non null type cannot wrap an existing non null type '%s'", GraphQLTypeUtil.simplePrint(wrappedType)));
5151
}
5252

5353
@Override

src/main/java/graphql/schema/GraphQLTypeUtil.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,26 +219,26 @@ private static GraphQLType unwrapAllImpl(GraphQLType type) {
219219

220220

221221
/**
222-
* Unwraps all non-nullable layers of the type until it reaches a type that is not {@link GraphQLNonNull}
222+
* Unwraps a single non-nullable layer of the type if its present. Note there can
223+
* only ever be one non-nullable wrapping of a type and this is enforced by {@link GraphQLNonNull}
223224
*
224225
* @param type the type to unwrap
225226
*
226227
* @return the underlying type that is not {@link GraphQLNonNull}
227228
*/
228229
public static GraphQLType unwrapNonNull(GraphQLType type) {
229-
// nominally its illegal to have a type that is a non null wrapping a non-null
230-
// but the code is like this just in case and anyway it has to do 1 non-null check
231-
// so this works even if it wont really loop
232-
while (isNonNull(type)) {
230+
// its illegal to have a type that is a non-null wrapping a non-null type
231+
// and GraphQLNonNull has code that prevents it so we can just check once during the unwrapping
232+
if (isNonNull(type)) {
233233
// is cheaper doing this direct rather than calling #unwrapOne
234234
type = ((GraphQLNonNull) type).getWrappedType();
235235
}
236236
return type;
237237
}
238238

239239
/**
240-
* Unwraps all non nullable layers of the type until it reaches a type that is not {@link GraphQLNonNull}
241-
* and then cast to the target type.
240+
* Unwraps a single non-nullable layer of the type if its present and then cast to the target type. Note there can
241+
* only ever be one non-nullable wrapping of a type and this is enforced by {@link GraphQLNonNull}
242242
*
243243
* @param type the type to unwrap
244244
* @param <T> for two

src/test/groovy/graphql/schema/GraphQLTypeUtilTest.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,19 +215,19 @@ class GraphQLTypeUtilTest extends Specification {
215215
(type as GraphQLNamedType).getName() == "String"
216216

217217
when:
218-
type = GraphQLTypeUtil.unwrapNonNull(nonNull(list(GraphQLString)))
218+
type = GraphQLTypeUtil.unwrapNonNull(nonNull(list(GraphQLString)))
219219

220220
then:
221221
type instanceof GraphQLList
222222

223223
when:
224-
type = GraphQLTypeUtil.unwrapNonNull(list(GraphQLString))
224+
type = GraphQLTypeUtil.unwrapNonNull(list(GraphQLString))
225225

226226
then:
227227
type instanceof GraphQLList
228228

229229
when:
230-
type = GraphQLTypeUtil.unwrapNonNull(GraphQLString)
230+
type = GraphQLTypeUtil.unwrapNonNull(GraphQLString)
231231

232232
then:
233233
(type as GraphQLNamedType).getName() == "String"

0 commit comments

Comments
 (0)