Skip to content

Commit

Permalink
Adding smallrye-graphQL-logVariables
Browse files Browse the repository at this point in the history
  • Loading branch information
parsharma committed Jul 15, 2021
1 parent 2a55e37 commit 6e9b212
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public interface ConfigKey extends org.eclipse.microprofile.graphql.ConfigKey {
public static final String ERROR_EXTENSION_FIELDS = "smallrye.graphql.errorExtensionFields";
public static final String FIELD_VISIBILITY = "smallrye.graphql.fieldVisibility";
public static final String UNWRAP_EXCEPTIONS = "smallrye.graphql.unwrapExceptions";

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.eclipse.microprofile.config.inject.ConfigProperty;

import io.smallrye.graphql.bootstrap.Config;
import io.smallrye.graphql.bootstrap.LogPayloadOption;

/**
* Configuration for GraphQL
Expand Down Expand Up @@ -91,8 +92,8 @@ public class GraphQLConfig implements Config {
private boolean includeIntrospectionTypesInSchema;

@Inject
@ConfigProperty(name = ConfigKey.LOG_PAYLOAD, defaultValue = "false")
private boolean logPayload;
@ConfigProperty(name = ConfigKey.LOG_PAYLOAD, defaultValue = "off")
private LogPayloadOption logPayload;

@Inject
@ConfigProperty(name = ConfigKey.FIELD_VISIBILITY, defaultValue = Config.FIELD_VISIBILITY_DEFAULT)
Expand Down Expand Up @@ -182,7 +183,7 @@ public boolean isIncludeScalarsInSchema() {
}

@Override
public boolean logPayload() {
public LogPayloadOption logPayload() {
return logPayload;
}

Expand Down Expand Up @@ -254,7 +255,7 @@ public void setIncludeIntrospectionTypesInSchema(boolean includeIntrospectionTyp
this.includeIntrospectionTypesInSchema = includeIntrospectionTypesInSchema;
}

public void setLogPayload(boolean logPayload) {
public void setLogPayload(LogPayloadOption logPayload) {
this.logPayload = logPayload;
}

Expand Down Expand Up @@ -286,4 +287,5 @@ private Optional<List<String>> mergeList(Optional<List<String>> currentList, Opt
return Optional.empty();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.smallrye.graphql.bootstrap;

import static io.smallrye.graphql.bootstrap.LogPayloadOption.OFF;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -75,8 +77,8 @@ default boolean shouldEmitEvents() {
return isTracingEnabled() || isMetricsEnabled() || isValidationEnabled() || isEventsEnabled();
}

default boolean logPayload() {
return false;
default LogPayloadOption logPayload() {
return OFF;
}

default String getFieldVisibility() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.smallrye.graphql.bootstrap;

public enum LogPayloadOption {

OFF("off"),
TRUE("withoutvariable"),
FALSE("off"),
WITHOUTVARIABLE("withoutvariable"),
WITHVARIABLE("withvariable");

private String payload;

LogPayloadOption(String payload) {
this.payload = payload;
}

public void setPayload(String payload) {
this.payload = payload;
}

public String getPayload() {
return payload;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import static io.smallrye.graphql.SmallRyeGraphQLServerLogging.log;

import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;

import javax.json.JsonObject;
Expand All @@ -24,6 +23,7 @@
import io.smallrye.graphql.api.Context;
import io.smallrye.graphql.bootstrap.Config;
import io.smallrye.graphql.bootstrap.DataFetcherFactory;
import io.smallrye.graphql.bootstrap.LogPayloadOption;
import io.smallrye.graphql.execution.context.SmallRyeBatchLoaderContextProvider;
import io.smallrye.graphql.execution.context.SmallRyeContext;
import io.smallrye.graphql.execution.datafetcher.helper.BatchLoaderHelper;
Expand Down Expand Up @@ -74,15 +74,20 @@ public ExecutionResponse execute(JsonObject jsonInput) {

// ExecutionId
ExecutionId finalExecutionId = ExecutionId.from(executionIdPrefix + executionId.getAndIncrement());
LogPayloadOption payloadOption = config.logPayload();

try {
String query = context.getQuery();
Optional<Map<String, Object>> variables = context.getVariables();

if (query == null || query.isEmpty()) {
throw new RuntimeException("Query can not be null");
}
if (config.logPayload()) {
if (payloadOption.getPayload().equals(LogPayloadOption.WITHOUTVARIABLE.getPayload())) {
log.payloadIn(query);
} else if (payloadOption.getPayload().equals(LogPayloadOption.WITHVARIABLE.getPayload())) {
log.payloadIn(query);
log.payloadIn(variables.toString());
}

GraphQL g = getGraphQL();
Expand Down Expand Up @@ -121,7 +126,7 @@ public ExecutionResponse execute(JsonObject jsonInput) {
eventEmitter.fireAfterExecute(context);

ExecutionResponse executionResponse = new ExecutionResponse(executionResult, config);
if (config.logPayload()) {
if (!payloadOption.getPayload().equals(LogPayloadOption.OFF.getPayload())) {
log.payloadOut(executionResponse.toString());
}

Expand Down

0 comments on commit 6e9b212

Please sign in to comment.