Skip to content

Commit

Permalink
Use dataload when fetching all the books for a shelf
Browse files Browse the repository at this point in the history
  • Loading branch information
siderakis committed Mar 21, 2018
1 parent 1964d31 commit d214ac9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
Expand Up @@ -29,6 +29,10 @@
import java.util.EnumSet;
import java.util.logging.Logger;

import static javax.servlet.DispatcherType.ASYNC;
import static javax.servlet.DispatcherType.REQUEST;
import static org.eclipse.jetty.servlet.ServletContextHandler.SESSIONS;

public class GraphQlServer {

private static final int HTTP_PORT = 8080;
Expand All @@ -38,7 +42,7 @@ public static void main(String[] args) throws Exception {
Server server = new Server(HTTP_PORT);

ServletContextHandler context =
new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
new ServletContextHandler(server, "/", SESSIONS);

context.addEventListener(
new GuiceServletContextListener() {
Expand All @@ -53,7 +57,7 @@ protected void configureServlets() {
},
new DataLoaderModule(),
new SchemaProviderModule(), // Part of Rejoiner framework (Provides `@Schema
// GraphQLSchema`)
// GraphQLSchema`)
new BookClientModule(), // Configures the Book gRPC client
new ShelfClientModule(), // Configures the Shelf gRPC client
new BookSchemaModule(), // Creates queries and mutations for the Book service
Expand All @@ -64,10 +68,7 @@ protected void configureServlets() {
}
});

context.addFilter(
GuiceFilter.class,
"/*",
EnumSet.of(javax.servlet.DispatcherType.REQUEST, javax.servlet.DispatcherType.ASYNC));
context.addFilter(GuiceFilter.class, "/*", EnumSet.of(REQUEST, ASYNC));

context.setBaseResource(
new PathResource(new File("./src/main/resources").toPath().toRealPath()));
Expand Down
Expand Up @@ -18,16 +18,16 @@
import com.google.api.graphql.rejoiner.Mutation;
import com.google.api.graphql.rejoiner.SchemaModification;
import com.google.api.graphql.rejoiner.SchemaModule;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.example.library.book.v1.Book;
import com.google.example.library.book.v1.BookServiceGrpc;
import com.google.example.library.book.v1.CreateBookRequest;
import com.google.example.library.book.v1.GetBookRequest;
import com.google.example.library.shelf.v1.GetShelfRequest;
import com.google.example.library.shelf.v1.Shelf;
import com.google.example.library.shelf.v1.ShelfServiceGrpc;
import net.javacrumbs.futureconverter.java8guava.FutureConverter;
import org.dataloader.DataLoaderRegistry;

import java.util.List;

/** A GraphQL {@link SchemaModule} backed by a gRPC service. */
Expand All @@ -47,14 +47,8 @@ Book createBookAndAddToShelf(
}

@SchemaModification(addField = "books", onType = Shelf.class)
ListenableFuture<List<Book>> shelfToBooks(
Shelf shelf, BookServiceGrpc.BookServiceFutureStub bookClient) {
// TODO: use a data loader or batch endpoint
return Futures.allAsList(
shelf
.getBookIdsList()
.stream()
.map(id -> bookClient.getBook(GetBookRequest.newBuilder().setId(id).build()))
.collect(ImmutableList.toImmutableList()));
ListenableFuture<List<Book>> shelfToBooks(Shelf shelf, DataLoaderRegistry dataLoaderRegistry) {
return FutureConverter.toListenableFuture(
dataLoaderRegistry.<String, Book>getDataLoader("books").loadMany(shelf.getBookIdsList()));
}
}

0 comments on commit d214ac9

Please sign in to comment.