1717
1818import java .util .List ;
1919import java .util .Map ;
20- import java .util .function .Function ;
2120import java .util .function .Predicate ;
2221
2322import graphql .language .FieldDefinition ;
@@ -49,19 +48,15 @@ class AutoRegistrationRuntimeWiringConfigurer implements RuntimeWiringConfigurer
4948 private final static Log logger = LogFactory .getLog (AutoRegistrationRuntimeWiringConfigurer .class );
5049
5150
52- private final Map <String , Function < Boolean , DataFetcher <?>> > dataFetcherFactories ;
51+ private final Map <String , DataFetcherFactory > dataFetcherFactories ;
5352
5453
5554 /**
56- * Constructor with a Map of GraphQL type names for which auto-registration
57- * can be performed.
58- * @param dataFetcherFactories Map with GraphQL type names as keys, and
59- * functions to create a corresponding {@link DataFetcher} as values.
55+ * Constructor with a Map of GraphQL type names as keys, and
56+ * {@code DataFetcher} factories as values.
6057 */
61- AutoRegistrationRuntimeWiringConfigurer (
62- Map <String , Function <Boolean , DataFetcher <?>>> dataFetcherFactories ) {
63-
64- this .dataFetcherFactories = dataFetcherFactories ;
58+ AutoRegistrationRuntimeWiringConfigurer (Map <String , DataFetcherFactory > factories ) {
59+ this .dataFetcherFactories = factories ;
6560 }
6661
6762
@@ -75,6 +70,24 @@ public void configure(RuntimeWiring.Builder builder, List<WiringFactory> contain
7570 }
7671
7772
73+ /**
74+ * Callback interface to create the desired type of {@code DataFetcher}.
75+ */
76+ interface DataFetcherFactory {
77+
78+ /**
79+ * Create a singe item {@code DataFetcher}.
80+ */
81+ DataFetcher <?> single ();
82+
83+ /**
84+ * Create {@code DataFetcher} for multiple items.
85+ */
86+ DataFetcher <?> many ();
87+
88+ }
89+
90+
7891 private class AutoRegistrationWiringFactory implements WiringFactory {
7992
8093 private final RuntimeWiring .Builder builder ;
@@ -112,16 +125,21 @@ public boolean providesDataFetcher(FieldWiringEnvironment environment) {
112125
113126 @ Nullable
114127 private String getOutputTypeName (FieldWiringEnvironment environment ) {
115- GraphQLType outputType = (environment .getFieldType () instanceof GraphQLList ?
116- ((GraphQLList ) environment .getFieldType ()).getWrappedType () :
117- environment .getFieldType ());
128+ GraphQLType outputType = removeNonNullWrapper (environment .getFieldType ());
129+
130+ if (outputType instanceof GraphQLList ) {
131+ outputType = removeNonNullWrapper (((GraphQLList ) outputType ).getWrappedType ());
132+ }
118133
119- if (outputType instanceof GraphQLNonNull ) {
120- outputType = (( GraphQLNonNull ) outputType ). getWrappedType ();
134+ if (outputType instanceof GraphQLNamedOutputType namedType ) {
135+ return namedType . getName ();
121136 }
122137
123- return (outputType instanceof GraphQLNamedOutputType ?
124- ((GraphQLNamedOutputType ) outputType ).getName () : null );
138+ return null ;
139+ }
140+
141+ private GraphQLType removeNonNullWrapper (GraphQLType outputType ) {
142+ return (outputType instanceof GraphQLNonNull wrapper ? wrapper .getWrappedType () : outputType );
125143 }
126144
127145 private boolean hasDataFetcherFor (FieldDefinition fieldDefinition ) {
@@ -132,13 +150,11 @@ private boolean hasDataFetcherFor(FieldDefinition fieldDefinition) {
132150 return this .existingQueryDataFetcherPredicate .test (fieldDefinition .getName ());
133151 }
134152
135- private void logTraceMessage (
136- FieldWiringEnvironment environment , @ Nullable String outputTypeName , boolean match ) {
137-
153+ private void logTraceMessage (FieldWiringEnvironment environment , @ Nullable String typeName , boolean match ) {
138154 if (logger .isTraceEnabled ()) {
139155 String query = environment .getFieldDefinition ().getName ();
140156 logger .trace ((match ? "Matched" : "Skipped" ) +
141- " output typeName " + (outputTypeName != null ? "'" + outputTypeName + "'" : "null" ) +
157+ " output typeName " + (typeName != null ? "'" + typeName + "'" : "null" ) +
142158 " for query '" + query + "'" );
143159 }
144160 }
@@ -149,11 +165,16 @@ public DataFetcher<?> getDataFetcher(FieldWiringEnvironment environment) {
149165 String outputTypeName = getOutputTypeName (environment );
150166 logTraceMessage (environment , outputTypeName , true );
151167
152- Function < Boolean , DataFetcher <?>> factory = dataFetcherFactories .get (outputTypeName );
168+ DataFetcherFactory factory = dataFetcherFactories .get (outputTypeName );
153169 Assert .notNull (factory , "Expected DataFetcher factory for typeName '" + outputTypeName + "'" );
154170
155- boolean single = !(environment .getFieldType () instanceof GraphQLList );
156- return factory .apply (single );
171+ GraphQLType outputType = removeNonNullWrapper (environment .getFieldType ());
172+ if (outputType instanceof GraphQLList ) {
173+ return factory .many ();
174+ }
175+ else {
176+ return factory .single ();
177+ }
157178 }
158179
159180 }
0 commit comments