diff --git a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/SchemaBuilder.java b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/SchemaBuilder.java index f9b382c24..4cc790c13 100644 --- a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/SchemaBuilder.java +++ b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/SchemaBuilder.java @@ -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 * @@ -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); diff --git a/server/implementation/src/test/java/io/smallrye/graphql/schema/SchemaTest.java b/server/implementation/src/test/java/io/smallrye/graphql/schema/SchemaTest.java index becceb117..11bfd425f 100644 --- a/server/implementation/src/test/java/io/smallrye/graphql/schema/SchemaTest.java +++ b/server/implementation/src/test/java/io/smallrye/graphql/schema/SchemaTest.java @@ -130,7 +130,7 @@ void testSchemaWithFederationDisabled() { " testTypeWithFederation(arg: String): TestTypeWithFederation\n" + "}\n" + "\n" + - "type TestTypeWithFederation @key(fields : [\"id\"]) {\n" + + "type TestTypeWithFederation {\n" + " id: String\n" + "}\n"); } @@ -138,26 +138,32 @@ void testSchemaWithFederationDisabled() { @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) { diff --git a/server/tck/pom.xml b/server/tck/pom.xml index ceee28175..52f59938d 100644 --- a/server/tck/pom.xml +++ b/server/tck/pom.xml @@ -132,6 +132,9 @@ testng.xml + + true +