-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor auto-registration of Querydsl and QBE fetchers
Replace the TypeVisitor with a WiringFactory that checks for existing registrations in RuntimeWiring.Builder. Closes gh-244
- Loading branch information
1 parent
2ecac0b
commit 5ff3f8b
Showing
8 changed files
with
237 additions
and
29 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
.../java/org/springframework/graphql/data/query/AutoRegistrationRuntimeWiringConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2002-2022 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.graphql.data.query; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
|
||
import graphql.language.FieldDefinition; | ||
import graphql.schema.DataFetcher; | ||
import graphql.schema.GraphQLList; | ||
import graphql.schema.GraphQLNamedOutputType; | ||
import graphql.schema.GraphQLType; | ||
import graphql.schema.idl.FieldWiringEnvironment; | ||
import graphql.schema.idl.RuntimeWiring; | ||
import graphql.schema.idl.WiringFactory; | ||
|
||
import org.springframework.graphql.execution.RuntimeWiringConfigurer; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Given a map of GraphQL type names and DataFetcher factories, find queries | ||
* with a matching return type and register DataFetcher's for them, unless they | ||
* already have registrations. | ||
* | ||
* @author Rossen Stoyanchev | ||
* @since 1.0.0 | ||
*/ | ||
class AutoRegistrationRuntimeWiringConfigurer implements RuntimeWiringConfigurer { | ||
|
||
private final Map<String, Function<Boolean, DataFetcher<?>>> dataFetcherFactories; | ||
|
||
|
||
public AutoRegistrationRuntimeWiringConfigurer( | ||
Map<String, Function<Boolean, DataFetcher<?>>> dataFetcherFactories) { | ||
|
||
this.dataFetcherFactories = dataFetcherFactories; | ||
} | ||
|
||
|
||
@Override | ||
public void configure(RuntimeWiring.Builder builder) { | ||
} | ||
|
||
@Override | ||
public void configure(RuntimeWiring.Builder builder, List<WiringFactory> container) { | ||
container.add(new AutoRegistrationWiringFactory(builder)); | ||
} | ||
|
||
|
||
private class AutoRegistrationWiringFactory implements WiringFactory { | ||
|
||
private final RuntimeWiring.Builder builder; | ||
|
||
@Nullable | ||
private Predicate<String> existingQueryDataFetcherPredicate; | ||
|
||
AutoRegistrationWiringFactory(RuntimeWiring.Builder builder) { | ||
this.builder = builder; | ||
} | ||
|
||
@Override | ||
public boolean providesDataFetcher(FieldWiringEnvironment environment) { | ||
if (dataFetcherFactories.isEmpty()) { | ||
return false; | ||
} | ||
|
||
if (!environment.getParentType().getName().equals("Query")) { | ||
return false; | ||
} | ||
|
||
GraphQLType outputType = (environment.getFieldType() instanceof GraphQLList ? | ||
((GraphQLList) environment.getFieldType()).getWrappedType() : | ||
environment.getFieldType()); | ||
|
||
String outputTypeName = (outputType instanceof GraphQLNamedOutputType ? | ||
((GraphQLNamedOutputType) outputType).getName() : null); | ||
|
||
return (outputTypeName != null && | ||
dataFetcherFactories.containsKey(outputTypeName) && | ||
!hasDataFetcherFor(environment.getFieldDefinition())); | ||
} | ||
|
||
private boolean hasDataFetcherFor(FieldDefinition fieldDefinition) { | ||
if (this.existingQueryDataFetcherPredicate == null) { | ||
Map<String, ?> map = this.builder.build().getDataFetcherForType("Query"); | ||
this.existingQueryDataFetcherPredicate = fieldName -> map.get(fieldName) != null; | ||
} | ||
return this.existingQueryDataFetcherPredicate.test(fieldDefinition.getName()); | ||
} | ||
|
||
@Override | ||
public DataFetcher<?> getDataFetcher(FieldWiringEnvironment environment) { | ||
return environment.getFieldType() instanceof GraphQLList ? | ||
initDataFetcher(((GraphQLList) environment.getFieldType()).getWrappedType(), false) : | ||
initDataFetcher(environment.getFieldType(), true); | ||
} | ||
|
||
private DataFetcher<?> initDataFetcher(GraphQLType type, boolean single) { | ||
Assert.isInstanceOf(GraphQLNamedOutputType.class, type); | ||
String typeName = ((GraphQLNamedOutputType) type).getName(); | ||
Function<Boolean, DataFetcher<?>> factory = dataFetcherFactories.get(typeName); | ||
Assert.notNull(factory, "Expected DataFetcher factory"); | ||
return factory.apply(single); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.