Skip to content

Commit

Permalink
Added union directive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mskacelik authored and jmartisk committed Aug 10, 2023
1 parent 65c9b23 commit 30d1b24
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public UnionType create(ClassInfo classInfo, Reference reference) {

}

// TODO: create feature that allows adding UNION directives (graphql.schema)
@Override
public String getDirectiveLocation() {
return "UNION";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static java.util.Objects.requireNonNull;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -20,6 +21,7 @@
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -124,6 +126,24 @@ void schemaWithEnumDirectives() {
"Enum value EnumWithDirectives.B should not have directive @enumDirective");
}

@Test
void schemaWithUnionDirectives() {
GraphQLSchema graphQLSchema = createGraphQLSchema(UnionDirective.class, UnionTestApi.class,
UnionTestApi.SomeUnion.class, UnionTestApi.SomeClass.class);

GraphQLUnionType unionWithDirectives = graphQLSchema.getTypeAs("SomeUnion");
List<GraphQLDirective> unionDirectives = unionWithDirectives.getDirectives("unionDirective");
assertFalse(unionDirectives.isEmpty(),
"Union SomeUnion should have directive @unionDirective");
assertEquals(3, unionDirectives.size(), "Union SomeUnion should have 3 @unionDirective instances");
Set<String> expectedDirectivesArgValues = new HashSet<>(Arrays.asList("A", "B", "C"));
unionDirectives.forEach(directive -> directive.getArguments()
.forEach(argument -> assertFalse(expectedDirectivesArgValues.add(argument.toAppliedArgument().getValue()),
"Unexpected directive argument value")));
assertTrue(unionWithDirectives.getDirectives("InputDirective").isEmpty(),
"Union SomeUnion should not have a directive @inputDirective");
}

@Test
void schemaWithInputDirectives() {
GraphQLSchema graphQLSchema = createGraphQLSchema(InputDirective.class, OutputDirective.class, InputTestApi.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.smallrye.graphql.schema;

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

import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;

import io.smallrye.graphql.api.Directive;

@Retention(RUNTIME)
@Directive(on = { UNION })
@Repeatable(UnionDirective.UnionDirectives.class)
public @interface UnionDirective {
String value();

@Retention(RUNTIME)
@interface UnionDirectives {
UnionDirective[] value();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.smallrye.graphql.schema;

import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Query;

import io.smallrye.graphql.api.Union;

@GraphQLApi
public class UnionTestApi {

@Query
public SomeUnion query() {
return null;
}

static class SomeClass implements SomeUnion {
private int number;

public int getNumber() {
return number;
}

}

@Union
@UnionDirective(value = "C")
@UnionDirective(value = "A")
@InputDirective // should be ignored
@UnionDirective(value = "B")
interface SomeUnion {
}

}

0 comments on commit 30d1b24

Please sign in to comment.