11/**
22 * Copyright 2016 Yurii Rashkovskii
3- *
3+ * <p>
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
66 * 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>
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
1212 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313 * See the License for the specific language governing permissions and
1414 */
1515package graphql .annotations .processor ;
1616
17+ import graphql .annotations .annotationTypes .GraphQLDirectiveDefinition ;
1718import graphql .annotations .annotationTypes .GraphQLName ;
19+ import graphql .annotations .directives .creation .DirectiveWiring ;
1820import graphql .annotations .processor .directives .CommonPropertiesCreator ;
1921import graphql .annotations .processor .directives .DirectiveArgumentCreator ;
2022import graphql .annotations .processor .directives .DirectiveCreator ;
3335import graphql .schema .GraphQLInterfaceType ;
3436import graphql .schema .GraphQLObjectType ;
3537
38+ import java .lang .reflect .Method ;
3639import java .util .Arrays ;
40+ import java .util .HashSet ;
3741import java .util .Map ;
42+ import java .util .Set ;
3843
3944import static graphql .annotations .processor .util .NamingKit .toGraphqlName ;
4045
@@ -125,8 +130,8 @@ public GraphQLObjectType object(Class<?> object) throws GraphQLAnnotationsExcept
125130 }
126131
127132 @ Deprecated
128- public GraphQLObjectType object (Class <?> object , GraphQLDirective ... directives ) throws GraphQLAnnotationsException {
129- Arrays .stream (directives ).forEach (directive -> this .getContainer ().getDirectiveRegistry ().put (directive .getName (), directive ));
133+ public GraphQLObjectType object (Class <?> object , DirectiveAndWiring ... directives ) throws GraphQLAnnotationsException {
134+ Arrays .stream (directives ).forEach (directive -> this .getContainer ().getDirectiveRegistry ().put (directive .getDirective (). getName (), directive ));
130135 try {
131136 return this .graphQLObjectHandler .getGraphQLType (object , this .getContainer ());
132137 } catch (GraphQLAnnotationsException e ) {
@@ -139,7 +144,11 @@ public GraphQLObjectType object(Class<?> object, GraphQLDirective... directives)
139144 public GraphQLDirective directive (Class <?> object ) throws GraphQLAnnotationsException {
140145 try {
141146 GraphQLDirective directive = this .directiveCreator .getDirective (object );
142- this .getContainer ().getDirectiveRegistry ().put (directive .getName (), directive );
147+ DirectiveWiring annotation = object .getAnnotation (DirectiveWiring .class );
148+ if (annotation ==null ){
149+ throw new GraphQLAnnotationsException (String .format ("No wiring is provided to directive class %s" , object .getSimpleName ()), null );
150+ }
151+ this .getContainer ().getDirectiveRegistry ().put (directive .getName (), new DirectiveAndWiring (directive , annotation .value ()));
143152 return directive ;
144153 } catch (GraphQLAnnotationsException e ) {
145154 this .getContainer ().getProcessing ().clear ();
@@ -148,6 +157,42 @@ public GraphQLDirective directive(Class<?> object) throws GraphQLAnnotationsExce
148157 }
149158 }
150159
160+ //todo: add tests
161+ public GraphQLDirective directiveViaAnnotation (Class <?> annotationClass ) {
162+ try {
163+ GraphQLDirective directive = this .directiveCreator .getDirective (annotationClass );
164+ DirectiveWiring annotation = annotationClass .getAnnotation (DirectiveWiring .class );
165+ if (annotation ==null ){
166+ throw new GraphQLAnnotationsException (String .format ("No wiring is provided to directive class %s" , annotationClass .getSimpleName ()), null );
167+ }
168+ this .getContainer ().getDirectiveRegistry ().put (directive .getName (), new DirectiveAndWiring (directive , annotation .value ()));
169+ return directive ;
170+ } catch (GraphQLAnnotationsException e ) {
171+ this .getContainer ().getProcessing ().clear ();
172+ this .getTypeRegistry ().clear ();
173+ throw e ;
174+ }
175+ }
176+
177+ //todo: add tests
178+ public Set <GraphQLDirective > directives (Class <?> directivesDeclarationClass ) {
179+ Method [] methods = directivesDeclarationClass .getMethods ();
180+ Set <GraphQLDirective > directiveSet = new HashSet <>();
181+ Arrays .stream (methods ).filter (method -> method .isAnnotationPresent (GraphQLDirectiveDefinition .class ))
182+ .forEach (method -> {
183+ try {
184+ DirectiveAndWiring directive = this .directiveCreator .getDirective (method );
185+ this .getContainer ().getDirectiveRegistry ().put (directive .getDirective ().getName (), directive );
186+ directiveSet .add (directive .getDirective ());
187+ } catch (GraphQLAnnotationsException e ) {
188+ this .getContainer ().getProcessing ().clear ();
189+ this .getTypeRegistry ().clear ();
190+ throw e ;
191+ }
192+ });
193+ return directiveSet ;
194+ }
195+
151196 public void registerTypeExtension (Class <?> objectClass ) {
152197 graphQLExtensionsHandler .registerTypeExtension (objectClass , container );
153198 }
0 commit comments