package examples;
import graphql.GraphQL;
import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.FieldWiringEnvironment;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.WiringFactory;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpHeaders;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.graphql.*;
import org.dataloader.BatchLoaderEnvironment;
import org.dataloader.BatchLoaderWithContext;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderRegistry;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
public class GraphQLExamples {
public void handlerSetup(Router router) {
GraphQL graphQL = setupGraphQLJava();
router.route("/graphql").handler(GraphQLHandler.create(graphQL));
}
public void handlerSetupPost(Router router) {
GraphQL graphQL = setupGraphQLJava();
router.post("/graphql").handler(GraphQLHandler.create(graphQL));
}
public void handlerSetupGraphiQL(GraphQL graphQL, Router router) {
GraphQLHandlerOptions options = new GraphQLHandlerOptions()
.setGraphiQLOptions(new GraphiQLOptions()
.setEnabled(true)
);
router.route("/graphql").handler(GraphQLHandler.create(graphQL, options));
}
public void handlerSetupGraphiQLAuthn(GraphQL graphQL, Router router) {
GraphQLHandlerOptions options = new GraphQLHandlerOptions()
.setGraphiQLOptions(new GraphiQLOptions()
.setEnabled(true)
);
GraphQLHandler graphQLHandler = GraphQLHandler.create(graphQL, options)
.graphiQLRequestHeaders(rc -> {
String token = rc.get("token");
return MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.AUTHORIZATION, "Bearer " + token);
});
}
public void handlerSetupBatching(GraphQL graphQL) {
GraphQLHandlerOptions options = new GraphQLHandlerOptions()
.setRequestBatchingEnabled(true);
GraphQLHandler handler = GraphQLHandler.create(graphQL, options);
}
private GraphQL setupGraphQLJava() {
return null;
}
class Link {}
private void completionStageDataFetcher() {
DataFetcher<CompletionStage<List<Link>>> dataFetcher = environment -> {
CompletableFuture<List<Link>> completableFuture = new CompletableFuture<>();
retrieveLinksFromBackend(environment, ar -> {
if (ar.succeeded()) {
completableFuture.complete(ar.result());
} else {
completableFuture.completeExceptionally(ar.cause());
}
});
return completableFuture;
};
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type("Query", builder -> builder.dataFetcher("allLinks", dataFetcher))
.build();
}
private void retrieveLinksFromBackend(DataFetchingEnvironment environment, Handler<AsyncResult<List<Link>>> handler) {
}
private void vertxDataFetcher() {
VertxDataFetcher<List<Link>> dataFetcher = new VertxDataFetcher<>((environment, future) -> {
retrieveLinksFromBackend(environment, future);
});
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type("Query", builder -> builder.dataFetcher("allLinks", dataFetcher))
.build();
}
class User {}
private void routingContextInDataFetchingEnvironment() {
VertxDataFetcher<List<Link>> dataFetcher = new VertxDataFetcher<>((environment, future) -> {
RoutingContext routingContext = environment.getContext();
User user = routingContext.get("user");
retrieveLinksPostedBy(user, future);
});
}
private void retrieveLinksPostedBy(User user, Handler<AsyncResult<List<Link>>> future) {
}
private void customContextInDataFetchingEnvironment(Router router) {
VertxDataFetcher<List<Link>> dataFetcher = new VertxDataFetcher<>((environment, future) -> {
User user = environment.getContext();
retrieveLinksPostedBy(user, future);
});
GraphQL graphQL = setupGraphQLJava(dataFetcher);
GraphQLHandler handler = GraphQLHandler.create(graphQL).queryContext(routingContext -> {
return routingContext.get("user");
});
router.route("/graphql").handler(handler);
}
private GraphQL setupGraphQLJava(DataFetcher<CompletionStage<List<Link>>> dataFetcher) {
return null;
}
private void jsonData() {
RuntimeWiring.Builder builder = RuntimeWiring.newRuntimeWiring();
builder.wiringFactory(new WiringFactory() {
@Override
public DataFetcher getDefaultDataFetcher(FieldWiringEnvironment environment) {
return new VertxPropertyDataFetcher(environment.getFieldDefinition().getName());
}
});
}
public void dataLoaderDispatcherInstrumentation(GraphQLSchema graphQLSchema) {
DataLoaderDispatcherInstrumentation dispatcherInstrumentation = new DataLoaderDispatcherInstrumentation();
GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema)
.instrumentation(dispatcherInstrumentation)
.build();
}
public void createBatchLoader() {
BatchLoaderWithContext<String, Link> linksBatchLoader = (keys, environment) -> {
return retrieveLinksFromBackend(keys, environment);
};
}
private CompletionStage<List<Link>> retrieveLinksFromBackend(List<String> ids, BatchLoaderEnvironment environment) {
return null;
}
public void dataLoaderRegistry(GraphQL graphQL, BatchLoaderWithContext<String, Link> linksBatchLoader) {
GraphQLHandler handler = GraphQLHandler.create(graphQL).dataLoaderRegistry(rc -> {
DataLoader<String, Link> linkDataLoader = DataLoader.newDataLoader(linksBatchLoader);
return new DataLoaderRegistry().register("link", linkDataLoader);
});
}
}