google / rejoiner
- воскресенье, 21 января 2018 г. в 03:16:23
Generates a unified GraphQL schema from gRPC microservices and other Protobuf sources
These features are actively being developed.
SchemaModule is a Guice module that is used to generate parts of a GraphQL schema. It finds methods and fields that have Rejoiner annotations when its instantiated. It then looks at the parameters and return type of these methods in order to generated the appropriate GraphQL schema. Examples of queries, mutations, and schema modifications are presented below.
final class TodoQuerySchemaModule extends SchemaModule {
@Query("listTodo")
ListenableFuture<ListTodoResponse> listTodo(ListTodoRequest request, TodoClient todoClient) {
return todoClient.listTodo(request);
}
}
In this example request
is of type ListTodoRequest
a protobuf message, so
it's used as a parameter in the generated GraphQL query. todoService
isn't a
protobuf message, so it's provided by the Guice injector.
This is useful for providing rpc services or database access objects for fetching data. Authentication data can also be provided here.
Common implementations for these annotated methods:
final class TodoMutationSchemaModule extends SchemaModule {
@Mutation("createTodo")
ListenableFuture<Todo> createTodo(
CreateTodoRequest request, TodoService todoService, @AuthenticatedUser String email) {
return todoService.createTodo(request, email);
}
}
In this example we are adding a reference to the User type on the Todo type.
final class TodoToUserSchemaModule extends SchemaModule {
@SchemaModification(addField = "creator", onType = Todo.class)
ListenableFuture<User> todoCreatorToUser(UserService userService, Todo todo) {
return userService.getUserByEmail(todo.getCreatorEmail());
}
}
In this case the Todo parameter is the parent object which can be referenced to get the creators email.
final class TodoModificationsSchemaModule extends SchemaModule {
@SchemaModification
TypeModification removePrivateTodoData =
Type.find(Todo.getDescriptor()).removeField("privateTodoData");
}
import com.google.api.graphql.rejoiner.SchemaProviderModule;
public final class TodoModule extends AbstractModule {
@Override
protected void configure() {
// Guice module that provides the generated GraphQLSchema instance
install(new SchemaProviderModule());
// Install schema modules
install(new TodoQuerySchemaModule());
install(new TodoMutationSchemaModule());
install(new TodoModificationsSchemaModule());
install(new TodoToUserSchemaModule());
}
}
import com.google.api.graphql.rejoiner.Query;
import graphql.schema.GraphQLSchema;
//...
@Inject @Schema GraphQLSchema schema;
All generated proto messages extend Message
.
Message
ImmutableList<? extends Message>
ListenableFuture<? extends Message>
ListenableFuture<ImmutableList<? extends Message>>