24
24
import graphql .schema .DataFetcher ;
25
25
import graphql .schema .GraphQLList ;
26
26
import graphql .schema .GraphQLNamedOutputType ;
27
+ import graphql .schema .GraphQLNonNull ;
27
28
import graphql .schema .GraphQLType ;
28
29
import graphql .schema .idl .FieldWiringEnvironment ;
29
30
import graphql .schema .idl .RuntimeWiring ;
30
31
import graphql .schema .idl .WiringFactory ;
32
+ import org .apache .commons .logging .Log ;
33
+ import org .apache .commons .logging .LogFactory ;
31
34
32
35
import org .springframework .graphql .execution .RuntimeWiringConfigurer ;
33
36
import org .springframework .lang .Nullable ;
43
46
*/
44
47
class AutoRegistrationRuntimeWiringConfigurer implements RuntimeWiringConfigurer {
45
48
49
+ private final static Log logger = LogFactory .getLog (AutoRegistrationRuntimeWiringConfigurer .class );
50
+
51
+
46
52
private final Map <String , Function <Boolean , DataFetcher <?>>> dataFetcherFactories ;
47
53
48
54
49
- public AutoRegistrationRuntimeWiringConfigurer (
55
+ /**
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.
60
+ */
61
+ AutoRegistrationRuntimeWiringConfigurer (
50
62
Map <String , Function <Boolean , DataFetcher <?>>> dataFetcherFactories ) {
51
63
52
64
this .dataFetcherFactories = dataFetcherFactories ;
@@ -84,16 +96,32 @@ public boolean providesDataFetcher(FieldWiringEnvironment environment) {
84
96
return false ;
85
97
}
86
98
99
+ String outputTypeName = getOutputTypeName (environment );
100
+
101
+ boolean result = (outputTypeName != null &&
102
+ dataFetcherFactories .containsKey (outputTypeName ) &&
103
+ !hasDataFetcherFor (environment .getFieldDefinition ()));
104
+
105
+ if (!result ) {
106
+ // This may be called multiples times on success, so log only rejections from here
107
+ logTraceMessage (environment , outputTypeName , false );
108
+ }
109
+
110
+ return result ;
111
+ }
112
+
113
+ @ Nullable
114
+ private String getOutputTypeName (FieldWiringEnvironment environment ) {
87
115
GraphQLType outputType = (environment .getFieldType () instanceof GraphQLList ?
88
116
((GraphQLList ) environment .getFieldType ()).getWrappedType () :
89
117
environment .getFieldType ());
90
118
91
- String outputTypeName = (outputType instanceof GraphQLNamedOutputType ?
92
- ((GraphQLNamedOutputType ) outputType ).getName () : null );
119
+ if (outputType instanceof GraphQLNonNull ) {
120
+ outputType = ((GraphQLNonNull ) outputType ).getWrappedType ();
121
+ }
93
122
94
- return (outputTypeName != null &&
95
- dataFetcherFactories .containsKey (outputTypeName ) &&
96
- !hasDataFetcherFor (environment .getFieldDefinition ()));
123
+ return (outputType instanceof GraphQLNamedOutputType ?
124
+ ((GraphQLNamedOutputType ) outputType ).getName () : null );
97
125
}
98
126
99
127
private boolean hasDataFetcherFor (FieldDefinition fieldDefinition ) {
@@ -104,18 +132,27 @@ private boolean hasDataFetcherFor(FieldDefinition fieldDefinition) {
104
132
return this .existingQueryDataFetcherPredicate .test (fieldDefinition .getName ());
105
133
}
106
134
135
+ private void logTraceMessage (
136
+ FieldWiringEnvironment environment , @ Nullable String outputTypeName , boolean match ) {
137
+
138
+ if (logger .isTraceEnabled ()) {
139
+ String query = environment .getFieldDefinition ().getName ();
140
+ logger .trace ((match ? "Matched" : "Skipped" ) +
141
+ " output typeName " + (outputTypeName != null ? "'" + outputTypeName + "'" : "null" ) +
142
+ " for query '" + query + "'" );
143
+ }
144
+ }
145
+
107
146
@ Override
108
147
public DataFetcher <?> getDataFetcher (FieldWiringEnvironment environment ) {
109
- return environment .getFieldType () instanceof GraphQLList ?
110
- initDataFetcher (((GraphQLList ) environment .getFieldType ()).getWrappedType (), false ) :
111
- initDataFetcher (environment .getFieldType (), true );
112
- }
113
148
114
- private DataFetcher <?> initDataFetcher (GraphQLType type , boolean single ) {
115
- Assert .isInstanceOf (GraphQLNamedOutputType .class , type );
116
- String typeName = ((GraphQLNamedOutputType ) type ).getName ();
117
- Function <Boolean , DataFetcher <?>> factory = dataFetcherFactories .get (typeName );
118
- Assert .notNull (factory , "Expected DataFetcher factory" );
149
+ String outputTypeName = getOutputTypeName (environment );
150
+ logTraceMessage (environment , outputTypeName , true );
151
+
152
+ Function <Boolean , DataFetcher <?>> factory = dataFetcherFactories .get (outputTypeName );
153
+ Assert .notNull (factory , "Expected DataFetcher factory for typeName '" + outputTypeName + "'" );
154
+
155
+ boolean single = !(environment .getFieldType () instanceof GraphQLList );
119
156
return factory .apply (single );
120
157
}
121
158
0 commit comments