2424import graphql .schema .DataFetcher ;
2525import graphql .schema .GraphQLList ;
2626import graphql .schema .GraphQLNamedOutputType ;
27+ import graphql .schema .GraphQLNonNull ;
2728import graphql .schema .GraphQLType ;
2829import graphql .schema .idl .FieldWiringEnvironment ;
2930import graphql .schema .idl .RuntimeWiring ;
3031import graphql .schema .idl .WiringFactory ;
32+ import org .apache .commons .logging .Log ;
33+ import org .apache .commons .logging .LogFactory ;
3134
3235import org .springframework .graphql .execution .RuntimeWiringConfigurer ;
3336import org .springframework .lang .Nullable ;
4346 */
4447class AutoRegistrationRuntimeWiringConfigurer implements RuntimeWiringConfigurer {
4548
49+ private final static Log logger = LogFactory .getLog (AutoRegistrationRuntimeWiringConfigurer .class );
50+
51+
4652 private final Map <String , Function <Boolean , DataFetcher <?>>> dataFetcherFactories ;
4753
4854
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 (
5062 Map <String , Function <Boolean , DataFetcher <?>>> dataFetcherFactories ) {
5163
5264 this .dataFetcherFactories = dataFetcherFactories ;
@@ -84,16 +96,32 @@ public boolean providesDataFetcher(FieldWiringEnvironment environment) {
8496 return false ;
8597 }
8698
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 ) {
87115 GraphQLType outputType = (environment .getFieldType () instanceof GraphQLList ?
88116 ((GraphQLList ) environment .getFieldType ()).getWrappedType () :
89117 environment .getFieldType ());
90118
91- String outputTypeName = (outputType instanceof GraphQLNamedOutputType ?
92- ((GraphQLNamedOutputType ) outputType ).getName () : null );
119+ if (outputType instanceof GraphQLNonNull ) {
120+ outputType = ((GraphQLNonNull ) outputType ).getWrappedType ();
121+ }
93122
94- return (outputTypeName != null &&
95- dataFetcherFactories .containsKey (outputTypeName ) &&
96- !hasDataFetcherFor (environment .getFieldDefinition ()));
123+ return (outputType instanceof GraphQLNamedOutputType ?
124+ ((GraphQLNamedOutputType ) outputType ).getName () : null );
97125 }
98126
99127 private boolean hasDataFetcherFor (FieldDefinition fieldDefinition ) {
@@ -104,18 +132,27 @@ private boolean hasDataFetcherFor(FieldDefinition fieldDefinition) {
104132 return this .existingQueryDataFetcherPredicate .test (fieldDefinition .getName ());
105133 }
106134
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+
107146 @ Override
108147 public DataFetcher <?> getDataFetcher (FieldWiringEnvironment environment ) {
109- return environment .getFieldType () instanceof GraphQLList ?
110- initDataFetcher (((GraphQLList ) environment .getFieldType ()).getWrappedType (), false ) :
111- initDataFetcher (environment .getFieldType (), true );
112- }
113148
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 );
119156 return factory .apply (single );
120157 }
121158
0 commit comments