Skip to content

Instantly share code, notes, and snippets.

@Component
@Internal
@Primary
@Profile({"request", "default"})
public class RequestScopedGraphQLInvocation implements GraphQLInvocation {
private final GraphQL graphQL;
private final GraphQLDataFetchers graphQLDataFetchers;
public RequestScopedGraphQLInvocation(GraphQL graphQL, GraphQLDataFetchers graphQLDataFetchers) {
@Component
@Internal
@Primary
public class CustomGraphQLInvocation implements GraphQLInvocation {
private final GraphQL graphQL;
private final GraphQLDataFetchers dataFetchers;
public CustomGraphQLInvocation(GraphQL graphQL, GraphQLDataFetchers dataFetchers) {
this.graphQL = graphQL;
private RuntimeWiring buildWiring() {
return RuntimeWiring.newRuntimeWiring()
.type(newTypeWiring("Query")
.dataFetcher("animals", animalsFetcher)
)
.type(newTypeWiring("Animal")
.dataFetcher("countries", animalCountriesFetcher)
)
.build();
}
DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry();
DataLoader<String, CountryTO> countryLoader = DataLoader.newDataLoader(GraphQLProvider.countryBatchLoader);
dataLoaderRegistry.register("countries", countryLoader);
-----------------------------------------------------------------------
--------------------------- Animals service ---------------------------
-----------------------------------------------------------------------
2019-09-26 23:36:33.296 INFO 23020 --- [ault-executor-1] grpcgraphql.animal.AnimalService : ================> getAnimals done for id:
-----------------------------------------------------------------------
--------------------------- Country service ---------------------------
-----------------------------------------------------------------------
2019-09-26 23:36:33.312 INFO 23021 --- [ault-executor-1] grpcgraphql.country.CountryService : ================> Adding country with id A
2019-09-26 23:36:33.312 INFO 23021 --- [ault-executor-1] grpcgraphql.country.CountryService : ================> Adding country with id F
-----------------------------------------------------------------------
--------------------------- Animals service ---------------------------
-----------------------------------------------------------------------
2019-09-26 23:33:58.868 INFO 23020 --- [ault-executor-0] grpcgraphql.animal.AnimalService : ================> getAnimals done for id: 0
-----------------------------------------------------------------------
--------------------------- Country service ---------------------------
-----------------------------------------------------------------------
2019-09-26 23:33:59.099 INFO 23021 --- [ault-executor-0] grpcgraphql.country.CountryService : ================> Adding country with id A
2019-09-26 23:33:59.099 INFO 23021 --- [ault-executor-0] grpcgraphql.country.CountryService : ================> Adding country with id F
{
animals {
name
color
countryIds
countries {
name
}
}
}
{
animals(id: 0) {
name
color
countryIds
countries {
name
}
}
}
$ java -jar build/libs/graphql-gateway-0.0.1-SNAPSHOT.jar
:: Spring Boot :: (v2.1.8.RELEASE)
2019-09-26 23:14:14.308 INFO 22676 --- [ main] grpcgraphql.graphql.GraphqlApplication : Starting GraphqlApplication on winter with PID 22676
2019-09-26 23:14:14.311 INFO 22676 --- [ main] grpcgraphql.graphql.GraphqlApplication : No active profile set, falling back to default profiles: default
2019-09-26 23:14:15.553 INFO 22676 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2019-09-26 23:14:15.599 INFO 22676 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-09-26 23:14:15.599 INFO 22676 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-26 23:14:15.703 INFO 22676 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
static BatchLoader<String, CountryTO> countryBatchLoader = countryIds ->
CompletableFuture.supplyAsync(() -> {
try {
List<String> countryList = countryServiceStub
.getCountries(Country.CountryRequest.newBuilder().addAllId(countryIds).build())
.get()
.getCountryList();
List<CountryTO> collect = countryList
.stream()