From 1fae647bcc8fa8b4024d8e2da92eef060c2785e1 Mon Sep 17 00:00:00 2001 From: Roman Lovakov Date: Fri, 13 Sep 2024 21:33:03 +0300 Subject: [PATCH] Fix for apollo --- .../graphql/schema/DirectiveIndexer.java | 73 +++++++++++++++++++ .../graphql/schema/SchemaBuilder.java | 8 +- .../graphql/schema/model/DirectiveType.java | 18 +++++ 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 common/schema-builder/src/main/java/io/smallrye/graphql/schema/DirectiveIndexer.java diff --git a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/DirectiveIndexer.java b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/DirectiveIndexer.java new file mode 100644 index 000000000..d73c91270 --- /dev/null +++ b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/DirectiveIndexer.java @@ -0,0 +1,73 @@ +package io.smallrye.graphql.schema; + +import java.io.IOException; + +import org.jboss.jandex.IndexView; +import org.jboss.jandex.Indexer; + +import io.smallrye.graphql.api.OneOf; +import io.smallrye.graphql.api.federation.Authenticated; +import io.smallrye.graphql.api.federation.ComposeDirective; +import io.smallrye.graphql.api.federation.Extends; +import io.smallrye.graphql.api.federation.External; +import io.smallrye.graphql.api.federation.FieldSet; +import io.smallrye.graphql.api.federation.Inaccessible; +import io.smallrye.graphql.api.federation.InterfaceObject; +import io.smallrye.graphql.api.federation.Key; +import io.smallrye.graphql.api.federation.Override; +import io.smallrye.graphql.api.federation.Provides; +import io.smallrye.graphql.api.federation.Requires; +import io.smallrye.graphql.api.federation.Shareable; +import io.smallrye.graphql.api.federation.Tag; +import io.smallrye.graphql.api.federation.link.Import; +import io.smallrye.graphql.api.federation.link.Link; +import io.smallrye.graphql.api.federation.link.Purpose; +import io.smallrye.graphql.api.federation.policy.Policy; +import io.smallrye.graphql.api.federation.policy.PolicyGroup; +import io.smallrye.graphql.api.federation.policy.PolicyItem; +import io.smallrye.graphql.api.federation.requiresscopes.RequiresScopes; +import io.smallrye.graphql.api.federation.requiresscopes.ScopeGroup; +import io.smallrye.graphql.api.federation.requiresscopes.ScopeItem; + +class DirectiveIndexer { + static IndexView directiveIndex() { + Indexer indexer = new Indexer(); + + try { + // directives from the API module + indexer.indexClass(io.smallrye.graphql.api.Deprecated.class); + indexer.indexClass(java.lang.Deprecated.class); + indexer.indexClass(OneOf.class); + + indexer.indexClass(Authenticated.class); + indexer.indexClass(ComposeDirective.class); + indexer.indexClass(Extends.class); + indexer.indexClass(External.class); + indexer.indexClass(FieldSet.class); + indexer.indexClass(Inaccessible.class); + indexer.indexClass(InterfaceObject.class); + indexer.indexClass(Key.class); + indexer.indexClass(Override.class); + indexer.indexClass(Provides.class); + indexer.indexClass(Requires.class); + indexer.indexClass(Shareable.class); + indexer.indexClass(Tag.class); + + indexer.indexClass(Link.class); + indexer.indexClass(Import.class); + indexer.indexClass(Purpose.class); + + indexer.indexClass(Policy.class); + indexer.indexClass(PolicyGroup.class); + indexer.indexClass(PolicyItem.class); + + indexer.indexClass(RequiresScopes.class); + indexer.indexClass(ScopeGroup.class); + indexer.indexClass(ScopeItem.class); + } catch (IOException e) { + throw new RuntimeException(e); + } + + return indexer.complete(); + } +} 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 0d819a3f1..fd47e738f 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 @@ -20,6 +20,7 @@ import org.jboss.jandex.AnnotationTarget; import org.jboss.jandex.AnnotationValue; import org.jboss.jandex.ClassInfo; +import org.jboss.jandex.CompositeIndex; import org.jboss.jandex.DotName; import org.jboss.jandex.IndexView; import org.jboss.jandex.MethodInfo; @@ -100,7 +101,7 @@ public static Schema build(IndexView index) { * @return the Schema */ public static Schema build(IndexView index, TypeAutoNameStrategy autoNameStrategy) { - ScanningContext.register(index); + ScanningContext.register(CompositeIndex.create(index, DirectiveIndexer.directiveIndex())); return new SchemaBuilder(autoNameStrategy).generateSchema(); } @@ -185,6 +186,8 @@ private List getAllMethodsIncludingFromSuperClasses(ClassInfo classI } private void addDirectiveTypes(Schema schema) { + Set directiveTypes = new HashSet<>(); + // custom directives from annotations for (AnnotationInstance annotationInstance : ScanningContext.getIndex().getAnnotations(DIRECTIVE)) { ClassInfo classInfo = annotationInstance.target().asClass(); @@ -193,10 +196,11 @@ private void addDirectiveTypes(Schema schema) { DotName packageName = classInfo.name().packagePrefixName(); if ((packageName == null || !packageName.toString().startsWith(FEDERATION_ANNOTATIONS_PACKAGE.toString()) || federationEnabled) && !isGraphQLJavaDirective(classInfo)) { - schema.addDirectiveType(directiveTypeCreator.create(classInfo)); + directiveTypes.add(directiveTypeCreator.create(classInfo)); } } + directiveTypes.forEach(schema::addDirectiveType); // bean validation directives schema.addDirectiveType(BeanValidationDirectivesHelper.CONSTRAINT_DIRECTIVE_TYPE); // rolesAllowed directive diff --git a/common/schema-model/src/main/java/io/smallrye/graphql/schema/model/DirectiveType.java b/common/schema-model/src/main/java/io/smallrye/graphql/schema/model/DirectiveType.java index 0127e92c7..f72deee58 100644 --- a/common/schema-model/src/main/java/io/smallrye/graphql/schema/model/DirectiveType.java +++ b/common/schema-model/src/main/java/io/smallrye/graphql/schema/model/DirectiveType.java @@ -4,6 +4,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; @@ -90,6 +91,23 @@ public void addArgumentType(DirectiveArgument type) { this.argumentTypes.add(type); } + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + DirectiveType that = (DirectiveType) o; + return repeatable == that.repeatable && Objects.equals(className, that.className) && Objects.equals(name, that.name) + && Objects.equals(description, that.description) && Objects.equals(locations, that.locations) + && Objects.equals(argumentTypes, that.argumentTypes); + } + + @Override + public int hashCode() { + return Objects.hash(className, name, description, locations, argumentTypes, repeatable); + } + @Override public String toString() { return "DirectiveType(" +