Skip to content

Commit

Permalink
Make non-blocking gRPC calls in library example
Browse files Browse the repository at this point in the history
  • Loading branch information
siderakis committed Jan 3, 2018
1 parent e778bcf commit 82da332
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
Expand Up @@ -31,7 +31,8 @@ final class BookSchemaModule extends SchemaModule {

@Query("getBook")
@RelayNode
ListenableFuture<Book> getBook(GetBookRequest request, BookServiceGrpc.BookServiceFutureStub client) {
ListenableFuture<Book> getBook(
GetBookRequest request, BookServiceGrpc.BookServiceFutureStub client) {
return client.getBook(request);
}

Expand All @@ -42,7 +43,8 @@ ListenableFuture<ListBooksResponse> listBooks(
}

@Mutation("createBook")
ListenableFuture<Book> createBook(CreateBookRequest request, BookServiceGrpc.BookServiceFutureStub client) {
ListenableFuture<Book> createBook(
CreateBookRequest request, BookServiceGrpc.BookServiceFutureStub client) {
return client.createBook(request);
}
}
Expand Up @@ -19,13 +19,16 @@
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 java.util.List;

/** A GraphQL {@link SchemaModule} backed by a gRPC service. */
final class LibrarySchemaModule extends SchemaModule {
Expand All @@ -44,13 +47,14 @@ Book createBookAndAddToShelf(
}

@SchemaModification(addField = "books", onType = Shelf.class)
ImmutableList<Book> shelfToBooks(
Shelf shelf, BookServiceGrpc.BookServiceBlockingStub bookClient) {
ListenableFuture<List<Book>> shelfToBooks(
Shelf shelf, BookServiceGrpc.BookServiceFutureStub bookClient) {
// TODO: use a data loader or batch endpoint
return shelf
.getBookIdsList()
.stream()
.map(id -> bookClient.getBook(GetBookRequest.newBuilder().setId(id).build()))
.collect(ImmutableList.toImmutableList());
return Futures.allAsList(
shelf
.getBookIdsList()
.stream()
.map(id -> bookClient.getBook(GetBookRequest.newBuilder().setId(id).build()))
.collect(ImmutableList.toImmutableList()));
}
}
Expand Up @@ -18,6 +18,7 @@
import com.google.api.graphql.rejoiner.Query;
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.shelf.v1.CreateShelfRequest;
import com.google.example.library.shelf.v1.GetShelfRequest;
import com.google.example.library.shelf.v1.ListShelvesRequest;
Expand All @@ -30,25 +31,27 @@
final class ShelfSchemaModule extends SchemaModule {

@Mutation("createShelf")
Shelf createShelf(CreateShelfRequest request, ShelfServiceGrpc.ShelfServiceBlockingStub client) {
ListenableFuture<Shelf> createShelf(
CreateShelfRequest request, ShelfServiceGrpc.ShelfServiceFutureStub client) {
return client.createShelf(request);
}

@Query("getShelf")
@RelayNode
Shelf getShelf(GetShelfRequest request, ShelfServiceGrpc.ShelfServiceBlockingStub client) {
ListenableFuture<Shelf> getShelf(
GetShelfRequest request, ShelfServiceGrpc.ShelfServiceFutureStub client) {
return client.getShelf(request);
}

@Query("listShelves")
ListShelvesResponse listShelves(
ListShelvesRequest request, ShelfServiceGrpc.ShelfServiceBlockingStub client) {
ListenableFuture<ListShelvesResponse> listShelves(
ListShelvesRequest request, ShelfServiceGrpc.ShelfServiceFutureStub client) {
return client.listShelves(request);
}

@Mutation("mergeShelves")
Shelf mergeShelves(
MergeShelvesRequest request, ShelfServiceGrpc.ShelfServiceBlockingStub client) {
ListenableFuture<Shelf> mergeShelves(
MergeShelvesRequest request, ShelfServiceGrpc.ShelfServiceFutureStub client) {
return client.mergeShelves(request);
}
}

0 comments on commit 82da332

Please sign in to comment.