Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
283 additions
and
12 deletions.
There are no files selected for viewing
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
37 changes: 37 additions & 0 deletions
37
examples/java/com/google/api/graphql/examples/library/graphqlserver/LibraryProtoWriter.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,37 @@ | ||
// Copyright 2017 Google LLC | ||
// | ||
// 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 | ||
// | ||
// http://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 com.google.api.graphql.examples.library.graphqlserver; | ||
|
||
import com.google.api.graphql.grpc.SchemaToProto; | ||
import com.google.common.base.Charsets; | ||
import com.google.common.io.Files; | ||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
final class LibraryProtoWriter { | ||
public static void main(String[] args) throws IOException { | ||
if (args.length == 0) { | ||
throw new RuntimeException("output path required"); | ||
} | ||
String proto = SchemaToProto.toProto(LibrarySchema.SCHEMA); | ||
BufferedWriter writer = Files.newWriter(new File(args[0]), Charsets.UTF_8); | ||
try { | ||
writer.write(proto); | ||
} finally { | ||
writer.close(); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
examples/java/com/google/api/graphql/examples/library/graphqlserver/LibrarySchema.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,34 @@ | ||
// Copyright 2017 Google LLC | ||
// | ||
// 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 | ||
// | ||
// http://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 com.google.api.graphql.examples.library.graphqlserver; | ||
|
||
import com.google.api.graphql.rejoiner.Schema; | ||
import com.google.api.graphql.rejoiner.SchemaProviderModule; | ||
import com.google.inject.Guice; | ||
import com.google.inject.Key; | ||
import graphql.schema.GraphQLSchema; | ||
|
||
final class LibrarySchema { | ||
static final GraphQLSchema SCHEMA = | ||
Guice.createInjector( | ||
new SchemaProviderModule(), | ||
new BookClientModule(), | ||
new BookSchemaModule(), | ||
new ShelfClientModule(), | ||
new ShelfSchemaModule(), | ||
new LibrarySchemaModule(), | ||
new SeedLibrarySchemaModule()) | ||
.getInstance(Key.get(GraphQLSchema.class, Schema.class)); | ||
} |
56 changes: 56 additions & 0 deletions
56
examples/java/com/google/api/graphql/examples/library/graphqlserver/LibrarySchemaModule.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,56 @@ | ||
// Copyright 2017 Google LLC | ||
// | ||
// 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 | ||
// | ||
// http://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 com.google.api.graphql.examples.library.graphqlserver; | ||
|
||
import com.google.api.graphql.rejoiner.Arg; | ||
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.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; | ||
|
||
/** A GraphQL {@link SchemaModule} backed by a gRPC service. */ | ||
final class LibrarySchemaModule extends SchemaModule { | ||
|
||
@Mutation("createBookAndAddToShelf") | ||
Book createBookAndAddToShelf( | ||
CreateBookRequest request, | ||
@Arg("shelf") GetShelfRequest shelfRequest, | ||
ShelfServiceGrpc.ShelfServiceBlockingStub shelfClient, | ||
BookServiceGrpc.BookServiceBlockingStub bookClient) { | ||
Book book = bookClient.createBook(request); | ||
Shelf shelf = shelfClient.getShelf(shelfRequest); | ||
// TODO: Add an update shelf RPC | ||
// shelfClient.updateShelf(shelf.toBuilder().addBookIds(book.getId()).build()); | ||
return book; | ||
} | ||
|
||
@SchemaModification(addField = "books", onType = Shelf.class) | ||
ImmutableList<Book> shelfToBooks( | ||
Shelf shelf, BookServiceGrpc.BookServiceBlockingStub 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()); | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
...s/java/com/google/api/graphql/examples/library/graphqlserver/SeedLibrarySchemaModule.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,129 @@ | ||
// Copyright 2017 Google LLC | ||
// | ||
// 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 | ||
// | ||
// http://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 com.google.api.graphql.examples.library.graphqlserver; | ||
|
||
import com.google.api.graphql.rejoiner.Mutation; | ||
import com.google.api.graphql.rejoiner.SchemaModule; | ||
import com.google.common.collect.ImmutableList; | ||
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.shelf.v1.CreateShelfRequest; | ||
import com.google.example.library.shelf.v1.Shelf; | ||
import com.google.example.library.shelf.v1.ShelfServiceGrpc; | ||
import com.google.protobuf.Empty; | ||
|
||
/** A GraphQL {@link SchemaModule} backed by a gRPC service. */ | ||
final class SeedLibrarySchemaModule extends SchemaModule { | ||
|
||
@Mutation("seed") | ||
Empty seed( | ||
ShelfServiceGrpc.ShelfServiceBlockingStub shelfClient, | ||
BookServiceGrpc.BookServiceBlockingStub bookClient) { | ||
|
||
String greatExpectations = | ||
bookClient | ||
.createBook( | ||
CreateBookRequest.newBuilder() | ||
.setBook( | ||
Book.newBuilder() | ||
.setAuthor("Charles Dickens") | ||
.setTitle("Great Expectations") | ||
.setRead(false)) | ||
.build()) | ||
.getId(); | ||
String thinkingFastAndSlow = | ||
bookClient | ||
.createBook( | ||
CreateBookRequest.newBuilder() | ||
.setBook( | ||
Book.newBuilder() | ||
.setAuthor("Daniel Kahnemann") | ||
.setTitle("Thinking, Fast and Slow") | ||
.setRead(true)) | ||
.build()) | ||
.getId(); | ||
String theCatcherInTheRye = | ||
bookClient | ||
.createBook( | ||
CreateBookRequest.newBuilder() | ||
.setBook( | ||
Book.newBuilder() | ||
.setAuthor("J. D. Salinger") | ||
.setTitle("The Catcher in the Rye") | ||
.setRead(false)) | ||
.build()) | ||
.getId(); | ||
String huckleberryFinn = | ||
bookClient | ||
.createBook( | ||
CreateBookRequest.newBuilder() | ||
.setBook( | ||
Book.newBuilder() | ||
.setAuthor("Mark Twain") | ||
.setTitle("The Adventures of Huckleberry Finn") | ||
.setRead(false)) | ||
.build()) | ||
.getId(); | ||
String masterAndMargarita = | ||
bookClient | ||
.createBook( | ||
CreateBookRequest.newBuilder() | ||
.setBook( | ||
Book.newBuilder() | ||
.setAuthor("Mikhail Bulgakov") | ||
.setTitle("The Master and Margarita") | ||
.setRead(false)) | ||
.build()) | ||
.getId(); | ||
String warAndPeace = | ||
bookClient | ||
.createBook( | ||
CreateBookRequest.newBuilder() | ||
.setBook( | ||
Book.newBuilder() | ||
.setAuthor("Leo Tolstoy") | ||
.setTitle("War and Peace") | ||
.setRead(true)) | ||
.build()) | ||
.getId(); | ||
|
||
shelfClient.createShelf( | ||
CreateShelfRequest.newBuilder() | ||
.setShelf( | ||
Shelf.newBuilder() | ||
.setTheme("Satire") | ||
.addAllBookIds(ImmutableList.of(greatExpectations, thinkingFastAndSlow)) | ||
.build()) | ||
.build()); | ||
shelfClient.createShelf( | ||
CreateShelfRequest.newBuilder() | ||
.setShelf( | ||
Shelf.newBuilder() | ||
.setTheme("Classics") | ||
.addAllBookIds(ImmutableList.of(theCatcherInTheRye, huckleberryFinn)) | ||
.build()) | ||
.build()); | ||
shelfClient.createShelf( | ||
CreateShelfRequest.newBuilder() | ||
.setShelf( | ||
Shelf.newBuilder() | ||
.setTheme("Russian") | ||
.addAllBookIds(ImmutableList.of(masterAndMargarita, warAndPeace)) | ||
.build()) | ||
.build()); | ||
return Empty.getDefaultInstance(); | ||
} | ||
} |
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