Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SmallRye GraphQL 1.9.1/2.0.1 + config property to control Federation #30227

Merged
merged 1 commit into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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