Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4f35a65
Refactoring
josie-atkins Mar 3, 2025
603b6dd
tests
josie-atkins Mar 3, 2025
dc120e4
WIP
josie-atkins Mar 12, 2025
b36f3a1
Revert "WIP"
josie-atkins Mar 16, 2025
bbd3c68
Can add Size annotation
josie-atkins Mar 16, 2025
7c63ec6
Tidy
josie-atkins Mar 16, 2025
c340d5a
Add all Jakarta annotations
josie-atkins Mar 17, 2025
5485c54
Merge branch 'master' into RBT-928-pass-through-jakarta-annotations-t…
josie-atkins Mar 17, 2025
0b984e8
Remove unused function
josie-atkins Mar 17, 2025
4d625b6
Fix the tests
josie-atkins Apr 28, 2025
62d8450
Add test for min and max on an integer
josie-atkins Apr 28, 2025
9f35bf3
Add test for a record
josie-atkins Apr 28, 2025
5ea1d0e
Tests pass
josie-atkins May 25, 2025
0debef3
Put validate behinda flag on the SchemaBuilder
josie-atkins May 26, 2025
e6192de
Merge branch 'master' into RBT-928-pass-through-jakarta-annotations-t…
josie-atkins May 26, 2025
0f0b741
Downgrade graphql-java to 22
josie-atkins May 26, 2025
aa7ce9e
formatter
josie-atkins May 26, 2025
23ad41b
Licenses
josie-atkins May 26, 2025
aa6b1cb
Downgrade graphql-java to 22
josie-atkins May 26, 2025
1a2e331
Merge remote-tracking branch 'origin/master' into RBT-928-pass-throug…
josie-atkins Jun 8, 2025
8208742
Downgrade to graphql 22
josie-atkins Jun 8, 2025
ee548da
Revert "Downgrade to graphql 22"
josie-atkins Jul 6, 2025
ad990e6
Merge remote-tracking branch 'origin/master' into RBT-928-pass-throug…
josie-atkins Jul 20, 2025
50e0b05
Upgrade libraries to graphql 24
josie-atkins Jul 20, 2025
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
Prev Previous commit
Next Next commit
Can add Size annotation
  • Loading branch information
josie-atkins committed Mar 16, 2025
commit bbd3c688561f5a355ded9828c2097242c5b9790c
22 changes: 18 additions & 4 deletions src/main/java/com/fleetpin/graphql/builder/DirectiveProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,29 @@ public DirectiveProcessor(GraphQLDirective directive, Map<String, Function<Objec

public static DirectiveProcessor build(EntityProcessor entityProcessor, Class<? extends Annotation> directive) {
var builder = GraphQLDirective.newDirective().name(directive.getSimpleName());
var validLocations = directive.getAnnotation(Directive.class).value();

Introspection.DirectiveLocation[] validLocations;
if (
!directive.isAnnotationPresent(Directive.class) &&
directive
.getName()
.startsWith("jakarta.validation.constraints") // Jakarta constraint annotations don't have the Directive annotation, so we need to manually add in the locations
) {
validLocations = new Introspection.DirectiveLocation[] {
Introspection.DirectiveLocation.ARGUMENT_DEFINITION,
Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION };
} else {
validLocations = directive.getAnnotation(Directive.class).value();

// Check for repeatable tag in annotation and add it
builder.repeatable(directive.getAnnotation(Directive.class).repeatable());
}

// loop through and add valid locations
for (Introspection.DirectiveLocation location : validLocations) {
builder.validLocation(location);
}

// Check for repeatable tag in annotation and add it
builder.repeatable(directive.getAnnotation(Directive.class).repeatable());

// Go through each argument and add name/type to directive
var methods = directive.getDeclaredMethods();
Map<String, Function<Object, GraphQLAppliedDirectiveArgument>> builders = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static DirectivesSchema build(List<RestrictTypeFactory<?>> globalDirectiv
}
continue;
}
if (!directiveType.isAnnotationPresent(Directive.class)) {
if (!directiveType.isAnnotationPresent(Directive.class) && !directiveType.getName().startsWith("jakarta.validation.constraints")) {
continue;
}
if (!directiveType.isAnnotation()) {
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/fleetpin/graphql/builder/MethodProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import com.fleetpin.graphql.builder.annotations.*;
import graphql.GraphQLContext;
import graphql.GraphQLError;
import graphql.execution.DataFetcherResult;
import graphql.schema.*;
import graphql.schema.GraphQLFieldDefinition.Builder;
import graphql.validation.rules.ValidationRules;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.function.Function;

import static com.fleetpin.graphql.builder.EntityUtil.isContext;
Expand Down Expand Up @@ -61,8 +65,7 @@ void process(AuthorizerSchema authorizer, Method method) throws ReflectiveOperat
object.field(process(authorizer, coordinates, null, method));
}

Builder process(AuthorizerSchema authorizer, FieldCoordinates coordinates, TypeMeta parentMeta, Method method)
throws InvocationTargetException, IllegalAccessException {
Builder process(AuthorizerSchema authorizer, FieldCoordinates coordinates, TypeMeta parentMeta, Method method) {
GraphQLFieldDefinition.Builder field = GraphQLFieldDefinition.newFieldDefinition();

entityProcessor.addSchemaDirective(method, method.getDeclaringClass(), field::withAppliedDirective);
Expand Down Expand Up @@ -133,8 +136,17 @@ private DataFetcher<?> buildDataFetcher(TypeMeta meta, Method method) {
resolvers[i] = buildResolver(name, argMeta, parameter.getAnnotations());
}

ValidationRules validationRules = ValidationRules
.newValidationRules()
.build();

DataFetcher<?> fetcher = env -> {
try {
List<GraphQLError> errors = validationRules.runValidationRules(env);
if (!errors.isEmpty()) {
return DataFetcherResult.newResult().errors(errors).data(null).build();
}

Object[] args = new Object[resolvers.length];
for (int i = 0; i < resolvers.length; i++) {
args[i] = resolvers[i].apply(env);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.fleetpin.graphql.builder.annotations.*;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchema;
import jakarta.validation.constraints.Size;
import org.reflections.Reflections;
import org.reflections.scanners.Scanners;

Expand Down Expand Up @@ -161,11 +162,17 @@ private static DirectivesSchema getDirectivesSchema(Reflections reflections) thr
Set<Class<?>> directivesTypes = reflections.getTypesAnnotatedWith(Directive.class);
directivesTypes.addAll(reflections.getTypesAnnotatedWith(DataFetcherWrapper.class));

addAllJakartaAnnotations(directivesTypes);

List<RestrictTypeFactory<?>> globalRestricts = getGlobalRestricts(reflections);

return DirectivesSchema.build(globalRestricts, directivesTypes);
}

private static void addAllJakartaAnnotations(Set<Class<?>> directivesTypes) {
directivesTypes.add(Size.class); // use reflection?
}

private static List<RestrictTypeFactory<?>> getGlobalRestricts(Reflections reflections)
throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Set<Class<?>> restrict = reflections.getTypesAnnotatedWith(Restrict.class);
Expand Down Expand Up @@ -198,7 +205,7 @@ private static List<RestrictTypeFactory<?>> getGlobalRestricts(Reflections refle
globalRestricts.add(factory);
}
}

return globalRestricts;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class JakartaValidationDirectiveTest {
public void testJakartaArgumentAnnotationChangedToConstraint() {
GraphQL schema = GraphQL.newGraphQL(SchemaBuilder.build("com.fleetpin.graphql.builder.type.directive")).build();
var name = schema.getGraphQLSchema().getFieldDefinition(FieldCoordinates.coordinates(schema.getGraphQLSchema().getMutationType(), "setName"));
var constraint = name.getArgument("name").getAppliedDirective("Constraint");
var constraint = name.getArgument("name").getAppliedDirective("Size");
var argument = constraint.getArgument("min");
var min = argument.getValue();
assertEquals(3, min);
Expand All @@ -30,7 +30,7 @@ public void testJakartaArgumentAnnotationChangedToConstraint() {
public void testDirectiveArgumentDefinition() {
Map<String, Object> response = execute("query IntrospectionQuery { __schema { directives { name locations args { name } } } }", null).getData();
List<LinkedHashMap<String, Object>> dir = (List<LinkedHashMap<String, Object>>) ((Map<String, Object>) response.get("__schema")).get("directives");
LinkedHashMap<String, Object> constraint = dir.stream().filter(map -> map.get("name").equals("Constraint")).collect(Collectors.toList()).get(0);
LinkedHashMap<String, Object> constraint = dir.stream().filter(map -> map.get("name").equals("Size")).collect(Collectors.toList()).get(0);

assertEquals(9, dir.size());
assertEquals("ARGUMENT_DEFINITION", ((List<String>) constraint.get("locations")).get(0));
Expand Down
Loading