Skip to content

Commit

Permalink
Merge pull request #30227 from jmartisk/main-srgql-1.9.1-and-issue-30119
Browse files Browse the repository at this point in the history
SmallRye GraphQL 1.9.1/2.0.1 + config property to control Federation
  • Loading branch information
gsmet authored Jan 9, 2023
2 parents d8bf362 + f19bef8 commit 5fe0d02
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<smallrye-health.version>3.3.1</smallrye-health.version>
<smallrye-metrics.version>3.0.5</smallrye-metrics.version>
<smallrye-open-api.version>3.1.1</smallrye-open-api.version>
<smallrye-graphql.version>1.9.0</smallrye-graphql.version>
<smallrye-graphql.version>1.9.1</smallrye-graphql.version>
<smallrye-opentracing.version>2.1.1</smallrye-opentracing.version>
<smallrye-fault-tolerance.version>5.6.0</smallrye-fault-tolerance.version>
<smallrye-jwt.version>3.6.0</smallrye-jwt.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;
Expand Down Expand Up @@ -272,8 +273,10 @@ void buildExecutionService(
SmallRyeGraphQLRecorder recorder,
SmallRyeGraphQLFinalIndexBuildItem graphQLFinalIndexBuildItem,
BeanContainerBuildItem beanContainer,
BuildProducer<SystemPropertyBuildItem> systemPropertyProducer,
SmallRyeGraphQLConfig graphQLConfig) {

activateFederation(graphQLConfig, systemPropertyProducer, graphQLFinalIndexBuildItem);
Schema schema = SchemaBuilder.build(graphQLFinalIndexBuildItem.getFinalIndex(), graphQLConfig.autoNameStrategy);

RuntimeValue<Boolean> initialized = recorder.createExecutionService(beanContainer.getValue(), schema);
Expand Down Expand Up @@ -627,6 +630,43 @@ void activateEventing(SmallRyeGraphQLConfig graphQLConfig, BuildProducer<SystemP
}
}

/*
* Decides whether we want to activate GraphQL federation and updates system properties accordingly.
* If quarkus.smallrye-graphql.federation.enabled is unspecified, enable federation automatically if we see
* any Federation annotations in the app.
* If it is specified, always respect that setting.
*
* This would normally be a separate step like other similar activations, but it updates
* system properties and needs to run before generating the schema, so it is called
* by the build step that generates the schema.
*
* Apart from generating a SystemPropertyBuildItem, it's necessary to also call
* System.setProperty to make sure that the SchemaBuilder, called at build time, sees the correct value.
*/
void activateFederation(SmallRyeGraphQLConfig config,
BuildProducer<SystemPropertyBuildItem> systemProperties,
SmallRyeGraphQLFinalIndexBuildItem index) {
if (config.federationEnabled.isPresent()) {
String value = config.federationEnabled.get().toString();
systemProperties.produce(new SystemPropertyBuildItem(ConfigKey.ENABLE_FEDERATION, value));
System.setProperty(ConfigKey.ENABLE_FEDERATION, value);
} else {
//
boolean foundAnyFederationAnnotation = false;
for (ClassInfo federationAnnotationType : index.getFinalIndex()
.getClassesInPackage("io.smallrye.graphql.api.federation")) {
if (federationAnnotationType.isAnnotation()) {
if (!index.getFinalIndex().getAnnotations(federationAnnotationType.name()).isEmpty()) {
foundAnyFederationAnnotation = true;
}
}
}
String value = Boolean.toString(foundAnyFederationAnnotation);
systemProperties.produce(new SystemPropertyBuildItem(ConfigKey.ENABLE_FEDERATION, value));
System.setProperty(ConfigKey.ENABLE_FEDERATION, value);
}
}

private boolean shouldActivateService(Optional<Boolean> serviceEnabled,
boolean linkedCapabilityIsPresent,
String linkedExtensionName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.quarkus.smallrye.graphql.deployment;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.containsString;

import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Query;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

/**
* Make sure that if no Federation related annotations are in the application, then Federation is
* disabled (unless explicitly enabled in the config).
*/
public class GraphQLFederationDisabledTest extends AbstractGraphQLTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(FooApi.class, Foo.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"));

@Test
public void checkSchemaDoesNotIncludeServiceDeclaration() {
RestAssured.given()
.get("/graphql/schema.graphql")
.then()
.body(not(containsString("type _Service {")));
}

@GraphQLApi
static class FooApi {

@Query
public Foo foo() {
return new Foo();
}

}

static class Foo {

public String name;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public class SmallRyeGraphQLConfig {
@ConfigItem(defaultValue = "graphql")
public String rootPath;

/**
* Enable Apollo Federation. If this value is unspecified, then federation will be enabled
* automatically if any GraphQL Federation annotations are detected in the application.
*/
@ConfigItem(name = "federation.enabled")
public Optional<Boolean> federationEnabled;

/**
* Enable metrics. By default, this is false. If set to true, a metrics extension is required.
*/
Expand Down
2 changes: 1 addition & 1 deletion jakarta/rewrite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ recipeList:
newValue: '6.1.0'
- org.openrewrite.maven.ChangePropertyValue:
key: smallrye-graphql.version
newValue: '2.0.0'
newValue: '2.0.1'
- org.openrewrite.maven.ChangePropertyValue:
key: smallrye-health.version
newValue: '4.0.1'
Expand Down

0 comments on commit 5fe0d02

Please sign in to comment.