Skip to content

Commit

Permalink
Merge pull request #12783 from phillip-kruger/graphql-warning-issue
Browse files Browse the repository at this point in the history
Clean up the config of service integration
  • Loading branch information
gsmet authored Oct 23, 2020
2 parents 73610ea + df99b6e commit 5b874da
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.smallrye.graphql.deployment;

import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigRoot;
Expand All @@ -15,10 +17,28 @@ public class SmallRyeGraphQLConfig {
String rootPath;

/**
* Enable metrics
* Enable metrics. By default this will be enabled if the metrics extension is added.
*/
@ConfigItem(name = "metrics.enabled")
Optional<Boolean> metricsEnabled;

/**
* Enable tracing. By default this will be enabled if the tracing extension is added.
*/
@ConfigItem(name = "tracing.enabled")
Optional<Boolean> tracingEnabled;

/**
* Enable validation. By default this will be enabled if the Hibernate Validator extension is added.
*/
@ConfigItem(name = "validation.enabled")
Optional<Boolean> validationEnabled;

/**
* Enable eventing. Allow you to receive events on bootstrap and execution.
*/
@ConfigItem(name = "metrics.enabled", defaultValue = "false")
boolean metricsEnabled;
@ConfigItem(name = "events.enabled", defaultValue = "false")
boolean eventsEnabled;

/**
* Change the type naming strategy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public class SmallRyeGraphQLProcessor {
private static final String SCHEMA_PATH = "/schema.graphql";
private static final String SPI_PATH = "META-INF/services/";

// For Service integration
private static final String SERVICE_NOT_AVAILABLE_WARNING = "The %s property is true, but the %s extension is not present. SmallRye GraphQL %s will be disabled.";
private static final String TRUE = "true";
private static final String FALSE = "false";

// For the UI
private static final String GRAPHQL_UI_WEBJAR_GROUP_ID = "io.smallrye";
private static final String GRAPHQL_UI_WEBJAR_ARTIFACT_ID = "smallrye-graphql-ui-graphiql";
Expand Down Expand Up @@ -155,28 +160,6 @@ void buildExecutionService(
reflectiveClassProducer.produce(new ReflectiveClassBuildItem(true, true, getGraphQLJavaClasses()));
}

@BuildStep
void activateMetrics(Capabilities capabilities,
Optional<MetricsCapabilityBuildItem> metricsCapability,
SmallRyeGraphQLConfig smallRyeGraphQLConfig,
BuildProducer<SystemPropertyBuildItem> systemProperties,
BuildProducer<UnremovableBeanBuildItem> unremovableBeans) {
if (smallRyeGraphQLConfig.metricsEnabled) {
if (metricsCapability.isPresent()) {
if (metricsCapability.get().metricsSupported(MetricsFactory.MP_METRICS)) {
unremovableBeans.produce(UnremovableBeanBuildItem.beanClassNames("io.smallrye.metrics.MetricRegistries"));
}
systemProperties.produce(new SystemPropertyBuildItem("smallrye.graphql.metrics.enabled", "true"));
} else {
LOG.info("The quarkus.smallrye-graphql.metrics.enabled property is true, but a metrics " +
"extension is not present. SmallRye GraphQL Metrics will be disabled.");
systemProperties.produce(new SystemPropertyBuildItem("smallrye.graphql.metrics.enabled", "false"));
}
} else {
systemProperties.produce(new SystemPropertyBuildItem("smallrye.graphql.metrics.enabled", "false"));
}
}

@BuildStep
void requireBody(BuildProducer<RequireBodyHandlerBuildItem> requireBodyHandlerProducer) {
// Because we need to read the body
Expand Down Expand Up @@ -224,17 +207,6 @@ void buildEndpoints(
new RouteBuildItem(quarkusConfig.rootPath + SCHEMA_PATH, schemaHandler, HandlerType.BLOCKING));
}

@BuildStep
void openTracingIntegration(Capabilities capabilities,
BuildProducer<SystemPropertyBuildItem> properties) {
// if there is an opentracing tracer available, enable tracing within SmallRye GraphQL
if (capabilities.isPresent(Capability.OPENTRACING)) {
properties.produce(new SystemPropertyBuildItem("smallrye.graphql.tracing.enabled", "true"));
} else {
properties.produce(new SystemPropertyBuildItem("smallrye.graphql.tracing.enabled", "false"));
}
}

private String[] getSchemaJavaClasses(Schema schema) {
// Unique list of classes we need to do reflection on
Set<String> classes = new HashSet<>();
Expand Down Expand Up @@ -341,6 +313,104 @@ private Set<String> getAllReferenceClasses(Reference reference) {
return classes;
}

// Services Integrations

@BuildStep
void activateMetrics(Capabilities capabilities,
Optional<MetricsCapabilityBuildItem> metricsCapability,
BuildProducer<SystemPropertyBuildItem> systemProperties,
BuildProducer<UnremovableBeanBuildItem> unremovableBeans) {

boolean activate = shouldActivateService(capabilities,
quarkusConfig.metricsEnabled,
metricsCapability.isPresent(),
"quarkus-smallrye-metrics",
"metrics",
"quarkus.smallrye-graphql.metrics.enabled");
if (activate) {
if (metricsCapability.isPresent() && metricsCapability.get().metricsSupported(MetricsFactory.MP_METRICS)) {
unremovableBeans.produce(UnremovableBeanBuildItem.beanClassNames("io.smallrye.metrics.MetricRegistries"));
}
systemProperties.produce(new SystemPropertyBuildItem(ConfigKey.ENABLE_METRICS, TRUE));
} else {
systemProperties.produce(new SystemPropertyBuildItem(ConfigKey.ENABLE_METRICS, FALSE));
}
}

@BuildStep
void activateTracing(Capabilities capabilities,
BuildProducer<SystemPropertyBuildItem> systemProperties) {

boolean activate = shouldActivateService(capabilities,
quarkusConfig.tracingEnabled,
"quarkus-smallrye-opentracing",
Capability.OPENTRACING,
"quarkus.smallrye-graphql.tracing.enabled");
if (activate) {
systemProperties.produce(new SystemPropertyBuildItem(ConfigKey.ENABLE_TRACING, TRUE));
} else {
systemProperties.produce(new SystemPropertyBuildItem(ConfigKey.ENABLE_TRACING, FALSE));
}
}

@BuildStep
void activateValidation(Capabilities capabilities,
BuildProducer<SystemPropertyBuildItem> systemProperties) {

boolean activate = shouldActivateService(capabilities,
quarkusConfig.validationEnabled,
"quarkus-hibernate-validator",
Capability.HIBERNATE_VALIDATOR,
"quarkus.smallrye-graphql.validation.enabled");
if (activate) {
systemProperties.produce(new SystemPropertyBuildItem(ConfigKey.ENABLE_VALIDATION, TRUE));
} else {
systemProperties.produce(new SystemPropertyBuildItem(ConfigKey.ENABLE_VALIDATION, FALSE));
}
}

@BuildStep
void activateEventing(BuildProducer<SystemPropertyBuildItem> systemProperties) {
if (quarkusConfig.eventsEnabled) {
systemProperties.produce(new SystemPropertyBuildItem(ConfigKey.ENABLE_EVENTS, TRUE));
} else {
systemProperties.produce(new SystemPropertyBuildItem(ConfigKey.ENABLE_EVENTS, FALSE));
}
}

private boolean shouldActivateService(Capabilities capabilities,
Optional<Boolean> serviceEnabled,
String linkedExtensionName,
Capability linkedCapability,
String configKey) {

return shouldActivateService(capabilities, serviceEnabled, capabilities.isPresent(linkedCapability),
linkedExtensionName, linkedCapability.getName(), configKey);
}

private boolean shouldActivateService(Capabilities capabilities,
Optional<Boolean> serviceEnabled,
boolean linkedCapabilityIsPresent,
String linkedExtensionName,
String linkedCapabilityName,
String configKey) {

if (serviceEnabled.isPresent()) {
// The user explicitly asked from something
boolean isEnabled = serviceEnabled.get();
if (isEnabled && !linkedCapabilityIsPresent) {
// Warn and disable
LOG.warnf(SERVICE_NOT_AVAILABLE_WARNING, configKey, linkedExtensionName, linkedCapabilityName);
}
return (isEnabled && linkedCapabilityIsPresent);
} else {
// Auto dis/enable
return linkedCapabilityIsPresent;
}
}

// UI Related

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
void registerGraphQLUiServletExtension(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void testSchema() {
private static Map<String, String> configuration() {
Map<String, String> m = new HashMap<>();
m.put("quarkus.smallrye-graphql.auto-name-strategy", "MergeInnerClass");
m.put("quarkus.smallrye-graphql.events.enabled", "true");
return m;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static io.quarkus.smallrye.graphql.deployment.AbstractGraphQLTest.MEDIATYPE_JSON;

import java.util.HashMap;
import java.util.Map;

import org.hamcrest.CoreMatchers;
import org.jboss.logging.Logger;
import org.jboss.shrinkwrap.api.ShrinkWrap;
Expand All @@ -28,7 +31,7 @@ public class GraphQLTest extends AbstractGraphQLTest {
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(TestResource.class, TestPojo.class, TestRandom.class, TestGenericsPojo.class)
.addAsResource(new StringAsset(getPropertyAsString()), "application.properties")
.addAsResource(new StringAsset(getPropertyAsString(configuration())), "application.properties")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"));

@Test
Expand Down Expand Up @@ -163,4 +166,10 @@ public void testContext() {
.and()
.body(CoreMatchers.containsString("{\"data\":{\"context\":\"/context\"}}"));
}

private static Map<String, String> configuration() {
Map<String, String> m = new HashMap<>();
m.put("quarkus.smallrye-graphql.events.enabled", "true");
return m;
}
}
5 changes: 0 additions & 5 deletions extensions/smallrye-graphql/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-metrics</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-graphql-cdi</artifactId>
Expand Down

0 comments on commit 5b874da

Please sign in to comment.