From 7f2cf5327b65362865ddf3a3c92508f9cb1bb26e Mon Sep 17 00:00:00 2001 From: robp94 Date: Sun, 10 Jul 2022 19:37:36 +0200 Subject: [PATCH] Fix #20802 CustomDirective for smallrye-graphql --- .../deployment/SmallRyeGraphQLProcessor.java | 12 ++++++++++++ .../graphql/deployment/CustomDirective.java | 18 ++++++++++++++++++ .../graphql/deployment/GraphQLTest.java | 7 ++++--- .../graphql/deployment/HotReloadTest.java | 2 +- .../smallrye/graphql/deployment/TestPojo.java | 2 ++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/CustomDirective.java diff --git a/extensions/smallrye-graphql/deployment/src/main/java/io/quarkus/smallrye/graphql/deployment/SmallRyeGraphQLProcessor.java b/extensions/smallrye-graphql/deployment/src/main/java/io/quarkus/smallrye/graphql/deployment/SmallRyeGraphQLProcessor.java index dec66bb633f27..c13441f40d9c7 100644 --- a/extensions/smallrye-graphql/deployment/src/main/java/io/quarkus/smallrye/graphql/deployment/SmallRyeGraphQLProcessor.java +++ b/extensions/smallrye-graphql/deployment/src/main/java/io/quarkus/smallrye/graphql/deployment/SmallRyeGraphQLProcessor.java @@ -75,6 +75,7 @@ import io.smallrye.graphql.schema.Annotations; import io.smallrye.graphql.schema.SchemaBuilder; import io.smallrye.graphql.schema.model.Argument; +import io.smallrye.graphql.schema.model.DirectiveType; import io.smallrye.graphql.schema.model.Field; import io.smallrye.graphql.schema.model.Group; import io.smallrye.graphql.schema.model.InputType; @@ -403,6 +404,7 @@ private String[] getSchemaJavaClasses(Schema schema) { classes.addAll(getTypeClassNames(schema.getTypes().values())); classes.addAll(getInputClassNames(schema.getInputs().values())); classes.addAll(getInterfaceClassNames(schema.getInterfaces().values())); + classes.addAll(getDirectiveTypeClassNames(schema.getDirectiveTypes())); return classes.toArray(String[]::new); } @@ -460,6 +462,16 @@ private Set getTypeClassNames(Collection complexGraphQLTypes) { return classes; } + private Set getDirectiveTypeClassNames(Collection complexGraphQLDirectiveTypes) { + Set classes = new HashSet<>(); + for (DirectiveType complexGraphQLDirectiveType : complexGraphQLDirectiveTypes) { + if (complexGraphQLDirectiveType.getClassName() != null) { + classes.add(complexGraphQLDirectiveType.getClassName()); + } + } + return classes; + } + private Set getInputClassNames(Collection complexGraphQLTypes) { Set classes = new HashSet<>(); for (InputType complexGraphQLType : complexGraphQLTypes) { diff --git a/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/CustomDirective.java b/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/CustomDirective.java new file mode 100644 index 0000000000000..09f76ec9fe8a2 --- /dev/null +++ b/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/CustomDirective.java @@ -0,0 +1,18 @@ +package io.quarkus.smallrye.graphql.deployment; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import org.eclipse.microprofile.graphql.Description; +import org.eclipse.microprofile.graphql.NonNull; + +import io.smallrye.graphql.api.Directive; +import io.smallrye.graphql.api.DirectiveLocation; + +@Directive(on = { DirectiveLocation.OBJECT, DirectiveLocation.INTERFACE, DirectiveLocation.FIELD }) +@Retention(RetentionPolicy.RUNTIME) +@Description("test-description") +public @interface CustomDirective { + @NonNull + String[] fields(); +} diff --git a/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/GraphQLTest.java b/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/GraphQLTest.java index 0da708f9f8335..8f2ec50a66ccc 100644 --- a/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/GraphQLTest.java +++ b/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/GraphQLTest.java @@ -1,7 +1,5 @@ package io.quarkus.smallrye.graphql.deployment; -import static io.quarkus.smallrye.graphql.deployment.AbstractGraphQLTest.MEDIATYPE_JSON; - import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; @@ -30,7 +28,7 @@ public class GraphQLTest extends AbstractGraphQLTest { static QuarkusUnitTest test = new QuarkusUnitTest() .withApplicationRoot((jar) -> jar .addClasses(TestResource.class, TestPojo.class, TestRandom.class, TestGenericsPojo.class, - BusinessException.class) + CustomDirective.class, BusinessException.class) .addAsResource(new StringAsset(getPropertyAsString(configuration())), "application.properties") .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")); @@ -51,6 +49,8 @@ public void testSchema() { Assertions.assertTrue(body.contains("type TestGenericsPojo_String {")); Assertions.assertTrue(body.contains("enum SomeEnum {")); Assertions.assertTrue(body.contains("enum Number {")); + Assertions.assertTrue(body.contains("type TestPojo @customDirective(fields : [\"test-pojo\"])")); + Assertions.assertTrue(body.contains("message: String @customDirective(fields : [\"message\"])")); } @Test @@ -253,6 +253,7 @@ public void testContext() { private static Map configuration() { Map m = new HashMap<>(); m.put("quarkus.smallrye-graphql.events.enabled", "true"); + m.put("quarkus.smallrye-graphql.schema-include-directives", "true"); return m; } } diff --git a/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/HotReloadTest.java b/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/HotReloadTest.java index b72d816c9a9d9..372a966b232dc 100644 --- a/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/HotReloadTest.java +++ b/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/HotReloadTest.java @@ -23,7 +23,7 @@ public class HotReloadTest extends AbstractGraphQLTest { final static QuarkusDevModeTest TEST = new QuarkusDevModeTest() .withApplicationRoot((jar) -> jar .addClasses(TestResource.class, TestPojo.class, TestRandom.class, TestGenericsPojo.class, - BusinessException.class) + CustomDirective.class, BusinessException.class) .addAsResource(new StringAsset(getPropertyAsString()), "application.properties") .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")); diff --git a/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/TestPojo.java b/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/TestPojo.java index ad707a3149bfd..28b84e1a263f5 100644 --- a/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/TestPojo.java +++ b/extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/TestPojo.java @@ -6,7 +6,9 @@ /** * Just a test pojo */ +@CustomDirective(fields = "test-pojo") public class TestPojo { + @CustomDirective(fields = "message") private String message; private List list = Arrays.asList("a", "b", "c");