Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
- adding configureShema method to SchemaModule so that subclasses can…
Browse files Browse the repository at this point in the history
… call addQuery/addMutation/...

- adding all RPCs to Firestore schema module.
  • Loading branch information
siderakis committed Mar 26, 2018
1 parent 9bba99d commit 30e74d7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
public abstract class SchemaModule extends AbstractModule {

private final ImmutableSet.Builder<Descriptor> referencedDescriptors = ImmutableSet.builder();
private final List<GraphQLFieldDefinition> allQueriesInModule = new ArrayList<>();
private final List<GraphQLFieldDefinition> allMutationsInModule = new ArrayList<>();

/**
* Returns a reference to the GraphQL type corresponding to the supplied proto.
Expand All @@ -76,28 +78,40 @@ protected ImmutableList<GraphQLFieldDefinition> extraMutations() {
return ImmutableList.of();
}

protected void addQuery(GraphQLFieldDefinition query) {
allQueriesInModule.add(query);
}
protected void addMutation(GraphQLFieldDefinition mutation) {
allMutationsInModule.add(mutation);
}
protected void addQueryList(List<GraphQLFieldDefinition> queries) {
allQueriesInModule.addAll(queries);
}
protected void addMutationList(List<GraphQLFieldDefinition> mutations) {
allMutationsInModule.addAll(mutations);
}

protected void configureSchema() {}

@Override
protected final void configure() {
configureSchema();
Multibinder<GraphQLFieldDefinition> queryMultibinder =
Multibinder.newSetBinder(
binder(), new TypeLiteral<GraphQLFieldDefinition>() {}, Annotations.Queries.class);
Multibinder<GraphQLFieldDefinition> mutationMultibinder =
Multibinder.newSetBinder(
binder(), new TypeLiteral<GraphQLFieldDefinition>() {}, Annotations.Mutations.class);
extraMutations().forEach(mutation -> mutationMultibinder.addBinding().toInstance(mutation));
Multibinder<TypeModification> typeModificationMultibinder =
Multibinder.newSetBinder(
binder(), new TypeLiteral<TypeModification>() {}, Annotations.GraphModifications.class);
Multibinder<FileDescriptor> extraTypesMultibinder =
Multibinder.newSetBinder(
binder(), new TypeLiteral<FileDescriptor>() {}, Annotations.ExtraTypes.class);

Multibinder<NodeDataFetcher> relayIdMultibinder =
Multibinder.newSetBinder(
binder(), new TypeLiteral<NodeDataFetcher>() {}, Annotations.Queries.class);

List<GraphQLFieldDefinition> allQueriesInModule = new ArrayList<>();
List<GraphQLFieldDefinition> allMutationsInModule = new ArrayList<>();
allMutationsInModule.addAll(extraMutations());

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,59 @@

import com.google.api.graphql.rejoiner.GaxSchemaModule;
import com.google.api.graphql.rejoiner.Namespace;
import com.google.api.graphql.rejoiner.Query;
import com.google.api.graphql.rejoiner.SchemaModule;
import com.google.cloud.container.v1.ClusterManagerClient;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.container.v1.Cluster;
import com.google.container.v1.GetClusterRequest;
import com.google.container.v1.ListClustersRequest;
import com.google.container.v1.ListClustersResponse;

import static com.google.api.graphql.schema.FuturesConverter.apiFutureToListenableFuture;
import com.google.common.collect.ImmutableList;

/**
* A GraphQL {@link SchemaModule} backed by a gRPC service.
*
* <p>Calls to this gRPC API is managed by an generated client library, which manages the channel,
* authentication, etc.
*
* <p>https://github.com/googleapis/googleapis/blob/master/google/container/v1/cluster_service.proto
*/
@Namespace("clusterManager")
public final class ContainerSchemaModule extends GaxSchemaModule {

@Query("listClusters")
ListenableFuture<ListClustersResponse> listClusters(
ClusterManagerClient client, ListClustersRequest request) {
return apiFutureToListenableFuture(client.listClustersCallable().futureCall(request));
}

@Query("getCluster")
ListenableFuture<Cluster> getCluster(ClusterManagerClient client, GetClusterRequest request) {
return apiFutureToListenableFuture(client.getClusterCallable().futureCall(request));
@Override
protected void configureSchema() {
addQueryList(
serviceToFields(
ClusterManagerClient.class,
ImmutableList.of(
"listClusters",
"getCluster",
"listOperations",
"getOperation",
"getServerConfig",
"listNodePools",
"getNodePool")));
addMutationList(
serviceToFields(
ClusterManagerClient.class,
ImmutableList.of(
"createCluster",
"updateCluster",
"setNodePoolAutoscaling",
"setLoggingService",
"setMonitoringService",
"setAddonsConfig",
"setLocations",
"updateMaster",
"setMasterAuth",
"deleteCluster",
"cancelOperation",
"createNodePool",
"deleteNodePool",
"rollbackNodePoolUpgrade",
"setNodePoolManagement",
"setLabels",
"setLabels",
"startIPRotation",
"completeIPRotation",
"setNodePoolSize",
"setNetworkPolicy",
"setMaintenancePolicy")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,9 @@

import com.google.api.graphql.rejoiner.GaxSchemaModule;
import com.google.api.graphql.rejoiner.Namespace;
import com.google.api.graphql.rejoiner.Query;
import com.google.api.graphql.rejoiner.SchemaModule;
import com.google.cloud.firestore.v1beta1.FirestoreClient;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.firestore.v1beta1.Document;
import com.google.firestore.v1beta1.GetDocumentRequest;
import com.google.firestore.v1beta1.ListDocumentsRequest;
import com.google.firestore.v1beta1.ListDocumentsResponse;
import graphql.schema.GraphQLFieldDefinition;

import static com.google.api.graphql.schema.FuturesConverter.apiFutureToListenableFuture;

/**
* A GraphQL {@link SchemaModule} backed by a gRPC service.
Expand All @@ -40,23 +31,13 @@
@Namespace("firestore")
public final class FirestoreSchemaModule extends GaxSchemaModule {

@Query("getDocument")
ListenableFuture<Document> getDocument(GetDocumentRequest request, FirestoreClient client) {
// TODO: consider using a dataloader
return apiFutureToListenableFuture(client.getDocumentCallable().futureCall(request));
}

@Query("listDocuments")
ListenableFuture<ListDocumentsResponse> listDocuments(
ListDocumentsRequest request, FirestoreClient client) {
return apiFutureToListenableFuture(client.listDocumentsCallable().futureCall(request));
}

@Override
protected ImmutableList<GraphQLFieldDefinition> extraMutations() {
return serviceToFields(
FirestoreClient.class,
ImmutableList.of("createDocument", "updateDocument", "deleteDocument"));
protected void configureSchema() {
addMutationList(
serviceToFields(
FirestoreClient.class,
ImmutableList.of("createDocument", "updateDocument", "deleteDocument")));
addQueryList(
serviceToFields(FirestoreClient.class, ImmutableList.of("getDocument", "listDocuments")));
}

}

0 comments on commit 30e74d7

Please sign in to comment.