Skip to content

Commit

Permalink
The dataloader seems to be working.
Browse files Browse the repository at this point in the history
We need a more complex example to test.

{
  a: getBook(input: {id: "Z29vZ2xlX2V4YW1wbGVfbGlicmFyeV9ib29rX3YxX0Jvb2s6MA=="}) {
    author
  }
  b: getBook(input: {id: "Z29vZ2xlX2V4YW1wbGVfbGlicmFyeV9ib29rX3YxX0Jvb2s6MA=="}) {
    author
  }
}

INFO: stats: Statistics{loadCount=2, loadErrorCount=0, batchLoadCount=1, batchLoadExceptionCount=0, cacheHitCount=1}
  • Loading branch information
siderakis committed Mar 21, 2018
1 parent 78123ee commit 1964d31
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,18 @@
import com.google.api.graphql.rejoiner.RelayNode;
import com.google.api.graphql.rejoiner.SchemaModule;
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.book.v1.ListBooksRequest;
import com.google.example.library.book.v1.ListBooksResponse;
import com.google.example.library.book.v1.*;
import net.javacrumbs.futureconverter.java8guava.FutureConverter;
import org.dataloader.DataLoaderRegistry;

/** A GraphQL {@link SchemaModule} backed by a gRPC service. */
final class BookSchemaModule extends SchemaModule {

@Query("getBook")
@RelayNode
ListenableFuture<Book> getBook(
GetBookRequest request, BookServiceGrpc.BookServiceFutureStub client) {
return client.getBook(request);
ListenableFuture<Book> getBook(GetBookRequest request, DataLoaderRegistry dataLoaderRegistry) {
return FutureConverter.toListenableFuture(
dataLoaderRegistry.<String, Book>getDataLoader("books").load(request.getId()));
}

@Query("listBooks")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ DataLoaderRegistry dataLoaderRegistry(BookServiceGrpc.BookServiceFutureStub book
keys -> {
ListenableFuture<List<Book>> listenableFuture =
Futures.transform(
bookService.listBooks(ListBooksRequest.newBuilder().build()),
bookService.listBooks(
ListBooksRequest.newBuilder()
.addAllIds(keys)
.setPageSize(keys.size())
.build()),
resp -> resp.getBooksList(),
MoreExecutors.directExecutor());
// ServletScopes.transferRequest(() -> ... ); ??
Expand All @@ -51,6 +55,7 @@ DataLoaderRegistry dataLoaderRegistry(BookServiceGrpc.BookServiceFutureStub book
DataLoaderRegistry registry = new DataLoaderRegistry();

registry.register("books", new DataLoader<>(bookBatchLoader));

return registry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@
import graphql.GraphQL;
import graphql.execution.instrumentation.ChainedInstrumentation;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation;
import graphql.execution.instrumentation.tracing.TracingInstrumentation;
import graphql.schema.GraphQLSchema;
import org.dataloader.DataLoaderRegistry;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -40,25 +43,28 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;

@Singleton
class GraphQlServlet extends HttpServlet {
final class GraphQlServlet extends HttpServlet {

private static final Gson GSON = new GsonBuilder().serializeNulls().create();
private static final TypeToken<Map<String, Object>> MAP_TYPE_TOKEN =
new TypeToken<Map<String, Object>>() {};

private static final Instrumentation instrumentation =
new ChainedInstrumentation(
Arrays.asList(
GuavaListenableFutureSupport.listenableFutureInstrumentation(),
new TracingInstrumentation()));

private static final Logger logger = Logger.getLogger(GraphQlServlet.class.getName());
@Inject @Schema GraphQLSchema schema;
@Inject Provider<DataLoaderRegistry> registryProvider;

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {

Instrumentation instrumentation =
new ChainedInstrumentation(
Arrays.asList(
GuavaListenableFutureSupport.listenableFutureInstrumentation(),
new DataLoaderDispatcherInstrumentation(registryProvider.get()),
new TracingInstrumentation()));
GraphQL graphql = GraphQL.newGraphQL(schema).instrumentation(instrumentation).build();

Map<String, Object> json = readJson(req);
Expand All @@ -81,6 +87,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
resp.setContentType("application/json");
resp.setStatus(HttpServletResponse.SC_OK);
GSON.toJson(executionResult.toSpecification(), resp.getWriter());
logger.info("stats: " + registryProvider.get().getStatistics());
}

private static Map<String, Object> getVariables(Object variables) {
Expand Down
7 changes: 5 additions & 2 deletions examples/src/main/proto/library/book_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,18 @@ message GetBookRequest {
}

message ListBooksRequest {
// The ids of the book to retrieve.
repeated string ids = 1;

// Requested page size. Server may return fewer books than requested.
// If unspecified, server will pick an appropriate default.
int32 page_size = 1;
int32 page_size = 2;

// A token identifying a page of results the server should return.
// Typically, this is the value of
// [ListBooksResponse.next_page_token][google.example.library.v1.ListBooksResponse.next_page_token].
// returned from the previous call to `ListBooks` method.
string page_token = 2;
string page_token = 3;
}

message ListBooksResponse {
Expand Down

0 comments on commit 1964d31

Please sign in to comment.