17
17
18
18
import java .util .List ;
19
19
import java .util .Map ;
20
- import java .util .function .Function ;
21
20
import java .util .function .Predicate ;
22
21
23
22
import graphql .language .FieldDefinition ;
@@ -49,19 +48,15 @@ class AutoRegistrationRuntimeWiringConfigurer implements RuntimeWiringConfigurer
49
48
private final static Log logger = LogFactory .getLog (AutoRegistrationRuntimeWiringConfigurer .class );
50
49
51
50
52
- private final Map <String , Function < Boolean , DataFetcher <?>> > dataFetcherFactories ;
51
+ private final Map <String , DataFetcherFactory > dataFetcherFactories ;
53
52
54
53
55
54
/**
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.
60
57
*/
61
- AutoRegistrationRuntimeWiringConfigurer (
62
- Map <String , Function <Boolean , DataFetcher <?>>> dataFetcherFactories ) {
63
-
64
- this .dataFetcherFactories = dataFetcherFactories ;
58
+ AutoRegistrationRuntimeWiringConfigurer (Map <String , DataFetcherFactory > factories ) {
59
+ this .dataFetcherFactories = factories ;
65
60
}
66
61
67
62
@@ -75,6 +70,24 @@ public void configure(RuntimeWiring.Builder builder, List<WiringFactory> contain
75
70
}
76
71
77
72
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
+
78
91
private class AutoRegistrationWiringFactory implements WiringFactory {
79
92
80
93
private final RuntimeWiring .Builder builder ;
@@ -112,16 +125,21 @@ public boolean providesDataFetcher(FieldWiringEnvironment environment) {
112
125
113
126
@ Nullable
114
127
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
+ }
118
133
119
- if (outputType instanceof GraphQLNonNull ) {
120
- outputType = (( GraphQLNonNull ) outputType ). getWrappedType ();
134
+ if (outputType instanceof GraphQLNamedOutputType namedType ) {
135
+ return namedType . getName ();
121
136
}
122
137
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 );
125
143
}
126
144
127
145
private boolean hasDataFetcherFor (FieldDefinition fieldDefinition ) {
@@ -132,13 +150,11 @@ private boolean hasDataFetcherFor(FieldDefinition fieldDefinition) {
132
150
return this .existingQueryDataFetcherPredicate .test (fieldDefinition .getName ());
133
151
}
134
152
135
- private void logTraceMessage (
136
- FieldWiringEnvironment environment , @ Nullable String outputTypeName , boolean match ) {
137
-
153
+ private void logTraceMessage (FieldWiringEnvironment environment , @ Nullable String typeName , boolean match ) {
138
154
if (logger .isTraceEnabled ()) {
139
155
String query = environment .getFieldDefinition ().getName ();
140
156
logger .trace ((match ? "Matched" : "Skipped" ) +
141
- " output typeName " + (outputTypeName != null ? "'" + outputTypeName + "'" : "null" ) +
157
+ " output typeName " + (typeName != null ? "'" + typeName + "'" : "null" ) +
142
158
" for query '" + query + "'" );
143
159
}
144
160
}
@@ -149,11 +165,16 @@ public DataFetcher<?> getDataFetcher(FieldWiringEnvironment environment) {
149
165
String outputTypeName = getOutputTypeName (environment );
150
166
logTraceMessage (environment , outputTypeName , true );
151
167
152
- Function < Boolean , DataFetcher <?>> factory = dataFetcherFactories .get (outputTypeName );
168
+ DataFetcherFactory factory = dataFetcherFactories .get (outputTypeName );
153
169
Assert .notNull (factory , "Expected DataFetcher factory for typeName '" + outputTypeName + "'" );
154
170
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
+ }
157
178
}
158
179
159
180
}
0 commit comments