Skip to content

Commit

Permalink
Only add Federation-related directive types to the schema if Federati…
Browse files Browse the repository at this point in the history
…on is enabled
  • Loading branch information
jmartisk committed Jan 6, 2023
1 parent 397490c commit 84ff9b3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class SchemaBuilder {
private final DirectiveTypeCreator directiveTypeCreator;
private final UnionCreator unionCreator;

private final DotName FEDERATION_ANNOTATIONS_PACKAGE = DotName.createSimple("io.smallrye.graphql.api.federation");

/**
* This builds the Schema from Jandex
*
Expand Down Expand Up @@ -160,7 +162,13 @@ private void addDirectiveTypes(Schema schema) {
// custom directives from annotations
for (AnnotationInstance annotationInstance : ScanningContext.getIndex().getAnnotations(DIRECTIVE)) {
ClassInfo classInfo = annotationInstance.target().asClass();
schema.addDirectiveType(directiveTypeCreator.create(classInfo));
boolean federationEnabled = Boolean.getBoolean("smallrye.graphql.federation.enabled");
// only add federation-related directive types to the schema if federation is enabled
DotName packageName = classInfo.name().packagePrefixName();
if (packageName == null || !packageName.equals(FEDERATION_ANNOTATIONS_PACKAGE) || federationEnabled) {
schema.addDirectiveType(directiveTypeCreator.create(classInfo));
}

}
// bean validation directives
schema.addDirectiveType(BeanValidationDirectivesHelper.CONSTRAINT_DIRECTIVE_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,34 +130,40 @@ void testSchemaWithFederationDisabled() {
" testTypeWithFederation(arg: String): TestTypeWithFederation\n" +
"}\n" +
"\n" +
"type TestTypeWithFederation @key(fields : [\"id\"]) {\n" +
"type TestTypeWithFederation {\n" +
" id: String\n" +
"}\n");
}

@Test
void testSchemaWithFederation() {
config.federationEnabled = true;
GraphQLSchema graphQLSchema = createGraphQLSchema(Directive.class, Key.class, TestTypeWithFederation.class,
FederationTestApi.class);

assertSchemaEndsWith(graphQLSchema, "\n" +
"union _Entity = TestTypeWithFederation\n" +
"\n" +
"\"Query root\"\n" +
"type Query {\n" +
" _entities(representations: [_Any!]!): [_Entity]!\n" +
" _service: _Service!\n" +
" testTypeWithFederation(arg: String): TestTypeWithFederation\n" +
"}\n" +
"\n" +
"type TestTypeWithFederation @key(fields : [\"id\"]) {\n" +
" id: String\n" +
"}\n" +
"\n" +
"type _Service {\n" +
" sdl: String!\n" +
"}\n");
// need to set it as system property because the SchemaBuilder doesn't have access to the Config object
System.setProperty("smallrye.graphql.federation.enabled", "true");
try {
GraphQLSchema graphQLSchema = createGraphQLSchema(Directive.class, Key.class, TestTypeWithFederation.class,
FederationTestApi.class);

assertSchemaEndsWith(graphQLSchema, "\n" +
"union _Entity = TestTypeWithFederation\n" +
"\n" +
"\"Query root\"\n" +
"type Query {\n" +
" _entities(representations: [_Any!]!): [_Entity]!\n" +
" _service: _Service!\n" +
" testTypeWithFederation(arg: String): TestTypeWithFederation\n" +
"}\n" +
"\n" +
"type TestTypeWithFederation @key(fields : [\"id\"]) {\n" +
" id: String\n" +
"}\n" +
"\n" +
"type _Service {\n" +
" sdl: String!\n" +
"}\n");
} finally {
System.clearProperty("smallrye.graphql.federation.enabled");
}
}

private GraphQLSchema createGraphQLSchema(Class<?>... api) {
Expand Down
3 changes: 3 additions & 0 deletions server/tck/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<systemProperties>
<smallrye.graphql.federation.enabled>true</smallrye.graphql.federation.enabled>
</systemProperties>
</configuration>
</plugin>
<plugin>
Expand Down

0 comments on commit 84ff9b3

Please sign in to comment.