|
1 | 1 | /** |
2 | 2 | * Copyright 2016 Yurii Rashkovskii |
3 | | - * |
| 3 | + * <p> |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
6 | 6 | * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
| 7 | + * <p> |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
10 | 10 | * Unless required by applicable law or agreed to in writing, software |
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17 | 17 | import graphql.annotations.annotationTypes.GraphQLDescription; |
18 | 18 | import graphql.annotations.annotationTypes.GraphQLField; |
19 | 19 | import graphql.annotations.annotationTypes.GraphQLName; |
| 20 | +import graphql.annotations.annotationTypes.directives.definition.DirectiveLocations; |
20 | 21 | import graphql.annotations.annotationTypes.directives.definition.GraphQLDirectiveDefinition; |
21 | 22 | import graphql.annotations.directives.AnnotationsDirectiveWiring; |
22 | 23 | import graphql.annotations.directives.AnnotationsWiringEnvironment; |
23 | | -import graphql.annotations.annotationTypes.directives.definition.DirectiveLocations; |
24 | 24 | import graphql.annotations.processor.GraphQLAnnotations; |
25 | 25 | import graphql.introspection.Introspection; |
26 | 26 | import graphql.schema.GraphQLDirective; |
|
30 | 30 | import org.testng.annotations.BeforeMethod; |
31 | 31 | import org.testng.annotations.Test; |
32 | 32 |
|
| 33 | +import java.lang.annotation.ElementType; |
| 34 | +import java.lang.annotation.Retention; |
| 35 | +import java.lang.annotation.RetentionPolicy; |
| 36 | +import java.lang.annotation.Target; |
33 | 37 | import java.util.HashSet; |
34 | 38 | import java.util.Set; |
35 | 39 |
|
@@ -110,7 +114,7 @@ public void build_Subscription_SchemaIsCreatedWithSubscription() { |
110 | 114 | assertThat(subscriptionType.getFieldDefinitions().size(), is(1)); |
111 | 115 | } |
112 | 116 |
|
113 | | - public static class GeneralWiring implements AnnotationsDirectiveWiring{ |
| 117 | + public static class GeneralWiring implements AnnotationsDirectiveWiring { |
114 | 118 | @Override |
115 | 119 | public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment) { |
116 | 120 | return null; |
@@ -158,6 +162,62 @@ public void build_MultipleDirectives_SchemaIsCreatedWithDirectives() { |
158 | 162 | assertThat(schema.getDirective("testDirective"), notNullValue()); |
159 | 163 | } |
160 | 164 |
|
| 165 | + |
| 166 | + @GraphQLDirectiveDefinition(wiring = GeneralWiring.class) |
| 167 | + @GraphQLName("upper") |
| 168 | + @Target(ElementType.METHOD) |
| 169 | + @Retention(RetentionPolicy.RUNTIME) |
| 170 | + @DirectiveLocations(Introspection.DirectiveLocation.FIELD_DEFINITION) |
| 171 | + @interface UpperAnnotation { |
| 172 | + boolean isActive() default true; |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + @Test |
| 177 | + public void build_directiveUsingAnnotation_schemaIsCreatedWithDirective() { |
| 178 | + // arrange + act |
| 179 | + GraphQLSchema schema = builder.query(QueryTest.class).directive(UpperAnnotation.class).build(); |
| 180 | + |
| 181 | + // assert |
| 182 | + GraphQLDirective testDirective = schema.getDirective("upper"); |
| 183 | + assertThat(testDirective, notNullValue()); |
| 184 | + assertThat(testDirective.getArguments().size(), is(1)); |
| 185 | + assertThat(testDirective.getArgument("isActive"), notNullValue()); |
| 186 | + } |
| 187 | + |
| 188 | + |
| 189 | + public static class DirectivesContainer { |
| 190 | + @GraphQLName("suffix") |
| 191 | + @GraphQLDirectiveDefinition(wiring = GeneralWiring.class) |
| 192 | + @DirectiveLocations({Introspection.DirectiveLocation.FIELD_DEFINITION, Introspection.DirectiveLocation.ARGUMENT_DEFINITION}) |
| 193 | + public static void suffixDirective(@GraphQLName("suffix") String suffix) { |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + @GraphQLName("upper") |
| 198 | + @GraphQLDirectiveDefinition(wiring = GeneralWiring.class) |
| 199 | + @DirectiveLocations({Introspection.DirectiveLocation.FIELD_DEFINITION, Introspection.DirectiveLocation.ARGUMENT_DEFINITION}) |
| 200 | + public static void upper() { |
| 201 | + |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + |
| 206 | + @Test |
| 207 | + public void build_directive_UsingDirectivesContainer_schemaIsCreatedWithDirective() { |
| 208 | + // arrange + act |
| 209 | + GraphQLSchema schema = builder.query(QueryTest.class).directives(DirectivesContainer.class).build(); |
| 210 | + |
| 211 | + // assert |
| 212 | + GraphQLDirective testDirective = schema.getDirective("suffix"); |
| 213 | + assertThat(testDirective, notNullValue()); |
| 214 | + assertThat(testDirective.getArguments().size(), is(1)); |
| 215 | + assertThat(testDirective.getArgument("suffix"), notNullValue()); |
| 216 | + |
| 217 | + GraphQLDirective upper = schema.getDirective("upper"); |
| 218 | + assertThat(upper, notNullValue()); |
| 219 | + } |
| 220 | + |
161 | 221 | @GraphQLName("additional") |
162 | 222 | public static class AdditionalTypeTest { |
163 | 223 | public int getI() { |
|
0 commit comments