diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/query/AutoRegistrationRuntimeWiringConfigurer.java b/spring-graphql/src/main/java/org/springframework/graphql/data/query/AutoRegistrationRuntimeWiringConfigurer.java index 0306e5a0..16c3f39a 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/query/AutoRegistrationRuntimeWiringConfigurer.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/query/AutoRegistrationRuntimeWiringConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -112,16 +112,21 @@ class AutoRegistrationRuntimeWiringConfigurer implements RuntimeWiringConfigurer @Nullable private String getOutputTypeName(FieldWiringEnvironment environment) { - GraphQLType outputType = (environment.getFieldType() instanceof GraphQLList ? - ((GraphQLList) environment.getFieldType()).getWrappedType() : - environment.getFieldType()); + GraphQLType outputType = removeNonNullWrapper(environment.getFieldType()); - if (outputType instanceof GraphQLNonNull) { - outputType = ((GraphQLNonNull) outputType).getWrappedType(); + if (outputType instanceof GraphQLList) { + outputType = removeNonNullWrapper(((GraphQLList) outputType).getWrappedType()); } - return (outputType instanceof GraphQLNamedOutputType ? - ((GraphQLNamedOutputType) outputType).getName() : null); + if (outputType instanceof GraphQLNamedOutputType namedType) { + return namedType.getName(); + } + + return null; + } + + private GraphQLType removeNonNullWrapper(GraphQLType outputType) { + return (outputType instanceof GraphQLNonNull wrapper ? wrapper.getWrappedType() : outputType); } private boolean hasDataFetcherFor(FieldDefinition fieldDefinition) { @@ -152,8 +157,8 @@ class AutoRegistrationRuntimeWiringConfigurer implements RuntimeWiringConfigurer Function> factory = dataFetcherFactories.get(outputTypeName); Assert.notNull(factory, "Expected DataFetcher factory for typeName '" + outputTypeName + "'"); - boolean single = !(environment.getFieldType() instanceof GraphQLList); - return factory.apply(single); + GraphQLType type = removeNonNullWrapper(environment.getFieldType()); + return factory.apply(!(type instanceof GraphQLList)); } } diff --git a/spring-graphql/src/test/resources/books/schema.graphqls b/spring-graphql/src/test/resources/books/schema.graphqls index 0f98f5f6..e1d7bcdd 100644 --- a/spring-graphql/src/test/resources/books/schema.graphqls +++ b/spring-graphql/src/test/resources/books/schema.graphqls @@ -1,7 +1,7 @@ type Query { bookById(id: ID): Book booksById(id: [ID]): [Book] - books(id: ID, name: String, author: String): [Book!] + books(id: ID, name: String, author: String): [Book!]! booksByCriteria(criteria:BookCriteria): [Book] booksByProjectedArguments(name: String, author: String): [Book] booksByProjectedCriteria(criteria:BookCriteria): [Book]