Skip to content

Commit

Permalink
Merge pull request #227 from yarinvak/support-new-directive-in-schema…
Browse files Browse the repository at this point in the history
…-creator

Support new directives way in the schema creator
  • Loading branch information
yarinvak authored Oct 14, 2019
2 parents 7aad713 + 14c1ac1 commit d193b7d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ In order to define a default value for the argument, use the `default` keyword l

After you created the class, you will be able to create the ``GraphQLDirective`` object using the following code:
```java
GraphQLDirective directive = graphqlAnnotations.directiveViaAnnotation(Suffix.class);
GraphQLDirective directive = graphqlAnnotations.directive(Suffix.class);
```

#### Using a method declaration
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/graphql/annotations/AnnotationsSchemaCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static class Builder {
private Class<?> mutationObject;
private Class<?> subscriptionObject;
private Set<Class<?>> directivesObjectList = new HashSet<>();
private Set<Class<?>> directiveContainerClasses = new HashSet<>();
private Set<Class<?>> additionalTypesList = new HashSet<>();
private Set<Class<?>> typeExtensions = new HashSet<>();
private Set<TypeFunction> typeFunctions = new HashSet<>();
Expand Down Expand Up @@ -113,6 +114,16 @@ public Builder directives(Set<Class<?>> directiveClasses) {
return this;
}

/**
* Add directive declaration class to create directives for the graphql schema
* @param directiveContainerClass a directive container class (directives are defined as methods inside the class)
* @return the builder after adding the directive container class to the list of directive container classes
*/
public Builder directives(Class<?> directiveContainerClass){
this.directiveContainerClasses.add(directiveContainerClass);
return this;
}

/**
* Add a directive to the graphql schema
* This method will generate a GraphQL Directive type out of your java class using the annotations processor
Expand Down Expand Up @@ -223,6 +234,8 @@ public GraphQLSchema build() {
}

Set<GraphQLDirective> directives = directivesObjectList.stream().map(dir -> graphQLAnnotations.directive(dir)).collect(Collectors.toSet());
directiveContainerClasses.forEach(dir->directives.addAll(graphQLAnnotations.directives(dir)));

Set<GraphQLType> additionalTypes = additionalTypesList.stream().map(additionalType ->
additionalType.isInterface() ?
graphQLAnnotations.generateInterface(additionalType) : graphQLAnnotations.object(additionalType)).collect(Collectors.toSet());
Expand All @@ -234,7 +247,7 @@ public GraphQLSchema build() {
if (this.subscriptionObject != null) {
this.graphqlSchemaBuilder.subscription(graphQLAnnotations.object(subscriptionObject));
}
if (!this.directivesObjectList.isEmpty()) {
if (!directives.isEmpty()) {
graphqlSchemaBuilder.additionalDirectives(directives);
}
this.graphqlSchemaBuilder.additionalTypes(additionalTypes).additionalType(Relay.pageInfoType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLObjectType;

import java.lang.annotation.Retention;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -159,21 +158,9 @@ public GraphQLDirective directive(Class<?> object) throws GraphQLAnnotationsExce
}
}

@Deprecated
public GraphQLDirective directiveViaAnnotation(Class<?> annotationClass) {
if (!annotationClass.isAnnotationPresent(GraphQLDirectiveDefinition.class) || !annotationClass.isAnnotationPresent(Retention.class)){
throw new GraphQLAnnotationsException(String.format("The supplied class %s is not annotated with a GraphQLDirectiveDefinition and/or Retention annotation", annotationClass.getSimpleName()), null);
}

try {
GraphQLDirective directive = this.directiveCreator.getDirective(annotationClass);
GraphQLDirectiveDefinition annotation = annotationClass.getAnnotation(GraphQLDirectiveDefinition.class);
this.getContainer().getDirectiveRegistry().put(directive.getName(), new DirectiveAndWiring(directive, annotation.wiring()));
return directive;
} catch (GraphQLAnnotationsException e) {
this.getContainer().getProcessing().clear();
this.getTypeRegistry().clear();
throw e;
}
return this.directive(annotationClass);
}

public Set<GraphQLDirective> directives(Class<?> directivesDeclarationClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import graphql.annotations.annotationTypes.GraphQLDescription;
import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.annotationTypes.GraphQLName;
import graphql.annotations.annotationTypes.directives.definition.DirectiveLocations;
import graphql.annotations.annotationTypes.directives.definition.GraphQLDirectiveDefinition;
import graphql.annotations.directives.AnnotationsDirectiveWiring;
import graphql.annotations.directives.AnnotationsWiringEnvironment;
import graphql.annotations.annotationTypes.directives.definition.DirectiveLocations;
import graphql.annotations.processor.GraphQLAnnotations;
import graphql.introspection.Introspection;
import graphql.schema.GraphQLDirective;
Expand All @@ -30,6 +30,10 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -110,7 +114,7 @@ public void build_Subscription_SchemaIsCreatedWithSubscription() {
assertThat(subscriptionType.getFieldDefinitions().size(), is(1));
}

public static class GeneralWiring implements AnnotationsDirectiveWiring{
public static class GeneralWiring implements AnnotationsDirectiveWiring {
@Override
public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment) {
return null;
Expand Down Expand Up @@ -158,6 +162,62 @@ public void build_MultipleDirectives_SchemaIsCreatedWithDirectives() {
assertThat(schema.getDirective("testDirective"), notNullValue());
}


@GraphQLDirectiveDefinition(wiring = GeneralWiring.class)
@GraphQLName("upper")
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@DirectiveLocations(Introspection.DirectiveLocation.FIELD_DEFINITION)
@interface UpperAnnotation {
boolean isActive() default true;
}


@Test
public void build_directiveUsingAnnotation_schemaIsCreatedWithDirective() {
// arrange + act
GraphQLSchema schema = builder.query(QueryTest.class).directive(UpperAnnotation.class).build();

// assert
GraphQLDirective testDirective = schema.getDirective("upper");
assertThat(testDirective, notNullValue());
assertThat(testDirective.getArguments().size(), is(1));
assertThat(testDirective.getArgument("isActive"), notNullValue());
}


public static class DirectivesContainer {
@GraphQLName("suffix")
@GraphQLDirectiveDefinition(wiring = GeneralWiring.class)
@DirectiveLocations({Introspection.DirectiveLocation.FIELD_DEFINITION, Introspection.DirectiveLocation.ARGUMENT_DEFINITION})
public static void suffixDirective(@GraphQLName("suffix") String suffix) {

}

@GraphQLName("upper")
@GraphQLDirectiveDefinition(wiring = GeneralWiring.class)
@DirectiveLocations({Introspection.DirectiveLocation.FIELD_DEFINITION, Introspection.DirectiveLocation.ARGUMENT_DEFINITION})
public static void upper() {

}
}


@Test
public void build_directive_UsingDirectivesContainer_schemaIsCreatedWithDirective() {
// arrange + act
GraphQLSchema schema = builder.query(QueryTest.class).directives(DirectivesContainer.class).build();

// assert
GraphQLDirective testDirective = schema.getDirective("suffix");
assertThat(testDirective, notNullValue());
assertThat(testDirective.getArguments().size(), is(1));
assertThat(testDirective.getArgument("suffix"), notNullValue());

GraphQLDirective upper = schema.getDirective("upper");
assertThat(upper, notNullValue());
}

@GraphQLName("additional")
public static class AdditionalTypeTest {
public int getI() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package graphql.annotations;

import graphql.annotations.annotationTypes.GraphQLDescription;
import graphql.annotations.annotationTypes.directives.definition.GraphQLDirectiveDefinition;
import graphql.annotations.annotationTypes.GraphQLName;
import graphql.annotations.directives.AnnotationsDirectiveWiring;
import graphql.annotations.annotationTypes.directives.definition.DirectiveLocations;
import graphql.annotations.annotationTypes.directives.definition.GraphQLDirectiveDefinition;
import graphql.annotations.directives.AnnotationsDirectiveWiring;
import graphql.annotations.processor.GraphQLAnnotations;
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
import graphql.introspection.Introspection;
Expand Down Expand Up @@ -171,7 +171,7 @@ public void directive_suppliedDirectiveMethodContainer_returnCorrectDirective()
@Test
public void directive_suppliedDirectiveAnnotation_returnCorrectDirective() {
// Act
GraphQLDirective upper = this.graphQLAnnotations.directiveViaAnnotation(UpperAnnotation.class);
GraphQLDirective upper = this.graphQLAnnotations.directive(UpperAnnotation.class);

// Assert
assertEquals(upper.getName(), "upper");
Expand All @@ -188,7 +188,7 @@ public void directive_suppliedDirectiveAnnotation_returnCorrectDirective() {
@Test(expectedExceptions = GraphQLAnnotationsException.class)
public void directive_suppliedNoDirectiveAnnotation_throwException() {
// Act
GraphQLDirective upper = this.graphQLAnnotations.directiveViaAnnotation(NoDirectiveAnnotation.class);
GraphQLDirective upper = this.graphQLAnnotations.directive(NoDirectiveAnnotation.class);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public class GraphQLDirectivesViaAnnotationDefinitionTest {
@BeforeMethod
public void setUp() {
this.graphQLAnnotations = new GraphQLAnnotations();
this.graphQLAnnotations.directiveViaAnnotation(Upper.class);
this.graphQLAnnotations.directiveViaAnnotation(Suffix.class);
this.graphQLAnnotations.directiveViaAnnotation(DirectiveWithList.class);
this.graphQLAnnotations.directive(Upper.class);
this.graphQLAnnotations.directive(Suffix.class);
this.graphQLAnnotations.directive(DirectiveWithList.class);
GraphQLObjectType object = this.graphQLAnnotations.object(Query.class);
GraphQLCodeRegistry codeRegistry = graphQLAnnotations.getContainer().getCodeRegistryBuilder().build();
this.schema = newSchema().query(object).codeRegistry(codeRegistry).build();
Expand Down

0 comments on commit d193b7d

Please sign in to comment.