Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit with dataloaders (not working)
- Loading branch information
Showing
5 changed files
with
212 additions
and
137 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
56 changes: 56 additions & 0 deletions
56
...src/main/java/com/google/api/graphql/examples/library/graphqlserver/DataLoaderModule.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 | Original file line | 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.common.util.concurrent.Futures; | |||
import com.google.common.util.concurrent.ListenableFuture; | |||
import com.google.common.util.concurrent.MoreExecutors; | |||
import com.google.example.library.book.v1.Book; | |||
import com.google.example.library.book.v1.BookServiceGrpc; | |||
import com.google.example.library.book.v1.ListBooksRequest; | |||
import com.google.inject.AbstractModule; | |||
import com.google.inject.Provides; | |||
import com.google.inject.servlet.RequestScoped; | |||
import net.javacrumbs.futureconverter.java8guava.FutureConverter; | |||
import org.dataloader.BatchLoader; | |||
import org.dataloader.DataLoader; | |||
import org.dataloader.DataLoaderRegistry; | |||
|
|||
import java.util.List; | |||
|
|||
final class DataLoaderModule extends AbstractModule { | |||
|
|||
@Provides | |||
@RequestScoped | |||
DataLoaderRegistry dataLoaderRegistry(BookServiceGrpc.BookServiceFutureStub bookService) { | |||
|
|||
// TODO: Use multibinder to modularize this, or automate this somehow | |||
BatchLoader<String, Book> bookBatchLoader = | |||
keys -> { | |||
ListenableFuture<List<Book>> listenableFuture = | |||
Futures.transform( | |||
bookService.listBooks(ListBooksRequest.newBuilder().build()), | |||
resp -> resp.getBooksList(), | |||
MoreExecutors.directExecutor()); | |||
// ServletScopes.transferRequest(() -> ... ); ?? | |||
return FutureConverter.toCompletableFuture(listenableFuture); | |||
}; | |||
|
|||
DataLoaderRegistry registry = new DataLoaderRegistry(); | |||
|
|||
registry.register("books", new DataLoader<>(bookBatchLoader)); | |||
return registry; | |||
} | |||
} |
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
110 changes: 110 additions & 0 deletions
110
...s/src/main/java/com/google/api/graphql/examples/library/graphqlserver/GraphQlServlet.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,110 @@ | |||
// 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.execution.GuavaListenableFutureSupport; | |||
import com.google.api.graphql.rejoiner.Schema; | |||
import com.google.common.base.Strings; | |||
import com.google.common.collect.ImmutableMap; | |||
import com.google.common.io.CharStreams; | |||
import com.google.gson.Gson; | |||
import com.google.gson.GsonBuilder; | |||
import com.google.gson.reflect.TypeToken; | |||
import graphql.ExecutionInput; | |||
import graphql.ExecutionResult; | |||
import graphql.GraphQL; | |||
import graphql.execution.instrumentation.ChainedInstrumentation; | |||
import graphql.execution.instrumentation.Instrumentation; | |||
import graphql.execution.instrumentation.tracing.TracingInstrumentation; | |||
import graphql.schema.GraphQLSchema; | |||
|
|||
import javax.inject.Inject; | |||
import javax.inject.Singleton; | |||
import javax.servlet.http.HttpServlet; | |||
import javax.servlet.http.HttpServletRequest; | |||
import javax.servlet.http.HttpServletResponse; | |||
import java.io.IOException; | |||
import java.util.Arrays; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
|
|||
@Singleton | |||
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())); | |||
|
|||
@Inject @Schema GraphQLSchema schema; | |||
|
|||
@Override | |||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { | |||
|
|||
GraphQL graphql = GraphQL.newGraphQL(schema).instrumentation(instrumentation).build(); | |||
|
|||
Map<String, Object> json = readJson(req); | |||
String query = (String) json.get("query"); | |||
if (query == null) { | |||
resp.setStatus(400); | |||
return; | |||
} | |||
String operationName = (String) json.get("operationName"); | |||
Map<String, Object> variables = getVariables(json.get("variables")); | |||
|
|||
ExecutionInput executionInput = | |||
ExecutionInput.newExecutionInput() | |||
.query(query) | |||
.operationName(operationName) | |||
.variables(variables) | |||
.context(new Object()) | |||
.build(); | |||
ExecutionResult executionResult = graphql.execute(executionInput); | |||
resp.setContentType("application/json"); | |||
resp.setStatus(HttpServletResponse.SC_OK); | |||
GSON.toJson(executionResult.toSpecification(), resp.getWriter()); | |||
} | |||
|
|||
private static Map<String, Object> getVariables(Object variables) { | |||
Map<String, Object> variablesWithStringKey = new HashMap<>(); | |||
if (variables instanceof Map) { | |||
((Map) variables).forEach((k, v) -> variablesWithStringKey.put(String.valueOf(k), v)); | |||
} | |||
return variablesWithStringKey; | |||
} | |||
|
|||
private static Map<String, Object> readJson(HttpServletRequest request) { | |||
try { | |||
String json = CharStreams.toString(request.getReader()); | |||
return jsonToMap(json); | |||
} catch (IOException e) { | |||
throw new RuntimeException(e); | |||
} | |||
} | |||
|
|||
private static Map<String, Object> jsonToMap(String json) { | |||
if (Strings.isNullOrEmpty(json)) { | |||
return ImmutableMap.of(); | |||
} | |||
return Optional.<Map<String, Object>>ofNullable(GSON.fromJson(json, MAP_TYPE_TOKEN.getType())) | |||
.orElse(ImmutableMap.of()); | |||
} | |||
} |
34 changes: 0 additions & 34 deletions
34
...es/src/main/java/com/google/api/graphql/examples/library/graphqlserver/LibrarySchema.java
This file was deleted.
Oops, something went wrong.