Queries and Mutations
GraphQL Query
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:
- Make gRPC calls to microservices which can be implemented in any language
- Load protobuf messages directly from storage
- Perform arbitrary logic to produce the result
GraphQL Mutation
final class TodoMutationSchemaModule extends SchemaModule {
@Mutation("createTodo")
ListenableFuture<Todo> createTodo(
CreateTodoRequest request, TodoService todoService, @AuthenticatedUser String email) {
return todoService.createTodo(request, email);
}
}
Nesting Queries and Mutations in a Namespace
Namespaces allow queries and mutations to be nested in logical groups. They can help organize a large number of RPCs and can be useful for avoiding RPC name collisions.
@Namespace("todo")
final class TodoMutationSchemaModule extends SchemaModule {
@Mutation("createTodo")
ListenableFuture<Todo> createTodo(
CreateTodoRequest request, TodoService todoService, @AuthenticatedUser String email) {
return todoService.createTodo(request, email);
}
}
Supported return types
All generated proto messages extend Message
.
- Any subclass of
Message
- Java types, including:
String
,Boolean
,Integer
,Long
, ... ImmutableList<? extends Message>
ListenableFuture<? extends Message>
ListenableFuture<ImmutableList<? extends Message>>