Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented annotation Deprecated as a directive for the graphQL schem… #1738

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.smallrye.graphql.schema.Annotations;
import io.smallrye.graphql.schema.SchemaBuilderException;
import io.smallrye.graphql.schema.helper.BeanValidationDirectivesHelper;
import io.smallrye.graphql.schema.helper.DeprecatedDirectivesHelper;
import io.smallrye.graphql.schema.helper.Direction;
import io.smallrye.graphql.schema.helper.IgnoreHelper;
import io.smallrye.graphql.schema.helper.MethodHelper;
Expand All @@ -28,12 +29,14 @@
public class ArgumentCreator extends ModelCreator {

private final BeanValidationDirectivesHelper validationHelper;
private final DeprecatedDirectivesHelper deprecatedHelper;

private final Logger logger = Logger.getLogger(ArgumentCreator.class.getName());

public ArgumentCreator(ReferenceCreator referenceCreator) {
super(referenceCreator);
validationHelper = new BeanValidationDirectivesHelper();
deprecatedHelper = new DeprecatedDirectivesHelper();
}

/**
Expand Down Expand Up @@ -94,6 +97,16 @@ public Optional<Argument> createArgument(Operation operation, MethodInfo methodI
argument.addDirectiveInstances(constraintDirectives);
}
}
if (deprecatedHelper != null && directives != null) {
List<DirectiveInstance> deprecatedDirectives = deprecatedHelper
.transformDeprecatedToDirectives(annotationsForThisArgument,
directives.getDirectiveTypes().get(DotName.createSimple("io.smallrye.graphql.api.Deprecated")));
if (!deprecatedDirectives.isEmpty()) {
logger.debug("Adding deprecated directives " + deprecatedDirectives + " to field '" + argument.getName()
+ "' of of method '" + argument.getMethodName() + "'");
argument.addDirectiveInstances(deprecatedDirectives);
}
}

populateField(Direction.IN, argument, argumentType, annotationsForThisArgument);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.Optional;

import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.Type;
Expand All @@ -13,6 +14,7 @@
import io.smallrye.graphql.schema.Classes;
import io.smallrye.graphql.schema.SchemaBuilderException;
import io.smallrye.graphql.schema.helper.BeanValidationDirectivesHelper;
import io.smallrye.graphql.schema.helper.DeprecatedDirectivesHelper;
import io.smallrye.graphql.schema.helper.Direction;
import io.smallrye.graphql.schema.helper.IgnoreHelper;
import io.smallrye.graphql.schema.helper.MethodHelper;
Expand All @@ -30,10 +32,12 @@ public class FieldCreator extends ModelCreator {
private final Logger logger = Logger.getLogger(FieldCreator.class.getName());

private final BeanValidationDirectivesHelper validationHelper;
private final DeprecatedDirectivesHelper deprecatedHelper;

public FieldCreator(ReferenceCreator referenceCreator) {
super(referenceCreator);
validationHelper = new BeanValidationDirectivesHelper();
deprecatedHelper = new DeprecatedDirectivesHelper();
}

/**
Expand Down Expand Up @@ -97,6 +101,7 @@ public Optional<Field> createFieldForPojo(Direction direction, FieldInfo fieldIn
reference);
if (direction == Direction.IN) {
addDirectivesForBeanValidationConstraints(annotationsForPojo, field, parentObjectReference);
addDirectivesForDeprecated(annotationsForPojo, field, parentObjectReference);
}

populateField(direction, field, fieldType, methodType, annotationsForPojo);
Expand All @@ -122,6 +127,7 @@ public Optional<Field> createFieldForParameter(MethodInfo method, short position
String fieldName = fieldInfo != null ? fieldInfo.name() : null;
Field field = new Field(null, fieldName, name, reference);
addDirectivesForBeanValidationConstraints(annotationsForPojo, field, parentObjectReference);
addDirectivesForDeprecated(annotationsForPojo, field, parentObjectReference);
populateField(Direction.IN, field, fieldType, method.parameterType(position), annotationsForPojo);

return Optional.of(field);
Expand Down Expand Up @@ -155,6 +161,7 @@ public Optional<Field> createFieldForPojo(Direction direction, FieldInfo fieldIn
reference);
if (direction == Direction.IN) {
addDirectivesForBeanValidationConstraints(annotationsForPojo, field, parentObjectReference);
addDirectivesForDeprecated(annotationsForPojo, field, parentObjectReference);
}
populateField(direction, field, fieldType, annotationsForPojo);

Expand All @@ -176,6 +183,20 @@ private void addDirectivesForBeanValidationConstraints(Annotations annotationsFo
}
}

private void addDirectivesForDeprecated(Annotations annotationsForPojo, Field field,
Reference parentObjectReference) {
if (deprecatedHelper != null && directives != null) {
List<DirectiveInstance> deprecatedDirectives = deprecatedHelper
.transformDeprecatedToDirectives(annotationsForPojo,
directives.getDirectiveTypes().get(DotName.createSimple("io.smallrye.graphql.api.Deprecated")));
if (!deprecatedDirectives.isEmpty()) {
logger.debug("Adding deprecated directives " + deprecatedDirectives + " to field '" + field.getName()
+ "' of parent type '" + parentObjectReference.getName() + "'");
field.addDirectiveInstances(deprecatedDirectives);
}
}
}

/**
* Checks if method and/or field are use-able as a GraphQL-Field.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public abstract class ModelCreator {

private Directives directives;
protected Directives directives;
protected final ReferenceCreator referenceCreator;

public ModelCreator(ReferenceCreator referenceCreator) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.smallrye.graphql.schema.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.jboss.jandex.DotName;
import org.jboss.logging.Logger;

import io.smallrye.graphql.schema.Annotations;
import io.smallrye.graphql.schema.model.DirectiveInstance;
import io.smallrye.graphql.schema.model.DirectiveType;

public class DeprecatedDirectivesHelper {

private static Logger LOGGER = Logger.getLogger(DeprecatedDirectivesHelper.class);

public List<DirectiveInstance> transformDeprecatedToDirectives(Annotations annotations, DirectiveType directiveType) {
List<DirectiveInstance> result = new ArrayList<>();
Set<DotName> annotationNames = annotations.getAnnotationNames();
for (DotName annotationName : annotationNames) {
if (annotationName.equals(DotName.createSimple("java.lang.Deprecated"))) {
DirectiveInstance directive = new DirectiveInstance();
directive.setType(directiveType);
result.add(directive);
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ private Object valueObject(AnnotationValue annotationValue) {
}
return annotationValue.value();
}

public Map<DotName, DirectiveType> getDirectiveTypes() {
return directiveTypes;
}
}
23 changes: 23 additions & 0 deletions server/api/src/main/java/io/smallrye/graphql/api/Deprecated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Directive(on = {
FIELD_DEFINITION,
ARGUMENT_DEFINITION,
INPUT_FIELD_DEFINITION,
ENUM_VALUE
})
public @interface Deprecated {
String reason() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
@Documented
@Experimental("Allow you to map to a certain scalar class. Not covered by the specification. " +
"Subject to change.")
@Deprecated
@java.lang.Deprecated
public @interface ToScalar {
/**
* @return the scalar to use.
Expand Down