Skip to content

Commit

Permalink
Added directive filtration for SchemaDirectives (TypeSystemDirectiveL…
Browse files Browse the repository at this point in the history
…ocation)
  • Loading branch information
mskacelik committed Aug 3, 2023
1 parent fd73c21 commit 282a260
Show file tree
Hide file tree
Showing 43 changed files with 186 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private Schema generateSchema() {
Annotations annotations = Annotations.getAnnotationsForClass(graphQLApiAnnotation.target().asClass());
schema.getDirectiveInstances()
.addAll(directivesHelper
.buildDirectiveInstances(annotations)
.buildDirectiveInstances(annotations, "SCHEMA")
.stream()
.map(directiveInstance -> {
String directiveClassName = directiveInstance.getType().getClassName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public ArgumentCreator(ReferenceCreator referenceCreator) {
deprecatedHelper = new DeprecatedDirectivesHelper();
}

@Override
public String getDirectiveLocation() {
return "ARGUMENT_DEFINITION";
}

/**
* Create an argument model. Arguments exist on Operations as input parameters
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public DirectiveTypeCreator(ReferenceCreator referenceCreator) {
super(referenceCreator);
}

@Override
public String getDirectiveLocation() {
throw new IllegalArgumentException(
"This method should never be called since 'DirectiveType' cannot have another directives");
}

private static DotName NON_NULL = DotName.createSimple("org.eclipse.microprofile.graphql.NonNull");

public DirectiveType create(ClassInfo classInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public FieldCreator(ReferenceCreator referenceCreator) {
deprecatedHelper = new DeprecatedDirectivesHelper();
}

@Override
public String getDirectiveLocation() {
return "FIELD_DEFINITION";
}

/**
* Creates a field from a method only.This is used in the case of an interface
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public TypeAutoNameStrategy getTypeAutoNameStrategy() {
}

/**
* The the return type.This is usually the method return type, but can also be adapted to something else
* The return type.This is usually the method return type, but can also be adapted to something else
*
* @param methodInfo method
* @return the return type
Expand All @@ -60,6 +60,8 @@ protected static Type getReturnType(FieldInfo fieldInfo) {

}

public abstract String getDirectiveLocation();

protected void populateField(Direction direction, Field field, Type type, Annotations annotations) {
// Wrapper
field.setWrapper(WrapperCreator.createWrapper(type).orElse(null));
Expand Down Expand Up @@ -97,7 +99,15 @@ private void doPopulateField(Direction direction, Field field, Type type, Annota

// Directives
if (directives != null) { // this happens while scanning for the directive types
field.addDirectiveInstances(directives.buildDirectiveInstances(annotations));
field.addDirectiveInstances(directives.buildDirectiveInstances(annotations, getDirectiveLocation(direction)));
}
}

private String getDirectiveLocation(Direction direction) {
String directiveLocation = getDirectiveLocation();
if (direction.equals(Direction.IN) && directiveLocation.equals("FIELD_DEFINITION")) {
return "INPUT_FIELD_DEFINITION";
}
return directiveLocation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,9 @@ private void addDirectivesForRolesAllowed(Annotations annotationsForPojo, Annota
operation.addDirectiveInstance(rolesAllowedDirectives);
}
}

@Override
public String getDirectiveLocation() {
return "FIELD_DEFINITION";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,14 @@ public Type create(ClassInfo classInfo, Reference reference) {
addOperations(type, classInfo);

// Directives
addDirectives(type, annotations);

addDirectives(type, annotations, getDirectiveLocation());
return type;
}

protected abstract void addFields(Type type, ClassInfo classInfo, Reference reference);

private void addDirectives(Type type, Annotations annotations) {
type.setDirectiveInstances(directives.buildDirectiveInstances(annotations));
private void addDirectives(Type type, Annotations annotations, String directiveLocation) {
type.setDirectiveInstances(directives.buildDirectiveInstances(annotations, directiveLocation));
}

private void addPolymorphicTypes(Type type, ClassInfo classInfo, Reference reference) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
public interface Creator<T> {

T create(ClassInfo classInfo, Reference reference);

String getDirectiveLocation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ public EnumType create(ClassInfo classInfo, Reference reference) {
return enumType;
}

@Override
public String getDirectiveLocation() {
return "ENUM";
}

private List<DirectiveInstance> getDirectiveInstances(Annotations annotations) {
return directives.buildDirectiveInstances(annotations);
return directives.buildDirectiveInstances(annotations, getDirectiveLocation());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.FieldInfo;
Expand Down Expand Up @@ -37,7 +38,6 @@
*/
public class InputTypeCreator implements Creator<InputType> {
private static final Logger LOG = Logger.getLogger(InputTypeCreator.class.getName());

private final FieldCreator fieldCreator;
private Directives directives;

Expand Down Expand Up @@ -78,6 +78,11 @@ public InputType create(ClassInfo classInfo, Reference reference) {
return inputType;
}

@Override
public String getDirectiveLocation() {
return "INPUT_OBJECT";
}

public boolean hasUseableConstructor(ClassInfo classInfo) {
MethodInfo constructor = findCreator(classInfo);
return constructor != null;
Expand Down Expand Up @@ -138,7 +143,15 @@ public void setDirectives(Directives directives) {
}

private List<DirectiveInstance> getDirectiveInstances(Annotations annotations) {
return directives.buildDirectiveInstances(annotations);
return directives.buildDirectiveInstances(annotations, getDirectiveLocation());
}

private List<DirectiveInstance> filterUnwantedDirectiveInstances(List<DirectiveInstance> directiveInstances,
String directivesLocation) {
return directiveInstances.stream()
.filter(directiveInstance -> directiveInstance.getType()
.getLocations().contains(directivesLocation))
.collect(Collectors.toList());
}

private void addFields(InputType inputType, ClassInfo classInfo, Reference reference) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ protected void addFields(Type interfaceType, ClassInfo classInfo, Reference refe
}
}

@Override
public String getDirectiveLocation() {
return "INTERFACE";
}

private static final String JAVA_DOT = "java.";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,8 @@ private boolean isNotGenericType(MethodInfo method) {
type.kind() != org.jboss.jandex.Type.Kind.PARAMETERIZED_TYPE);
}

@Override
public String getDirectiveLocation() {
return "OBJECT";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@ public UnionType create(ClassInfo classInfo, Reference reference) {

return new UnionType(classInfo.name().toString(), name, maybeDescription.orElse(null));
}

// TODO: create feature that allows adding UNION directives (graphql.schema)
@Override
public String getDirectiveLocation() {
return "UNION";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ public Directives(List<DirectiveType> directiveTypes) {
}
}

public List<DirectiveInstance> buildDirectiveInstances(Annotations annotations) {
public List<DirectiveInstance> buildDirectiveInstances(Annotations annotations, String directiveLocation) {
// only build directive instances from `@Directive` annotations here (that means the `directiveTypes` map),
// because `directiveTypesOther` directives get their instances added on-the-go by classes that extend `ModelCreator`
return directiveTypes.keySet().stream()
.flatMap(annotations::resolve)
.map(this::toDirectiveInstance)
.filter(directiveInstance -> directiveInstance.getType().getLocations().contains(directiveLocation))
.collect(toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void testSchemaWithDirectives() throws IOException {
Path apiDir = Paths.get(System.getProperty("user.dir"), "../../server/api/target/classes/io/smallrye/graphql/api")
.normalize();
indexer.index(Files.newInputStream(apiDir.resolve("Directive.class")));
indexer.index(Files.newInputStream(apiDir.resolve("DirectiveLocation.class")));
indexer.index(Files.newInputStream(apiDir.resolve("TypeSystemDirectiveLocation.class")));
indexer.index(getResourceAsStream("io/smallrye/graphql/index/app/SomeDirective.class"));
indexer.index(getResourceAsStream("io/smallrye/graphql/index/app/Movie.class"));
indexer.index(getResourceAsStream("io/smallrye/graphql/index/app/MovieTriviaController.class"));
Expand All @@ -153,7 +153,7 @@ public void testSchemaWithDirectives() throws IOException {
assertEquals("someDirective", someDirective.getName());
assertEquals(SomeDirective.class.getName(), someDirective.getClassName());
assertEquals(singleton("value"), someDirective.argumentNames());
assertEquals(new HashSet<>(asList("INTERFACE", "FIELD", "OBJECT")), someDirective.getLocations());
assertEquals(new HashSet<>(asList("INTERFACE", "FIELD_DEFINITION", "OBJECT")), someDirective.getLocations());

// check directive instances on type
Type movie = schema.getTypes().get("Movie");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package io.smallrye.graphql.index.app;

import static io.smallrye.graphql.api.DirectiveLocation.FIELD;
import static io.smallrye.graphql.api.DirectiveLocation.INTERFACE;
import static io.smallrye.graphql.api.DirectiveLocation.OBJECT;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.FIELD_DEFINITION;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.INTERFACE;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.OBJECT;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;

import io.smallrye.graphql.api.Directive;

@Directive(on = { OBJECT, INTERFACE, FIELD })
@Directive(on = { OBJECT, INTERFACE, FIELD_DEFINITION })
@Retention(RUNTIME)
public @interface SomeDirective {
String[] value();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.smallrye.graphql.api;

import static io.smallrye.graphql.api.DirectiveLocation.ARGUMENT_DEFINITION;
import static io.smallrye.graphql.api.DirectiveLocation.ENUM_VALUE;
import static io.smallrye.graphql.api.DirectiveLocation.FIELD_DEFINITION;
import static io.smallrye.graphql.api.DirectiveLocation.INPUT_FIELD_DEFINITION;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.ARGUMENT_DEFINITION;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.ENUM_VALUE;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.FIELD_DEFINITION;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.INPUT_FIELD_DEFINITION;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
@Target(ANNOTATION_TYPE)
@Experimental("Not covered by the specification, yet, so subject to change.")
public @interface Directive {
DirectiveLocation[] on();
TypeSystemDirectiveLocation[] on();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.smallrye.graphql.api;

public enum ExecutableDirectiveLocation {
QUERY,
MUTATION,
SUBSCRIPTION,
FIELD,
FRAGMENT_DEFINITION,
FRAGMENT_SPREAD,
INLINE_FRAGMENT,
VARIABLE_DEFINITION,
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,7 @@
*
* @see <a href="http://spec.graphql.org/draft/#DirectiveLocations">GraphQL Spec Directives</a>
*/
public enum DirectiveLocation {
// ExecutableDirectiveLocations:
QUERY,
MUTATION,
SUBSCRIPTION,
FIELD,
FRAGMENT_DEFINITION,
FRAGMENT_SPREAD,
INLINE_FRAGMENT,
VARIABLE_DEFINITION,

// TypeSystemDirectiveLocation
public enum TypeSystemDirectiveLocation {
SCHEMA,
SCALAR,
OBJECT,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.smallrye.graphql.api.federation;

import static io.smallrye.graphql.api.DirectiveLocation.SCHEMA;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.SCHEMA;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Repeatable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.smallrye.graphql.api.federation;

import static io.smallrye.graphql.api.DirectiveLocation.INTERFACE;
import static io.smallrye.graphql.api.DirectiveLocation.OBJECT;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.INTERFACE;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.OBJECT;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.smallrye.graphql.api.federation;

import static io.smallrye.graphql.api.DirectiveLocation.FIELD_DEFINITION;
import static io.smallrye.graphql.api.DirectiveLocation.OBJECT;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.FIELD_DEFINITION;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.OBJECT;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package io.smallrye.graphql.api.federation;

import static io.smallrye.graphql.api.DirectiveLocation.ARGUMENT_DEFINITION;
import static io.smallrye.graphql.api.DirectiveLocation.ENUM;
import static io.smallrye.graphql.api.DirectiveLocation.ENUM_VALUE;
import static io.smallrye.graphql.api.DirectiveLocation.FIELD_DEFINITION;
import static io.smallrye.graphql.api.DirectiveLocation.INPUT_FIELD_DEFINITION;
import static io.smallrye.graphql.api.DirectiveLocation.INPUT_OBJECT;
import static io.smallrye.graphql.api.DirectiveLocation.INTERFACE;
import static io.smallrye.graphql.api.DirectiveLocation.OBJECT;
import static io.smallrye.graphql.api.DirectiveLocation.SCALAR;
import static io.smallrye.graphql.api.DirectiveLocation.UNION;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.ARGUMENT_DEFINITION;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.ENUM;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.ENUM_VALUE;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.FIELD_DEFINITION;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.INPUT_FIELD_DEFINITION;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.INPUT_OBJECT;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.INTERFACE;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.OBJECT;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.SCALAR;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.UNION;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.smallrye.graphql.api.federation;

import static io.smallrye.graphql.api.DirectiveLocation.*;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.smallrye.graphql.api.federation;

import static io.smallrye.graphql.api.DirectiveLocation.INTERFACE;
import static io.smallrye.graphql.api.DirectiveLocation.OBJECT;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.INTERFACE;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.OBJECT;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Repeatable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.smallrye.graphql.api.federation;

import static io.smallrye.graphql.api.DirectiveLocation.FIELD_DEFINITION;
import static io.smallrye.graphql.api.TypeSystemDirectiveLocation.FIELD_DEFINITION;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
Expand Down
Loading

0 comments on commit 282a260

Please sign in to comment.