Skip to content

Commit

Permalink
GraphQLWS: store the RoutingContext in GraphQLContext (#2474)
Browse files Browse the repository at this point in the history
This can be useful when, e.g. the webapp is protected with authentication.

This was already possible with the GraphQL handler

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
  • Loading branch information
tsegismont committed Aug 29, 2023
1 parent 447fec7 commit a2ee6c8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
7 changes: 3 additions & 4 deletions vertx-web-graphql/src/main/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
----
Expand Down
7 changes: 4 additions & 3 deletions vertx-web-graphql/src/main/java/examples/GraphQLExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -155,17 +156,17 @@ static class User {
public void routingContextInDataFetchingEnvironment() {
DataFetcher<CompletionStage<List<Link>>> 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<List<Link>> future = retrieveLinksPostedBy(user);
return future.toCompletionStage();

};
}

private Future<List<Link>> retrieveLinksPostedBy(User user) {
private Future<List<Link>> retrieveLinksPostedBy(UserContext user) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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();
}

Expand Down Expand Up @@ -351,6 +355,8 @@ void subscribe(MessageImpl msg) {
builder.query(PersistedQuerySupport.PERSISTED_QUERY_MARKER);
}

builder.graphQLContext(Collections.singletonMap(RoutingContext.class, routingContext));

Handler<ExecutionInputBuilderWithContext<Message>> beforeExecute = graphQLWSHandler.getBeforeExecute();
if (beforeExecute != null) {
beforeExecute.handle(new ExecutionInputBuilderWithContext<Message>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a2ee6c8

Please sign in to comment.