Skip to content

Commit

Permalink
Add a way to pass in a logPublisher
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger authored and jmartisk committed Sep 25, 2024
1 parent b0eb3f2 commit 85e136e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.smallrye.graphql.cdi.producer;

import java.util.Optional;
import java.util.concurrent.SubmissionPublisher;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.Produces;
Expand All @@ -23,6 +26,10 @@ public void setSchema(Schema schema) {
this.schema = schema;
}

public void setTraficPublisher(SubmissionPublisher<String> traficPublisher) {
this.traficPublisher = Optional.of(traficPublisher);
}

public GraphQLSchema initialize(Schema schema) {
return initialize(schema, null, null);
}
Expand Down Expand Up @@ -50,7 +57,7 @@ public GraphQLSchema initialize(boolean allowMultipleDeployments, ExecutionStrat
ExecutionStrategy mutationExecutionStrategy) {

this.graphQLSchema = Bootstrap.bootstrap(schema, allowMultipleDeployments);
this.executionService = new ExecutionService(graphQLSchema, this.schema, queryExecutionStrategy,
this.executionService = new ExecutionService(graphQLSchema, this.schema, this.traficPublisher, queryExecutionStrategy,
mutationExecutionStrategy);
return this.graphQLSchema;
}
Expand All @@ -72,6 +79,8 @@ public GraphQLSchema initialize() {
@Produces
Schema schema;

private Optional<SubmissionPublisher<String>> traficPublisher = Optional.empty();

@Produces
@Dependent
public CDISmallRyeContext produceSmallRyeContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.SubmissionPublisher;
import java.util.concurrent.atomic.AtomicLong;

import jakarta.json.JsonObject;
Expand Down Expand Up @@ -76,14 +77,26 @@ public class ExecutionService {
private final QueryCache queryCache;
private final LogPayloadOption payloadOption;

private final Optional<SubmissionPublisher<String>> traficPublisher;
private final ExecutionStrategy queryExecutionStrategy;
private final ExecutionStrategy mutationExecutionStrategy;

public ExecutionService(GraphQLSchema graphQLSchema, Schema schema) {
this(graphQLSchema, schema, null, null);
this(graphQLSchema, schema, Optional.empty(), null, null);
}

public ExecutionService(GraphQLSchema graphQLSchema, Schema schema, ExecutionStrategy queryExecutionStrategy,
public ExecutionService(GraphQLSchema graphQLSchema, Schema schema, Optional<SubmissionPublisher<String>> traficPublisher) {
this(graphQLSchema, schema, traficPublisher, null, null);
}

public ExecutionService(GraphQLSchema graphQLSchema, Schema schema,
ExecutionStrategy queryExecutionStrategy,
ExecutionStrategy mutationExecutionStrategy) {
this(graphQLSchema, schema, Optional.empty(), queryExecutionStrategy, mutationExecutionStrategy);
}

public ExecutionService(GraphQLSchema graphQLSchema, Schema schema, Optional<SubmissionPublisher<String>> traficPublisher,
ExecutionStrategy queryExecutionStrategy,
ExecutionStrategy mutationExecutionStrategy) {

this.graphQLSchema = graphQLSchema;
Expand All @@ -98,6 +111,7 @@ public ExecutionService(GraphQLSchema graphQLSchema, Schema schema, ExecutionStr

Config config = Config.get();
this.payloadOption = config.logPayload();
this.traficPublisher = traficPublisher;
}

@Deprecated
Expand Down Expand Up @@ -149,12 +163,8 @@ public void execute(JsonObject jsonInput, Map<String, Object> context, Execution
sendError("Missing 'query' field in the request", writer);
return;
}
if (payloadOption.equals(LogPayloadOption.queryOnly)) {
log.payloadIn(query);
} else if (payloadOption.equals(LogPayloadOption.queryAndVariables)) {
log.payloadIn(query);
log.payloadIn(variables.toString());
}

logInput(query, variables);

GraphQL g = getGraphQL();
if (g != null) {
Expand Down Expand Up @@ -267,9 +277,8 @@ private void notifyAndWrite(SmallRyeContext smallRyeContext,
eventEmitter.fireAfterExecute(smallRyeContext);
ExecutionResponse executionResponse = new ExecutionResponse(smallRyeContext.unwrap(ExecutionResult.class),
smallRyeContext.getAddedExtensions());
if (!payloadOption.equals(LogPayloadOption.off)) {
log.payloadOut(executionResponse.toString());
}

logOutput(executionResponse);

writer.write(executionResponse);
}
Expand Down Expand Up @@ -377,4 +386,34 @@ private void setParserOptions(Config config) {
ParserOptions.setDefaultParserOptions(parserOptionsBuilder.build());
}
}

private void logInput(String query, Optional<Map<String, Object>> variables) {
if (payloadOption.equals(LogPayloadOption.queryOnly)) {
log.payloadIn(query);
} else if (payloadOption.equals(LogPayloadOption.queryAndVariables)) {
log.payloadIn(query);
if (variables.isPresent()) {
log.payloadIn(variables.get().toString());
}
}

// Also submit to the provided publisher
if (traficPublisher.isPresent()) {
if (variables.isPresent()) {
traficPublisher.get().submit(variables.get().toString());
}
traficPublisher.get().submit("> " + query);
}
}

private void logOutput(ExecutionResponse executionResponse) {
if (!payloadOption.equals(LogPayloadOption.off)) {
log.payloadOut(executionResponse.getExecutionResultAsJsonObject().toString());
}

// Also submit to the provided publisher
if (traficPublisher.isPresent()) {
traficPublisher.get().submit("< " + executionResponse.getExecutionResultAsJsonObject().toString());
}
}
}

0 comments on commit 85e136e

Please sign in to comment.