diff --git a/vertx-web-graphql/src/main/asciidoc/index.adoc b/vertx-web-graphql/src/main/asciidoc/index.adoc index dfe18a27ff..6ec214319e 100644 --- a/vertx-web-graphql/src/main/asciidoc/index.adoc +++ b/vertx-web-graphql/src/main/asciidoc/index.adoc @@ -179,13 +179,12 @@ Then you can return Vert.x futures directly. === Providing data fetchers with some context -Very often, the {@link io.vertx.ext.web.handler.graphql.GraphQLHandler} will be declared after other route handlers. +Very often, the {@link io.vertx.ext.web.handler.graphql.GraphQLHandler}, or the {@link io.vertx.ext.web.handler.graphql.ws.GraphQLWSHandler}, will be declared after other route handlers. For example, you could protect your application with authentication. -In this case, it is likely that your data fetchers will need to know which user is logged-in to narrow down the results. -Let's say your authentication layer stores a `User` object in the {@link io.vertx.ext.web.RoutingContext}. +In this case, it is likely that your data fetchers will need to know which user is logged-in, to narrow down the results. -You may retrieve this object by inspecting the `DataFetchingEnvironment`: +For this, you may retrieve the {@link io.vertx.ext.web.RoutingContext} object by inspecting the `DataFetchingEnvironment`: [source,$lang] ---- diff --git a/vertx-web-graphql/src/main/java/examples/GraphQLExamples.java b/vertx-web-graphql/src/main/java/examples/GraphQLExamples.java index ba1e66ee31..04ee3f2c63 100644 --- a/vertx-web-graphql/src/main/java/examples/GraphQLExamples.java +++ b/vertx-web-graphql/src/main/java/examples/GraphQLExamples.java @@ -31,6 +31,7 @@ import io.vertx.ext.web.FileUpload; import io.vertx.ext.web.Router; import io.vertx.ext.web.RoutingContext; +import io.vertx.ext.web.UserContext; import io.vertx.ext.web.handler.BodyHandler; import io.vertx.ext.web.handler.graphql.ApolloWSHandler; import io.vertx.ext.web.handler.graphql.GraphQLHandler; @@ -155,9 +156,9 @@ static class User { public void routingContextInDataFetchingEnvironment() { DataFetcher>> dataFetcher = environment -> { - RoutingContext routingContext = GraphQLHandler.getRoutingContext(environment.getGraphQlContext()); + RoutingContext routingContext = environment.getGraphQlContext().get(RoutingContext.class); - User user = routingContext.get("user"); + UserContext user = routingContext.user(); Future> future = retrieveLinksPostedBy(user); return future.toCompletionStage(); @@ -165,7 +166,7 @@ public void routingContextInDataFetchingEnvironment() { }; } - private Future> retrieveLinksPostedBy(User user) { + private Future> retrieveLinksPostedBy(UserContext user) { return null; } diff --git a/vertx-web-graphql/src/main/java/io/vertx/ext/web/handler/graphql/impl/ws/ConnectionHandler.java b/vertx-web-graphql/src/main/java/io/vertx/ext/web/handler/graphql/impl/ws/ConnectionHandler.java index f792a34b48..8ebd620b24 100644 --- a/vertx-web-graphql/src/main/java/io/vertx/ext/web/handler/graphql/impl/ws/ConnectionHandler.java +++ b/vertx-web-graphql/src/main/java/io/vertx/ext/web/handler/graphql/impl/ws/ConnectionHandler.java @@ -30,6 +30,7 @@ import io.vertx.core.impl.logging.LoggerFactory; import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; +import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.handler.graphql.ExecutionInputBuilderWithContext; import io.vertx.ext.web.handler.graphql.impl.GraphQLQuery; import io.vertx.ext.web.handler.graphql.ws.ConnectionInitEvent; @@ -38,12 +39,13 @@ import org.reactivestreams.Publisher; import org.reactivestreams.Subscription; +import java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.Executor; -import static io.vertx.ext.web.handler.graphql.impl.ErrorUtil.*; +import static io.vertx.ext.web.handler.graphql.impl.ErrorUtil.toJsonObject; import static io.vertx.ext.web.handler.graphql.ws.MessageType.*; public class ConnectionHandler { @@ -53,13 +55,15 @@ public class ConnectionHandler { private final GraphQLWSHandlerImpl graphQLWSHandler; private final ContextInternal context; private final ServerWebSocket socket; + private final RoutingContext routingContext; private ConnectionState state; - public ConnectionHandler(GraphQLWSHandlerImpl graphQLWSHandler, ContextInternal context, ServerWebSocket socket) { + public ConnectionHandler(GraphQLWSHandlerImpl graphQLWSHandler, ContextInternal context, ServerWebSocket socket, RoutingContext routingContext) { this.graphQLWSHandler = graphQLWSHandler; this.context = context; this.socket = socket; + this.routingContext = routingContext; state = new InitialState(); } @@ -351,6 +355,8 @@ void subscribe(MessageImpl msg) { builder.query(PersistedQuerySupport.PERSISTED_QUERY_MARKER); } + builder.graphQLContext(Collections.singletonMap(RoutingContext.class, routingContext)); + Handler> beforeExecute = graphQLWSHandler.getBeforeExecute(); if (beforeExecute != null) { beforeExecute.handle(new ExecutionInputBuilderWithContext() { diff --git a/vertx-web-graphql/src/main/java/io/vertx/ext/web/handler/graphql/impl/ws/GraphQLWSHandlerImpl.java b/vertx-web-graphql/src/main/java/io/vertx/ext/web/handler/graphql/impl/ws/GraphQLWSHandlerImpl.java index f66a42280a..6e7fb94c96 100644 --- a/vertx-web-graphql/src/main/java/io/vertx/ext/web/handler/graphql/impl/ws/GraphQLWSHandlerImpl.java +++ b/vertx-web-graphql/src/main/java/io/vertx/ext/web/handler/graphql/impl/ws/GraphQLWSHandlerImpl.java @@ -91,7 +91,7 @@ public void handle(RoutingContext rc) { .toWebSocket() .onFailure(rc::fail) .onSuccess(socket -> { - ConnectionHandler handler = new ConnectionHandler(this, context, socket); + ConnectionHandler handler = new ConnectionHandler(this, context, socket, rc); handler.handleConnection(); }); } else {