From b6b2cb31be70a649413e2baf3b0b9ff6f920e7c4 Mon Sep 17 00:00:00 2001 From: Phillip Kruger Date: Wed, 25 Sep 2024 08:38:07 +1000 Subject: [PATCH] Add a way to pass in a logPublisher Signed-off-by: Phillip Kruger --- .../graphql/cdi/producer/GraphQLProducer.java | 11 +++- .../graphql/execution/ExecutionService.java | 61 +++++++++++++++---- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/server/implementation-cdi/src/main/java/io/smallrye/graphql/cdi/producer/GraphQLProducer.java b/server/implementation-cdi/src/main/java/io/smallrye/graphql/cdi/producer/GraphQLProducer.java index 584f8f3ee..93ec36780 100644 --- a/server/implementation-cdi/src/main/java/io/smallrye/graphql/cdi/producer/GraphQLProducer.java +++ b/server/implementation-cdi/src/main/java/io/smallrye/graphql/cdi/producer/GraphQLProducer.java @@ -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; @@ -23,6 +26,10 @@ public void setSchema(Schema schema) { this.schema = schema; } + public void setTraficPublisher(SubmissionPublisher traficPublisher) { + this.traficPublisher = Optional.of(traficPublisher); + } + public GraphQLSchema initialize(Schema schema) { return initialize(schema, null, null); } @@ -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; } @@ -72,6 +79,8 @@ public GraphQLSchema initialize() { @Produces Schema schema; + private Optional> traficPublisher = Optional.empty(); + @Produces @Dependent public CDISmallRyeContext produceSmallRyeContext() { diff --git a/server/implementation/src/main/java/io/smallrye/graphql/execution/ExecutionService.java b/server/implementation/src/main/java/io/smallrye/graphql/execution/ExecutionService.java index db07c72df..4abf6dc47 100644 --- a/server/implementation/src/main/java/io/smallrye/graphql/execution/ExecutionService.java +++ b/server/implementation/src/main/java/io/smallrye/graphql/execution/ExecutionService.java @@ -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; @@ -76,14 +77,26 @@ public class ExecutionService { private final QueryCache queryCache; private final LogPayloadOption payloadOption; + private final Optional> 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> 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> traficPublisher, + ExecutionStrategy queryExecutionStrategy, ExecutionStrategy mutationExecutionStrategy) { this.graphQLSchema = graphQLSchema; @@ -98,6 +111,7 @@ public ExecutionService(GraphQLSchema graphQLSchema, Schema schema, ExecutionStr Config config = Config.get(); this.payloadOption = config.logPayload(); + this.traficPublisher = traficPublisher; } @Deprecated @@ -149,12 +163,8 @@ public void execute(JsonObject jsonInput, Map 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) { @@ -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); } @@ -377,4 +386,34 @@ private void setParserOptions(Config config) { ParserOptions.setDefaultParserOptions(parserOptionsBuilder.build()); } } + + private void logInput(String query, Optional> 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()); + } + } }